utopia_query/schema/feature.rs
1//! What a schema can do beyond tables and indexes: each trait is one
2//! capability a dialect may implement (foreign keys, procedures, triggers,
3//! views, partitions, ...), so callers can require it in a bound.
4//!
5//! PHP: the `Utopia\Query\Schema\Feature\*` interfaces.
6
7use super::Schema;
8use super::enums::{ParameterDirection, TriggerEvent, TriggerTiming};
9use super::table::{ForeignKey, Table};
10use crate::builder::{AnyBuilder, Statement};
11use crate::error::Result;
12
13/// Adding and dropping foreign-key constraints on existing tables.
14///
15/// PHP: `Schema\Feature\ForeignKeys`.
16pub trait ForeignKeys: Schema {
17 /// `ALTER TABLE table ADD CONSTRAINT name FOREIGN KEY ...` for `key`.
18 ///
19 /// PHP: `addForeignKey()`.
20 fn add_foreign_key(&self, table: &str, name: &str, key: &ForeignKey) -> Result<Statement>;
21
22 /// Drops the foreign key `name` of `table`.
23 ///
24 /// PHP: `dropForeignKey()`.
25 fn drop_foreign_key(&self, table: &str, name: &str) -> Result<Statement>;
26}
27
28/// Stored procedures.
29///
30/// PHP: `Schema\Feature\Procedures`.
31pub trait Procedures: Schema {
32 /// Creates procedure `name` with `params` (direction, name, SQL type) and
33 /// `body`, which is emitted verbatim and must be trusted.
34 ///
35 /// PHP: `createProcedure()`.
36 fn create_procedure(
37 &self,
38 name: &str,
39 params: &[(ParameterDirection, &str, &str)],
40 body: &str,
41 ) -> Result<Statement>;
42
43 /// Drops procedure `name`.
44 ///
45 /// PHP: `dropProcedure()`.
46 fn drop_procedure(&self, name: &str) -> Result<Statement>;
47}
48
49/// Row triggers.
50///
51/// PHP: `Schema\Feature\Triggers`.
52pub trait Triggers: Schema {
53 /// Creates trigger `name` on `table`, running `body` (verbatim, trusted)
54 /// for each row at `timing` of `event`.
55 ///
56 /// PHP: `createTrigger()`.
57 fn create_trigger(
58 &self,
59 name: &str,
60 table: &str,
61 timing: TriggerTiming,
62 event: TriggerEvent,
63 body: &str,
64 ) -> Result<Statement>;
65
66 /// Drops trigger `name` (PostgreSQL needs its `table`).
67 ///
68 /// PHP: `dropTrigger()`.
69 fn drop_trigger(&self, name: &str, table: Option<&str>) -> Result<Statement>;
70}
71
72/// User-defined enum types.
73///
74/// PHP: `Schema\Feature\Types`.
75pub trait Types: Schema {
76 /// `CREATE TYPE name AS ENUM (values...)`.
77 ///
78 /// PHP: `createType()`.
79 fn create_type(&self, name: &str, values: &[&str]) -> Result<Statement>;
80
81 /// Drops type `name`.
82 ///
83 /// PHP: `dropType()`.
84 fn drop_type(&self, name: &str) -> Result<Statement>;
85}
86
87/// Sequences.
88///
89/// PHP: `Schema\Feature\Sequences`.
90pub trait Sequences: Schema {
91 /// Creates sequence `name` starting at `start`, stepping by `increment_by`
92 /// (PHP's defaults are 1 and 1).
93 ///
94 /// PHP: `createSequence()`.
95 fn create_sequence(&self, name: &str, start: i64, increment_by: i64) -> Result<Statement>;
96
97 /// Drops sequence `name`.
98 ///
99 /// PHP: `dropSequence()`.
100 fn drop_sequence(&self, name: &str) -> Result<Statement>;
101
102 /// Reads the next value of sequence `name`.
103 ///
104 /// PHP: `nextVal()`.
105 fn next_val(&self, name: &str) -> Result<Statement>;
106}
107
108/// Table comments.
109///
110/// PHP: `Schema\Feature\TableComments`.
111pub trait TableComments: Schema {
112 /// Sets the comment of `table`.
113 ///
114 /// PHP: `commentOnTable()`.
115 fn comment_on_table(&self, table: &str, comment: &str) -> Result<Statement>;
116}
117
118/// Column comments as separate statements.
119///
120/// PHP: `Schema\Feature\ColumnComments`.
121pub trait ColumnComments: Schema {
122 /// Sets the comment of `column` of `table`.
123 ///
124 /// PHP: `commentOnColumn()`.
125 fn comment_on_column(&self, table: &str, column: &str, comment: &str) -> Result<Statement>;
126}
127
128/// Adding partitions.
129///
130/// PHP: `Schema\Feature\CreatePartition`.
131pub trait CreatePartition: Schema {
132 /// Adds partition `name` of `parent` for `expression`.
133 ///
134 /// PHP: `createPartition()`.
135 fn create_partition(&self, parent: &str, name: &str, expression: &str) -> Result<Statement>;
136}
137
138/// Dropping partitions.
139///
140/// PHP: `Schema\Feature\DropPartition`.
141pub trait DropPartition: Schema {
142 /// Drops partition `name` of `table`.
143 ///
144 /// PHP: `dropPartition()`.
145 fn drop_partition(&self, table: &str, name: &str) -> Result<Statement>;
146}
147
148/// Views.
149///
150/// PHP: `Schema\Feature\Views`.
151pub trait Views: Schema {
152 /// Creates view `name` of the query `query` builds (its bindings ride
153 /// along).
154 ///
155 /// PHP: `createView()`.
156 fn create_view(&self, name: &str, query: &mut dyn AnyBuilder) -> Result<Statement>;
157
158 /// Drops view `name`.
159 ///
160 /// PHP: `dropView()`.
161 fn drop_view(&self, name: &str) -> Result<Statement>;
162}
163
164/// `CREATE OR REPLACE VIEW`.
165///
166/// PHP: `Schema\Feature\ReplaceView`.
167pub trait ReplaceView: Schema {
168 /// Creates or replaces view `name` of the query `query` builds.
169 ///
170 /// PHP: `createOrReplaceView()`.
171 fn create_or_replace_view(&self, name: &str, query: &mut dyn AnyBuilder) -> Result<Statement>;
172}
173
174/// Databases (schemas on PostgreSQL).
175///
176/// PHP: `Schema\Feature\Databases`.
177pub trait Databases: Schema {
178 /// Creates database `name`.
179 ///
180 /// PHP: `createDatabase()`.
181 fn create_database(&self, name: &str) -> Result<Statement>;
182
183 /// Drops database `name`.
184 ///
185 /// PHP: `dropDatabase()`.
186 fn drop_database(&self, name: &str) -> Result<Statement>;
187}
188
189/// Renaming indexes.
190///
191/// PHP: `Schema\Feature\RenameIndex`.
192pub trait RenameIndex: Schema {
193 /// Renames index `from` of `table` to `to`.
194 ///
195 /// PHP: `renameIndex()`.
196 fn rename_index(&self, table: &str, from: &str, to: &str) -> Result<Statement>;
197}
198
199/// Refreshing table statistics.
200///
201/// PHP: `Schema\Feature\AnalyzeTable`.
202pub trait AnalyzeTable: Schema {
203 /// Analyzes `table`.
204 ///
205 /// PHP: `analyzeTable()`.
206 fn analyze_table(&self, table: &str) -> Result<Statement>;
207}
208
209/// The body of a materialized view: a builder (its SQL inlined, its
210/// bindings carried) or raw SQL, emitted verbatim (trusted input only).
211///
212/// PHP: `Builder|string $body`.
213pub enum ViewBody<'a> {
214 /// A query builder.
215 Builder(&'a mut dyn AnyBuilder),
216 /// Raw SQL.
217 Sql(&'a str),
218}
219
220/// Materialized views.
221///
222/// PHP: `Schema\Feature\MaterializedViews`.
223pub trait MaterializedViews: Schema {
224 /// `CREATE MATERIALIZED VIEW [IF NOT EXISTS] name [TO target] AS body`
225 /// (PHP's default for `if_not_exists` is `true`).
226 ///
227 /// PHP: `createMaterializedView()`.
228 fn create_materialized_view(
229 &self,
230 name: &str,
231 body: ViewBody<'_>,
232 target_table: Option<&str>,
233 if_not_exists: bool,
234 ) -> Result<Statement>;
235
236 /// Drops materialized view `name` (PHP's default for `if_exists` is `true`).
237 ///
238 /// PHP: `dropMaterializedView()`.
239 fn drop_materialized_view(&self, name: &str, if_exists: bool) -> Result<Statement>;
240}
241
242/// `PARTITION BY` in `CREATE TABLE`.
243///
244/// PHP: `Schema\Feature\Partitioning`.
245pub trait Partitioning: Schema {
246 /// The `PARTITION BY ...` clause of `table`, or empty.
247 ///
248 /// PHP: `compileCreatePartitioning()`.
249 fn compile_create_partitioning(&self, table: &Table<Self>) -> String;
250}