Skip to main content

ColumnBuilder

Struct ColumnBuilder 

Source
pub struct ColumnBuilder { /* private fields */ }
Expand description

Builder for creating database columns

This builder provides a fluent, chainable API for constructing columns with comprehensive validation and configuration options.

§Examples

use ignis_db::builders::{ColumnBuilder, column};
use ignis_db::schema::ColumnType;

// Basic column
let id_column = ColumnBuilder::new("id", ColumnType::Int4)
    .primary_key()
    .build()?;

// Advanced column with constraints
let email_column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
    .not_null()
    .unique()
    .default("'unknown@example.com'")
    .check("email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$'")
    .comment("User email address")
    .build()?;

// Generated column
let full_name_column = ColumnBuilder::new("full_name", ColumnType::Varchar(Some(200)))
    .generated_as("first_name || ' ' || last_name", true)
    .comment("Automatically generated full name")
    .build()?;

// Foreign key column
let user_id_column = ColumnBuilder::new("user_id", ColumnType::Int4)
    .not_null()
    .references("users", "id")
    .on_delete("CASCADE")
    .build()?;

Implementations§

Source§

impl ColumnBuilder

Source

pub fn new(name: impl Into<String>, column_type: ColumnType) -> Self

Create a new column builder

§Parameters
  • name: The name of the column
  • column_type: The data type of the column
§Examples
use ignis_db::builders::ColumnBuilder;
use ignis_db::schema::ColumnType;

let column = ColumnBuilder::new("id", ColumnType::Int4);
Source

pub fn from_definition( name: impl Into<String>, definition: impl Into<String>, ) -> Self

Create a column builder from a string definition

This is a convenience method for creating columns from raw SQL definitions. It parses the definition to extract basic information.

§Parameters
  • name: The name of the column
  • definition: The column definition (e.g., “UUID PRIMARY KEY DEFAULT gen_random_uuid()”)
§Examples
let column = ColumnBuilder::from_definition("id", "UUID PRIMARY KEY DEFAULT gen_random_uuid()");
let column = ColumnBuilder::from_definition("email", "VARCHAR(255) UNIQUE NOT NULL");
Source

pub fn nullable(self) -> Self

Make the column nullable (default behavior)

§Examples
let column = ColumnBuilder::new("description", ColumnType::Text)
    .nullable()
    .build()?;
Source

pub fn not_null(self) -> Self

Make the column NOT NULL

§Examples
let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
    .not_null()
    .build()?;
Source

pub fn default(self, default: impl Into<String>) -> Self

Set a default value for the column

§Parameters
  • default: The default value expression
§Examples
let column = ColumnBuilder::new("created_at", ColumnType::Timestamptz)
    .default("NOW()")
    .build()?;

let column = ColumnBuilder::new("status", ColumnType::Varchar(Some(20)))
    .default("'active'")
    .build()?;
Source

pub fn primary_key(self) -> Self

Make the column a primary key

This automatically sets the column to NOT NULL.

§Examples
let column = ColumnBuilder::new("id", ColumnType::Int4)
    .primary_key()
    .build()?;
Source

pub fn unique(self) -> Self

Make the column unique

§Examples
let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
    .unique()
    .build()?;
Source

pub fn references( self, table: impl Into<String>, column: impl Into<String>, ) -> Self

Add a foreign key reference

§Parameters
  • table: The referenced table name
  • column: The referenced column name
§Examples
let column = ColumnBuilder::new("user_id", ColumnType::Int4)
    .references("users", "id")
    .build()?;
Source

pub fn check(self, constraint: impl Into<String>) -> Self

Add a check constraint

§Parameters
  • constraint: The check constraint expression
§Examples
let column = ColumnBuilder::new("age", ColumnType::Int4)
    .check("age >= 0")
    .build()?;

let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
    .check("email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$'")
    .build()?;
Source

pub fn comment(self, comment: impl Into<String>) -> Self

Add a comment to the column

