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