1use serde::{Deserialize, Serialize};
13
14mod acl_role_specification;
15mod assignment_target;
16mod composites;
17mod constraints;
18mod cte;
19mod domains;
20mod enum_functions;
21mod enums;
22mod events;
23mod expressions;
24mod foreign_wrappers;
25mod from;
26mod function_binding;
27mod identity_sequence;
28mod indexes;
29mod interval;
30mod legacy_dispatch;
31mod locking;
32mod namespaces;
33mod object_acl;
34mod overriding;
35mod ranges;
36mod referenced_partition;
37mod relation_hierarchy;
38mod relation_lifecycle;
39mod role_specification;
40mod routine_attributes;
41mod routine_security;
42mod routines;
43mod sequence;
44mod sequence_declaration;
45mod table_alteration;
46mod type_lifecycle;
47mod type_privileges;
48mod types;
49
50pub use acl_role_specification::AclRoleSpecification;
51pub use assignment_target::{AssignmentStep, AssignmentTarget, AssignmentTargets};
52pub use composites::*;
53pub use constraints::*;
54pub use cte::*;
55pub use domains::*;
56pub use enum_functions::EnumFunctionOperation;
57pub use enums::*;
58pub use events::*;
59pub use expressions::*;
60pub use foreign_wrappers::*;
61pub use from::*;
62pub use function_binding::*;
63pub use identity_sequence::{DeferredSQLError, IdentitySequenceDeclaration, IdentitySequenceName};
64pub use indexes::*;
65pub use interval::*;
66pub use locking::*;
67pub use namespaces::*;
68pub use object_acl::ObjectAclEntry;
69pub use overriding::OverridingKind;
70pub use ranges::*;
71pub use referenced_partition::ReferencedPartitionConstraint;
72pub use relation_hierarchy::*;
73pub use relation_lifecycle::*;
74pub use role_specification::RoleSpecification;
75pub use routine_attributes::*;
76pub use routine_security::*;
77pub use routines::*;
78pub use sequence::*;
79pub use sequence_declaration::{SequenceDeclaration, SequenceOptionValue};
80pub use table_alteration::*;
81pub use type_lifecycle::*;
82pub use type_privileges::*;
83pub use types::*;
84
85const fn default_include_descendants() -> bool {
86 true
87}
88
89const fn default_true() -> bool {
90 true
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94pub enum GeneratedColumnKind {
95 Virtual,
96 Stored,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct GeneratedColumn {
101 pub kind: GeneratedColumnKind,
102 pub expression: Box<Expr>,
103 #[serde(default, skip_serializing_if = "Vec::is_empty")]
104 pub function_dependencies: Vec<GeneratedFunctionDependency>,
105}
106
107#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
108pub struct IndexColumnOrder {
109 pub descending: bool,
110 pub nulls_first: bool,
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct CreateIndex {
115 #[serde(default)]
116 pub included_columns: Vec<String>,
117 #[serde(default)]
118 pub column_order: Vec<IndexColumnOrder>,
119 #[serde(default)]
120 pub predicate: Option<Box<Expr>>,
121 pub name: Option<String>,
122 pub table: String,
123 pub access_method: String,
125 pub columns: Vec<IndexKey>,
126 #[serde(default)]
127 pub unique: bool,
128 #[serde(default)]
129 pub nulls_not_distinct: bool,
130 pub if_not_exists: bool,
132 pub options: Vec<(String, String)>,
136 #[serde(default, skip_serializing_if = "Vec::is_empty")]
138 pub option_namespaces: Vec<String>,
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct DropStmt {
143 pub kind: DropKind,
144 pub names: Vec<String>,
145 pub if_exists: bool,
146 pub cascade: bool,
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
150pub enum DropKind {
151 ForeignWrapper,
152 Table,
153 ForeignTable,
154 Index,
155 View,
156 MaterializedView,
157 Schema,
158 Sequence,
159 Domain,
160 Type,
162 ForeignServer,
163}
164
165#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub struct InsertStmt {
167 pub table: String,
168 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
170 pub target_relation_bound: bool,
171 pub target_qualifier: String,
173 #[serde(default, skip_serializing_if = "Option::is_none")]
175 pub target_alias: Option<String>,
176 #[serde(default = "default_include_descendants")]
177 pub include_descendants: bool,
178 pub columns: Vec<AssignmentTarget>,
179 #[serde(default, skip_serializing_if = "Option::is_none")]
181 pub overriding: Option<OverridingKind>,
182 pub with: Vec<CTE>,
184 pub rows: Vec<Vec<ValueExpr>>,
186 pub select_source: Option<Box<SelectStmt>>,
190 pub on_conflict: Option<OnConflict>,
193 pub returning: Vec<Projection>,
195 pub returning_aliases: ReturningAliases,
198}
199
200#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
201pub struct ReturningAliases {
202 pub old: String,
203 pub new: String,
204 #[serde(default)]
205 pub old_explicit: bool,
206 #[serde(default)]
207 pub new_explicit: bool,
208}
209
210impl Default for ReturningAliases {
211 fn default() -> Self {
212 Self {
213 old: "old".into(),
214 new: "new".into(),
215 old_explicit: false,
216 new_explicit: false,
217 }
218 }
219}
220
221#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
222pub struct OnConflict {
223 #[serde(default)]
224 pub predicate: Option<Box<Expr>>,
225 #[serde(default)]
226 pub constraint: Option<String>,
227 pub conflict_columns: Vec<String>,
231 #[serde(default)]
232 pub expressions: Vec<Expr>,
233 pub action: OnConflictAction,
234}
235
236#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
237pub enum OnConflictAction {
238 Nothing,
240 Update {
244 assignments: Vec<(AssignmentTargets, Expr)>,
245 r#where: Option<Box<Expr>>,
246 },
247}
248
249#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
250pub struct SelectStmt {
251 #[serde(default, skip_serializing_if = "Vec::is_empty")]
253 pub windows: Vec<WindowDefinition>,
254 pub projections: Vec<Projection>,
255 #[serde(default, skip_serializing_if = "Vec::is_empty")]
259 pub values: Vec<Vec<Expr>>,
260 pub from: Option<FromClause>,
261 pub r#where: Option<Expr>,
262 pub group_by: Vec<Expr>,
263 pub grouping_sets: Vec<Vec<Expr>>,
269 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
271 pub group_distinct: bool,
272 pub having: Option<Expr>,
276 pub order_by: Vec<OrderBy>,
277 pub limit: Option<Expr>,
281 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
283 pub with_ties: bool,
284 pub offset: Option<Expr>,
286 pub with: Vec<CTE>,
288 pub set_op: Option<Box<SetOp>>,
292 pub distinct: bool,
295 pub distinct_on: Vec<Expr>,
298 #[serde(default, skip_serializing_if = "Vec::is_empty")]
300 pub locking: Vec<LockingClause>,
301}
302
303#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
304pub struct SetOp {
305 pub kind: SetOpKind,
306 pub all: bool,
307 #[serde(default, skip_serializing_if = "Option::is_none")]
311 pub left: Option<Box<SelectStmt>>,
312 pub right: SelectStmt,
313 pub combined_order_by: Vec<OrderBy>,
316 pub combined_limit: Option<Expr>,
319 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
321 pub combined_with_ties: bool,
322 pub combined_offset: Option<Expr>,
324}
325
326#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
327pub enum SetOpKind {
328 Union,
329 Intersect,
330 Except,
331}
332
333#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
335pub enum DiscardTarget {
336 All,
337 Plans,
338 Sequences,
339 Temp,
340}
341
342#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
343pub struct UpdateStmt {
344 pub table: String,
345 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
347 pub target_relation_bound: bool,
348 pub target_qualifier: String,
349 #[serde(default, skip_serializing_if = "Option::is_none")]
351 pub target_alias: Option<String>,
352 #[serde(default = "default_include_descendants")]
353 pub include_descendants: bool,
354 pub assignments: Vec<(AssignmentTargets, Expr)>,
355 pub r#where: Option<Expr>,
356 pub with: Vec<CTE>,
358 pub from: Option<FromClause>,
361 pub returning: Vec<Projection>,
363 pub returning_aliases: ReturningAliases,
364}
365
366#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
367pub struct DeleteStmt {
368 pub table: String,
369 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
371 pub target_relation_bound: bool,
372 pub target_qualifier: String,
373 #[serde(default, skip_serializing_if = "Option::is_none")]
375 pub target_alias: Option<String>,
376 #[serde(default = "default_include_descendants")]
377 pub include_descendants: bool,
378 pub r#where: Option<Expr>,
379 pub with: Vec<CTE>,
381 pub using: Option<FromClause>,
385 pub returning: Vec<Projection>,
387 pub returning_aliases: ReturningAliases,
388}
389
390#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
391pub struct SetConstraintName {
392 pub catalog: Option<String>,
393 pub schema: Option<String>,
394 pub name: String,
395}
396
397#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
399pub struct VacuumOption {
400 pub name: String,
401 pub value: Option<VacuumOptionValue>,
402}
403
404#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
405pub enum VacuumOptionValue {
406 Boolean(bool),
407 Integer(i32),
408 String(String),
409}
410
411#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
413pub struct VacuumTarget {
414 pub catalog: Option<String>,
415 pub table: String,
416 #[serde(default = "default_include_descendants")]
417 pub include_descendants: bool,
418 #[serde(default, skip_serializing_if = "Vec::is_empty")]
419 pub columns: Vec<String>,
420}
421
422#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
423pub struct VacuumStmt {
424 #[serde(default, skip_serializing_if = "Vec::is_empty")]
425 pub options: Vec<VacuumOption>,
426 #[serde(default, skip_serializing_if = "Vec::is_empty")]
427 pub targets: Vec<VacuumTarget>,
428}
429
430#[derive(Debug, Clone, Serialize, Deserialize)]
431pub enum Statement {
432 CreateDomain(CreateDomain),
433 AlterDomain(AlterDomain),
434 CreateEnum(CreateEnum),
435 CreateCompositeType(CreateCompositeType),
436 AlterEnum(AlterEnum),
437 AlterTypeObject(AlterTypeObject),
438 GrantType(GrantTypeStmt),
439 CreateTable(CreateTable),
440 CreateTableIfNotExists(DeferredCreateTable),
441 CreateIndex(CreateIndex),
442 RenameIndex(RenameIndexStmt),
443 Insert(InsertStmt),
444 Select(Box<SelectStmt>),
448 Update(UpdateStmt),
449 Delete(DeleteStmt),
450 Drop(DropStmt),
451 AlterTable(AlterTableStmt),
452 AlterForeignTable(AlterForeignTableStmt),
453 AlterView(AlterViewStmt),
454 CreateView {
456 name: String,
457 #[serde(default)]
458 column_names: Vec<String>,
459 body: Box<SelectStmt>,
460 or_replace: bool,
461 #[serde(default)]
462 persistence: RelationPersistence,
463 #[serde(default, skip_serializing_if = "Vec::is_empty")]
465 options: Vec<(String, String)>,
466 },
467 CreateMaterializedView {
469 name: String,
470 #[serde(default)]
471 column_names: Vec<String>,
472 #[serde(default)]
473 if_not_exists: bool,
474 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
475 with_no_data: bool,
476 #[serde(default, skip_serializing_if = "Vec::is_empty")]
477 options: Vec<(String, String)>,
478 body: Box<SelectStmt>,
479 },
480 RefreshMaterializedView {
482 name: String,
483 concurrently: bool,
484 with_no_data: bool,
485 },
486 CreateSchema {
488 name: Option<String>,
489 if_not_exists: bool,
490 #[serde(default, skip_serializing_if = "Option::is_none")]
491 authorization: Option<SchemaAuthorization>,
492 },
493 AlterSchemaOwner {
494 name: String,
495 new_owner: RoleSpecification,
496 },
497 RenameSchema {
499 name: String,
500 new_name: String,
501 },
502 Notify {
504 channel: String,
505 payload: String,
506 },
507 Listen {
509 channel: String,
510 },
511 Unlisten {
513 channel: Option<String>,
514 },
515 SetVariable {
519 name: String,
520 value: String,
521 #[serde(default)]
522 local: bool,
523 #[serde(default)]
524 is_default: bool,
525 },
526 ResetVariable {
528 name: String,
529 },
530 ResetAllVariables,
532 SetConstraints {
534 constraints: Vec<SetConstraintName>,
535 deferred: bool,
536 },
537 ShowVariable {
540 name: String,
541 },
542 Discard {
545 target: DiscardTarget,
546 },
547 Load {
552 library: String,
553 },
554 Explain {
557 analyze: bool,
558 verbose: bool,
559 format: Option<String>,
560 body: Box<Statement>,
561 },
562 Analyze {
565 table: Option<String>,
566 },
567 Vacuum(VacuumStmt),
569 LockTable(LockTableStmt),
571 Truncate {
574 tables: Vec<TruncateTarget>,
575 cascade: bool,
576 #[serde(default)]
577 restart_identity: bool,
578 },
579 Transaction(TransactionStmt),
581 DeclareCursor(DeclareCursorStmt),
583 FetchCursor(FetchCursorStmt),
585 CloseCursor {
587 name: Option<String>,
588 },
589 CreateSequence(CreateSequence),
591 AlterSequence(AlterSequence),
594 CreateTableAs {
596 name: String,
597 if_not_exists: bool,
598 #[serde(default, skip_serializing_if = "Vec::is_empty")]
599 column_names: Vec<String>,
600 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
601 with_no_data: bool,
602 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
604 select_into: bool,
605 #[serde(default)]
606 persistence: RelationPersistence,
607 #[serde(default)]
608 on_commit: OnCommitAction,
609 body: Box<SelectStmt>,
610 },
611 Prepare {
613 name: String,
614 #[serde(default)]
615 parameter_types: Vec<ColumnType>,
616 body: Box<Statement>,
617 },
618 Execute {
620 name: String,
621 params: Vec<Expr>,
622 },
623 Deallocate {
625 name: Option<String>,
626 },
627 Values {
630 rows: Vec<Vec<Expr>>,
631 },
632 CreateForeignWrapper(CreateForeignWrapper),
634 CreateForeignServer(CreateForeignServer),
636 CreateForeignTable(CreateForeignTable),
638 #[serde(alias = "CreateForeignTableIfNotExists")]
640 CreateForeignTableDefinition(DeferredCreateForeignTable),
641 Merge(MergeStmt),
644 CreateFunction(Box<CreateFunction>),
647 DropFunction(DropFunctionStmt),
649 AlterRoutine(AlterRoutineStmt),
651 AlterRoutineOwner(AlterRoutineOwnerStmt),
652 RenameRoutine(RenameRoutineStmt),
653 GrantRoutine(GrantRoutineStmt),
654 GrantTable(GrantTableStmt),
655 GrantSequence(GrantSequenceStmt),
656 GrantDatabase(GrantDatabaseStmt),
657 GrantSchema(GrantSchemaStmt),
658 GrantRole(GrantRoleStmt),
659 CreateRole(CreateRoleStmt),
660 AlterRole(AlterRoleStmt),
661 RenameRole(RenameRoleStmt),
662 DropRole(DropRoleStmt),
663 CreateTrigger(CreateTrigger),
665 DropTrigger(DropTrigger),
667 CreateRule(CreateRule),
669 DropRule(DropRule),
671 DoBlock {
673 language: String,
674 body: String,
675 },
676 Call {
679 name: String,
680 args: Vec<Expr>,
681 },
682}
683
684#[derive(Debug, Clone, Serialize, Deserialize)]
685pub struct TruncateTarget {
686 pub table: String,
687 #[serde(default = "default_include_descendants")]
688 pub include_descendants: bool,
689}
690
691#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
692pub struct MergeTargetColumnBinding {
693 pub object_id: [u8; 16],
694 #[serde(default, skip_serializing_if = "Option::is_none")]
696 pub attribute_number: Option<i16>,
697 #[serde(default, skip_serializing_if = "std::collections::BTreeSet::is_empty")]
699 pub domain_dependencies: std::collections::BTreeSet<u32>,
700}
701
702#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
703pub struct MergeStmt {
704 #[serde(default)]
705 pub with: Vec<CTE>,
706 pub target: String,
707 pub target_qualifier: String,
708 pub target_alias: Option<String>,
709 #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
711 pub target_column_bindings: std::collections::BTreeMap<String, MergeTargetColumnBinding>,
712 #[serde(default = "default_include_descendants")]
713 pub include_descendants: bool,
714 pub source: FromClause,
715 pub join_condition: Expr,
716 pub when_clauses: Vec<MergeWhen>,
717 pub returning: Vec<Projection>,
719 pub returning_aliases: ReturningAliases,
720}
721
722#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
723pub enum MergeWhen {
724 UpdateMatched {
726 condition: Option<Expr>,
727 assignments: Vec<(AssignmentTargets, Expr)>,
728 },
729 DeleteMatched { condition: Option<Expr> },
731 UpdateNotMatchedBySource {
733 condition: Option<Expr>,
734 assignments: Vec<(AssignmentTargets, Expr)>,
735 },
736 DeleteNotMatchedBySource { condition: Option<Expr> },
738 InsertNotMatched {
740 condition: Option<Expr>,
741 columns: Vec<AssignmentTarget>,
742 #[serde(default, skip_serializing_if = "Option::is_none")]
743 overriding: Option<OverridingKind>,
744 values: Vec<Expr>,
745 },
746 NothingMatched { condition: Option<Expr> },
748 NothingNotMatched { condition: Option<Expr> },
750 NothingNotMatchedBySource { condition: Option<Expr> },
752}
753
754#[derive(Debug, Clone, Serialize, Deserialize)]
755pub struct CreateForeignServer {
756 pub name: String,
757 pub fdw_type: String,
758 #[serde(default)]
759 pub server_type: Option<String>,
760 #[serde(default)]
761 pub version: Option<String>,
762 pub options: Vec<(String, String)>,
763 pub if_not_exists: bool,
764}
765
766#[derive(Debug, Clone, Serialize, Deserialize)]
767pub struct CreateForeignTable {
768 pub name: String,
769 pub server_name: String,
770 pub columns: Vec<ColumnDef>,
771 #[serde(default)]
772 pub checks: Vec<TableCheck>,
773 #[serde(default, skip_serializing_if = "Option::is_none")]
775 pub not_null_declarations: Option<Vec<NotNullDeclaration>>,
776 pub options: Vec<(String, String)>,
777 pub if_not_exists: bool,
778}
779
780#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
782pub struct DeferredCreateForeignTable {
783 pub name: String,
784 pub server_name: String,
785 pub definition_sql: String,
786 #[serde(default = "default_true")]
788 pub if_not_exists: bool,
789}
790
791#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
792pub enum TransactionIsolationLevel {
793 ReadUncommitted,
794 ReadCommitted,
795 RepeatableRead,
796 Serializable,
797}
798
799impl TransactionIsolationLevel {
800 #[must_use]
801 pub const fn as_str(self) -> &'static str {
802 match self {
803 Self::ReadUncommitted => "read uncommitted",
804 Self::ReadCommitted => "read committed",
805 Self::RepeatableRead => "repeatable read",
806 Self::Serializable => "serializable",
807 }
808 }
809}
810
811#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
812pub struct TransactionCharacteristics {
813 pub isolation: Option<TransactionIsolationLevel>,
814 pub read_only: Option<bool>,
815 pub deferrable: Option<bool>,
816}
817
818#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
819pub enum TransactionStmt {
820 Begin,
821 BeginWithCharacteristics(TransactionCharacteristics),
822 Commit,
823 CommitAndChain,
824 Rollback,
825 RollbackAndChain,
826 SetCharacteristics(TransactionCharacteristics),
827 SetSessionCharacteristics(TransactionCharacteristics),
828 SetSnapshot(String),
829 Savepoint(String),
830 ReleaseSavepoint(String),
831 RollbackToSavepoint(String),
832}
833
834#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
835pub enum CursorDirection {
836 Forward,
837 Backward,
838 Absolute,
839 Relative,
840}
841
842#[derive(Debug, Clone, Serialize, Deserialize)]
843pub struct DeclareCursorStmt {
844 pub name: String,
845 pub binary: bool,
846 pub scroll: Option<bool>,
848 pub hold: bool,
849 pub query: Box<SelectStmt>,
850}
851
852#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
853pub struct FetchCursorStmt {
854 pub name: String,
855 pub direction: CursorDirection,
856 pub count: i64,
858 pub move_only: bool,
859}
860
861#[cfg(test)]
862mod tests;