§Parameters
  • comment: The comment text
§Examples
let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
    .comment("User email address")
    .build()?;
Source

pub fn collation(self, collation: impl Into<String>) -> Self

Set the collation for the column

§Parameters
  • collation: The collation name
§Examples
let column = ColumnBuilder::new("name", ColumnType::Varchar(Some(100)))
    .collation("en_US.utf8")
    .build()?;
Source

pub fn generated_as(self, expression: impl Into<String>, stored: bool) -> Self

Make the column a generated column

§Parameters
  • expression: The generation expression
  • stored: Whether the column is stored or virtual
§Examples
let column = ColumnBuilder::new("full_name", ColumnType::Varchar(Some(200)))
    .generated_as("first_name || ' ' || last_name", true)
    .build()?;

let column = ColumnBuilder::new("area", ColumnType::Numeric(10, 2))
    .generated_as("width * height", false)
    .build()?;
Source

pub fn storage_parameter( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Add a storage parameter

§Parameters
  • key: The storage parameter name
  • value: The storage parameter value
§Examples
let column = ColumnBuilder::new("data", ColumnType::Text)
    .storage_parameter("compression", "gzip")
    .build()?;
Source

pub fn on_delete(self, action: impl Into<String>) -> Self

Configure foreign key behavior

This method allows you to configure the ON DELETE and ON UPDATE behavior for foreign key references.

§Examples
let column = ColumnBuilder::new("user_id", ColumnType::Int4)
    .references("users", "id")
    .on_delete("CASCADE")
    .on_update("CASCADE")
    .build()?;
Source

pub fn on_update(self, action: impl Into<String>) -> Self

Configure foreign key update behavior

This method allows you to configure the ON UPDATE behavior for foreign key references.

§Examples
let column = ColumnBuilder::new("user_id", ColumnType::Int4)
    .references("users", "id")
    .on_update("CASCADE")
    .build()?;
Source

pub fn name(&self) -> &str

Get the column name

Source

pub fn column_type(&self) -> &ColumnType

Get the column type

Source

pub fn is_nullable(&self) -> bool

Check if the column is nullable

Source

pub fn is_primary_key(&self) -> bool

Check if the column is a primary key

Source

pub fn is_unique(&self) -> bool

Check if the column is unique

Source

pub fn has_foreign_key(&self) -> bool

Check if the column has a foreign key reference

Source

pub fn is_generated(&self) -> bool

Check if the column is generated

Source

pub fn storage_parameter_count(&self) -> usize

Get the number of storage parameters

Trait Implementations§

Source§

impl Clone for ColumnBuilder

Source§

fn clone(&self) -> ColumnBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ConditionalBuilder for ColumnBuilder

Source§

fn when<F>(self, condition: bool, config_fn: F) -> Self
where F: FnOnce(&mut Self), Self: Sized,

Apply configuration conditionally Read more
Source§

fn when_else<F, G>(self, condition: bool, if_true: F, if_false: G) -> Self
where F: FnOnce(&mut Self), G: FnOnce(&mut Self), Self: Sized,

Apply configuration conditionally with a default fallback Read more
Source§

impl Debug for ColumnBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl InspectableBuilder for ColumnBuilder

Source§

fn debug_info(&self) -> String

Get a debug representation of the current builder state Read more
Source§

fn summary(&self) -> String

Get a summary of the current configuration Read more
Source§

impl SchemaBuilder for ColumnBuilder

Source§

type Output = Column

The type of schema object that this builder produces
Source§

type Error = ColumnBuildError

The type of error that can occur during building
Source§

fn build(self) -> Result<Self::Output, Self::Error>

Build the final schema object from the builder configuration Read more
Source§

impl ValidatableBuilder for ColumnBuilder

Source§

fn is_valid(&self) -> bool

Check if the current builder configuration is valid Read more
Source§

fn validation_errors(&self) -> Vec<String>

Get a list of validation errors for the current configuration Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more