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 reference_name;
39mod referenced_partition;
40mod relation_hierarchy;
41mod relation_lifecycle;
42mod retained_literal;
43mod retained_rows;
44pub use retained_literal::RetainedInputLiteral;
45mod role_specification;
46mod routine_attributes;
47mod routine_security;
48mod routine_source;
49mod routines;
50mod sequence;
51mod sequence_declaration;
52mod table_alteration;
53mod type_lifecycle;
54mod type_privileges;
55mod types;
56
57pub use acl_role_specification::AclRoleSpecification;
58pub use array_access::{ArrayAccess, ArraySliceBounds, MAX_ARRAY_SUBSCRIPT_DIMENSIONS};
59pub use assignment_target::{
60 AssignmentBinding, AssignmentPathStep, AssignmentStep, AssignmentTarget, AssignmentTargets,
61 MultipleAssignmentTargets,
62};
63pub use composites::*;
64pub use constraints::*;
65pub use cte::*;
66pub use domains::*;
67pub use enum_functions::EnumFunctionOperation;
68pub use enums::*;
69pub use events::*;
70pub use expressions::*;
71pub use foreign_wrappers::*;
72pub use from::*;
73pub use function_binding::*;
74pub use identity_sequence::{DeferredSQLError, IdentitySequenceDeclaration, IdentitySequenceName};
75pub use indexes::*;
76pub use interval::*;
77pub use locking::*;
78pub use namespaces::*;
79pub use object_acl::ObjectAclEntry;
80pub use overriding::OverridingKind;
81pub use pattern_operator::PatternOperator;
82pub use ranges::*;
83pub use reference_name::ReferenceName;
84pub use referenced_partition::ReferencedPartitionConstraint;
85pub use relation_hierarchy::*;
86pub use relation_lifecycle::*;
87pub use retained_rows::RetainedRowSource;
88pub use role_specification::RoleSpecification;
89pub use routine_attributes::*;
90pub use routine_security::*;
91pub use routine_source::RoutineBodyOrigin;
92pub use routines::*;
93pub use sequence::*;
94pub use sequence_declaration::{SequenceDeclaration, SequenceOptionValue};
95pub use table_alteration::*;
96pub use type_lifecycle::*;
97pub use type_privileges::*;
98pub use types::*;
99
100const fn default_include_descendants() -> bool {
101 true
102}
103
104const fn default_true() -> bool {
105 true
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
109pub enum GeneratedColumnKind {
110 Virtual,
111 Stored,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct GeneratedColumn {
116 pub kind: GeneratedColumnKind,
117 pub expression: Box<Expr>,
118 #[serde(default, skip_serializing_if = "Vec::is_empty")]
119 pub function_dependencies: Vec<GeneratedFunctionDependency>,
120}
121
122#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
123pub struct IndexColumnOrder {
124 pub descending: bool,
125 pub nulls_first: bool,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct CreateIndex {
130 #[serde(default)]
131 pub included_columns: Vec<String>,
132 #[serde(default)]
133 pub column_order: Vec<IndexColumnOrder>,
134 #[serde(default)]
135 pub predicate: Option<Box<Expr>>,
136 pub name: Option<String>,
137 pub table: String,
138 pub access_method: String,
140 pub columns: Vec<IndexKey>,
141 #[serde(default)]
142 pub unique: bool,
143 #[serde(default)]
144 pub nulls_not_distinct: bool,
145 pub if_not_exists: bool,
147 pub options: Vec<(String, String)>,
151 #[serde(default, skip_serializing_if = "Vec::is_empty")]
153 pub option_namespaces: Vec<String>,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct DropStmt {
158 pub kind: DropKind,
159 pub names: Vec<String>,
160 pub if_exists: bool,
161 pub cascade: bool,
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
165pub enum DropKind {
166 ForeignWrapper,
167 Table,
168 ForeignTable,
169 Index,
170 View,
171 MaterializedView,
172 Schema,
173 Sequence,
174 Domain,
175 Type,
177 ForeignServer,
178}
179
180#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
181pub struct InsertStmt {
182 pub table: String,
183 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
185 pub target_relation_bound: bool,
186 pub target_qualifier: String,
188 #[serde(default, skip_serializing_if = "Option::is_none")]
190 pub target_alias: Option<String>,
191 #[serde(default = "default_include_descendants")]
192 pub include_descendants: bool,
193 pub columns: Vec<AssignmentTarget>,
194 #[serde(default, skip_serializing_if = "Option::is_none")]
196 pub overriding: Option<OverridingKind>,
197 pub with: Vec<CTE>,
199 pub rows: Vec<Vec<ValueExpr>>,
201 pub select_source: Option<Box<SelectStmt>>,
205 pub on_conflict: Option<OnConflict>,
208 pub returning: Vec<Projection>,
210 pub returning_aliases: ReturningAliases,
213}
214
215#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
216pub struct ReturningAliases {
217 pub old: String,
218 pub new: String,
219 #[serde(default)]
220 pub old_explicit: bool,
221 #[serde(default)]
222 pub new_explicit: bool,
223}
224
225impl Default for ReturningAliases {
226 fn default() -> Self {
227 Self {
228 old: "old".into(),
229 new: "new".into(),
230 old_explicit: false,
231 new_explicit: false,
232 }
233 }
234}
235
236#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
237pub struct OnConflict {
238 #[serde(default)]
239 pub predicate: Option<Box<Expr>>,
240 #[serde(default)]
241 pub constraint: Option<String>,
242 pub conflict_columns: Vec<String>,
246 #[serde(default)]
247 pub expressions: Vec<Expr>,
248 pub action: OnConflictAction,
249}
250
251#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
252pub enum OnConflictAction {
253 Nothing,
255 Update {
259 assignments: Vec<(AssignmentTargets, Expr)>,
260 r#where: Option<Box<Expr>>,
261 },
262}
263
264#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
265pub struct SelectStmt {
266 #[serde(default, skip_serializing_if = "Vec::is_empty")]
268 pub windows: Vec<WindowDefinition>,
269 pub projections: Vec<Projection>,
270 #[serde(default, skip_serializing_if = "Vec::is_empty")]
274 pub values: Vec<Vec<Expr>>,
275 pub from: Option<FromClause>,
276 pub r#where: Option<Expr>,
277 pub group_by: Vec<Expr>,
278 pub grouping_sets: Vec<Vec<Expr>>,
284 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
286 pub group_distinct: bool,
287 pub having: Option<Expr>,
291 pub order_by: Vec<OrderBy>,
292 pub limit: Option<Expr>,
296 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
298 pub with_ties: bool,
299 pub offset: Option<Expr>,
301 pub with: Vec<CTE>,
303 pub set_op: Option<Box<SetOp>>,
307 pub distinct: bool,
310 pub distinct_on: Vec<Expr>,
313 #[serde(default, skip_serializing_if = "Vec::is_empty")]
315 pub locking: Vec<LockingClause>,
316}
317
318#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
319pub struct SetOp {
320 pub kind: SetOpKind,
321 pub all: bool,
322 #[serde(default, skip_serializing_if = "Option::is_none")]
326 pub left: Option<Box<SelectStmt>>,
327 pub right: SelectStmt,
328 pub combined_order_by: Vec<OrderBy>,
331 pub combined_limit: Option<Expr>,
334 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
336 pub combined_with_ties: bool,
337 pub combined_offset: Option<Expr>,
339}
340
341#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
342pub enum SetOpKind {
343 Union,
344 Intersect,
345 Except,
346}
347
348#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
350pub enum DiscardTarget {
351 All,
352 Plans,
353 Sequences,
354 Temp,
355}
356
357#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
358pub struct UpdateStmt {
359 pub table: String,
360 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
362 pub target_relation_bound: bool,
363 pub target_qualifier: String,
364 #[serde(default, skip_serializing_if = "Option::is_none")]
366 pub target_alias: Option<String>,
367 #[serde(default = "default_include_descendants")]
368 pub include_descendants: bool,
369 pub assignments: Vec<(AssignmentTargets, Expr)>,
370 pub r#where: Option<Expr>,
371 pub with: Vec<CTE>,
373 pub from: Option<FromClause>,
376 pub returning: Vec<Projection>,
378 pub returning_aliases: ReturningAliases,
379}
380
381#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
382pub struct DeleteStmt {
383 pub table: String,
384 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
386 pub target_relation_bound: bool,
387 pub target_qualifier: String,
388 #[serde(default, skip_serializing_if = "Option::is_none")]
390 pub target_alias: Option<String>,
391 #[serde(default = "default_include_descendants")]
392 pub include_descendants: bool,
393 pub r#where: Option<Expr>,
394 pub with: Vec<CTE>,
396 pub using: Option<FromClause>,
400 pub returning: Vec<Projection>,
402 pub returning_aliases: ReturningAliases,
403}
404
405#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
406pub struct SetConstraintName {
407 pub catalog: Option<String>,
408 pub schema: Option<String>,
409 pub name: String,
410}
411
412#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
414pub struct VacuumOption {
415 pub name: String,
416 pub value: Option<VacuumOptionValue>,
417}
418
419#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
420pub enum VacuumOptionValue {
421 Boolean(bool),
422 Integer(i32),
423 String(String),
424}
425
426#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
428pub struct VacuumTarget {
429 pub catalog: Option<String>,
430 pub table: String,
431 #[serde(default = "default_include_descendants")]
432 pub include_descendants: bool,
433 #[serde(default, skip_serializing_if = "Vec::is_empty")]
434 pub columns: Vec<String>,
435}
436
437#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
438pub struct VacuumStmt {
439 #[serde(default, skip_serializing_if = "Vec::is_empty")]
440 pub options: Vec<VacuumOption>,
441 #[serde(default, skip_serializing_if = "Vec::is_empty")]
442 pub targets: Vec<VacuumTarget>,
443}
444
445#[derive(Debug, Clone, Serialize, Deserialize)]
446pub enum Statement {
447 CreateDomain(CreateDomain),
448 AlterDomain(AlterDomain),
449 CreateShellType(String),
450 CommentType(CommentType),
451 CreateEnum(CreateEnum),
452 CreateCompositeType(CreateCompositeType),
453 AlterEnum(AlterEnum),
454 AlterTypeObject(AlterTypeObject),
455 GrantType(GrantTypeStmt),
456 CreateTable(CreateTable),
457 CreateTableIfNotExists(DeferredCreateTable),
458 CreateIndex(CreateIndex),
459 RenameIndex(RenameIndexStmt),
460 Insert(InsertStmt),
461 Select(Box<SelectStmt>),
465 Update(UpdateStmt),
466 Delete(DeleteStmt),
467 Drop(DropStmt),
468 AlterTable(AlterTableStmt),
469 AlterForeignTable(AlterForeignTableStmt),
470 AlterView(AlterViewStmt),
471 CreateView {
473 name: String,
474 #[serde(default)]
475 column_names: Vec<String>,
476 body: Box<SelectStmt>,
477 or_replace: bool,
478 #[serde(default)]
479 persistence: RelationPersistence,
480 #[serde(default, skip_serializing_if = "Vec::is_empty")]
482 options: Vec<(String, String)>,
483 },
484 CreateMaterializedView {
486 name: String,
487 #[serde(default)]
488 column_names: Vec<String>,
489 #[serde(default)]
490 if_not_exists: bool,
491 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
492 with_no_data: bool,
493 #[serde(default, skip_serializing_if = "Vec::is_empty")]
494 options: Vec<(String, String)>,
495 body: Box<SelectStmt>,
496 },
497 RefreshMaterializedView {
499 name: String,
500 concurrently: bool,
501 with_no_data: bool,
502 },
503 CreateSchema {
505 name: Option<String>,
506 if_not_exists: bool,
507 #[serde(default, skip_serializing_if = "Option::is_none")]
508 authorization: Option<SchemaAuthorization>,
509 },
510 AlterSchemaOwner {
511 name: String,
512 new_owner: RoleSpecification,
513 },
514 RenameSchema {
516 name: String,
517 new_name: String,
518 },
519 Notify {
521 channel: String,
522 payload: String,
523 },
524 Listen {
526 channel: String,
527 },
528 Unlisten {
530 channel: Option<String>,
531 },
532 SetVariable {
536 name: String,
537 value: String,
538 #[serde(default)]
539 local: bool,
540 #[serde(default)]
541 is_default: bool,
542 },
543 ResetVariable {
545 name: String,
546 },
547 ResetAllVariables,
549 SetConstraints {
551 constraints: Vec<SetConstraintName>,
552 deferred: bool,
553 },
554 ShowVariable {
557 name: String,
558 },
559 Discard {
562 target: DiscardTarget,
563 },
564 Load {
569 library: String,
570 },
571 Explain {
574 #[serde(flatten)]
575 options: crate::explain::ExplainOptions,
576 body: Box<Statement>,
577 },
578 Analyze {
581 table: Option<String>,
582 },
583 Vacuum(VacuumStmt),
585 LockTable(LockTableStmt),
587 Truncate {
590 tables: Vec<TruncateTarget>,
591 cascade: bool,
592 #[serde(default)]
593 restart_identity: bool,
594 },
595 Transaction(TransactionStmt),
597 DeclareCursor(DeclareCursorStmt),
599 FetchCursor(FetchCursorStmt),
601 CloseCursor {
603 name: Option<String>,
604 },
605 CreateSequence(CreateSequence),
607 AlterSequence(AlterSequence),
610 CreateTableAs {
612 name: String,
613 if_not_exists: bool,
614 #[serde(default, skip_serializing_if = "Vec::is_empty")]
615 column_names: Vec<String>,
616 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
617 with_no_data: bool,
618 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
620 select_into: bool,
621 #[serde(default)]
622 persistence: RelationPersistence,
623 #[serde(default)]
624 on_commit: OnCommitAction,
625 body: Box<SelectStmt>,
626 },
627 Prepare {
629 name: String,
630 #[serde(default)]
631 parameter_types: Vec<ColumnType>,
632 body: Box<Statement>,
633 },
634 Execute {
636 name: String,
637 params: Vec<Expr>,
638 },
639 Deallocate {
641 name: Option<String>,
642 },
643 Values {
646 rows: Vec<Vec<Expr>>,
647 },
648 CreateForeignWrapper(CreateForeignWrapper),
650 CreateForeignServer(CreateForeignServer),
652 CreateForeignTable(CreateForeignTable),
654 #[serde(alias = "CreateForeignTableIfNotExists")]
656 CreateForeignTableDefinition(DeferredCreateForeignTable),
657 Merge(MergeStmt),
660 CreateFunction(Box<CreateFunction>),
663 DropFunction(DropFunctionStmt),
665 AlterRoutine(AlterRoutineStmt),
667 AlterRoutineOwner(AlterRoutineOwnerStmt),
668 RenameRoutine(RenameRoutineStmt),
669 GrantRoutine(GrantRoutineStmt),
670 GrantTable(GrantTableStmt),
671 GrantSequence(GrantSequenceStmt),
672 GrantDatabase(GrantDatabaseStmt),
673 GrantSchema(GrantSchemaStmt),
674 GrantRole(GrantRoleStmt),
675 CreateRole(CreateRoleStmt),
676 AlterRole(AlterRoleStmt),
677 RenameRole(RenameRoleStmt),
678 DropRole(DropRoleStmt),
679 CreateTrigger(CreateTrigger),
681 DropTrigger(DropTrigger),
683 CreateRule(CreateRule),
685 DropRule(DropRule),
687 DoBlock {
689 language: String,
690 body: String,
691 },
692 Call {
695 name: String,
696 args: Vec<Expr>,
697 },
698 Empty,
700}
701
702#[derive(Debug, Clone, Serialize, Deserialize)]
703pub struct TruncateTarget {
704 pub table: String,
705 #[serde(default = "default_include_descendants")]
706 pub include_descendants: bool,
707}
708
709#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
710pub struct MergeTargetColumnBinding {
711 pub object_id: [u8; 16],
712 #[serde(default, skip_serializing_if = "Option::is_none")]
714 pub attribute_number: Option<i16>,
715 #[serde(default, skip_serializing_if = "std::collections::BTreeSet::is_empty")]
717 pub domain_dependencies: std::collections::BTreeSet<u32>,
718}
719
720#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
721pub struct MergeStmt {
722 #[serde(default)]
723 pub with: Vec<CTE>,
724 pub target: String,
725 pub target_qualifier: String,
726 pub target_alias: Option<String>,
727 #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
729 pub target_column_bindings: std::collections::BTreeMap<String, MergeTargetColumnBinding>,
730 #[serde(default = "default_include_descendants")]
731 pub include_descendants: bool,
732 pub source: FromClause,
733 pub join_condition: Expr,
734 pub when_clauses: Vec<MergeWhen>,
735 pub returning: Vec<Projection>,
737 pub returning_aliases: ReturningAliases,
738}
739
740#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
741pub enum MergeWhen {
742 UpdateMatched {
744 condition: Option<Expr>,
745 assignments: Vec<(AssignmentTargets, Expr)>,
746 },
747 DeleteMatched { condition: Option<Expr> },
749 UpdateNotMatchedBySource {
751 condition: Option<Expr>,
752 assignments: Vec<(AssignmentTargets, Expr)>,
753 },
754 DeleteNotMatchedBySource { condition: Option<Expr> },
756 InsertNotMatched {
758 condition: Option<Expr>,
759 columns: Vec<AssignmentTarget>,
760 #[serde(default, skip_serializing_if = "Option::is_none")]
761 overriding: Option<OverridingKind>,
762 values: Vec<Expr>,
763 },
764 NothingMatched { condition: Option<Expr> },
766 NothingNotMatched { condition: Option<Expr> },
768 NothingNotMatchedBySource { condition: Option<Expr> },
770}
771
772#[derive(Debug, Clone, Serialize, Deserialize)]
773pub struct CreateForeignServer {
774 pub name: String,
775 pub fdw_type: String,
776 #[serde(default)]
777 pub server_type: Option<String>,
778 #[serde(default)]
779 pub version: Option<String>,
780 pub options: Vec<(String, String)>,
781 pub if_not_exists: bool,
782}
783
784#[derive(Debug, Clone, Serialize, Deserialize)]
785pub struct CreateForeignTable {
786 pub name: String,
787 pub server_name: String,
788 pub columns: Vec<ColumnDef>,
789 #[serde(default)]
790 pub checks: Vec<TableCheck>,
791 #[serde(default, skip_serializing_if = "Option::is_none")]
793 pub not_null_declarations: Option<Vec<NotNullDeclaration>>,
794 pub options: Vec<(String, String)>,
795 pub if_not_exists: bool,
796}
797
798#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
800pub struct DeferredCreateForeignTable {
801 pub name: String,
802 pub server_name: String,
803 pub definition_sql: String,
804 #[serde(default = "default_true")]
806 pub if_not_exists: bool,
807}
808
809#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
810pub enum TransactionIsolationLevel {
811 ReadUncommitted,
812 ReadCommitted,
813 RepeatableRead,
814 Serializable,
815}
816
817impl TransactionIsolationLevel {
818 #[must_use]
819 pub const fn as_str(self) -> &'static str {
820 match self {
821 Self::ReadUncommitted => "read uncommitted",
822 Self::ReadCommitted => "read committed",
823 Self::RepeatableRead => "repeatable read",
824 Self::Serializable => "serializable",
825 }
826 }
827}
828
829#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
830pub struct TransactionCharacteristics {
831 pub isolation: Option<TransactionIsolationLevel>,
832 pub read_only: Option<bool>,
833 pub deferrable: Option<bool>,
834}
835
836#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
837pub enum TransactionStmt {
838 Begin,
839 BeginWithCharacteristics(TransactionCharacteristics),
840 Commit,
841 CommitAndChain,
842 Rollback,
843 RollbackAndChain,
844 SetCharacteristics(TransactionCharacteristics),
845 SetSessionCharacteristics(TransactionCharacteristics),
846 SetSnapshot(String),
847 Savepoint(String),
848 ReleaseSavepoint(String),
849 RollbackToSavepoint(String),
850}
851
852#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
853pub enum CursorDirection {
854 Forward,
855 Backward,
856 Absolute,
857 Relative,
858}
859
860#[derive(Debug, Clone, Serialize, Deserialize)]
861pub struct DeclareCursorStmt {
862 pub name: String,
863 pub binary: bool,
864 pub scroll: Option<bool>,
866 pub hold: bool,
867 pub query: Box<SelectStmt>,
868}
869
870#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
871pub struct FetchCursorStmt {
872 pub name: String,
873 pub direction: CursorDirection,
874 pub count: i64,
876 pub move_only: bool,
877}
878
879#[cfg(test)]
880mod tests;