Skip to main content

sea_query/index/
create.rs

1use crate::{ConditionHolder, ConditionalStatement, IntoCondition};
2use crate::{SchemaStatementBuilder, backend::SchemaBuilder, types::*};
3
4use super::common::*;
5
6/// Create an index for an existing table
7///
8/// # Examples
9///
10/// ```
11/// use sea_query::{tests_cfg::*, *};
12///
13/// let index = Index::create()
14///     .name("idx-glyph-aspect")
15///     .table(Glyph::Table)
16///     .col(Glyph::Aspect)
17///     .to_owned();
18///
19/// assert_eq!(
20///     index.to_string(MysqlQueryBuilder),
21///     r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"#
22/// );
23/// assert_eq!(
24///     index.to_string(PostgresQueryBuilder),
25///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
26/// );
27/// assert_eq!(
28///     index.to_string(SqliteQueryBuilder),
29///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
30/// );
31/// ```
32/// Create index if not exists
33/// ```
34/// use sea_query::{tests_cfg::*, *};
35///
36/// let index = Index::create()
37///     .if_not_exists()
38///     .name("idx-glyph-aspect")
39///     .table(Glyph::Table)
40///     .col(Glyph::Aspect)
41///     .to_owned();
42///
43/// assert_eq!(
44///     index.to_string(MysqlQueryBuilder),
45///     r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect`)"#
46/// );
47/// assert_eq!(
48///     index.to_string(PostgresQueryBuilder),
49///     r#"CREATE INDEX IF NOT EXISTS "idx-glyph-aspect" ON "glyph" ("aspect")"#
50/// );
51/// assert_eq!(
52///     index.to_string(SqliteQueryBuilder),
53///     r#"CREATE INDEX IF NOT EXISTS "idx-glyph-aspect" ON "glyph" ("aspect")"#
54/// );
55/// ```
56/// Index with prefix
57/// ```
58/// use sea_query::{tests_cfg::*, *};
59///
60/// let index = Index::create()
61///     .name("idx-glyph-aspect")
62///     .table(Glyph::Table)
63///     .col((Glyph::Aspect, 128))
64///     .to_owned();
65///
66/// assert_eq!(
67///     index.to_string(MysqlQueryBuilder),
68///     r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` (128))"#
69/// );
70/// assert_eq!(
71///     index.to_string(PostgresQueryBuilder),
72///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (128))"#
73/// );
74/// assert_eq!(
75///     index.to_string(SqliteQueryBuilder),
76///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect")"#
77/// );
78/// ```
79/// Index with order
80/// ```
81/// use sea_query::{tests_cfg::*, *};
82///
83/// let index = Index::create()
84///     .name("idx-glyph-aspect")
85///     .table(Glyph::Table)
86///     .col((Glyph::Aspect, IndexOrder::Desc))
87///     .to_owned();
88///
89/// assert_eq!(
90///     index.to_string(MysqlQueryBuilder),
91///     r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` DESC)"#
92/// );
93/// assert_eq!(
94///     index.to_string(PostgresQueryBuilder),
95///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" DESC)"#
96/// );
97/// assert_eq!(
98///     index.to_string(SqliteQueryBuilder),
99///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" DESC)"#
100/// );
101/// ```
102/// Index on multi-columns
103/// ```
104/// use sea_query::{tests_cfg::*, *};
105///
106/// let index = Index::create()
107///     .name("idx-glyph-aspect")
108///     .table(Glyph::Table)
109///     .col((Glyph::Image, IndexOrder::Asc))
110///     .col((Glyph::Aspect, IndexOrder::Desc))
111///     .unique()
112///     .to_owned();
113///
114/// assert_eq!(
115///     index.to_string(MysqlQueryBuilder),
116///     r#"CREATE UNIQUE INDEX `idx-glyph-aspect` ON `glyph` (`image` ASC, `aspect` DESC)"#
117/// );
118/// assert_eq!(
119///     index.to_string(PostgresQueryBuilder),
120///     r#"CREATE UNIQUE INDEX "idx-glyph-aspect" ON "glyph" ("image" ASC, "aspect" DESC)"#
121/// );
122/// assert_eq!(
123///     index.to_string(SqliteQueryBuilder),
124///     r#"CREATE UNIQUE INDEX "idx-glyph-aspect" ON "glyph" ("image" ASC, "aspect" DESC)"#
125/// );
126/// ```
127/// Index with prefix and order
128/// ```
129/// use sea_query::{tests_cfg::*, *};
130///
131/// let index = Index::create()
132///     .name("idx-glyph-aspect")
133///     .table(Glyph::Table)
134///     .col((Glyph::Aspect, 64, IndexOrder::Asc))
135///     .to_owned();
136///
137/// assert_eq!(
138///     index.to_string(MysqlQueryBuilder),
139///     r#"CREATE INDEX `idx-glyph-aspect` ON `glyph` (`aspect` (64) ASC)"#
140/// );
141/// assert_eq!(
142///     index.to_string(PostgresQueryBuilder),
143///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (64) ASC)"#
144/// );
145/// assert_eq!(
146///     index.to_string(SqliteQueryBuilder),
147///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" ASC)"#
148/// );
149/// ```
150///
151/// Partial Index with prefix and order
152/// ```
153/// use sea_query::{tests_cfg::*, *};
154///
155/// let index = Index::create()
156///     .name("idx-glyph-aspect")
157///     .table(Glyph::Table)
158///     .col((Glyph::Aspect, 64, IndexOrder::Asc))
159///     .and_where(Expr::col((Glyph::Table, Glyph::Aspect)).is_in(vec![3, 4]))
160///     .to_owned();
161///
162/// assert_eq!(
163///     index.to_string(PostgresQueryBuilder),
164///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" (64) ASC) WHERE "glyph"."aspect" IN (3, 4)"#
165/// );
166/// assert_eq!(
167///     index.to_string(SqliteQueryBuilder),
168///     r#"CREATE INDEX "idx-glyph-aspect" ON "glyph" ("aspect" ASC) WHERE "glyph"."aspect" IN (3, 4)"#
169/// );
170/// ```
171///
172/// Index include non-key columns
173/// ```
174/// use sea_query::{tests_cfg::*, *};
175///
176/// let index = Index::create()
177///     .name("idx-font-name-include-language")
178///     .table(Font::Table)
179///     .col(Font::Name)
180///     .include(Font::Language)
181///     .to_owned();
182///
183/// assert_eq!(
184///     index.to_string(PostgresQueryBuilder),
185///     r#"CREATE INDEX "idx-font-name-include-language" ON "font" ("name") INCLUDE ("language")"#
186/// )
187/// ```
188///
189/// Functional Index
190/// ```
191/// use sea_query::{tests_cfg::*, *};
192///
193/// let index = Index::create()
194///     .name("idx-character-area")
195///     .table(Character::Table)
196///     .col(Expr::col(Character::SizeH).mul(Expr::col(Character::SizeW)))
197///     .to_owned();
198///
199/// assert_eq!(
200///     index.to_string(MysqlQueryBuilder),
201///     "CREATE INDEX `idx-character-area` ON `character` ((`size_h` * `size_w`))"
202/// );
203/// assert_eq!(
204///     index.to_string(PostgresQueryBuilder),
205///     r#"CREATE INDEX "idx-character-area" ON "character" (("size_h" * "size_w"))"#
206/// );
207/// ```
208#[derive(Default, Debug, Clone)]
209pub struct IndexCreateStatement {
210    pub(crate) table: Option<TableRef>,
211    pub(crate) index: TableIndex,
212    pub(crate) primary: bool,
213    pub(crate) unique: bool,
214    pub(crate) concurrently: bool,
215    pub(crate) nulls_not_distinct: bool,
216    pub(crate) index_type: Option<IndexType>,
217    pub(crate) if_not_exists: bool,
218    pub(crate) r#where: ConditionHolder,
219    pub(crate) include_columns: Vec<DynIden>,
220}
221
222/// Specification of a table index
223#[derive(Debug, Clone)]
224#[non_exhaustive]
225pub enum IndexType {
226    BTree,
227    FullText,
228    Hash,
229    Custom(DynIden),
230}
231
232impl IndexCreateStatement {
233    /// Construct a new [`IndexCreateStatement`]
234    pub fn new() -> Self {
235        Self {
236            table: None,
237            index: Default::default(),
238            primary: false,
239            unique: false,
240            concurrently: false,
241            nulls_not_distinct: false,
242            index_type: None,
243            if_not_exists: false,
244            r#where: ConditionHolder::new(),
245            include_columns: vec![],
246        }
247    }
248
249    /// Create index if index not exists
250    pub fn if_not_exists(&mut self) -> &mut Self {
251        self.if_not_exists = true;
252        self
253    }
254
255    /// Set index name
256    pub fn name<T>(&mut self, name: T) -> &mut Self
257    where
258        T: Into<String>,
259    {
260        self.index.name(name);
261        self
262    }
263
264    /// Set target table
265    pub fn table<T>(&mut self, table: T) -> &mut Self
266    where
267        T: IntoTableRef,
268    {
269        self.table = Some(table.into_table_ref());
270        self
271    }
272
273    /// Add index column
274    pub fn col<C>(&mut self, col: C) -> &mut Self
275    where
276        C: IntoIndexColumn,
277    {
278        self.index.col(col.into_index_column());
279        self
280    }
281
282    /// Set index as primary
283    pub fn primary(&mut self) -> &mut Self {
284        self.primary = true;
285        self
286    }
287
288    /// Set index as unique
289    pub fn unique(&mut self) -> &mut Self {
290        self.unique = true;
291        self
292    }
293
294    /// Set index to be created concurrently. Only available on Postgres.
295    pub fn concurrently(&mut self) -> &mut Self {
296        self.concurrently = true;
297        self
298    }
299
300    /// Set nulls to not be treated as distinct values. Only available on Postgres.
301    pub fn nulls_not_distinct(&mut self) -> &mut Self {
302        self.nulls_not_distinct = true;
303        self
304    }
305
306    /// Set index as full text.
307    /// On MySQL, this is `FULLTEXT`.
308    /// On PgSQL, this is `GIN`.
309    pub fn full_text(&mut self) -> &mut Self {
310        self.index_type(IndexType::FullText)
311    }
312
313    /// Set index type. Not available on Sqlite.
314    pub fn index_type(&mut self, index_type: IndexType) -> &mut Self {
315        self.index_type = Some(index_type);
316        self
317    }
318
319    pub fn include<C>(&mut self, col: C) -> &mut Self
320    where
321        C: IntoIden,
322    {
323        self.include_columns.push(col.into_iden());
324        self
325    }
326
327    pub fn is_primary_key(&self) -> bool {
328        self.primary
329    }
330
331    pub fn is_unique_key(&self) -> bool {
332        self.unique
333    }
334
335    pub fn is_nulls_not_distinct(&self) -> bool {
336        self.nulls_not_distinct
337    }
338
339    pub fn get_index_spec(&self) -> &TableIndex {
340        &self.index
341    }
342
343    pub fn take(&mut self) -> Self {
344        Self {
345            table: self.table.take(),
346            index: self.index.take(),
347            primary: self.primary,
348            unique: self.unique,
349            concurrently: self.concurrently,
350            nulls_not_distinct: self.nulls_not_distinct,
351            index_type: self.index_type.take(),
352            if_not_exists: self.if_not_exists,
353            r#where: self.r#where.clone(),
354            include_columns: self.include_columns.clone(),
355        }
356    }
357}
358
359impl SchemaStatementBuilder for IndexCreateStatement {
360    fn build<T>(&self, schema_builder: T) -> String
361    where
362        T: SchemaBuilder,
363    {
364        let mut sql = String::with_capacity(256);
365        schema_builder.prepare_index_create_statement(self, &mut sql);
366        sql
367    }
368}
369
370impl IndexCreateStatement {
371    pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
372        <Self as SchemaStatementBuilder>::build(self, schema_builder)
373    }
374
375    pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String {
376        <Self as SchemaStatementBuilder>::to_string(self, schema_builder)
377    }
378}
379
380impl ConditionalStatement for IndexCreateStatement {
381    fn and_or_where(&mut self, condition: LogicalChainOper) -> &mut Self {
382        self.r#where.add_and_or(condition);
383        self
384    }
385
386    fn cond_where<C>(&mut self, condition: C) -> &mut Self
387    where
388        C: IntoCondition,
389    {
390        self.r#where.add_condition(condition.into_condition());
391        self
392    }
393}