1use alloc::string::String;
2use alloc::vec::Vec;
3use core::str::FromStr;
4
5use super::*;
6
7#[derive(Debug, Clone, Default)]
9pub struct TableBlock {
10 pub span_range: SpanRange,
12 pub cols: Vec<TableColumn>,
14 pub ident: TableIdent,
16 pub note: Option<NoteBlock>,
18 pub indexes: Option<IndexesBlock>,
20 pub settings: Option<TableSettings>,
22}
23
24#[derive(Debug, Clone, Default)]
26pub struct TableSettings {
27 pub span_range: SpanRange,
29 pub attributes: Vec<Attribute>,
31}
32
33#[derive(Debug, Clone, Default)]
35pub struct TableColumn {
36 pub span_range: SpanRange,
38 pub name: Ident,
40 pub r#type: ColumnType,
42 pub settings: Option<ColumnSettings>,
44}
45
46#[derive(Debug, Clone, Default)]
48pub struct ColumnType {
49 pub span_range: SpanRange,
51 pub raw: String,
53 pub type_name: ColumnTypeName,
55 pub args: Vec<Value>,
57 pub arrays: Vec<Option<u32>>,
59}
60
61#[derive(Debug, PartialEq, Eq, Clone, Default)]
63pub enum ColumnTypeName {
64 #[default]
67 Undef,
68 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#[derive(Debug, Clone, Default)]
169pub struct ColumnSettings {
170 pub span_range: SpanRange,
172 pub attributes: Vec<Attribute>,
174 pub is_pk: bool,
176 pub is_unique: bool,
178 pub nullable: Option<Nullable>,
180 pub is_incremental: bool,
182 pub note: Option<String>,
184 pub default: Option<Value>,
186 pub refs: Vec<RefInline>,
188}
189
190#[derive(Debug, Clone, Default)]
192pub struct TableIdent {
193 pub span_range: SpanRange,
195 pub name: Ident,
197 pub schema: Option<Ident>,
199 pub alias: Option<Ident>,
201}