pub struct TableCreateStatement { /* private fields */ }
Expand description

Create a table

Examples

use sea_query::{*, tests_cfg::*};

let table = Table::create()
    .table(Char::Table)
    .if_not_exists()
    .col(ColumnDef::new(Char::Id).integer().not_null().auto_increment().primary_key())
    .col(ColumnDef::new(Char::FontSize).integer().not_null())
    .col(ColumnDef::new(Char::Character).string().not_null())
    .col(ColumnDef::new(Char::SizeW).integer().not_null())
    .col(ColumnDef::new(Char::SizeH).integer().not_null())
    .col(ColumnDef::new(Char::FontId).integer().default(Value::Int(None)))
    .foreign_key(
        ForeignKey::create()
            .name("FK_2e303c3a712662f1fc2a4d0aad6")
            .from(Char::Table, Char::FontId)
            .to(Font::Table, Font::Id)
            .on_delete(ForeignKeyAction::Cascade)
            .on_update(ForeignKeyAction::Cascade)
    )
    .to_owned();

assert_eq!(
    table.to_string(MysqlQueryBuilder),
    [
        r#"CREATE TABLE IF NOT EXISTS `character` ("#,
            r#"`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,"#,
            r#"`font_size` int NOT NULL,"#,
            r#"`character` varchar(255) NOT NULL,"#,
            r#"`size_w` int NOT NULL,"#,
            r#"`size_h` int NOT NULL,"#,
            r#"`font_id` int DEFAULT NULL,"#,
            r#"CONSTRAINT `FK_2e303c3a712662f1fc2a4d0aad6`"#,
                r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)"#,
                r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
        r#")"#,
    ].join(" ")
);
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE IF NOT EXISTS "character" ("#,
            r#""id" serial NOT NULL PRIMARY KEY,"#,
            r#""font_size" integer NOT NULL,"#,
            r#""character" varchar NOT NULL,"#,
            r#""size_w" integer NOT NULL,"#,
            r#""size_h" integer NOT NULL,"#,
            r#""font_id" integer DEFAULT NULL,"#,
            r#"CONSTRAINT "FK_2e303c3a712662f1fc2a4d0aad6""#,
                r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id")"#,
                r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
        r#")"#,
    ].join(" ")
);
assert_eq!(
    table.to_string(SqliteQueryBuilder),
    [
       r#"CREATE TABLE IF NOT EXISTS "character" ("#,
           r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
           r#""font_size" integer NOT NULL,"#,
           r#""character" text NOT NULL,"#,
           r#""size_w" integer NOT NULL,"#,
           r#""size_h" integer NOT NULL,"#,
           r#""font_id" integer DEFAULT NULL,"#,
           r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id") ON DELETE CASCADE ON UPDATE CASCADE"#,
       r#")"#,
    ].join(" ")
);

Implementations§

§

impl TableCreateStatement

pub fn new() -> TableCreateStatement

Construct create table statement

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

Create table if table not exists

pub fn table<T>(&mut self, table: T) -> &mut TableCreateStatementwhere T: IntoTableRef,

Set table name

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

Add a new table column

pub fn index( &mut self, index: &mut IndexCreateStatement ) -> &mut TableCreateStatement

Add an index. MySQL only.

Examples
use sea_query::{tests_cfg::*, *};

assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(ColumnDef::new(Glyph::Id).integer().not_null())
        .index(Index::create().unique().name("idx-glyph-id").col(Glyph::Id))
        .to_string(MysqlQueryBuilder),
    [
        "CREATE TABLE `glyph` (",
        "`id` int NOT NULL,",
        "UNIQUE KEY `idx-glyph-id` (`id`)",
        ")",
    ]
    .join(" ")
);

pub fn primary_key( &mut self, index: &mut IndexCreateStatement ) -> &mut TableCreateStatement

Add an primary key.

Examples
use sea_query::{tests_cfg::*, *};

let mut statement = Table::create();
statement
    .table(Glyph::Table)
    .col(ColumnDef::new(Glyph::Id).integer().not_null())
    .col(ColumnDef::new(Glyph::Image).string().not_null())
    .primary_key(Index::create().col(Glyph::Id).col(Glyph::Image));
assert_eq!(
    statement.to_string(MysqlQueryBuilder),
    [
        "CREATE TABLE `glyph` (",
        "`id` int NOT NULL,",
        "`image` varchar(255) NOT NULL,",
        "PRIMARY KEY (`id`, `image`)",
        ")",
    ]
    .join(" ")
);
assert_eq!(
    statement.to_string(PostgresQueryBuilder),
    [
        "CREATE TABLE \"glyph\" (",
        "\"id\" integer NOT NULL,",
        "\"image\" varchar NOT NULL,",
        "PRIMARY KEY (\"id\", \"image\")",
        ")",
    ]
    .join(" ")
);
assert_eq!(
    statement.to_string(SqliteQueryBuilder),
    [
        r#"CREATE TABLE "glyph" ("#,
        r#""id" integer NOT NULL,"#,
        r#""image" text NOT NULL,"#,
        r#"PRIMARY KEY ("id", "image")"#,
        r#")"#,
    ]
    .join(" ")
);

pub fn foreign_key( &mut self, foreign_key: &mut ForeignKeyCreateStatement ) -> &mut TableCreateStatement

Add a foreign key

pub fn engine(&mut self, string: &str) -> &mut TableCreateStatement

Set database engine. MySQL only.

pub fn collate(&mut self, string: &str) -> &mut TableCreateStatement

Set database collate. MySQL only.

pub fn character_set(&mut self, string: &str) -> &mut TableCreateStatement

Set database character set. MySQL only.

pub fn get_table_name(&self) -> Option<&TableRef>

pub fn get_columns(&self) -> &Vec<ColumnDef, Global>

pub fn get_foreign_key_create_stmts( &self ) -> &Vec<ForeignKeyCreateStatement, Global>

pub fn get_indexes(&self) -> &Vec<IndexCreateStatement, Global>

pub fn take(&mut self) -> TableCreateStatement

§

impl TableCreateStatement

pub fn to_string<T>(&self, schema_builder: T) -> Stringwhere T: SchemaBuilder,

pub fn build<T>(&self, schema_builder: T) -> Stringwhere T: SchemaBuilder,

pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String

Trait Implementations§

§

impl Clone for TableCreateStatement

§

fn clone(&self) -> TableCreateStatement

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl Debug for TableCreateStatement

§

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

Formats the value using the given formatter. Read more
§

impl Default for TableCreateStatement

§

fn default() -> TableCreateStatement

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

impl SchemaStatementBuilder for TableCreateStatement

§

fn build<T>(&self, schema_builder: T) -> Stringwhere T: SchemaBuilder,

Build corresponding SQL statement for certain database backend and return SQL string
§

fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String

Build corresponding SQL statement for certain database backend and return SQL string
§

fn to_string<T>(&self, schema_builder: T) -> Stringwhere T: SchemaBuilder,

Build corresponding SQL statement for certain database backend and return SQL string
source§

impl StatementBuilder for TableCreateStatement

source§

fn build(&self, db_backend: &DatabaseBackend) -> Statement

Method to call in order to build a Statement

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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