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
impl ColumnBuilder
Sourcepub fn new(name: impl Into<String>, column_type: ColumnType) -> Self
pub fn new(name: impl Into<String>, column_type: ColumnType) -> Self
Sourcepub fn from_definition(
name: impl Into<String>,
definition: impl Into<String>,
) -> Self
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 columndefinition: 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");Sourcepub fn nullable(self) -> Self
pub fn nullable(self) -> Self
Make the column nullable (default behavior)
§Examples
let column = ColumnBuilder::new("description", ColumnType::Text)
.nullable()
.build()?;Sourcepub fn not_null(self) -> Self
pub fn not_null(self) -> Self
Make the column NOT NULL
§Examples
let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
.not_null()
.build()?;Sourcepub fn primary_key(self) -> Self
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()?;Sourcepub fn unique(self) -> Self
pub fn unique(self) -> Self
Make the column unique
§Examples
let column = ColumnBuilder::new("email", ColumnType::Varchar(Some(255)))
.unique()
.build()?;Sourcepub fn check(self, constraint: impl Into<String>) -> Self
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()?;Sourcepub fn generated_as(self, expression: impl Into<String>, stored: bool) -> Self
pub fn generated_as(self, expression: impl Into<String>, stored: bool) -> Self
Make the column a generated column
§Parameters
expression: The generation expressionstored: 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()?;Sourcepub fn on_delete(self, action: impl Into<String>) -> Self
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()?;Sourcepub fn on_update(self, action: impl Into<String>) -> Self
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()?;Sourcepub fn column_type(&self) -> &ColumnType
pub fn column_type(&self) -> &ColumnType
Get the column type
Sourcepub fn is_nullable(&self) -> bool
pub fn is_nullable(&self) -> bool
Check if the column is nullable
Sourcepub fn is_primary_key(&self) -> bool
pub fn is_primary_key(&self) -> bool
Check if the column is a primary key
Sourcepub fn has_foreign_key(&self) -> bool
pub fn has_foreign_key(&self) -> bool
Check if the column has a foreign key reference
Sourcepub fn is_generated(&self) -> bool
pub fn is_generated(&self) -> bool
Check if the column is generated
Sourcepub fn storage_parameter_count(&self) -> usize
pub fn storage_parameter_count(&self) -> usize
Get the number of storage parameters
Trait Implementations§
Source§impl Clone for ColumnBuilder
impl Clone for ColumnBuilder
Source§fn clone(&self) -> ColumnBuilder
fn clone(&self) -> ColumnBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl ConditionalBuilder for ColumnBuilder
impl ConditionalBuilder for ColumnBuilder
Source§impl Debug for ColumnBuilder
impl Debug for ColumnBuilder
Source§impl InspectableBuilder for ColumnBuilder
impl InspectableBuilder for ColumnBuilder
Source§impl SchemaBuilder for ColumnBuilder
impl SchemaBuilder for ColumnBuilder
Source§impl ValidatableBuilder for ColumnBuilder
impl ValidatableBuilder for ColumnBuilder
Auto Trait Implementations§
impl Freeze for ColumnBuilder
impl RefUnwindSafe for ColumnBuilder
impl Send for ColumnBuilder
impl Sync for ColumnBuilder
impl Unpin for ColumnBuilder
impl UnsafeUnpin for ColumnBuilder
impl UnwindSafe for ColumnBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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