Skip to main content

CreateTableStatement

Struct CreateTableStatement 

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

CREATE TABLE statement builder

This struct provides a fluent API for constructing CREATE TABLE queries.

§Examples

use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::ColumnType;

let query = Query::create_table()
    .table("users")
    .if_not_exists()
    .col(
        ColumnDef::new("id")
            .column_type(ColumnType::Integer)
            .primary_key(true)
            .auto_increment(true)
    )
    .col(
        ColumnDef::new("name")
            .column_type(ColumnType::String(Some(100)))
            .not_null(true)
    );

Implementations§

Source§

impl CreateTableStatement

Source

pub fn new() -> Self

Create a new CREATE TABLE statement

Source

pub fn take(&mut self) -> Self

Take the ownership of data in the current CreateTableStatement

Source

pub fn table<T>(&mut self, tbl: T) -> &mut Self
where T: IntoTableRef,

Set the table to create

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_table()
    .table("users");
Source

pub fn if_not_exists(&mut self) -> &mut Self

Add IF NOT EXISTS clause

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_table()
    .table("users")
    .if_not_exists();
Source

pub fn col(&mut self, column: ColumnDef) -> &mut Self

Add a column definition

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::{ColumnDef, ColumnType};

let query = Query::create_table()
    .table("users")
    .col(
        ColumnDef::new("id")
            .column_type(ColumnType::Integer)
            .primary_key(true)
    );
Source

pub fn cols<I>(&mut self, columns: I) -> &mut Self
where I: IntoIterator<Item = ColumnDef>,

Add multiple column definitions

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::{ColumnDef, ColumnType};

let query = Query::create_table()
    .table("users")
    .cols(vec![
        ColumnDef::new("id").column_type(ColumnType::Integer),
        ColumnDef::new("name").column_type(ColumnType::String(Some(100))),
    ]);
Source

pub fn constraint(&mut self, constraint: TableConstraint) -> &mut Self

Add a table constraint

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::TableConstraint;

let query = Query::create_table()
    .table("users")
    .constraint(TableConstraint::PrimaryKey {
        name: Some("pk_users".into()),
        columns: vec!["id".into()],
    });
Source

pub fn constraints<I>(&mut self, constraints: I) -> &mut Self
where I: IntoIterator<Item = TableConstraint>,

Add multiple table constraints

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::TableConstraint;

let query = Query::create_table()
    .table("order_items")
    .constraints(vec![
        TableConstraint::PrimaryKey {
            name: None,
            columns: vec!["order_id".into(), "item_id".into()],
        },
        TableConstraint::ForeignKey {
            name: Some("fk_order".into()),
            columns: vec!["order_id".into()],
            ref_table: Box::new("orders".into()),
            ref_columns: vec!["id".into()],
            on_delete: None,
            on_update: None,
        },
    ]);
Source

pub fn index(&mut self, index: IndexDef) -> &mut Self

Add an index definition

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::IndexDef;

let query = Query::create_table()
    .table("users")
    .index(
        IndexDef::new("idx_email", "users")
            .column("email")
            .unique(true)
    );
Source

pub fn indexes<I>(&mut self, indexes: I) -> &mut Self
where I: IntoIterator<Item = IndexDef>,

Add multiple index definitions

Source

pub fn comment<S: Into<String>>(&mut self, comment: S) -> &mut Self

Set table comment

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_table()
    .table("users")
    .comment("User accounts table");
Source

pub fn primary_key<I, C>(&mut self, columns: I) -> &mut Self
where I: IntoIterator<Item = C>, C: IntoIden,

Add a primary key constraint

This is a convenience method for adding a PRIMARY KEY constraint.

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_table()
    .table("users")
    .primary_key(vec!["id"]);
Source

pub fn unique<I, C>(&mut self, columns: I) -> &mut Self
where I: IntoIterator<Item = C>, C: IntoIden,

Add a unique constraint

This is a convenience method for adding a UNIQUE constraint.

§Examples
use reinhardt_query::prelude::*;

let query = Query::create_table()
    .table("users")
    .unique(vec!["email"]);
Source

pub fn foreign_key<I1, C1, T, I2, C2>( &mut self, columns: I1, ref_table: T, ref_columns: I2, on_delete: Option<ForeignKeyAction>, on_update: Option<ForeignKeyAction>, ) -> &mut Self
where I1: IntoIterator<Item = C1>, C1: IntoIden, T: IntoTableRef, I2: IntoIterator<Item = C2>, C2: IntoIden,

Add a foreign key constraint

This is a convenience method for adding a FOREIGN KEY constraint.

§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::ddl::ForeignKeyAction;

let query = Query::create_table()
    .table("posts")
    .foreign_key(
        vec!["user_id"],
        "users",
        vec!["id"],
        Some(ForeignKeyAction::Cascade),
        None,
    );
Source

pub fn foreign_key_from_builder( &mut self, fk: &mut ForeignKeyCreateStatement, ) -> &mut Self

Add a foreign key constraint from a [ForeignKeyCreateStatement] builder.

This method accepts the builder-pattern style used by ForeignKey::create().

§Examples
use reinhardt_query::prelude::*;

let mut fk = ForeignKey::create();
fk.from_tbl(Alias::new("posts"))
    .from_col(Alias::new("user_id"))
    .to_tbl(Alias::new("users"))
    .to_col(Alias::new("id"));

let mut stmt = Query::create_table();
stmt.table("posts")
    .foreign_key_from_builder(&mut fk);

Trait Implementations§

Source§

impl Clone for CreateTableStatement

Source§

fn clone(&self) -> CreateTableStatement

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CreateTableStatement

Source§

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

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

impl Default for CreateTableStatement

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl QueryStatementBuilder for CreateTableStatement

Source§

fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, Values)

Build SQL statement for a database backend and collect query parameters Read more
Source§

fn to_string<T: QueryBuilderTrait>(&self, query_builder: T) -> String

Build SQL statement for a database backend and return SQL string with values inlined as SQL literals. Read more
Source§

fn build<T: QueryBuilderTrait>(&self, query_builder: T) -> (String, Values)

Build SQL statement with parameter collection Read more
Source§

impl QueryStatementWriter for CreateTableStatement

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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.