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 role_specification;
42mod routine_attributes;
43mod routine_security;
44mod routines;
45mod sequence;
46mod sequence_declaration;
47mod table_alteration;
48mod type_lifecycle;
49mod type_privileges;
50mod types;
51
52pub use acl_role_specification::AclRoleSpecification;
53pub use array_access::{ArrayAccess, ArraySliceBounds, MAX_ARRAY_SUBSCRIPT_DIMENSIONS};
54pub use assignment_target::{AssignmentStep, AssignmentTarget, AssignmentTargets};
55pub use composites::*;
56pub use constraints::*;
57pub use cte::*;
58pub use domains::*;
59pub use enum_functions::EnumFunctionOperation;
60pub use enums::*;
61pub use events::*;
62pub use expressions::*;
63pub use foreign_wrappers::*;
64pub use from::*;
65pub use function_binding::*;
66pub use identity_sequence::{DeferredSQLError, IdentitySequenceDeclaration, IdentitySequenceName};
67pub use indexes::*;
68pub use interval::*;
69pub use locking::*;
70pub use namespaces::*;
71pub use object_acl::ObjectAclEntry;
72pub use overriding::OverridingKind;
73pub use pattern_operator::PatternOperator;
74pub use ranges::*;
75pub use referenced_partition::ReferencedPartitionConstraint;
76pub use relation_hierarchy::*;
77pub use relation_lifecycle::*;
78pub use role_specification::RoleSpecification;
79pub use routine_attributes::*;
80pub use routine_security::*;
81pub use routines::*;
82pub use sequence::*;
83pub use sequence_declaration::{SequenceDeclaration, SequenceOptionValue};
84pub use table_alteration::*;
85pub use type_lifecycle::*;
86pub use type_privileges::*;
87pub use types::*;
88
89const fn default_include_descendants() -> bool {
90 true
91}
92
93const fn default_true() -> bool {
94 true
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
98pub enum GeneratedColumnKind {
99 Virtual,
100 Stored,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct GeneratedColumn {
105 pub kind: GeneratedColumnKind,
106 pub expression: Box<Expr>,
107 #[serde(default, skip_serializing_if = "Vec::is_empty")]
108 pub function_dependencies: Vec<GeneratedFunctionDependency>,
109}
110
111#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
112pub struct IndexColumnOrder {
113 pub descending: bool,
114 pub nulls_first: bool,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct CreateIndex {
119 #[serde(default)]
120 pub included_columns: Vec<String>,
121 #[serde(default)]
122 pub column_order: Vec<IndexColumnOrder>,
123 #[serde(default)]
124 pub predicate: Option<Box<Expr>>,
125 pub name: Option<String>,
126 pub table: String,
127 pub access_method: String,
129 pub columns: Vec<IndexKey>,
130 #[serde(default)]
131 pub unique: bool,
132 #[serde(default)]
133 pub nulls_not_distinct: bool,
134 pub if_not_exists: bool,
136 pub options: Vec<(String, String)>,
140 #[serde(default, skip_serializing_if = "Vec::is_empty")]
142 pub option_namespaces: Vec<String>,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct DropStmt {
147 pub kind: DropKind,
148 pub names: Vec<String>,
149 pub if_exists: bool,
150 pub cascade: bool,
151}
152
153#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
154pub enum DropKind {
155 ForeignWrapper,
156 Table,
157 ForeignTable,
158 Index,
159 View,
160 MaterializedView,
161 Schema,
162 Sequence,
163 Domain,
164 Type,
166 ForeignServer,
167}
168
169#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
170pub struct InsertStmt {
171 pub table: String,
172 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
174 pub target_relation_bound: bool,
175 pub target_qualifier: String,
177 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub target_alias: Option<String>,
180 #[serde(default = "default_include_descendants")]
181 pub include_descendants: bool,
182 pub columns: Vec<AssignmentTarget>,
183 #[serde(default, skip_serializing_if = "Option::is_none")]
185 pub overriding: Option<OverridingKind>,
186 pub with: Vec<CTE>,
188 pub rows: Vec<Vec<ValueExpr>>,
190 pub select_source: Option<Box<SelectStmt>>,
194 pub on_conflict: Option<OnConflict>,
197 pub returning: Vec<Projection>,
199 pub returning_aliases: ReturningAliases,
202}
203
204#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
205pub struct ReturningAliases {
206 pub old: String,
207 pub new: String,
208 #[serde(default)]
209 pub old_explicit: bool,
210 #[serde(default)]
211 pub new_explicit: bool,
212}
213
214impl Default for ReturningAliases {
215 fn default() -> Self {
216 Self {
217 old: "old".into(),
218 new: "new".into(),
219 old_explicit: false,
220 new_explicit: false,
221 }
222 }
223}
224
225#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
226pub struct OnConflict {
227 #[serde(default)]
228 pub predicate: Option<Box<Expr>>,
229 #[serde(default)]
230 pub constraint: Option<String>,
231 pub conflict_columns: Vec<String>,
235 #[serde(default)]
236 pub expressions: Vec<Expr>,
237 pub action: OnConflictAction,
238}
239
240#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
241pub enum OnConflictAction {
242 Nothing,
244 Update {
248 assignments: Vec<(AssignmentTargets, Expr)>,
249 r#where: Option<Box<Expr>>,
250 },
251}
252
253#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
254pub struct SelectStmt {
255 #[serde(default, skip_serializing_if = "Vec::is_empty")]
257 pub windows: Vec<WindowDefinition>,
258 pub projections: Vec<Projection>,
259 #[serde(default, skip_serializing_if = "Vec::is_empty")]
263 pub values: Vec<Vec<Expr>>,
264 pub from: Option<FromClause>,
265 pub r#where: Option<Expr>,
266 pub group_by: Vec<Expr>,
267 pub grouping_sets: Vec<Vec<Expr>>,
273 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
275 pub group_distinct: bool,
276 pub having: Option<Expr>,
280 pub order_by: Vec<OrderBy>,
281 pub limit: Option<Expr>,
285 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
287 pub with_ties: bool,
288 pub offset: Option<Expr>,
290 pub with: Vec<CTE>,
292 pub set_op: Option<Box<SetOp>>,
296 pub distinct: bool,
299 pub distinct_on: Vec<Expr>,
302 #[serde(default, skip_serializing_if = "Vec::is_empty")]
304 pub locking: Vec<LockingClause>,
305}
306
307#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
308pub struct SetOp {
309 pub kind: SetOpKind,
310 pub all: bool,
311 #[serde(default, skip_serializing_if = "Option::is_none")]
315 pub left: Option<Box<SelectStmt>>,
316 pub right: SelectStmt,
317 pub combined_order_by: Vec<OrderBy>,
320 pub combined_limit: Option<Expr>,
323 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
325 pub combined_with_ties: bool,
326 pub combined_offset: Option<Expr>,
328}
329
330#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
331pub enum SetOpKind {
332 Union,
333 Intersect,
334 Except,
335}
336
337#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
339pub enum DiscardTarget {
340 All,
341 Plans,
342 Sequences,
343 Temp,
344}
345
346#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
347pub struct UpdateStmt {
348 pub table: String,
349 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
351 pub target_relation_bound: bool,
352 pub target_qualifier: String,
353 #[serde(default, skip_serializing_if = "Option::is_none")]
355 pub target_alias: Option<String>,
356 #[serde(default = "default_include_descendants")]
357 pub include_descendants: bool,
358 pub assignments: Vec<(AssignmentTargets, Expr)>,
359 pub r#where: Option<Expr>,
360 pub with: Vec<CTE>,
362 pub from: Option<FromClause>,
365 pub returning: Vec<Projection>,
367 pub returning_aliases: ReturningAliases,
368}
369
370#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
371pub struct DeleteStmt {
372 pub table: String,
373 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
375 pub target_relation_bound: bool,
376 pub target_qualifier: String,
377 #[serde(default, skip_serializing_if = "Option::is_none")]
379 pub target_alias: Option<String>,
380 #[serde(default = "default_include_descendants")]
381 pub include_descendants: bool,
382 pub r#where: Option<Expr>,
383 pub with: Vec<CTE>,
385 pub using: Option<FromClause>,
389 pub returning: Vec<Projection>,
391 pub returning_aliases: ReturningAliases,
392}
393
394#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
395pub struct SetConstraintName {
396 pub catalog: Option<String>,
397 pub schema: Option<String>,
398 pub name: String,
399}
400
401#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
403pub struct VacuumOption {
404 pub name: String,
405 pub value: Option<VacuumOptionValue>,
406}
407
408#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
409pub enum VacuumOptionValue {
410 Boolean(bool),
411 Integer(i32),
412 String(String),
413}
414
415#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
417pub struct VacuumTarget {
418 pub catalog: Option<String>,
419 pub table: String,
420 #[serde(default = "default_include_descendants")]
421 pub include_descendants: bool,
422 #[serde(default, skip_serializing_if = "Vec::is_empty")]
423 pub columns: Vec<String>,
424}
425
426#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
427pub struct VacuumStmt {
428 #[serde(default, skip_serializing_if = "Vec::is_empty")]
429 pub options: Vec<VacuumOption>,
430 #[serde(default, skip_serializing_if = "Vec::is_empty")]
431 pub targets: Vec<VacuumTarget>,
432}
433
434#[derive(Debug, Clone, Serialize, Deserialize)]
435pub enum Statement {
436 CreateDomain(CreateDomain),
437 AlterDomain(AlterDomain),
438 CreateShellType(String),
439 CommentType(CommentType),
440 CreateEnum(CreateEnum),
441 CreateCompositeType(CreateCompositeType),
442 AlterEnum(AlterEnum),
443 AlterTypeObject(AlterTypeObject),
444 GrantType(GrantTypeStmt),
445 CreateTable(CreateTable),
446 CreateTableIfNotExists(DeferredCreateTable),
447 CreateIndex(CreateIndex),
448 RenameIndex(RenameIndexStmt),
449 Insert(InsertStmt),
450 Select(Box<SelectStmt>),
454 Update(UpdateStmt),
455 Delete(DeleteStmt),
456 Drop(DropStmt),
457 AlterTable(AlterTableStmt),
458 AlterForeignTable(AlterForeignTableStmt),
459 AlterView(AlterViewStmt),
460 CreateView {
462 name: String,
463 #[serde(default)]
464 column_names: Vec<String>,
465 body: Box<SelectStmt>,
466 or_replace: bool,
467 #[serde(default)]
468 persistence: RelationPersistence,
469 #[serde(default, skip_serializing_if = "Vec::is_empty")]
471 options: Vec<(String, String)>,
472 },
473 CreateMaterializedView {
475 name: String,
476 #[serde(default)]
477 column_names: Vec<String>,
478 #[serde(default)]
479 if_not_exists: bool,
480 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
481 with_no_data: bool,
482 #[serde(default, skip_serializing_if = "Vec::is_empty")]
483 options: Vec<(String, String)>,
484 body: Box<SelectStmt>,
485 },
486 RefreshMaterializedView {
488 name: String,
489 concurrently: bool,
490 with_no_data: bool,
491 },
492 CreateSchema {
494 name: Option<String>,
495 if_not_exists: bool,
496 #[serde(default, skip_serializing_if = "Option::is_none")]
497 authorization: Option<SchemaAuthorization>,
498 },
499 AlterSchemaOwner {
500 name: String,
501 new_owner: RoleSpecification,
502 },
503 RenameSchema {
505 name: String,
506 new_name: String,
507 },
508 Notify {
510 channel: String,
511 payload: String,
512 },
513 Listen {
515 channel: String,
516 },
517 Unlisten {
519 channel: Option<String>,
520 },
521 SetVariable {
525 name: String,
526 value: String,
527 #[serde(default)]
528 local: bool,
529 #[serde(default)]
530 is_default: bool,
531 },
532 ResetVariable {
534 name: String,
535 },
536 ResetAllVariables,
538 SetConstraints {
540 constraints: Vec<SetConstraintName>,
541 deferred: bool,
542 },
543 ShowVariable {
546 name: String,
547 },
548 Discard {
551 target: DiscardTarget,
552 },
553 Load {
558 library: String,
559 },
560 Explain {
563 analyze: bool,
564 verbose: bool,
565 format: Option<String>,
566 body: Box<Statement>,
567 },
568 Analyze {
571 table: Option<String>,
572 },
573 Vacuum(VacuumStmt),
575 LockTable(LockTableStmt),
577 Truncate {
580 tables: Vec<TruncateTarget>,
581 cascade: bool,
582 #[serde(default)]
583 restart_identity: bool,
584 },
585 Transaction(TransactionStmt),
587 DeclareCursor(DeclareCursorStmt),
589 FetchCursor(FetchCursorStmt),
591 CloseCursor {
593 name: Option<String>,
594 },
595 CreateSequence(CreateSequence),
597 AlterSequence(AlterSequence),
600 CreateTableAs {
602 name: String,
603 if_not_exists: bool,
604 #[serde(default, skip_serializing_if = "Vec::is_empty")]
605 column_names: Vec<String>,
606 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
607 with_no_data: bool,
608 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
610 select_into: bool,
611 #[serde(default)]
612 persistence: RelationPersistence,
613 #[serde(default)]
614 on_commit: OnCommitAction,
615 body: Box<SelectStmt>,
616 },
617 Prepare {
619 name: String,
620 #[serde(default)]
621 parameter_types: Vec<ColumnType>,
622 body: Box<Statement>,
623 },
624 Execute {
626 name: String,
627 params: Vec<Expr>,
628 },
629 Deallocate {
631 name: Option<String>,
632 },
633 Values {
636 rows: Vec<Vec<Expr>>,
637 },
638 CreateForeignWrapper(CreateForeignWrapper),
640 CreateForeignServer(CreateForeignServer),
642 CreateForeignTable(CreateForeignTable),
644 #[serde(alias = "CreateForeignTableIfNotExists")]
646 CreateForeignTableDefinition(DeferredCreateForeignTable),
647 Merge(MergeStmt),
650 CreateFunction(Box<CreateFunction>),
653 DropFunction(DropFunctionStmt),
655 AlterRoutine(AlterRoutineStmt),
657 AlterRoutineOwner(AlterRoutineOwnerStmt),
658 RenameRoutine(RenameRoutineStmt),
659 GrantRoutine(GrantRoutineStmt),
660 GrantTable(GrantTableStmt),
661 GrantSequence(GrantSequenceStmt),
662 GrantDatabase(GrantDatabaseStmt),
663 GrantSchema(GrantSchemaStmt),
664 GrantRole(GrantRoleStmt),
665 CreateRole(CreateRoleStmt),
666 AlterRole(AlterRoleStmt),
667 RenameRole(RenameRoleStmt),
668 DropRole(DropRoleStmt),
669 CreateTrigger(CreateTrigger),
671 DropTrigger(DropTrigger),
673 CreateRule(CreateRule),
675 DropRule(DropRule),
677 DoBlock {
679 language: String,
680 body: String,
681 },
682 Call {
685 name: String,
686 args: Vec<Expr>,
687 },
688}
689
690#[derive(Debug, Clone, Serialize, Deserialize)]
691pub struct TruncateTarget {
692 pub table: String,
693 #[serde(default = "default_include_descendants")]
694 pub include_descendants: bool,
695}
696
697#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
698pub struct MergeTargetColumnBinding {
699 pub object_id: [u8; 16],
700 #[serde(default, skip_serializing_if = "Option::is_none")]
702 pub attribute_number: Option<i16>,
703 #[serde(default, skip_serializing_if = "std::collections::BTreeSet::is_empty")]
705 pub domain_dependencies: std::collections::BTreeSet<u32>,
706}
707
708#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
709pub struct MergeStmt {
710 #[serde(default)]
711 pub with: Vec<CTE>,
712 pub target: String,
713 pub target_qualifier: String,
714 pub target_alias: Option<String>,
715 #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
717 pub target_column_bindings: std::collections::BTreeMap<String, MergeTargetColumnBinding>,
718 #[serde(default = "default_include_descendants")]
719 pub include_descendants: bool,
720 pub source: FromClause,
721 pub join_condition: Expr,
722 pub when_clauses: Vec<MergeWhen>,
723 pub returning: Vec<Projection>,
725 pub returning_aliases: ReturningAliases,
726}
727
728#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
729pub enum MergeWhen {
730 UpdateMatched {
732 condition: Option<Expr>,
733 assignments: Vec<(AssignmentTargets, Expr)>,
734 },
735 DeleteMatched { condition: Option<Expr> },
737 UpdateNotMatchedBySource {
739 condition: Option<Expr>,
740 assignments: Vec<(AssignmentTargets, Expr)>,
741 },
742 DeleteNotMatchedBySource { condition: Option<Expr> },
744 InsertNotMatched {
746 condition: Option<Expr>,
747 columns: Vec<AssignmentTarget>,
748 #[serde(default, skip_serializing_if = "Option::is_none")]
749 overriding: Option<OverridingKind>,
750 values: Vec<Expr>,
751 },
752 NothingMatched { condition: Option<Expr> },
754 NothingNotMatched { condition: Option<Expr> },
756 NothingNotMatchedBySource { condition: Option<Expr> },
758}
759
760#[derive(Debug, Clone, Serialize, Deserialize)]
761pub struct CreateForeignServer {
762 pub name: String,
763 pub fdw_type: String,
764 #[serde(default)]
765 pub server_type: Option<String>,
766 #[serde(default)]
767 pub version: Option<String>,
768 pub options: Vec<(String, String)>,
769 pub if_not_exists: bool,
770}
771
772#[derive(Debug, Clone, Serialize, Deserialize)]
773pub struct CreateForeignTable {
774 pub name: String,
775 pub server_name: String,
776 pub columns: Vec<ColumnDef>,
777 #[serde(default)]
778 pub checks: Vec<TableCheck>,
779 #[serde(default, skip_serializing_if = "Option::is_none")]
781 pub not_null_declarations: Option<Vec<NotNullDeclaration>>,
782 pub options: Vec<(String, String)>,
783 pub if_not_exists: bool,
784}
785
786#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
788pub struct DeferredCreateForeignTable {
789 pub name: String,
790 pub server_name: String,
791 pub definition_sql: String,
792 #[serde(default = "default_true")]
794 pub if_not_exists: bool,
795}
796
797#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
798pub enum TransactionIsolationLevel {
799 ReadUncommitted,
800 ReadCommitted,
801 RepeatableRead,
802 Serializable,
803}
804
805impl TransactionIsolationLevel {
806 #[must_use]
807 pub const fn as_str(self) -> &'static str {
808 match self {
809 Self::ReadUncommitted => "read uncommitted",
810 Self::ReadCommitted => "read committed",
811 Self::RepeatableRead => "repeatable read",
812 Self::Serializable => "serializable",
813 }
814 }
815}
816
817#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
818pub struct TransactionCharacteristics {
819 pub isolation: Option<TransactionIsolationLevel>,
820 pub read_only: Option<bool>,
821 pub deferrable: Option<bool>,
822}
823
824#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
825pub enum TransactionStmt {
826 Begin,
827 BeginWithCharacteristics(TransactionCharacteristics),
828 Commit,
829 CommitAndChain,
830 Rollback,
831 RollbackAndChain,
832 SetCharacteristics(TransactionCharacteristics),
833 SetSessionCharacteristics(TransactionCharacteristics),
834 SetSnapshot(String),
835 Savepoint(String),
836 ReleaseSavepoint(String),
837 RollbackToSavepoint(String),
838}
839
840#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
841pub enum CursorDirection {
842 Forward,
843 Backward,
844 Absolute,
845 Relative,
846}
847
848#[derive(Debug, Clone, Serialize, Deserialize)]
849pub struct DeclareCursorStmt {
850 pub name: String,
851 pub binary: bool,
852 pub scroll: Option<bool>,
854 pub hold: bool,
855 pub query: Box<SelectStmt>,
856}
857
858#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
859pub struct FetchCursorStmt {
860 pub name: String,
861 pub direction: CursorDirection,
862 pub count: i64,
864 pub move_only: bool,
865}
866
867#[cfg(test)]
868mod tests;