1use crate::ast::{
2 Action, Cage, Condition, Distance, Expr, GroupByMode, IndexDef, Join, LockMode, OverridingKind,
3 SampleMethod, SetOp, TableConstraint,
4};
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
8pub struct Qail {
9 pub action: Action,
11 pub table: String,
13 pub columns: Vec<Expr>,
15 pub joins: Vec<Join>,
17 pub cages: Vec<Cage>,
19 pub distinct: bool,
21 pub index_def: Option<IndexDef>,
23 pub table_constraints: Vec<TableConstraint>,
25 pub set_ops: Vec<(SetOp, Box<Qail>)>,
27 pub having: Vec<Condition>,
29 pub group_by_mode: GroupByMode,
31 pub ctes: Vec<CTEDef>,
33 pub distinct_on: Vec<Expr>,
35 pub returning: Option<Vec<Expr>>,
37 pub on_conflict: Option<OnConflict>,
39 #[serde(default)]
41 pub merge: Option<Merge>,
42 pub source_query: Option<Box<Qail>>,
44 pub channel: Option<String>,
46 pub payload: Option<String>,
48 pub savepoint_name: Option<String>,
50 pub from_tables: Vec<String>,
52 pub using_tables: Vec<String>,
54 pub lock_mode: Option<LockMode>,
56 pub skip_locked: bool,
58 pub fetch: Option<(u64, bool)>,
60 pub default_values: bool,
62 pub overriding: Option<OverridingKind>,
64 pub sample: Option<(SampleMethod, f64, Option<u64>)>,
66 pub only_table: bool,
68 pub vector: Option<Vec<f32>>,
71 pub score_threshold: Option<f32>,
73 pub vector_name: Option<String>,
75 pub with_vector: bool,
77 pub vector_size: Option<u64>,
79 pub distance: Option<Distance>,
81 pub on_disk: Option<bool>,
83 pub function_def: Option<crate::ast::FunctionDef>,
86 pub trigger_def: Option<crate::ast::TriggerDef>,
88 pub policy_def: Option<crate::migrate::policy::RlsPolicy>,
90}
91
92#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
94pub struct CTEDef {
95 pub name: String,
97 pub recursive: bool,
99 pub columns: Vec<String>,
101 pub base_query: Box<Qail>,
103 pub recursive_query: Option<Box<Qail>>,
105 pub source_table: Option<String>,
107}
108
109#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
111pub struct OnConflict {
112 pub columns: Vec<String>,
114 pub action: ConflictAction,
116}
117
118#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
120pub enum ConflictAction {
121 DoNothing,
123 DoUpdate {
125 assignments: Vec<(String, Expr)>,
127 },
128}
129
130#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
132pub struct Merge {
133 pub target_alias: Option<String>,
135 pub source: MergeSource,
137 pub on: Vec<Condition>,
139 pub clauses: Vec<MergeClause>,
141}
142
143#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
145pub enum MergeSource {
146 Table {
148 name: String,
150 alias: Option<String>,
152 },
153 Query {
155 query: Box<Qail>,
157 alias: Option<String>,
159 },
160}
161
162#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
164pub struct MergeClause {
165 pub match_kind: MergeMatchKind,
167 pub condition: Vec<Condition>,
169 pub action: MergeAction,
171}
172
173#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
175pub enum MergeMatchKind {
176 Matched,
178 NotMatchedByTarget,
180 NotMatchedBySource,
182}
183
184#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
186pub enum MergeAction {
187 Update {
189 assignments: Vec<(String, Expr)>,
191 },
192 Insert {
194 columns: Vec<String>,
196 values: Vec<Expr>,
198 },
199 Delete,
201 DoNothing,
203}
204
205impl Default for OnConflict {
206 fn default() -> Self {
207 Self {
208 columns: vec![],
209 action: ConflictAction::DoNothing,
210 }
211 }
212}
213
214impl ConflictAction {
215 pub(crate) fn update_assignments(&self) -> Option<&[(String, Expr)]> {
216 match self {
217 Self::DoNothing => None,
218 Self::DoUpdate { assignments } => Some(assignments),
219 }
220 }
221}
222
223impl Default for Qail {
224 fn default() -> Self {
225 Self {
226 action: Action::Get,
227 table: String::new(),
228 columns: vec![],
229 joins: vec![],
230 cages: vec![],
231 distinct: false,
232 index_def: None,
233 table_constraints: vec![],
234 set_ops: vec![],
235 having: vec![],
236 group_by_mode: GroupByMode::Simple,
237 ctes: vec![],
238 distinct_on: vec![],
239 returning: None,
240 on_conflict: None,
241 merge: None,
242 source_query: None,
243 channel: None,
244 payload: None,
245 savepoint_name: None,
246 from_tables: vec![],
247 using_tables: vec![],
248 lock_mode: None,
249 skip_locked: false,
250 fetch: None,
251 default_values: false,
252 overriding: None,
253 sample: None,
254 only_table: false,
255 vector: None,
257 score_threshold: None,
258 vector_name: None,
259 with_vector: false,
260 vector_size: None,
261 distance: None,
262 on_disk: None,
263 function_def: None,
265 trigger_def: None,
266 policy_def: None,
267 }
268 }
269}
270
271mod advanced;
273mod constructors;
274mod cte;
275mod merge;
276mod query;
277mod rls;
278mod vector;
279
280impl std::fmt::Display for Qail {
281 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
282 use crate::fmt::Formatter;
284 match Formatter::new().format(self) {
285 Ok(s) => write!(f, "{}", s),
286 Err(_) => write!(f, "{:?}", self), }
288 }
289}