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 CreateEnum(CreateEnum),
439 CreateCompositeType(CreateCompositeType),
440 AlterEnum(AlterEnum),
441 AlterTypeObject(AlterTypeObject),
442 GrantType(GrantTypeStmt),
443 CreateTable(CreateTable),
444 CreateTableIfNotExists(DeferredCreateTable),
445 CreateIndex(CreateIndex),
446 RenameIndex(RenameIndexStmt),
447 Insert(InsertStmt),
448 Select(Box<SelectStmt>),
452 Update(UpdateStmt),
453 Delete(DeleteStmt),
454 Drop(DropStmt),
455 AlterTable(AlterTableStmt),
456 AlterForeignTable(AlterForeignTableStmt),
457 AlterView(AlterViewStmt),
458 CreateView {
460 name: String,
461 #[serde(default)]
462 column_names: Vec<String>,
463 body: Box<SelectStmt>,
464 or_replace: bool,
465 #[serde(default)]
466 persistence: RelationPersistence,
467 #[serde(default, skip_serializing_if = "Vec::is_empty")]
469 options: Vec<(String, String)>,
470 },
471 CreateMaterializedView {
473 name: String,
474 #[serde(default)]
475 column_names: Vec<String>,
476 #[serde(default)]
477 if_not_exists: bool,
478 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
479 with_no_data: bool,
480 #[serde(default, skip_serializing_if = "Vec::is_empty")]
481 options: Vec<(String, String)>,
482 body: Box<SelectStmt>,
483 },
484 RefreshMaterializedView {
486 name: String,
487 concurrently: bool,
488 with_no_data: bool,
489 },
490 CreateSchema {
492 name: Option<String>,
493 if_not_exists: bool,
494 #[serde(default, skip_serializing_if = "Option::is_none")]
495 authorization: Option<SchemaAuthorization>,
496 },
497 AlterSchemaOwner {
498 name: String,
499 new_owner: RoleSpecification,
500 },
501 RenameSchema {
503 name: String,
504 new_name: String,
505 },
506 Notify {
508 channel: String,
509 payload: String,
510 },
511 Listen {
513 channel: String,
514 },
515 Unlisten {
517 channel: Option<String>,
518 },
519 SetVariable {
523 name: String,
524 value: String,
525 #[serde(default)]
526 local: bool,
527 #[serde(default)]
528 is_default: bool,
529 },
530 ResetVariable {
532 name: String,
533 },
534 ResetAllVariables,
536 SetConstraints {
538 constraints: Vec<SetConstraintName>,
539 deferred: bool,
540 },
541 ShowVariable {
544 name: String,
545 },
546 Discard {
549 target: DiscardTarget,
550 },
551 Load {
556 library: String,
557 },
558 Explain {
561 analyze: bool,
562 verbose: bool,
563 format: Option<String>,
564 body: Box<Statement>,
565 },
566 Analyze {
569 table: Option<String>,
570 },
571 Vacuum(VacuumStmt),
573 LockTable(LockTableStmt),
575 Truncate {
578 tables: Vec<TruncateTarget>,
579 cascade: bool,
580 #[serde(default)]
581 restart_identity: bool,
582 },
583 Transaction(TransactionStmt),
585 DeclareCursor(DeclareCursorStmt),
587 FetchCursor(FetchCursorStmt),
589 CloseCursor {
591 name: Option<String>,
592 },
593 CreateSequence(CreateSequence),
595 AlterSequence(AlterSequence),
598 CreateTableAs {
600 name: String,
601 if_not_exists: bool,
602 #[serde(default, skip_serializing_if = "Vec::is_empty")]
603 column_names: Vec<String>,
604 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
605 with_no_data: bool,
606 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
608 select_into: bool,
609 #[serde(default)]
610 persistence: RelationPersistence,
611 #[serde(default)]
612 on_commit: OnCommitAction,
613 body: Box<SelectStmt>,
614 },
615 Prepare {
617 name: String,
618 #[serde(default)]
619 parameter_types: Vec<ColumnType>,
620 body: Box<Statement>,
621 },
622 Execute {
624 name: String,
625 params: Vec<Expr>,
626 },
627 Deallocate {
629 name: Option<String>,
630 },
631 Values {
634 rows: Vec<Vec<Expr>>,
635 },
636 CreateForeignWrapper(CreateForeignWrapper),
638 CreateForeignServer(CreateForeignServer),
640 CreateForeignTable(CreateForeignTable),
642 #[serde(alias = "CreateForeignTableIfNotExists")]
644 CreateForeignTableDefinition(DeferredCreateForeignTable),
645 Merge(MergeStmt),
648 CreateFunction(Box<CreateFunction>),
651 DropFunction(DropFunctionStmt),
653 AlterRoutine(AlterRoutineStmt),
655 AlterRoutineOwner(AlterRoutineOwnerStmt),
656 RenameRoutine(RenameRoutineStmt),
657 GrantRoutine(GrantRoutineStmt),
658 GrantTable(GrantTableStmt),
659 GrantSequence(GrantSequenceStmt),
660 GrantDatabase(GrantDatabaseStmt),
661 GrantSchema(GrantSchemaStmt),
662 GrantRole(GrantRoleStmt),
663 CreateRole(CreateRoleStmt),
664 AlterRole(AlterRoleStmt),
665 RenameRole(RenameRoleStmt),
666 DropRole(DropRoleStmt),
667 CreateTrigger(CreateTrigger),
669 DropTrigger(DropTrigger),
671 CreateRule(CreateRule),
673 DropRule(DropRule),
675 DoBlock {
677 language: String,
678 body: String,
679 },
680 Call {
683 name: String,
684 args: Vec<Expr>,
685 },
686}
687
688#[derive(Debug, Clone, Serialize, Deserialize)]
689pub struct TruncateTarget {
690 pub table: String,
691 #[serde(default = "default_include_descendants")]
692 pub include_descendants: bool,
693}
694
695#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
696pub struct MergeTargetColumnBinding {
697 pub object_id: [u8; 16],
698 #[serde(default, skip_serializing_if = "Option::is_none")]
700 pub attribute_number: Option<i16>,
701 #[serde(default, skip_serializing_if = "std::collections::BTreeSet::is_empty")]
703 pub domain_dependencies: std::collections::BTreeSet<u32>,
704}
705
706#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
707pub struct MergeStmt {
708 #[serde(default)]
709 pub with: Vec<CTE>,
710 pub target: String,
711 pub target_qualifier: String,
712 pub target_alias: Option<String>,
713 #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
715 pub target_column_bindings: std::collections::BTreeMap<String, MergeTargetColumnBinding>,
716 #[serde(default = "default_include_descendants")]
717 pub include_descendants: bool,
718 pub source: FromClause,
719 pub join_condition: Expr,
720 pub when_clauses: Vec<MergeWhen>,
721 pub returning: Vec<Projection>,
723 pub returning_aliases: ReturningAliases,
724}
725
726#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
727pub enum MergeWhen {
728 UpdateMatched {
730 condition: Option<Expr>,
731 assignments: Vec<(AssignmentTargets, Expr)>,
732 },
733 DeleteMatched { condition: Option<Expr> },
735 UpdateNotMatchedBySource {
737 condition: Option<Expr>,
738 assignments: Vec<(AssignmentTargets, Expr)>,
739 },
740 DeleteNotMatchedBySource { condition: Option<Expr> },
742 InsertNotMatched {
744 condition: Option<Expr>,
745 columns: Vec<AssignmentTarget>,
746 #[serde(default, skip_serializing_if = "Option::is_none")]
747 overriding: Option<OverridingKind>,
748 values: Vec<Expr>,
749 },
750 NothingMatched { condition: Option<Expr> },
752 NothingNotMatched { condition: Option<Expr> },
754 NothingNotMatchedBySource { condition: Option<Expr> },
756}
757
758#[derive(Debug, Clone, Serialize, Deserialize)]
759pub struct CreateForeignServer {
760 pub name: String,
761 pub fdw_type: String,
762 #[serde(default)]
763 pub server_type: Option<String>,
764 #[serde(default)]
765 pub version: Option<String>,
766 pub options: Vec<(String, String)>,
767 pub if_not_exists: bool,
768}
769
770#[derive(Debug, Clone, Serialize, Deserialize)]
771pub struct CreateForeignTable {
772 pub name: String,
773 pub server_name: String,
774 pub columns: Vec<ColumnDef>,
775 #[serde(default)]
776 pub checks: Vec<TableCheck>,
777 #[serde(default, skip_serializing_if = "Option::is_none")]
779 pub not_null_declarations: Option<Vec<NotNullDeclaration>>,
780 pub options: Vec<(String, String)>,
781 pub if_not_exists: bool,
782}
783
784#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
786pub struct DeferredCreateForeignTable {
787 pub name: String,
788 pub server_name: String,
789 pub definition_sql: String,
790 #[serde(default = "default_true")]
792 pub if_not_exists: bool,
793}
794
795#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
796pub enum TransactionIsolationLevel {
797 ReadUncommitted,
798 ReadCommitted,
799 RepeatableRead,
800 Serializable,
801}
802
803impl TransactionIsolationLevel {
804 #[must_use]
805 pub const fn as_str(self) -> &'static str {
806 match self {
807 Self::ReadUncommitted => "read uncommitted",
808 Self::ReadCommitted => "read committed",
809 Self::RepeatableRead => "repeatable read",
810 Self::Serializable => "serializable",
811 }
812 }
813}
814
815#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
816pub struct TransactionCharacteristics {
817 pub isolation: Option<TransactionIsolationLevel>,
818 pub read_only: Option<bool>,
819 pub deferrable: Option<bool>,
820}
821
822#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
823pub enum TransactionStmt {
824 Begin,
825 BeginWithCharacteristics(TransactionCharacteristics),
826 Commit,
827 CommitAndChain,
828 Rollback,
829 RollbackAndChain,
830 SetCharacteristics(TransactionCharacteristics),
831 SetSessionCharacteristics(TransactionCharacteristics),
832 SetSnapshot(String),
833 Savepoint(String),
834 ReleaseSavepoint(String),
835 RollbackToSavepoint(String),
836}
837
838#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
839pub enum CursorDirection {
840 Forward,
841 Backward,
842 Absolute,
843 Relative,
844}
845
846#[derive(Debug, Clone, Serialize, Deserialize)]
847pub struct DeclareCursorStmt {
848 pub name: String,
849 pub binary: bool,
850 pub scroll: Option<bool>,
852 pub hold: bool,
853 pub query: Box<SelectStmt>,
854}
855
856#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
857pub struct FetchCursorStmt {
858 pub name: String,
859 pub direction: CursorDirection,
860 pub count: i64,
862 pub move_only: bool,
863}
864
865#[cfg(test)]
866mod tests;