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(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(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(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(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(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)"#
);

Implementationsยง

sourceยง

impl IndexCreateStatement

source

pub fn new() -> Self

Construct a new IndexCreateStatement

source

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

Create index if index not exists

source

pub fn name<T>(&mut self, name: T) -> &mut Self
where T: Into<String>,

Set index name

source

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

Set target table

source

pub fn col<C>(&mut self, col: C) -> &mut Self
where C: IntoIndexColumn,

Add index column

source

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

Set index as primary

source

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

Set index as unique

source

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

Set nulls to not be treated as distinct values. Only available on Postgres.

source

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

Set index as full text. On MySQL, this is FULLTEXT. On PgSQL, this is GIN.

source

pub fn index_type(&mut self, index_type: IndexType) -> &mut Self

Set index type. Not available on Sqlite.

source

pub fn is_primary_key(&self) -> bool

source

pub fn is_unique_key(&self) -> bool

source

pub fn is_nulls_not_distinct(&self) -> bool

source

pub fn get_index_spec(&self) -> &TableIndex

source

pub fn take(&mut self) -> Self

sourceยง

impl IndexCreateStatement

source

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

source

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

source

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

Trait Implementationsยง

sourceยง

impl Clone for IndexCreateStatement

sourceยง

fn clone(&self) -> IndexCreateStatement

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
sourceยง

impl Debug for IndexCreateStatement

sourceยง

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

Formats the value using the given formatter. Read more
sourceยง

impl Default for IndexCreateStatement

sourceยง

fn default() -> IndexCreateStatement

Returns the โ€œdefault valueโ€ for a type. Read more
sourceยง

impl SchemaStatementBuilder for IndexCreateStatement

sourceยง

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

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

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

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

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

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

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

ยง

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

ยง

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

ยง

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.