utopia_query/schema/enums.rs
1//! The schema enumerations: column and index types, foreign-key actions,
2//! partitioning, triggers, procedure parameters and ClickHouse engines.
3
4use crate::method::backed_enum;
5
6backed_enum! {
7 /// The portable type of a column; each dialect compiles it to its own
8 /// SQL type (or BSON type).
9 ///
10 /// PHP: `Utopia\Query\Schema\ColumnType`.
11 pub enum ColumnType: "Utopia\\Query\\Schema\\ColumnType" {
12 /// A bounded string (`VARCHAR(n)`).
13 String = "string",
14 /// A bounded string (`VARCHAR(n)`).
15 Varchar = "varchar",
16 /// Text.
17 Text = "text",
18 /// Medium text (`MEDIUMTEXT` on MySQL).
19 MediumText = "mediumtext",
20 /// Long text (`LONGTEXT` on MySQL).
21 LongText = "longtext",
22 /// An 8-bit integer.
23 TinyInteger = "tinyinteger",
24 /// A 16-bit integer.
25 SmallInteger = "smallinteger",
26 /// A 32-bit integer.
27 Integer = "integer",
28 /// A 64-bit integer.
29 BigInteger = "biginteger",
30 /// A double-precision float.
31 Float = "float",
32 /// A double-precision float.
33 Double = "double",
34 /// A fixed-point decimal.
35 Decimal = "decimal",
36 /// A boolean.
37 Boolean = "boolean",
38 /// A date and time.
39 Datetime = "datetime",
40 /// A timestamp.
41 Timestamp = "timestamp",
42 /// JSON.
43 Json = "json",
44 /// Binary data.
45 Binary = "binary",
46 /// One of a list of values.
47 Enum = "enum",
48 /// A geometry point.
49 Point = "point",
50 /// A geometry line string.
51 Linestring = "linestring",
52 /// A geometry polygon.
53 Polygon = "polygon",
54 /// A vector of floats.
55 Vector = "vector",
56 /// An identifier (a 64-bit integer).
57 Id = "id",
58 /// A UUID.
59 Uuid = "uuid",
60 /// A UUIDv7, stored as a 36-character string.
61 Uuid7 = "uuid7",
62 /// A JSON object.
63 Object = "object",
64 /// A relationship key (a string).
65 Relationship = "relationship",
66 /// An auto-incrementing integer.
67 Serial = "serial",
68 /// An auto-incrementing big integer.
69 BigSerial = "bigserial",
70 /// An auto-incrementing small integer.
71 SmallSerial = "smallserial",
72 /// A ClickHouse `Array(T)`.
73 Array = "array",
74 /// A ClickHouse `Tuple(...)`.
75 Tuple = "tuple",
76 }
77}
78
79backed_enum! {
80 /// The kind of an index.
81 ///
82 /// PHP: `Utopia\Query\Schema\IndexType`.
83 pub enum IndexType: "Utopia\\Query\\Schema\\IndexType" {
84 /// A key.
85 Key = "key",
86 /// A plain index.
87 Index = "index",
88 /// A unique index.
89 Unique = "unique",
90 /// A full-text index.
91 Fulltext = "fulltext",
92 /// A spatial index.
93 Spatial = "spatial",
94 /// An object index.
95 Object = "object",
96 /// An HNSW vector index on euclidean distance.
97 HnswEuclidean = "hnsw_euclidean",
98 /// An HNSW vector index on cosine distance.
99 HnswCosine = "hnsw_cosine",
100 /// An HNSW vector index on inner product.
101 HnswDot = "hnsw_dot",
102 /// A trigram index.
103 Trigram = "trigram",
104 /// A TTL index.
105 Ttl = "ttl",
106 }
107}
108
109backed_enum! {
110 /// What a foreign key does when the referenced row is deleted or updated.
111 ///
112 /// PHP: `Utopia\Query\Schema\ForeignKeyAction`.
113 pub enum ForeignKeyAction: "Utopia\\Query\\Schema\\ForeignKeyAction" {
114 /// `CASCADE`.
115 Cascade = "cascade",
116 /// `SET NULL`.
117 SetNull = "setNull",
118 /// `SET DEFAULT`.
119 SetDefault = "setDefault",
120 /// `RESTRICT`.
121 Restrict = "restrict",
122 /// `NO ACTION`.
123 NoAction = "noAction",
124 }
125}
126
127impl ForeignKeyAction {
128 /// The SQL: `CASCADE`, `SET NULL`, `SET DEFAULT`, `RESTRICT` or `NO ACTION`.
129 ///
130 /// PHP: `toSql()`.
131 pub const fn to_sql(self) -> &'static str {
132 match self {
133 ForeignKeyAction::Cascade => "CASCADE",
134 ForeignKeyAction::SetNull => "SET NULL",
135 ForeignKeyAction::SetDefault => "SET DEFAULT",
136 ForeignKeyAction::Restrict => "RESTRICT",
137 ForeignKeyAction::NoAction => "NO ACTION",
138 }
139 }
140}
141
142backed_enum! {
143 /// The order of an index column.
144 ///
145 /// PHP: `Utopia\Query\Schema\Order`.
146 pub enum Order: "Utopia\\Query\\Schema\\Order" {
147 /// `ASC`.
148 Asc = "ASC",
149 /// `DESC`.
150 Desc = "DESC",
151 }
152}
153
154backed_enum! {
155 /// The direction of a stored procedure parameter.
156 ///
157 /// PHP: `Utopia\Query\Schema\ParameterDirection`.
158 pub enum ParameterDirection: "Utopia\\Query\\Schema\\ParameterDirection" {
159 /// `IN`.
160 In = "IN",
161 /// `OUT`.
162 Out = "OUT",
163 /// `INOUT`.
164 InOut = "INOUT",
165 }
166}
167
168backed_enum! {
169 /// How a table is partitioned.
170 ///
171 /// PHP: `Utopia\Query\Schema\PartitionType`.
172 pub enum PartitionType: "Utopia\\Query\\Schema\\PartitionType" {
173 /// `RANGE`.
174 Range = "RANGE",
175 /// `LIST`.
176 List = "LIST",
177 /// `HASH`.
178 Hash = "HASH",
179 }
180}
181
182backed_enum! {
183 /// The statement a trigger fires on.
184 ///
185 /// PHP: `Utopia\Query\Schema\TriggerEvent`.
186 pub enum TriggerEvent: "Utopia\\Query\\Schema\\TriggerEvent" {
187 /// `INSERT`.
188 Insert = "INSERT",
189 /// `UPDATE`.
190 Update = "UPDATE",
191 /// `DELETE`.
192 Delete = "DELETE",
193 }
194}
195
196backed_enum! {
197 /// When a trigger fires.
198 ///
199 /// PHP: `Utopia\Query\Schema\TriggerTiming`.
200 pub enum TriggerTiming: "Utopia\\Query\\Schema\\TriggerTiming" {
201 /// `BEFORE`.
202 Before = "BEFORE",
203 /// `AFTER`.
204 After = "AFTER",
205 /// `INSTEAD OF`.
206 InsteadOf = "INSTEAD OF",
207 }
208}
209
210backed_enum! {
211 /// A ClickHouse table engine.
212 ///
213 /// PHP: `Utopia\Query\Schema\ClickHouse\Engine`.
214 pub enum Engine: "Utopia\\Query\\Schema\\ClickHouse\\Engine" {
215 /// `MergeTree()`.
216 MergeTree = "MergeTree",
217 /// `ReplacingMergeTree([version])`.
218 ReplacingMergeTree = "ReplacingMergeTree",
219 /// `SummingMergeTree([columns])`.
220 SummingMergeTree = "SummingMergeTree",
221 /// `AggregatingMergeTree()`.
222 AggregatingMergeTree = "AggregatingMergeTree",
223 /// `CollapsingMergeTree(sign)`.
224 CollapsingMergeTree = "CollapsingMergeTree",
225 /// `ReplicatedMergeTree(path, replica)`.
226 ReplicatedMergeTree = "ReplicatedMergeTree",
227 /// `Memory`.
228 Memory = "Memory",
229 /// `Log`.
230 Log = "Log",
231 /// `TinyLog`.
232 TinyLog = "TinyLog",
233 /// `StripeLog`.
234 StripeLog = "StripeLog",
235 }
236}
237
238impl Engine {
239 /// Whether the engine takes an `ORDER BY` clause (the MergeTree family
240 /// does, the log and memory engines do not).
241 ///
242 /// PHP: `requiresOrderBy()`.
243 pub const fn requires_order_by(self) -> bool {
244 !matches!(self, Engine::Memory | Engine::Log | Engine::TinyLog | Engine::StripeLog)
245 }
246}
247
248backed_enum! {
249 /// The algorithm of a ClickHouse data-skipping index.
250 ///
251 /// PHP: `Utopia\Query\Schema\ClickHouse\IndexAlgorithm`.
252 pub enum IndexAlgorithm: "Utopia\\Query\\Schema\\ClickHouse\\IndexAlgorithm" {
253 /// `minmax`.
254 MinMax = "minmax",
255 /// `set(n)`.
256 Set = "set",
257 /// `bloom_filter([rate])`.
258 BloomFilter = "bloom_filter",
259 /// `ngrambf_v1(n, size, hashes, seed)`.
260 NgramBloomFilter = "ngrambf_v1",
261 /// `tokenbf_v1(size, hashes, seed)`.
262 TokenBloomFilter = "tokenbf_v1",
263 /// `inverted`.
264 Inverted = "inverted",
265 }
266}
267
268impl IndexAlgorithm {
269 /// Whether the algorithm takes arguments (`minmax` and `inverted` do not).
270 pub const fn accepts_args(self) -> bool {
271 !matches!(self, IndexAlgorithm::MinMax | IndexAlgorithm::Inverted)
272 }
273}