dbml_rs/ast/
table.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3use core::str::FromStr;
4
5use super::*;
6
7/// Represents a block of table.
8#[derive(Debug, Clone, Default)]
9pub struct TableBlock {
10  /// The range of the span in the source text.
11  pub span_range: SpanRange,
12  /// Columns or fields of the table.
13  pub cols: Vec<TableColumn>,
14  /// The identifier for the table.
15  pub ident: TableIdent,
16  /// The note block associated with the table block.
17  pub note: Option<NoteBlock>,
18  /// The indexes block.
19  pub indexes: Option<IndexesBlock>,
20  /// The settings for the table.
21  pub settings: Option<TableSettings>,
22}
23
24/// Represents settings of the table.
25#[derive(Debug, Clone, Default)]
26pub struct TableSettings {
27  /// The range of the span in the source text.
28  pub span_range: SpanRange,
29  /// A vector of key and optional value pairs representing attributes of the table.
30  pub attributes: Vec<Attribute>,
31}
32
33/// Represents a single column or field of the table.
34#[derive(Debug, Clone, Default)]
35pub struct TableColumn {
36  /// The range of the span in the source text.
37  pub span_range: SpanRange,
38  /// The column name.
39  pub name: Ident,
40  /// The data type of the column.
41  pub r#type: ColumnType,
42  /// The settings of the column.
43  pub settings: Option<ColumnSettings>,
44}
45
46/// Represents details of the table column.
47#[derive(Debug, Clone, Default)]
48pub struct ColumnType {
49  /// The range of the span in the source text.
50  pub span_range: SpanRange,
51  /// The raw type including type name, args, and arrays.
52  pub raw: String,
53  /// The parsed data type.
54  pub type_name: ColumnTypeName,
55  /// Type arguments.
56  pub args: Vec<Value>,
57  /// Type arrays.
58  pub arrays: Vec<Option<u32>>,
59}
60
61/// Represents data types of the database.
62#[derive(Debug, PartialEq, Eq, Clone, Default)]
63pub enum ColumnTypeName {
64  /// An initial value (default).
65  /// This should not present as a final parsing result.
66  #[default]
67  Undef,
68  /// A type waiting to be parsed and validated.
69  Raw(String),
70  Enum(String),
71  Bit,
72  Varbit,
73  Char,
74  VarChar,
75  Box,
76  Cidr,
77  Circle,
78  Inet,
79  Line,
80  LineSegment,
81  MacAddr,
82  MacAddr8,
83  Money,
84  Path,
85  PGLongSequenceNumber,
86  PGSnapshot,
87  Point,
88  Polygon,
89  TSQuery,
90  TSVector,
91  SmallSerial,
92  Serial,
93  BigSerial,
94  SmallInt,
95  Integer,
96  BigInt,
97  Real,
98  DoublePrecision,
99  Bool,
100  ByteArray,
101  Date,
102  Text,
103  Time,
104  Timetz,
105  Timestamp,
106  Timestamptz,
107  Uuid,
108  Json,
109  Jsonb,
110  Decimal,
111  Xml,
112}
113
114impl FromStr for ColumnTypeName {
115  type Err = ();
116
117  fn from_str(s: &str) -> Result<Self, Self::Err> {
118    let v = match s {
119      "bit" => Self::Bit,
120      "varbit" | "bit varying" => Self::Varbit,
121      "char" | "character" => Self::Char,
122      "varchar" | "character varying" => Self::VarChar,
123      "box" => Self::Box,
124      "cidr" => Self::Cidr,
125      "circle" => Self::Circle,
126      "inet" => Self::Inet,
127      "line" => Self::Line,
128      "lseg" => Self::LineSegment,
129      "macaddr" => Self::MacAddr,
130      "macaddr8" => Self::MacAddr8,
131      "money" => Self::Money,
132      "path" => Self::Path,
133      "pg_lsn" => Self::PGLongSequenceNumber,
134      "pg_snapshot" => Self::PGSnapshot,
135      "point" => Self::Point,
136      "polygon" => Self::Polygon,
137      "tsquery" => Self::TSQuery,
138      "tsvector" => Self::TSVector,
139      "smallserial" | "serial2" => Self::SmallSerial,
140      "serial" | "serial4" => Self::Serial,
141      "bigserial" | "serial8" => Self::BigSerial,
142      "smallint" | "int2" => Self::SmallInt,
143      "integer" | "int" | "int4" => Self::Integer,
144      "bigint" | "int8" => Self::BigInt,
145      "real" | "float4" => Self::Real,
146      "double precision" | "float8" => Self::DoublePrecision,
147      "bool" | "boolean" => Self::Bool,
148      "bytea" => Self::ByteArray,
149      "date" => Self::Date,
150      "text" => Self::Text,
151      "time" | "time without time zone" => Self::Time,
152      "timetz" | "time with time zone" => Self::Timetz,
153      "timestamp" | "timestamp without time zone" => Self::Timestamp,
154      "timestamptz" | "timestamp with time zone" => Self::Timestamptz,
155      "uuid" => Self::Uuid,
156      "json" => Self::Json,
157      "jsonb" => Self::Jsonb,
158      "decimal" | "numeric" => Self::Decimal,
159      "xml" => Self::Xml,
160      _ => return Err(()),
161    };
162
163    Ok(v)
164  }
165}
166
167/// Represents settings of a column.
168#[derive(Debug, Clone, Default)]
169pub struct ColumnSettings {
170  /// The range of the span in the source text.
171  pub span_range: SpanRange,
172  /// A vector of key and optional value pairs representing attributes of the column.
173  pub attributes: Vec<Attribute>,
174  /// A boolean indicating if the column is a primary key.
175  pub is_pk: bool,
176  /// A boolean indicating if the column is unique.
177  pub is_unique: bool,
178  /// An enum indicating the nullable status of the column.
179  pub nullable: Option<Nullable>,
180  /// A boolean indicating if the column is incremental.
181  pub is_incremental: bool,
182  /// A note associated with the column.
183  pub note: Option<String>,
184  /// A default value for the column.
185  pub default: Option<Value>,
186  /// A vector of inline references associated with the column.
187  pub refs: Vec<RefInline>,
188}
189
190/// Represents a table identifier.
191#[derive(Debug, Clone, Default)]
192pub struct TableIdent {
193  /// The range of the span in the source text.
194  pub span_range: SpanRange,
195  /// The name of the table.
196  pub name: Ident,
197  /// The schema of the table, if specified.
198  pub schema: Option<Ident>,
199  /// The alias for the table.
200  pub alias: Option<Ident>,
201}