dbml_rs/ast/
indexes.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3use core::str::FromStr;
4
5use super::*;
6
7/// Represents an indexes block inside a table block.
8/// Indexes allow users to quickly locate and access the data. Users can define single or multi-column indexes.
9#[derive(Debug, Clone, Default)]
10pub struct IndexesBlock {
11  /// The range of the span in the source text.
12  pub span_range: SpanRange,
13  /// Defined items inside an indexes block.
14  pub defs: Vec<IndexesDef>,
15}
16
17/// Represents an indexes definition or each item in an indexes block.
18#[derive(Debug, Clone, Default)]
19pub struct IndexesDef {
20  /// The range of the span in the source text.
21  pub span_range: SpanRange,
22  /// Table column names for indexing which can be composite.
23  pub cols: Vec<IndexesColumnType>,
24  /// A Configuration for the specified column names.
25  pub settings: Option<IndexesSettings>,
26}
27
28/// Represents settings of an indexes definition.
29#[derive(Debug, Clone, Default)]
30pub struct IndexesSettings {
31  /// The range of the span in the source text.
32  pub span_range: SpanRange,
33  /// A vector of key and optional value pairs representing attributes of the indexes definition.
34  pub attributes: Vec<Attribute>,
35  /// A Type of index (btree, gin, gist, hash depending on DB).
36  pub r#type: Option<IndexesType>,
37  /// A unique index.
38  pub is_unique: bool,
39  /// A primary index.
40  pub is_pk: bool,
41  /// A note.
42  pub note: Option<String>,
43  /// An index name.
44  pub name: Option<String>,
45}
46
47/// Represents the type of column for indexing.
48#[derive(Debug, Clone)]
49pub enum IndexesColumnType {
50  /// Represents a column name with the given identifier.
51  String(Ident),
52  /// Represents an expression with the given literal expression.
53  Expr(Literal),
54}
55
56/// Represents different types of indexes that can be used.
57#[derive(Debug, PartialEq, Eq, Clone)]
58pub enum IndexesType {
59  /// Represents a B-tree index.
60  BTree,
61  /// Represents a GIN (Generalized Inverted Index) index.
62  Gin,
63  /// Represents a GiST (Generalized Search Tree) index.
64  Gist,
65  /// Represents a hash index.
66  Hash,
67}
68
69impl FromStr for IndexesType {
70  type Err = String;
71
72  fn from_str(s: &str) -> Result<Self, Self::Err> {
73    match s {
74      "btree" => Ok(Self::BTree),
75      "gin" => Ok(Self::Gin),
76      "gist" => Ok(Self::Gist),
77      "hash" => Ok(Self::Hash),
78      _ => Err(format!("'{}' type is not supported!", s)),
79    }
80  }
81}