Struct IndexCreateStatement
pub struct IndexCreateStatement { /* private fields */ }Expand description
Create an index for an existing table
§Examples
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col(Glyph::Aspect)
.to_owned();
assert_eq!(
index.to_string(MsSqlQueryBuilder),
r#"CREATE INDEX [idx-glyph-aspect] ON [glyph] ([aspect])"#
);
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
);Create index if not exists
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.if_not_exists()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col(Glyph::Aspect)
.to_owned();
assert_eq!(
index.to_string(MsSqlQueryBuilder),
r#"CREATE INDEX [idx-glyph-aspect] ON [glyph] ([aspect])"#
);
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX IF NOT EXISTS "idx-glyph-aspect" ON "glyph" ("aspect")"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX IF NOT EXISTS "idx-glyph-aspect" ON "glyph" ("aspect")"#
);Index with prefix
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col((Glyph::Aspect, 128))
.to_owned();
assert_eq!(
index.to_string(MsSqlQueryBuilder),
r#"CREATE INDEX [idx-glyph-aspect] ON [glyph] ([aspect] (128))"#
);
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` (128))"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (128))"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
);Index with order
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col((Glyph::Aspect, IndexOrder::Desc))
.to_owned();
assert_eq!(
index.to_string(MsSqlQueryBuilder),
r#"CREATE INDEX [idx-glyph-aspect] ON [glyph] ([aspect] DESC)"#
);
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` DESC)"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" DESC)"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" DESC)"#
);Index on multi-columns
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col((Glyph::Image, IndexOrder::Asc))
.col((Glyph::Aspect, IndexOrder::Desc))
.unique()
.to_owned();
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE UNIQUE INDEX `idx-glyph-aspect` ON `glyph` (`image` ASC, `aspect` DESC)"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE UNIQUE INDEX "idx-glyph-aspect" ON "glyph" ("image" ASC, "aspect" DESC)"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE UNIQUE INDEX "idx-glyph-aspect" ON "glyph" ("image" ASC, "aspect" DESC)"#
);Index with prefix and order
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col((Glyph::Aspect, 64, IndexOrder::Asc))
.to_owned();
assert_eq!(
index.to_string(MsSqlQueryBuilder),
r#"CREATE INDEX [idx-glyph-aspect] ON [glyph] ([aspect] (64) ASC)"#
);
assert_eq!(
index.to_string(MysqlQueryBuilder),
r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` (64) ASC)"#
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (64) ASC)"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" ASC)"#
);Partial Index with prefix and order
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-glyph-aspect")
.table(Glyph::Table)
.col((Glyph::Aspect, 64, IndexOrder::Asc))
.and_where(Expr::col((Glyph::Table, Glyph::Aspect)).is_in(vec![3, 4]))
.to_owned();
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (64) ASC) WHERE "glyph"."aspect" IN (3, 4)"#
);
assert_eq!(
index.to_string(SqliteQueryBuilder),
r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" ASC) WHERE "glyph"."aspect" IN (3, 4)"#
);Index include non-key columns
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-font-name-include-language")
.table(Font::Table)
.col(Font::Name)
.include(Font::Language)
.to_owned();
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-font-name-include-language" ON "font" ("name") INCLUDE ("language")"#
)Functional Index
use sea_query::{tests_cfg::*, *};
let index = Index::create()
.name("idx-character-area")
.table(Character::Table)
.col(Expr::col(Character::SizeH).mul(Expr::col(Character::SizeW)))
.to_owned();
assert_eq!(
index.to_string(MysqlQueryBuilder),
"CREATE INDEX `idx-character-area` ON `character` ((`size_h` * `size_w`))"
);
assert_eq!(
index.to_string(PostgresQueryBuilder),
r#"CREATE INDEX "idx-character-area" ON "character" (("size_h" * "size_w"))"#
);Implementations§
§impl IndexCreateStatement
impl IndexCreateStatement
pub fn new() -> Self
pub fn new() -> Self
Construct a new IndexCreateStatement
pub fn if_not_exists(&mut self) -> &mut Self
pub fn if_not_exists(&mut self) -> &mut Self
Create index if index not exists
pub fn table<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
pub fn table<T>(&mut self, table: T) -> &mut Selfwhere
T: IntoTableRef,
Set target table
pub fn col<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoIndexColumn,
pub fn col<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoIndexColumn,
Add index column
pub fn primary(&mut self) -> &mut Self
pub fn primary(&mut self) -> &mut Self
Set index as primary
pub fn unique(&mut self) -> &mut Self
pub fn unique(&mut self) -> &mut Self
Set index as unique
pub fn concurrently(&mut self) -> &mut Self
pub fn concurrently(&mut self) -> &mut Self
Set index to be created concurrently. Only available on Postgres.
pub fn nulls_not_distinct(&mut self) -> &mut Self
pub fn nulls_not_distinct(&mut self) -> &mut Self
Set nulls to not be treated as distinct values. Only available on Postgres.
pub fn full_text(&mut self) -> &mut Self
pub fn full_text(&mut self) -> &mut Self
Set index as full text.
On MySQL, this is FULLTEXT.
On PgSQL, this is GIN.
pub fn index_type(&mut self, index_type: IndexType) -> &mut Self
pub fn index_type(&mut self, index_type: IndexType) -> &mut Self
Set index type. Not available on Sqlite.
pub fn include<C>(&mut self, col: C) -> &mut Selfwhere
C: IntoIden,
pub fn is_primary_key(&self) -> bool
pub fn is_unique_key(&self) -> bool
pub fn is_nulls_not_distinct(&self) -> bool
pub fn get_index_spec(&self) -> &TableIndex
pub fn take(&mut self) -> Self
§impl IndexCreateStatement
impl IndexCreateStatement
pub fn build<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
pub fn build<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
pub fn to_string<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
pub fn to_string<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
Trait Implementations§
§impl Clone for IndexCreateStatement
impl Clone for IndexCreateStatement
§fn clone(&self) -> IndexCreateStatement
fn clone(&self) -> IndexCreateStatement
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more§impl ConditionalStatement for IndexCreateStatement
impl ConditionalStatement for IndexCreateStatement
§fn cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
fn cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
Where condition, expressed with
any and all.
Calling cond_where multiple times will conjoin them.
Calling or_where after cond_where will panic. Read more§fn and_where_option(&mut self, other: Option<Expr>) -> &mut Self
fn and_where_option(&mut self, other: Option<Expr>) -> &mut Self
Optional and where, short hand for
if c.is_some() q.and_where(c). Read more§impl Debug for IndexCreateStatement
impl Debug for IndexCreateStatement
§impl Default for IndexCreateStatement
impl Default for IndexCreateStatement
§fn default() -> IndexCreateStatement
fn default() -> IndexCreateStatement
Returns the “default value” for a type. Read more
§impl SchemaStatementBuilder for IndexCreateStatement
impl SchemaStatementBuilder for IndexCreateStatement
§fn build<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
fn build<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
Build corresponding SQL statement for certain database backend and return SQL string
§fn to_string<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
fn to_string<T>(&self, schema_builder: T) -> Stringwhere
T: SchemaBuilder,
Build corresponding SQL statement for certain database backend and return SQL string
Auto Trait Implementations§
impl Freeze for IndexCreateStatement
impl RefUnwindSafe for IndexCreateStatement
impl Send for IndexCreateStatement
impl Sync for IndexCreateStatement
impl Unpin for IndexCreateStatement
impl UnwindSafe for IndexCreateStatement
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
Mutably borrows from an owned value. Read more