1#[cfg(not(feature = "std"))]
22use alloc::{
23 boxed::Box,
24 format,
25 string::{String, ToString},
26 vec,
27 vec::Vec,
28};
29use core::fmt::{self, Display, Write};
30
31#[cfg(feature = "serde")]
32use serde::{Deserialize, Serialize};
33
34#[cfg(feature = "visitor")]
35use sqlparser_derive::{Visit, VisitMut};
36
37use crate::ast::value::escape_single_quote_string;
38use crate::ast::{
39 display_comma_separated, display_separated,
40 table_constraints::{
41 CheckConstraint, ForeignKeyConstraint, PrimaryKeyConstraint, TableConstraint,
42 UniqueConstraint,
43 },
44 ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
45 CreateFunctionUsing, CreateServerOption, CreateTableLikeKind, CreateTableOptions,
46 CreateViewParams, DataType, Expr,
47 FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDefinitionSetParam, FunctionDesc,
48 FunctionDeterminismSpecifier, FunctionParallel, FunctionSecurity, HiveDistributionStyle,
49 HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident, InitializeKind,
50 MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens, OperateFunctionArg,
51 OrderByExpr, ProjectionSelect, Query, RefreshModeKind, ResetConfig, RowAccessPolicy,
52 SequenceOptions, Spanned, SqlOption, StorageLifecyclePolicy, StorageSerializationPolicy,
53 TableVersion, Tag, TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod,
54 TriggerReferencing, Value, ValueWithSpan, WrappedCollection,
55};
56use crate::display_utils::{DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
57use crate::keywords::Keyword;
58use crate::tokenizer::{Span, Token};
59
60#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
62#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
64pub struct IndexColumn {
65 pub column: OrderByExpr,
67 pub operator_class: Option<ObjectName>,
69}
70
71impl From<Ident> for IndexColumn {
72 fn from(c: Ident) -> Self {
73 Self {
74 column: OrderByExpr::from(c),
75 operator_class: None,
76 }
77 }
78}
79
80impl<'a> From<&'a str> for IndexColumn {
81 fn from(c: &'a str) -> Self {
82 let ident = Ident::new(c);
83 ident.into()
84 }
85}
86
87impl fmt::Display for IndexColumn {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89 write!(f, "{}", self.column)?;
90 if let Some(operator_class) = &self.operator_class {
91 write!(f, " {operator_class}")?;
92 }
93 Ok(())
94 }
95}
96
97#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
102pub enum ReplicaIdentity {
103 Nothing,
105 Full,
107 Default,
109 Index(Ident),
111}
112
113impl fmt::Display for ReplicaIdentity {
114 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115 match self {
116 ReplicaIdentity::Nothing => f.write_str("NOTHING"),
117 ReplicaIdentity::Full => f.write_str("FULL"),
118 ReplicaIdentity::Default => f.write_str("DEFAULT"),
119 ReplicaIdentity::Index(idx) => write!(f, "USING INDEX {idx}"),
120 }
121 }
122}
123
124#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
127#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
128pub enum AlterTableOperation {
129 AddConstraint {
131 constraint: TableConstraint,
133 not_valid: bool,
135 },
136 AddColumn {
138 column_keyword: bool,
140 if_not_exists: bool,
142 column_def: ColumnDef,
144 column_position: Option<MySQLColumnPosition>,
146 },
147 AddProjection {
152 if_not_exists: bool,
154 name: Ident,
156 select: ProjectionSelect,
158 },
159 DropProjection {
164 if_exists: bool,
166 name: Ident,
168 },
169 MaterializeProjection {
174 if_exists: bool,
176 name: Ident,
178 partition: Option<Ident>,
180 },
181 ClearProjection {
186 if_exists: bool,
188 name: Ident,
190 partition: Option<Ident>,
192 },
193 DisableRowLevelSecurity,
198 DisableRule {
202 name: Ident,
204 },
205 DisableTrigger {
209 name: Ident,
211 },
212 DropConstraint {
214 if_exists: bool,
216 name: Ident,
218 drop_behavior: Option<DropBehavior>,
220 },
221 DropColumn {
223 has_column_keyword: bool,
225 column_names: Vec<Ident>,
227 if_exists: bool,
229 drop_behavior: Option<DropBehavior>,
231 },
232 AttachPartition {
236 partition: Partition,
240 },
241 DetachPartition {
245 partition: Partition,
248 },
249 FreezePartition {
253 partition: Partition,
255 with_name: Option<Ident>,
257 },
258 UnfreezePartition {
262 partition: Partition,
264 with_name: Option<Ident>,
266 },
267 DropPrimaryKey {
272 drop_behavior: Option<DropBehavior>,
274 },
275 DropForeignKey {
280 name: Ident,
282 drop_behavior: Option<DropBehavior>,
284 },
285 DropIndex {
289 name: Ident,
291 },
292 EnableAlwaysRule {
296 name: Ident,
298 },
299 EnableAlwaysTrigger {
303 name: Ident,
305 },
306 EnableReplicaRule {
310 name: Ident,
312 },
313 EnableReplicaTrigger {
317 name: Ident,
319 },
320 EnableRowLevelSecurity,
325 ForceRowLevelSecurity,
330 NoForceRowLevelSecurity,
335 EnableRule {
339 name: Ident,
341 },
342 EnableTrigger {
346 name: Ident,
348 },
349 RenamePartitions {
351 old_partitions: Vec<Expr>,
353 new_partitions: Vec<Expr>,
355 },
356 ReplicaIdentity {
361 identity: ReplicaIdentity,
363 },
364 AddPartitions {
366 if_not_exists: bool,
368 new_partitions: Vec<Partition>,
370 },
371 DropPartitions {
373 partitions: Vec<Expr>,
375 if_exists: bool,
377 },
378 RenameColumn {
380 old_column_name: Ident,
382 new_column_name: Ident,
384 },
385 RenameTable {
387 table_name: RenameTableNameKind,
389 },
390 ChangeColumn {
393 old_name: Ident,
395 new_name: Ident,
397 data_type: DataType,
399 options: Vec<ColumnOption>,
401 column_position: Option<MySQLColumnPosition>,
403 },
404 ModifyColumn {
407 col_name: Ident,
409 data_type: DataType,
411 options: Vec<ColumnOption>,
413 column_position: Option<MySQLColumnPosition>,
415 },
416 RenameConstraint {
421 old_name: Ident,
423 new_name: Ident,
425 },
426 AlterColumn {
429 column_name: Ident,
431 op: AlterColumnOperation,
433 },
434 SwapWith {
438 table_name: ObjectName,
440 },
441 SetTblProperties {
443 table_properties: Vec<SqlOption>,
445 },
446 OwnerTo {
450 new_owner: Owner,
452 },
453 ClusterBy {
456 exprs: Vec<Expr>,
458 },
459 DropClusteringKey,
461 AlterSortKey {
464 columns: Vec<Expr>,
466 },
467 SuspendRecluster,
469 ResumeRecluster,
471 Refresh {
477 subpath: Option<String>,
479 },
480 Suspend,
484 Resume,
488 Algorithm {
494 equals: bool,
496 algorithm: AlterTableAlgorithm,
498 },
499
500 Lock {
506 equals: bool,
508 lock: AlterTableLock,
510 },
511 AutoIncrement {
517 equals: bool,
519 value: ValueWithSpan,
521 },
522 ValidateConstraint {
524 name: Ident,
526 },
527 SetOptionsParens {
535 options: Vec<SqlOption>,
537 },
538 SetTablespace {
543 tablespace_name: Ident,
545 },
546}
547
548#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
552#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
553#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
554pub enum AlterPolicyOperation {
555 Rename {
557 new_name: Ident,
559 },
560 Apply {
562 to: Option<Vec<Owner>>,
564 using: Option<Expr>,
566 with_check: Option<Expr>,
568 },
569}
570
571impl fmt::Display for AlterPolicyOperation {
572 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
573 match self {
574 AlterPolicyOperation::Rename { new_name } => {
575 write!(f, " RENAME TO {new_name}")
576 }
577 AlterPolicyOperation::Apply {
578 to,
579 using,
580 with_check,
581 } => {
582 if let Some(to) = to {
583 write!(f, " TO {}", display_comma_separated(to))?;
584 }
585 if let Some(using) = using {
586 write!(f, " USING ({using})")?;
587 }
588 if let Some(with_check) = with_check {
589 write!(f, " WITH CHECK ({with_check})")?;
590 }
591 Ok(())
592 }
593 }
594 }
595}
596
597#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
601#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
602#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
603pub enum AlterTableAlgorithm {
605 Default,
607 Instant,
609 Inplace,
611 Copy,
613}
614
615impl fmt::Display for AlterTableAlgorithm {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
617 f.write_str(match self {
618 Self::Default => "DEFAULT",
619 Self::Instant => "INSTANT",
620 Self::Inplace => "INPLACE",
621 Self::Copy => "COPY",
622 })
623 }
624}
625
626#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
630#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
631#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
632pub enum AlterTableLock {
634 Default,
636 None,
638 Shared,
640 Exclusive,
642}
643
644impl fmt::Display for AlterTableLock {
645 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
646 f.write_str(match self {
647 Self::Default => "DEFAULT",
648 Self::None => "NONE",
649 Self::Shared => "SHARED",
650 Self::Exclusive => "EXCLUSIVE",
651 })
652 }
653}
654
655#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
656#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
657#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
658pub enum Owner {
660 Ident(Ident),
662 CurrentRole,
664 CurrentUser,
666 SessionUser,
668}
669
670impl fmt::Display for Owner {
671 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
672 match self {
673 Owner::Ident(ident) => write!(f, "{ident}"),
674 Owner::CurrentRole => write!(f, "CURRENT_ROLE"),
675 Owner::CurrentUser => write!(f, "CURRENT_USER"),
676 Owner::SessionUser => write!(f, "SESSION_USER"),
677 }
678 }
679}
680
681#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
682#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
683#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
684pub enum AlterConnectorOwner {
686 User(Ident),
688 Role(Ident),
690}
691
692impl fmt::Display for AlterConnectorOwner {
693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
694 match self {
695 AlterConnectorOwner::User(ident) => write!(f, "USER {ident}"),
696 AlterConnectorOwner::Role(ident) => write!(f, "ROLE {ident}"),
697 }
698 }
699}
700
701#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
702#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
703#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
704pub enum AlterIndexOperation {
706 RenameIndex {
708 index_name: ObjectName,
710 },
711 SetTablespace {
716 tablespace_name: Ident,
718 },
719}
720
721impl fmt::Display for AlterTableOperation {
722 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
723 match self {
724 AlterTableOperation::AddPartitions {
725 if_not_exists,
726 new_partitions,
727 } => write!(
728 f,
729 "ADD{ine} {}",
730 display_separated(new_partitions, " "),
731 ine = if *if_not_exists { " IF NOT EXISTS" } else { "" }
732 ),
733 AlterTableOperation::AddConstraint {
734 not_valid,
735 constraint,
736 } => {
737 write!(f, "ADD {constraint}")?;
738 if *not_valid {
739 write!(f, " NOT VALID")?;
740 }
741 Ok(())
742 }
743 AlterTableOperation::AddColumn {
744 column_keyword,
745 if_not_exists,
746 column_def,
747 column_position,
748 } => {
749 write!(f, "ADD")?;
750 if *column_keyword {
751 write!(f, " COLUMN")?;
752 }
753 if *if_not_exists {
754 write!(f, " IF NOT EXISTS")?;
755 }
756 write!(f, " {column_def}")?;
757
758 if let Some(position) = column_position {
759 write!(f, " {position}")?;
760 }
761
762 Ok(())
763 }
764 AlterTableOperation::AddProjection {
765 if_not_exists,
766 name,
767 select: query,
768 } => {
769 write!(f, "ADD PROJECTION")?;
770 if *if_not_exists {
771 write!(f, " IF NOT EXISTS")?;
772 }
773 write!(f, " {name} ({query})")
774 }
775 AlterTableOperation::Algorithm { equals, algorithm } => {
776 write!(
777 f,
778 "ALGORITHM {}{}",
779 if *equals { "= " } else { "" },
780 algorithm
781 )
782 }
783 AlterTableOperation::DropProjection { if_exists, name } => {
784 write!(f, "DROP PROJECTION")?;
785 if *if_exists {
786 write!(f, " IF EXISTS")?;
787 }
788 write!(f, " {name}")
789 }
790 AlterTableOperation::MaterializeProjection {
791 if_exists,
792 name,
793 partition,
794 } => {
795 write!(f, "MATERIALIZE PROJECTION")?;
796 if *if_exists {
797 write!(f, " IF EXISTS")?;
798 }
799 write!(f, " {name}")?;
800 if let Some(partition) = partition {
801 write!(f, " IN PARTITION {partition}")?;
802 }
803 Ok(())
804 }
805 AlterTableOperation::ClearProjection {
806 if_exists,
807 name,
808 partition,
809 } => {
810 write!(f, "CLEAR PROJECTION")?;
811 if *if_exists {
812 write!(f, " IF EXISTS")?;
813 }
814 write!(f, " {name}")?;
815 if let Some(partition) = partition {
816 write!(f, " IN PARTITION {partition}")?;
817 }
818 Ok(())
819 }
820 AlterTableOperation::AlterColumn { column_name, op } => {
821 write!(f, "ALTER COLUMN {column_name} {op}")
822 }
823 AlterTableOperation::DisableRowLevelSecurity => {
824 write!(f, "DISABLE ROW LEVEL SECURITY")
825 }
826 AlterTableOperation::DisableRule { name } => {
827 write!(f, "DISABLE RULE {name}")
828 }
829 AlterTableOperation::DisableTrigger { name } => {
830 write!(f, "DISABLE TRIGGER {name}")
831 }
832 AlterTableOperation::DropPartitions {
833 partitions,
834 if_exists,
835 } => write!(
836 f,
837 "DROP{ie} PARTITION ({})",
838 display_comma_separated(partitions),
839 ie = if *if_exists { " IF EXISTS" } else { "" }
840 ),
841 AlterTableOperation::DropConstraint {
842 if_exists,
843 name,
844 drop_behavior,
845 } => {
846 write!(
847 f,
848 "DROP CONSTRAINT {}{}",
849 if *if_exists { "IF EXISTS " } else { "" },
850 name
851 )?;
852 if let Some(drop_behavior) = drop_behavior {
853 write!(f, " {drop_behavior}")?;
854 }
855 Ok(())
856 }
857 AlterTableOperation::DropPrimaryKey { drop_behavior } => {
858 write!(f, "DROP PRIMARY KEY")?;
859 if let Some(drop_behavior) = drop_behavior {
860 write!(f, " {drop_behavior}")?;
861 }
862 Ok(())
863 }
864 AlterTableOperation::DropForeignKey {
865 name,
866 drop_behavior,
867 } => {
868 write!(f, "DROP FOREIGN KEY {name}")?;
869 if let Some(drop_behavior) = drop_behavior {
870 write!(f, " {drop_behavior}")?;
871 }
872 Ok(())
873 }
874 AlterTableOperation::DropIndex { name } => write!(f, "DROP INDEX {name}"),
875 AlterTableOperation::DropColumn {
876 has_column_keyword,
877 column_names: column_name,
878 if_exists,
879 drop_behavior,
880 } => {
881 write!(
882 f,
883 "DROP {}{}{}",
884 if *has_column_keyword { "COLUMN " } else { "" },
885 if *if_exists { "IF EXISTS " } else { "" },
886 display_comma_separated(column_name),
887 )?;
888 if let Some(drop_behavior) = drop_behavior {
889 write!(f, " {drop_behavior}")?;
890 }
891 Ok(())
892 }
893 AlterTableOperation::AttachPartition { partition } => {
894 write!(f, "ATTACH {partition}")
895 }
896 AlterTableOperation::DetachPartition { partition } => {
897 write!(f, "DETACH {partition}")
898 }
899 AlterTableOperation::EnableAlwaysRule { name } => {
900 write!(f, "ENABLE ALWAYS RULE {name}")
901 }
902 AlterTableOperation::EnableAlwaysTrigger { name } => {
903 write!(f, "ENABLE ALWAYS TRIGGER {name}")
904 }
905 AlterTableOperation::EnableReplicaRule { name } => {
906 write!(f, "ENABLE REPLICA RULE {name}")
907 }
908 AlterTableOperation::EnableReplicaTrigger { name } => {
909 write!(f, "ENABLE REPLICA TRIGGER {name}")
910 }
911 AlterTableOperation::EnableRowLevelSecurity => {
912 write!(f, "ENABLE ROW LEVEL SECURITY")
913 }
914 AlterTableOperation::ForceRowLevelSecurity => {
915 write!(f, "FORCE ROW LEVEL SECURITY")
916 }
917 AlterTableOperation::NoForceRowLevelSecurity => {
918 write!(f, "NO FORCE ROW LEVEL SECURITY")
919 }
920 AlterTableOperation::EnableRule { name } => {
921 write!(f, "ENABLE RULE {name}")
922 }
923 AlterTableOperation::EnableTrigger { name } => {
924 write!(f, "ENABLE TRIGGER {name}")
925 }
926 AlterTableOperation::RenamePartitions {
927 old_partitions,
928 new_partitions,
929 } => write!(
930 f,
931 "PARTITION ({}) RENAME TO PARTITION ({})",
932 display_comma_separated(old_partitions),
933 display_comma_separated(new_partitions)
934 ),
935 AlterTableOperation::RenameColumn {
936 old_column_name,
937 new_column_name,
938 } => write!(f, "RENAME COLUMN {old_column_name} TO {new_column_name}"),
939 AlterTableOperation::RenameTable { table_name } => {
940 write!(f, "RENAME {table_name}")
941 }
942 AlterTableOperation::ChangeColumn {
943 old_name,
944 new_name,
945 data_type,
946 options,
947 column_position,
948 } => {
949 write!(f, "CHANGE COLUMN {old_name} {new_name} {data_type}")?;
950 if !options.is_empty() {
951 write!(f, " {}", display_separated(options, " "))?;
952 }
953 if let Some(position) = column_position {
954 write!(f, " {position}")?;
955 }
956
957 Ok(())
958 }
959 AlterTableOperation::ModifyColumn {
960 col_name,
961 data_type,
962 options,
963 column_position,
964 } => {
965 write!(f, "MODIFY COLUMN {col_name} {data_type}")?;
966 if !options.is_empty() {
967 write!(f, " {}", display_separated(options, " "))?;
968 }
969 if let Some(position) = column_position {
970 write!(f, " {position}")?;
971 }
972
973 Ok(())
974 }
975 AlterTableOperation::RenameConstraint { old_name, new_name } => {
976 write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
977 }
978 AlterTableOperation::SwapWith { table_name } => {
979 write!(f, "SWAP WITH {table_name}")
980 }
981 AlterTableOperation::OwnerTo { new_owner } => {
982 write!(f, "OWNER TO {new_owner}")
983 }
984 AlterTableOperation::SetTblProperties { table_properties } => {
985 write!(
986 f,
987 "SET TBLPROPERTIES({})",
988 display_comma_separated(table_properties)
989 )
990 }
991 AlterTableOperation::FreezePartition {
992 partition,
993 with_name,
994 } => {
995 write!(f, "FREEZE {partition}")?;
996 if let Some(name) = with_name {
997 write!(f, " WITH NAME {name}")?;
998 }
999 Ok(())
1000 }
1001 AlterTableOperation::UnfreezePartition {
1002 partition,
1003 with_name,
1004 } => {
1005 write!(f, "UNFREEZE {partition}")?;
1006 if let Some(name) = with_name {
1007 write!(f, " WITH NAME {name}")?;
1008 }
1009 Ok(())
1010 }
1011 AlterTableOperation::ClusterBy { exprs } => {
1012 write!(f, "CLUSTER BY ({})", display_comma_separated(exprs))?;
1013 Ok(())
1014 }
1015 AlterTableOperation::DropClusteringKey => {
1016 write!(f, "DROP CLUSTERING KEY")?;
1017 Ok(())
1018 }
1019 AlterTableOperation::AlterSortKey { columns } => {
1020 write!(f, "ALTER SORTKEY({})", display_comma_separated(columns))?;
1021 Ok(())
1022 }
1023 AlterTableOperation::SuspendRecluster => {
1024 write!(f, "SUSPEND RECLUSTER")?;
1025 Ok(())
1026 }
1027 AlterTableOperation::ResumeRecluster => {
1028 write!(f, "RESUME RECLUSTER")?;
1029 Ok(())
1030 }
1031 AlterTableOperation::Refresh { subpath } => {
1032 write!(f, "REFRESH")?;
1033 if let Some(path) = subpath {
1034 write!(f, " '{path}'")?;
1035 }
1036 Ok(())
1037 }
1038 AlterTableOperation::Suspend => {
1039 write!(f, "SUSPEND")
1040 }
1041 AlterTableOperation::Resume => {
1042 write!(f, "RESUME")
1043 }
1044 AlterTableOperation::AutoIncrement { equals, value } => {
1045 write!(
1046 f,
1047 "AUTO_INCREMENT {}{}",
1048 if *equals { "= " } else { "" },
1049 value
1050 )
1051 }
1052 AlterTableOperation::Lock { equals, lock } => {
1053 write!(f, "LOCK {}{}", if *equals { "= " } else { "" }, lock)
1054 }
1055 AlterTableOperation::ReplicaIdentity { identity } => {
1056 write!(f, "REPLICA IDENTITY {identity}")
1057 }
1058 AlterTableOperation::ValidateConstraint { name } => {
1059 write!(f, "VALIDATE CONSTRAINT {name}")
1060 }
1061 AlterTableOperation::SetOptionsParens { options } => {
1062 write!(f, "SET ({})", display_comma_separated(options))
1063 }
1064 AlterTableOperation::SetTablespace { tablespace_name } => {
1065 write!(f, "SET TABLESPACE {tablespace_name}")
1066 }
1067 }
1068 }
1069}
1070
1071impl fmt::Display for AlterIndexOperation {
1072 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1073 match self {
1074 AlterIndexOperation::RenameIndex { index_name } => {
1075 write!(f, "RENAME TO {index_name}")
1076 }
1077 AlterIndexOperation::SetTablespace { tablespace_name } => {
1078 write!(f, "SET TABLESPACE {tablespace_name}")
1079 }
1080 }
1081 }
1082}
1083
1084#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1086#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1087#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1088pub struct AlterType {
1089 pub name: ObjectName,
1091 pub operation: AlterTypeOperation,
1093}
1094
1095#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1097#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1098#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1099pub enum AlterTypeOperation {
1100 Rename(AlterTypeRename),
1102 AddValue(AlterTypeAddValue),
1104 RenameValue(AlterTypeRenameValue),
1106}
1107
1108#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1110#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1111#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1112pub struct AlterTypeRename {
1113 pub new_name: Ident,
1115}
1116
1117#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1119#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1120#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1121pub struct AlterTypeAddValue {
1122 pub if_not_exists: bool,
1124 pub value: Ident,
1126 pub position: Option<AlterTypeAddValuePosition>,
1128}
1129
1130#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1132#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1133#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1134pub enum AlterTypeAddValuePosition {
1135 Before(Ident),
1137 After(Ident),
1139}
1140
1141#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1143#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1144#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1145pub struct AlterTypeRenameValue {
1146 pub from: Ident,
1148 pub to: Ident,
1150}
1151
1152impl fmt::Display for AlterTypeOperation {
1153 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1154 match self {
1155 Self::Rename(AlterTypeRename { new_name }) => {
1156 write!(f, "RENAME TO {new_name}")
1157 }
1158 Self::AddValue(AlterTypeAddValue {
1159 if_not_exists,
1160 value,
1161 position,
1162 }) => {
1163 write!(f, "ADD VALUE")?;
1164 if *if_not_exists {
1165 write!(f, " IF NOT EXISTS")?;
1166 }
1167 write!(f, " {value}")?;
1168 match position {
1169 Some(AlterTypeAddValuePosition::Before(neighbor_value)) => {
1170 write!(f, " BEFORE {neighbor_value}")?;
1171 }
1172 Some(AlterTypeAddValuePosition::After(neighbor_value)) => {
1173 write!(f, " AFTER {neighbor_value}")?;
1174 }
1175 None => {}
1176 };
1177 Ok(())
1178 }
1179 Self::RenameValue(AlterTypeRenameValue { from, to }) => {
1180 write!(f, "RENAME VALUE {from} TO {to}")
1181 }
1182 }
1183 }
1184}
1185
1186#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1189#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1190#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1191pub struct AlterOperator {
1192 pub name: ObjectName,
1194 pub left_type: Option<DataType>,
1196 pub right_type: DataType,
1198 pub operation: AlterOperatorOperation,
1200}
1201
1202#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1204#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1205#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1206pub enum AlterOperatorOperation {
1207 OwnerTo(Owner),
1209 SetSchema {
1212 schema_name: ObjectName,
1214 },
1215 Set {
1217 options: Vec<OperatorOption>,
1219 },
1220}
1221
1222#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1224#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1225#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1226pub enum OperatorOption {
1227 Restrict(Option<ObjectName>),
1229 Join(Option<ObjectName>),
1231 Commutator(ObjectName),
1233 Negator(ObjectName),
1235 Hashes,
1237 Merges,
1239}
1240
1241impl fmt::Display for AlterOperator {
1242 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1243 write!(f, "ALTER OPERATOR {} (", self.name)?;
1244 if let Some(left_type) = &self.left_type {
1245 write!(f, "{}", left_type)?;
1246 } else {
1247 write!(f, "NONE")?;
1248 }
1249 write!(f, ", {}) {}", self.right_type, self.operation)
1250 }
1251}
1252
1253impl fmt::Display for AlterOperatorOperation {
1254 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1255 match self {
1256 Self::OwnerTo(owner) => write!(f, "OWNER TO {}", owner),
1257 Self::SetSchema { schema_name } => write!(f, "SET SCHEMA {}", schema_name),
1258 Self::Set { options } => {
1259 write!(f, "SET (")?;
1260 for (i, option) in options.iter().enumerate() {
1261 if i > 0 {
1262 write!(f, ", ")?;
1263 }
1264 write!(f, "{}", option)?;
1265 }
1266 write!(f, ")")
1267 }
1268 }
1269 }
1270}
1271
1272impl fmt::Display for OperatorOption {
1273 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1274 match self {
1275 Self::Restrict(Some(proc_name)) => write!(f, "RESTRICT = {}", proc_name),
1276 Self::Restrict(None) => write!(f, "RESTRICT = NONE"),
1277 Self::Join(Some(proc_name)) => write!(f, "JOIN = {}", proc_name),
1278 Self::Join(None) => write!(f, "JOIN = NONE"),
1279 Self::Commutator(op_name) => write!(f, "COMMUTATOR = {}", op_name),
1280 Self::Negator(op_name) => write!(f, "NEGATOR = {}", op_name),
1281 Self::Hashes => write!(f, "HASHES"),
1282 Self::Merges => write!(f, "MERGES"),
1283 }
1284 }
1285}
1286
1287#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1289#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1290#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1291pub enum AlterColumnOperation {
1292 SetNotNull,
1294 DropNotNull,
1296 SetDefault {
1299 value: Expr,
1301 },
1302 DropDefault,
1304 SetDataType {
1306 data_type: DataType,
1308 using: Option<Expr>,
1310 had_set: bool,
1312 },
1313
1314 AddGenerated {
1318 generated_as: Option<GeneratedAs>,
1320 sequence_options: Option<Vec<SequenceOptions>>,
1322 },
1323}
1324
1325impl fmt::Display for AlterColumnOperation {
1326 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1327 match self {
1328 AlterColumnOperation::SetNotNull => write!(f, "SET NOT NULL",),
1329 AlterColumnOperation::DropNotNull => write!(f, "DROP NOT NULL",),
1330 AlterColumnOperation::SetDefault { value } => {
1331 write!(f, "SET DEFAULT {value}")
1332 }
1333 AlterColumnOperation::DropDefault => {
1334 write!(f, "DROP DEFAULT")
1335 }
1336 AlterColumnOperation::SetDataType {
1337 data_type,
1338 using,
1339 had_set,
1340 } => {
1341 if *had_set {
1342 write!(f, "SET DATA ")?;
1343 }
1344 write!(f, "TYPE {data_type}")?;
1345 if let Some(expr) = using {
1346 write!(f, " USING {expr}")?;
1347 }
1348 Ok(())
1349 }
1350 AlterColumnOperation::AddGenerated {
1351 generated_as,
1352 sequence_options,
1353 } => {
1354 let generated_as = match generated_as {
1355 Some(GeneratedAs::Always) => " ALWAYS",
1356 Some(GeneratedAs::ByDefault) => " BY DEFAULT",
1357 _ => "",
1358 };
1359
1360 write!(f, "ADD GENERATED{generated_as} AS IDENTITY",)?;
1361 if let Some(options) = sequence_options {
1362 write!(f, " (")?;
1363
1364 for sequence_option in options {
1365 write!(f, "{sequence_option}")?;
1366 }
1367
1368 write!(f, " )")?;
1369 }
1370 Ok(())
1371 }
1372 }
1373 }
1374}
1375
1376#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1384#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1385#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1386pub enum KeyOrIndexDisplay {
1387 None,
1389 Key,
1391 Index,
1393}
1394
1395impl KeyOrIndexDisplay {
1396 pub fn is_none(self) -> bool {
1398 matches!(self, Self::None)
1399 }
1400}
1401
1402impl fmt::Display for KeyOrIndexDisplay {
1403 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1404 let left_space = matches!(f.align(), Some(fmt::Alignment::Right));
1405
1406 if left_space && !self.is_none() {
1407 f.write_char(' ')?
1408 }
1409
1410 match self {
1411 KeyOrIndexDisplay::None => {
1412 write!(f, "")
1413 }
1414 KeyOrIndexDisplay::Key => {
1415 write!(f, "KEY")
1416 }
1417 KeyOrIndexDisplay::Index => {
1418 write!(f, "INDEX")
1419 }
1420 }
1421 }
1422}
1423
1424#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1433#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1434#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1435pub enum IndexType {
1436 BTree,
1438 Hash,
1440 GIN,
1442 GiST,
1444 SPGiST,
1446 BRIN,
1448 Bloom,
1450 Custom(Ident),
1453}
1454
1455impl fmt::Display for IndexType {
1456 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1457 match self {
1458 Self::BTree => write!(f, "BTREE"),
1459 Self::Hash => write!(f, "HASH"),
1460 Self::GIN => write!(f, "GIN"),
1461 Self::GiST => write!(f, "GIST"),
1462 Self::SPGiST => write!(f, "SPGIST"),
1463 Self::BRIN => write!(f, "BRIN"),
1464 Self::Bloom => write!(f, "BLOOM"),
1465 Self::Custom(name) => write!(f, "{name}"),
1466 }
1467 }
1468}
1469
1470#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1476#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1477#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1478pub enum IndexOption {
1479 Using(IndexType),
1483 Comment(String),
1485}
1486
1487impl fmt::Display for IndexOption {
1488 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1489 match self {
1490 Self::Using(index_type) => write!(f, "USING {index_type}"),
1491 Self::Comment(s) => write!(f, "COMMENT '{s}'"),
1492 }
1493 }
1494}
1495
1496#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1500#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1501#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1502pub enum NullsDistinctOption {
1503 None,
1505 Distinct,
1507 NotDistinct,
1509}
1510
1511impl fmt::Display for NullsDistinctOption {
1512 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1513 match self {
1514 Self::None => Ok(()),
1515 Self::Distinct => write!(f, " NULLS DISTINCT"),
1516 Self::NotDistinct => write!(f, " NULLS NOT DISTINCT"),
1517 }
1518 }
1519}
1520
1521#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1522#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1523#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1524pub struct ProcedureParam {
1526 pub name: Ident,
1528 pub data_type: DataType,
1530 pub mode: Option<ArgMode>,
1532 pub default: Option<Expr>,
1534}
1535
1536impl fmt::Display for ProcedureParam {
1537 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1538 if let Some(mode) = &self.mode {
1539 if let Some(default) = &self.default {
1540 write!(f, "{mode} {} {} = {}", self.name, self.data_type, default)
1541 } else {
1542 write!(f, "{mode} {} {}", self.name, self.data_type)
1543 }
1544 } else if let Some(default) = &self.default {
1545 write!(f, "{} {} = {}", self.name, self.data_type, default)
1546 } else {
1547 write!(f, "{} {}", self.name, self.data_type)
1548 }
1549 }
1550}
1551
1552#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1554#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1555#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1556pub struct ColumnDef {
1557 pub name: Ident,
1559 pub data_type: DataType,
1561 pub options: Vec<ColumnOptionDef>,
1563}
1564
1565impl fmt::Display for ColumnDef {
1566 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1567 if self.data_type == DataType::Unspecified {
1568 write!(f, "{}", self.name)?;
1569 } else {
1570 write!(f, "{} {}", self.name, self.data_type)?;
1571 }
1572 for option in &self.options {
1573 write!(f, " {option}")?;
1574 }
1575 Ok(())
1576 }
1577}
1578
1579#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1596#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1597#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1598pub struct ViewColumnDef {
1599 pub name: Ident,
1601 pub data_type: Option<DataType>,
1603 pub options: Option<ColumnOptions>,
1605}
1606
1607#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1608#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1609#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1610pub enum ColumnOptions {
1612 CommaSeparated(Vec<ColumnOption>),
1614 SpaceSeparated(Vec<ColumnOption>),
1616}
1617
1618impl ColumnOptions {
1619 pub fn as_slice(&self) -> &[ColumnOption] {
1621 match self {
1622 ColumnOptions::CommaSeparated(options) => options.as_slice(),
1623 ColumnOptions::SpaceSeparated(options) => options.as_slice(),
1624 }
1625 }
1626}
1627
1628impl fmt::Display for ViewColumnDef {
1629 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1630 write!(f, "{}", self.name)?;
1631 if let Some(data_type) = self.data_type.as_ref() {
1632 write!(f, " {data_type}")?;
1633 }
1634 if let Some(options) = self.options.as_ref() {
1635 match options {
1636 ColumnOptions::CommaSeparated(column_options) => {
1637 write!(f, " {}", display_comma_separated(column_options.as_slice()))?;
1638 }
1639 ColumnOptions::SpaceSeparated(column_options) => {
1640 write!(f, " {}", display_separated(column_options.as_slice(), " "))?
1641 }
1642 }
1643 }
1644 Ok(())
1645 }
1646}
1647
1648#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1665#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1666#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1667pub struct ColumnOptionDef {
1668 pub name: Option<Ident>,
1670 pub option: ColumnOption,
1672}
1673
1674impl fmt::Display for ColumnOptionDef {
1675 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1676 write!(f, "{}{}", display_constraint_name(&self.name), self.option)
1677 }
1678}
1679
1680#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1688#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1689#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1690pub enum IdentityPropertyKind {
1691 Autoincrement(IdentityProperty),
1699 Identity(IdentityProperty),
1712}
1713
1714impl fmt::Display for IdentityPropertyKind {
1715 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1716 let (command, property) = match self {
1717 IdentityPropertyKind::Identity(property) => ("IDENTITY", property),
1718 IdentityPropertyKind::Autoincrement(property) => ("AUTOINCREMENT", property),
1719 };
1720 write!(f, "{command}")?;
1721 if let Some(parameters) = &property.parameters {
1722 write!(f, "{parameters}")?;
1723 }
1724 if let Some(order) = &property.order {
1725 write!(f, "{order}")?;
1726 }
1727 Ok(())
1728 }
1729}
1730
1731#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1733#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1734#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1735pub struct IdentityProperty {
1736 pub parameters: Option<IdentityPropertyFormatKind>,
1738 pub order: Option<IdentityPropertyOrder>,
1740}
1741
1742#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1757#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1758#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1759pub enum IdentityPropertyFormatKind {
1760 FunctionCall(IdentityParameters),
1768 StartAndIncrement(IdentityParameters),
1775}
1776
1777impl fmt::Display for IdentityPropertyFormatKind {
1778 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1779 match self {
1780 IdentityPropertyFormatKind::FunctionCall(parameters) => {
1781 write!(f, "({}, {})", parameters.seed, parameters.increment)
1782 }
1783 IdentityPropertyFormatKind::StartAndIncrement(parameters) => {
1784 write!(
1785 f,
1786 " START {} INCREMENT {}",
1787 parameters.seed, parameters.increment
1788 )
1789 }
1790 }
1791 }
1792}
1793#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1795#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1796#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1797pub struct IdentityParameters {
1798 pub seed: Expr,
1800 pub increment: Expr,
1802}
1803
1804#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
1811#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1812#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1813pub enum IdentityPropertyOrder {
1814 Order,
1816 NoOrder,
1818}
1819
1820impl fmt::Display for IdentityPropertyOrder {
1821 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1822 match self {
1823 IdentityPropertyOrder::Order => write!(f, " ORDER"),
1824 IdentityPropertyOrder::NoOrder => write!(f, " NOORDER"),
1825 }
1826 }
1827}
1828
1829#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1837#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1838#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1839pub enum ColumnPolicy {
1840 MaskingPolicy(ColumnPolicyProperty),
1842 ProjectionPolicy(ColumnPolicyProperty),
1844}
1845
1846impl fmt::Display for ColumnPolicy {
1847 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1848 let (command, property) = match self {
1849 ColumnPolicy::MaskingPolicy(property) => ("MASKING POLICY", property),
1850 ColumnPolicy::ProjectionPolicy(property) => ("PROJECTION POLICY", property),
1851 };
1852 if property.with {
1853 write!(f, "WITH ")?;
1854 }
1855 write!(f, "{command} {}", property.policy_name)?;
1856 if let Some(using_columns) = &property.using_columns {
1857 write!(f, " USING ({})", display_comma_separated(using_columns))?;
1858 }
1859 Ok(())
1860 }
1861}
1862
1863#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1864#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1865#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1866pub struct ColumnPolicyProperty {
1868 pub with: bool,
1875 pub policy_name: ObjectName,
1877 pub using_columns: Option<Vec<Ident>>,
1879}
1880
1881#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1888#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1889#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1890pub struct TagsColumnOption {
1891 pub with: bool,
1898 pub tags: Vec<Tag>,
1900}
1901
1902impl fmt::Display for TagsColumnOption {
1903 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1904 if self.with {
1905 write!(f, "WITH ")?;
1906 }
1907 write!(f, "TAG ({})", display_comma_separated(&self.tags))?;
1908 Ok(())
1909 }
1910}
1911
1912#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1915#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1916#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1917pub enum ColumnOption {
1918 Null,
1920 NotNull,
1922 Default(Expr),
1924
1925 Materialized(Expr),
1930 Ephemeral(Option<Expr>),
1934 Alias(Expr),
1938
1939 PrimaryKey(PrimaryKeyConstraint),
1941 Unique(UniqueConstraint),
1943 ForeignKey(ForeignKeyConstraint),
1951 Check(CheckConstraint),
1953 DialectSpecific(Vec<Token>),
1957 CharacterSet(ObjectName),
1959 Collation(ObjectName),
1961 Comment(String),
1963 OnUpdate(Expr),
1965 Generated {
1968 generated_as: GeneratedAs,
1970 sequence_options: Option<Vec<SequenceOptions>>,
1972 generation_expr: Option<Expr>,
1974 generation_expr_mode: Option<GeneratedExpressionMode>,
1976 generated_keyword: bool,
1978 },
1979 Options(Vec<SqlOption>),
1987 Identity(IdentityPropertyKind),
1995 OnConflict(Keyword),
1998 Policy(ColumnPolicy),
2006 Tags(TagsColumnOption),
2013 Srid(Box<Expr>),
2020 Invisible,
2027}
2028
2029impl From<UniqueConstraint> for ColumnOption {
2030 fn from(c: UniqueConstraint) -> Self {
2031 ColumnOption::Unique(c)
2032 }
2033}
2034
2035impl From<PrimaryKeyConstraint> for ColumnOption {
2036 fn from(c: PrimaryKeyConstraint) -> Self {
2037 ColumnOption::PrimaryKey(c)
2038 }
2039}
2040
2041impl From<CheckConstraint> for ColumnOption {
2042 fn from(c: CheckConstraint) -> Self {
2043 ColumnOption::Check(c)
2044 }
2045}
2046impl From<ForeignKeyConstraint> for ColumnOption {
2047 fn from(fk: ForeignKeyConstraint) -> Self {
2048 ColumnOption::ForeignKey(fk)
2049 }
2050}
2051
2052impl fmt::Display for ColumnOption {
2053 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2054 use ColumnOption::*;
2055 match self {
2056 Null => write!(f, "NULL"),
2057 NotNull => write!(f, "NOT NULL"),
2058 Default(expr) => write!(f, "DEFAULT {expr}"),
2059 Materialized(expr) => write!(f, "MATERIALIZED {expr}"),
2060 Ephemeral(expr) => {
2061 if let Some(e) = expr {
2062 write!(f, "EPHEMERAL {e}")
2063 } else {
2064 write!(f, "EPHEMERAL")
2065 }
2066 }
2067 Alias(expr) => write!(f, "ALIAS {expr}"),
2068 PrimaryKey(constraint) => {
2069 write!(f, "PRIMARY KEY")?;
2070 if let Some(characteristics) = &constraint.characteristics {
2071 write!(f, " {characteristics}")?;
2072 }
2073 Ok(())
2074 }
2075 Unique(constraint) => {
2076 write!(f, "UNIQUE{:>}", constraint.index_type_display)?;
2077 if let Some(characteristics) = &constraint.characteristics {
2078 write!(f, " {characteristics}")?;
2079 }
2080 Ok(())
2081 }
2082 ForeignKey(constraint) => {
2083 write!(f, "REFERENCES {}", constraint.foreign_table)?;
2084 if !constraint.referred_columns.is_empty() {
2085 write!(
2086 f,
2087 " ({})",
2088 display_comma_separated(&constraint.referred_columns)
2089 )?;
2090 }
2091 if let Some(match_kind) = &constraint.match_kind {
2092 write!(f, " {match_kind}")?;
2093 }
2094 if let Some(action) = &constraint.on_delete {
2095 write!(f, " ON DELETE {action}")?;
2096 }
2097 if let Some(action) = &constraint.on_update {
2098 write!(f, " ON UPDATE {action}")?;
2099 }
2100 if let Some(characteristics) = &constraint.characteristics {
2101 write!(f, " {characteristics}")?;
2102 }
2103 Ok(())
2104 }
2105 Check(constraint) => write!(f, "{constraint}"),
2106 DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
2107 CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
2108 Collation(n) => write!(f, "COLLATE {n}"),
2109 Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
2110 OnUpdate(expr) => write!(f, "ON UPDATE {expr}"),
2111 Generated {
2112 generated_as,
2113 sequence_options,
2114 generation_expr,
2115 generation_expr_mode,
2116 generated_keyword,
2117 } => {
2118 if let Some(expr) = generation_expr {
2119 let modifier = match generation_expr_mode {
2120 None => "",
2121 Some(GeneratedExpressionMode::Virtual) => " VIRTUAL",
2122 Some(GeneratedExpressionMode::Stored) => " STORED",
2123 };
2124 if *generated_keyword {
2125 write!(f, "GENERATED ALWAYS AS ({expr}){modifier}")?;
2126 } else {
2127 write!(f, "AS ({expr}){modifier}")?;
2128 }
2129 Ok(())
2130 } else {
2131 let when = match generated_as {
2133 GeneratedAs::Always => "ALWAYS",
2134 GeneratedAs::ByDefault => "BY DEFAULT",
2135 GeneratedAs::ExpStored => "",
2137 };
2138 write!(f, "GENERATED {when} AS IDENTITY")?;
2139 if sequence_options.is_some() {
2140 let so = sequence_options.as_ref().unwrap();
2141 if !so.is_empty() {
2142 write!(f, " (")?;
2143 }
2144 for sequence_option in so {
2145 write!(f, "{sequence_option}")?;
2146 }
2147 if !so.is_empty() {
2148 write!(f, " )")?;
2149 }
2150 }
2151 Ok(())
2152 }
2153 }
2154 Options(options) => {
2155 write!(f, "OPTIONS({})", display_comma_separated(options))
2156 }
2157 Identity(parameters) => {
2158 write!(f, "{parameters}")
2159 }
2160 OnConflict(keyword) => {
2161 write!(f, "ON CONFLICT {keyword:?}")?;
2162 Ok(())
2163 }
2164 Policy(parameters) => {
2165 write!(f, "{parameters}")
2166 }
2167 Tags(tags) => {
2168 write!(f, "{tags}")
2169 }
2170 Srid(srid) => {
2171 write!(f, "SRID {srid}")
2172 }
2173 Invisible => {
2174 write!(f, "INVISIBLE")
2175 }
2176 }
2177 }
2178}
2179
2180#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
2183#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2184#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2185pub enum GeneratedAs {
2186 Always,
2188 ByDefault,
2190 ExpStored,
2192}
2193
2194#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
2197#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2198#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2199pub enum GeneratedExpressionMode {
2200 Virtual,
2202 Stored,
2204}
2205
2206#[must_use]
2207pub(crate) fn display_constraint_name(name: &'_ Option<Ident>) -> impl fmt::Display + '_ {
2208 struct ConstraintName<'a>(&'a Option<Ident>);
2209 impl fmt::Display for ConstraintName<'_> {
2210 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2211 if let Some(name) = self.0 {
2212 write!(f, "CONSTRAINT {name} ")?;
2213 }
2214 Ok(())
2215 }
2216 }
2217 ConstraintName(name)
2218}
2219
2220#[must_use]
2224pub(crate) fn display_option<'a, T: fmt::Display>(
2225 prefix: &'a str,
2226 postfix: &'a str,
2227 option: &'a Option<T>,
2228) -> impl fmt::Display + 'a {
2229 struct OptionDisplay<'a, T>(&'a str, &'a str, &'a Option<T>);
2230 impl<T: fmt::Display> fmt::Display for OptionDisplay<'_, T> {
2231 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2232 if let Some(inner) = self.2 {
2233 let (prefix, postfix) = (self.0, self.1);
2234 write!(f, "{prefix}{inner}{postfix}")?;
2235 }
2236 Ok(())
2237 }
2238 }
2239 OptionDisplay(prefix, postfix, option)
2240}
2241
2242#[must_use]
2246pub(crate) fn display_option_spaced<T: fmt::Display>(option: &Option<T>) -> impl fmt::Display + '_ {
2247 display_option(" ", "", option)
2248}
2249
2250#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Default, Eq, Ord, Hash)]
2254#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2255#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2256pub struct ConstraintCharacteristics {
2257 pub deferrable: Option<bool>,
2259 pub initially: Option<DeferrableInitial>,
2261 pub enforced: Option<bool>,
2263}
2264
2265#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2267#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2268#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2269pub enum DeferrableInitial {
2270 Immediate,
2272 Deferred,
2274}
2275
2276impl ConstraintCharacteristics {
2277 fn deferrable_text(&self) -> Option<&'static str> {
2278 self.deferrable.map(|deferrable| {
2279 if deferrable {
2280 "DEFERRABLE"
2281 } else {
2282 "NOT DEFERRABLE"
2283 }
2284 })
2285 }
2286
2287 fn initially_immediate_text(&self) -> Option<&'static str> {
2288 self.initially
2289 .map(|initially_immediate| match initially_immediate {
2290 DeferrableInitial::Immediate => "INITIALLY IMMEDIATE",
2291 DeferrableInitial::Deferred => "INITIALLY DEFERRED",
2292 })
2293 }
2294
2295 fn enforced_text(&self) -> Option<&'static str> {
2296 self.enforced.map(
2297 |enforced| {
2298 if enforced {
2299 "ENFORCED"
2300 } else {
2301 "NOT ENFORCED"
2302 }
2303 },
2304 )
2305 }
2306}
2307
2308impl fmt::Display for ConstraintCharacteristics {
2309 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2310 let deferrable = self.deferrable_text();
2311 let initially_immediate = self.initially_immediate_text();
2312 let enforced = self.enforced_text();
2313
2314 match (deferrable, initially_immediate, enforced) {
2315 (None, None, None) => Ok(()),
2316 (None, None, Some(enforced)) => write!(f, "{enforced}"),
2317 (None, Some(initial), None) => write!(f, "{initial}"),
2318 (None, Some(initial), Some(enforced)) => write!(f, "{initial} {enforced}"),
2319 (Some(deferrable), None, None) => write!(f, "{deferrable}"),
2320 (Some(deferrable), None, Some(enforced)) => write!(f, "{deferrable} {enforced}"),
2321 (Some(deferrable), Some(initial), None) => write!(f, "{deferrable} {initial}"),
2322 (Some(deferrable), Some(initial), Some(enforced)) => {
2323 write!(f, "{deferrable} {initial} {enforced}")
2324 }
2325 }
2326 }
2327}
2328
2329#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2334#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2335#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2336pub enum ReferentialAction {
2337 Restrict,
2339 Cascade,
2341 SetNull,
2343 NoAction,
2345 SetDefault,
2347}
2348
2349impl fmt::Display for ReferentialAction {
2350 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2351 f.write_str(match self {
2352 ReferentialAction::Restrict => "RESTRICT",
2353 ReferentialAction::Cascade => "CASCADE",
2354 ReferentialAction::SetNull => "SET NULL",
2355 ReferentialAction::NoAction => "NO ACTION",
2356 ReferentialAction::SetDefault => "SET DEFAULT",
2357 })
2358 }
2359}
2360
2361#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2365#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2366#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2367pub enum DropBehavior {
2368 Restrict,
2370 Cascade,
2372}
2373
2374impl fmt::Display for DropBehavior {
2375 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2376 f.write_str(match self {
2377 DropBehavior::Restrict => "RESTRICT",
2378 DropBehavior::Cascade => "CASCADE",
2379 })
2380 }
2381}
2382
2383#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2385#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2386#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2387pub enum UserDefinedTypeRepresentation {
2388 Composite {
2390 attributes: Vec<UserDefinedTypeCompositeAttributeDef>,
2392 },
2393 Enum {
2398 labels: Vec<Ident>,
2400 },
2401 Range {
2405 options: Vec<UserDefinedTypeRangeOption>,
2407 },
2408 SqlDefinition {
2414 options: Vec<UserDefinedTypeSqlDefinitionOption>,
2416 },
2417}
2418
2419impl fmt::Display for UserDefinedTypeRepresentation {
2420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2421 match self {
2422 Self::Composite { attributes } => {
2423 write!(f, "AS ({})", display_comma_separated(attributes))
2424 }
2425 Self::Enum { labels } => {
2426 write!(f, "AS ENUM ({})", display_comma_separated(labels))
2427 }
2428 Self::Range { options } => {
2429 write!(f, "AS RANGE ({})", display_comma_separated(options))
2430 }
2431 Self::SqlDefinition { options } => {
2432 write!(f, "({})", display_comma_separated(options))
2433 }
2434 }
2435 }
2436}
2437
2438#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2440#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2441#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2442pub struct UserDefinedTypeCompositeAttributeDef {
2443 pub name: Ident,
2445 pub data_type: DataType,
2447 pub collation: Option<ObjectName>,
2449}
2450
2451impl fmt::Display for UserDefinedTypeCompositeAttributeDef {
2452 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2453 write!(f, "{} {}", self.name, self.data_type)?;
2454 if let Some(collation) = &self.collation {
2455 write!(f, " COLLATE {collation}")?;
2456 }
2457 Ok(())
2458 }
2459}
2460
2461#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2484#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2485#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2486pub enum UserDefinedTypeInternalLength {
2487 Fixed(u64),
2489 Variable,
2491}
2492
2493impl fmt::Display for UserDefinedTypeInternalLength {
2494 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2495 match self {
2496 UserDefinedTypeInternalLength::Fixed(n) => write!(f, "{}", n),
2497 UserDefinedTypeInternalLength::Variable => write!(f, "VARIABLE"),
2498 }
2499 }
2500}
2501
2502#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2521#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2522#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2523pub enum Alignment {
2524 Char,
2526 Int2,
2528 Int4,
2530 Double,
2532}
2533
2534impl fmt::Display for Alignment {
2535 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2536 match self {
2537 Alignment::Char => write!(f, "char"),
2538 Alignment::Int2 => write!(f, "int2"),
2539 Alignment::Int4 => write!(f, "int4"),
2540 Alignment::Double => write!(f, "double"),
2541 }
2542 }
2543}
2544
2545#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2565#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2566#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2567pub enum UserDefinedTypeStorage {
2568 Plain,
2570 External,
2572 Extended,
2574 Main,
2576}
2577
2578impl fmt::Display for UserDefinedTypeStorage {
2579 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2580 match self {
2581 UserDefinedTypeStorage::Plain => write!(f, "plain"),
2582 UserDefinedTypeStorage::External => write!(f, "external"),
2583 UserDefinedTypeStorage::Extended => write!(f, "extended"),
2584 UserDefinedTypeStorage::Main => write!(f, "main"),
2585 }
2586 }
2587}
2588
2589#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2607#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2608#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2609pub enum UserDefinedTypeRangeOption {
2610 Subtype(DataType),
2612 SubtypeOpClass(ObjectName),
2614 Collation(ObjectName),
2616 Canonical(ObjectName),
2618 SubtypeDiff(ObjectName),
2620 MultirangeTypeName(ObjectName),
2622}
2623
2624impl fmt::Display for UserDefinedTypeRangeOption {
2625 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2626 match self {
2627 UserDefinedTypeRangeOption::Subtype(dt) => write!(f, "SUBTYPE = {}", dt),
2628 UserDefinedTypeRangeOption::SubtypeOpClass(name) => {
2629 write!(f, "SUBTYPE_OPCLASS = {}", name)
2630 }
2631 UserDefinedTypeRangeOption::Collation(name) => write!(f, "COLLATION = {}", name),
2632 UserDefinedTypeRangeOption::Canonical(name) => write!(f, "CANONICAL = {}", name),
2633 UserDefinedTypeRangeOption::SubtypeDiff(name) => write!(f, "SUBTYPE_DIFF = {}", name),
2634 UserDefinedTypeRangeOption::MultirangeTypeName(name) => {
2635 write!(f, "MULTIRANGE_TYPE_NAME = {}", name)
2636 }
2637 }
2638 }
2639}
2640
2641#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2662#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2663#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2664pub enum UserDefinedTypeSqlDefinitionOption {
2665 Input(ObjectName),
2667 Output(ObjectName),
2669 Receive(ObjectName),
2671 Send(ObjectName),
2673 TypmodIn(ObjectName),
2675 TypmodOut(ObjectName),
2677 Analyze(ObjectName),
2679 Subscript(ObjectName),
2681 InternalLength(UserDefinedTypeInternalLength),
2683 PassedByValue,
2685 Alignment(Alignment),
2687 Storage(UserDefinedTypeStorage),
2689 Like(ObjectName),
2691 Category(char),
2693 Preferred(bool),
2695 Default(Expr),
2697 Element(DataType),
2699 Delimiter(String),
2701 Collatable(bool),
2703}
2704
2705impl fmt::Display for UserDefinedTypeSqlDefinitionOption {
2706 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2707 match self {
2708 UserDefinedTypeSqlDefinitionOption::Input(name) => write!(f, "INPUT = {}", name),
2709 UserDefinedTypeSqlDefinitionOption::Output(name) => write!(f, "OUTPUT = {}", name),
2710 UserDefinedTypeSqlDefinitionOption::Receive(name) => write!(f, "RECEIVE = {}", name),
2711 UserDefinedTypeSqlDefinitionOption::Send(name) => write!(f, "SEND = {}", name),
2712 UserDefinedTypeSqlDefinitionOption::TypmodIn(name) => write!(f, "TYPMOD_IN = {}", name),
2713 UserDefinedTypeSqlDefinitionOption::TypmodOut(name) => {
2714 write!(f, "TYPMOD_OUT = {}", name)
2715 }
2716 UserDefinedTypeSqlDefinitionOption::Analyze(name) => write!(f, "ANALYZE = {}", name),
2717 UserDefinedTypeSqlDefinitionOption::Subscript(name) => {
2718 write!(f, "SUBSCRIPT = {}", name)
2719 }
2720 UserDefinedTypeSqlDefinitionOption::InternalLength(len) => {
2721 write!(f, "INTERNALLENGTH = {}", len)
2722 }
2723 UserDefinedTypeSqlDefinitionOption::PassedByValue => write!(f, "PASSEDBYVALUE"),
2724 UserDefinedTypeSqlDefinitionOption::Alignment(align) => {
2725 write!(f, "ALIGNMENT = {}", align)
2726 }
2727 UserDefinedTypeSqlDefinitionOption::Storage(storage) => {
2728 write!(f, "STORAGE = {}", storage)
2729 }
2730 UserDefinedTypeSqlDefinitionOption::Like(name) => write!(f, "LIKE = {}", name),
2731 UserDefinedTypeSqlDefinitionOption::Category(c) => write!(f, "CATEGORY = '{}'", c),
2732 UserDefinedTypeSqlDefinitionOption::Preferred(b) => write!(f, "PREFERRED = {}", b),
2733 UserDefinedTypeSqlDefinitionOption::Default(expr) => write!(f, "DEFAULT = {}", expr),
2734 UserDefinedTypeSqlDefinitionOption::Element(dt) => write!(f, "ELEMENT = {}", dt),
2735 UserDefinedTypeSqlDefinitionOption::Delimiter(s) => {
2736 write!(f, "DELIMITER = '{}'", escape_single_quote_string(s))
2737 }
2738 UserDefinedTypeSqlDefinitionOption::Collatable(b) => write!(f, "COLLATABLE = {}", b),
2739 }
2740 }
2741}
2742
2743#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2747#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2748#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2749pub enum Partition {
2750 Identifier(Ident),
2752 Expr(Expr),
2754 Part(Expr),
2757 Partitions(Vec<Expr>),
2759}
2760
2761impl fmt::Display for Partition {
2762 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2763 match self {
2764 Partition::Identifier(id) => write!(f, "PARTITION ID {id}"),
2765 Partition::Expr(expr) => write!(f, "PARTITION {expr}"),
2766 Partition::Part(expr) => write!(f, "PART {expr}"),
2767 Partition::Partitions(partitions) => {
2768 write!(f, "PARTITION ({})", display_comma_separated(partitions))
2769 }
2770 }
2771 }
2772}
2773
2774#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2777#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2778#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2779pub enum Deduplicate {
2780 All,
2782 ByExpression(Expr),
2784}
2785
2786impl fmt::Display for Deduplicate {
2787 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2788 match self {
2789 Deduplicate::All => write!(f, "DEDUPLICATE"),
2790 Deduplicate::ByExpression(expr) => write!(f, "DEDUPLICATE BY {expr}"),
2791 }
2792 }
2793}
2794
2795#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2800#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2801#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2802pub struct ClusteredBy {
2803 pub columns: Vec<Ident>,
2805 pub sorted_by: Option<Vec<OrderByExpr>>,
2807 pub num_buckets: Value,
2809}
2810
2811impl fmt::Display for ClusteredBy {
2812 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2813 write!(
2814 f,
2815 "CLUSTERED BY ({})",
2816 display_comma_separated(&self.columns)
2817 )?;
2818 if let Some(ref sorted_by) = self.sorted_by {
2819 write!(f, " SORTED BY ({})", display_comma_separated(sorted_by))?;
2820 }
2821 write!(f, " INTO {} BUCKETS", self.num_buckets)
2822 }
2823}
2824
2825#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2827#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2828#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2829pub struct CreateIndex {
2830 pub name: Option<ObjectName>,
2832 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
2833 pub table_name: ObjectName,
2835 pub using: Option<IndexType>,
2838 pub columns: Vec<IndexColumn>,
2840 pub unique: bool,
2842 pub concurrently: bool,
2844 pub if_not_exists: bool,
2846 pub include: Vec<Ident>,
2848 pub nulls_distinct: Option<bool>,
2850 pub with: Vec<Expr>,
2852 pub predicate: Option<Expr>,
2854 pub index_options: Vec<IndexOption>,
2856 pub alter_options: Vec<AlterTableOperation>,
2863}
2864
2865impl fmt::Display for CreateIndex {
2866 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2867 write!(
2868 f,
2869 "CREATE {unique}INDEX {concurrently}{if_not_exists}",
2870 unique = if self.unique { "UNIQUE " } else { "" },
2871 concurrently = if self.concurrently {
2872 "CONCURRENTLY "
2873 } else {
2874 ""
2875 },
2876 if_not_exists = if self.if_not_exists {
2877 "IF NOT EXISTS "
2878 } else {
2879 ""
2880 },
2881 )?;
2882 if let Some(value) = &self.name {
2883 write!(f, "{value} ")?;
2884 }
2885 write!(f, "ON {}", self.table_name)?;
2886 if let Some(value) = &self.using {
2887 write!(f, " USING {value} ")?;
2888 }
2889 write!(f, "({})", display_comma_separated(&self.columns))?;
2890 if !self.include.is_empty() {
2891 write!(f, " INCLUDE ({})", display_comma_separated(&self.include))?;
2892 }
2893 if let Some(value) = self.nulls_distinct {
2894 if value {
2895 write!(f, " NULLS DISTINCT")?;
2896 } else {
2897 write!(f, " NULLS NOT DISTINCT")?;
2898 }
2899 }
2900 if !self.with.is_empty() {
2901 write!(f, " WITH ({})", display_comma_separated(&self.with))?;
2902 }
2903 if let Some(predicate) = &self.predicate {
2904 write!(f, " WHERE {predicate}")?;
2905 }
2906 if !self.index_options.is_empty() {
2907 write!(f, " {}", display_separated(&self.index_options, " "))?;
2908 }
2909 if !self.alter_options.is_empty() {
2910 write!(f, " {}", display_separated(&self.alter_options, " "))?;
2911 }
2912 Ok(())
2913 }
2914}
2915
2916#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2918#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2919#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2920pub struct CreateTable {
2921 pub or_replace: bool,
2923 pub temporary: bool,
2925 pub external: bool,
2927 pub dynamic: bool,
2929 pub global: Option<bool>,
2931 pub if_not_exists: bool,
2933 pub transient: bool,
2935 pub volatile: bool,
2937 pub iceberg: bool,
2939 pub snapshot: bool,
2942 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
2944 pub name: ObjectName,
2945 pub columns: Vec<ColumnDef>,
2947 pub constraints: Vec<TableConstraint>,
2949 pub hive_distribution: HiveDistributionStyle,
2951 pub hive_formats: Option<HiveFormat>,
2953 pub table_options: CreateTableOptions,
2955 pub file_format: Option<FileFormat>,
2957 pub location: Option<String>,
2959 pub query: Option<Box<Query>>,
2961 pub without_rowid: bool,
2963 pub like: Option<CreateTableLikeKind>,
2965 pub clone: Option<ObjectName>,
2967 pub version: Option<TableVersion>,
2969 pub comment: Option<CommentDef>,
2973 pub on_commit: Option<OnCommit>,
2976 pub on_cluster: Option<Ident>,
2979 pub primary_key: Option<Box<Expr>>,
2982 pub order_by: Option<OneOrManyWithParens<Expr>>,
2986 pub partition_by: Option<Box<Expr>>,
2989 pub cluster_by: Option<WrappedCollection<Vec<Expr>>>,
2994 pub clustered_by: Option<ClusteredBy>,
2997 pub inherits: Option<Vec<ObjectName>>,
3002 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
3006 pub partition_of: Option<ObjectName>,
3007 pub for_values: Option<ForValues>,
3010 pub strict: bool,
3014 pub copy_grants: bool,
3017 pub enable_schema_evolution: Option<bool>,
3020 pub change_tracking: Option<bool>,
3023 pub data_retention_time_in_days: Option<u64>,
3026 pub max_data_extension_time_in_days: Option<u64>,
3029 pub default_ddl_collation: Option<String>,
3032 pub with_aggregation_policy: Option<ObjectName>,
3035 pub with_row_access_policy: Option<RowAccessPolicy>,
3038 pub with_storage_lifecycle_policy: Option<StorageLifecyclePolicy>,
3041 pub with_tags: Option<Vec<Tag>>,
3044 pub external_volume: Option<String>,
3047 pub base_location: Option<String>,
3050 pub catalog: Option<String>,
3053 pub catalog_sync: Option<String>,
3056 pub storage_serialization_policy: Option<StorageSerializationPolicy>,
3059 pub target_lag: Option<String>,
3062 pub warehouse: Option<Ident>,
3065 pub refresh_mode: Option<RefreshModeKind>,
3068 pub initialize: Option<InitializeKind>,
3071 pub require_user: bool,
3074 pub diststyle: Option<DistStyle>,
3077 pub distkey: Option<Expr>,
3080 pub sortkey: Option<Vec<Expr>>,
3083 pub backup: Option<bool>,
3086}
3087
3088impl fmt::Display for CreateTable {
3089 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3090 write!(
3098 f,
3099 "CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{dynamic}{iceberg}{snapshot}TABLE {if_not_exists}{name}",
3100 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
3101 external = if self.external { "EXTERNAL " } else { "" },
3102 snapshot = if self.snapshot { "SNAPSHOT " } else { "" },
3103 global = self.global
3104 .map(|global| {
3105 if global {
3106 "GLOBAL "
3107 } else {
3108 "LOCAL "
3109 }
3110 })
3111 .unwrap_or(""),
3112 if_not_exists = if self.if_not_exists { "IF NOT EXISTS " } else { "" },
3113 temporary = if self.temporary { "TEMPORARY " } else { "" },
3114 transient = if self.transient { "TRANSIENT " } else { "" },
3115 volatile = if self.volatile { "VOLATILE " } else { "" },
3116 iceberg = if self.iceberg { "ICEBERG " } else { "" },
3118 dynamic = if self.dynamic { "DYNAMIC " } else { "" },
3119 name = self.name,
3120 )?;
3121 if let Some(partition_of) = &self.partition_of {
3122 write!(f, " PARTITION OF {partition_of}")?;
3123 }
3124 if let Some(on_cluster) = &self.on_cluster {
3125 write!(f, " ON CLUSTER {on_cluster}")?;
3126 }
3127 if !self.columns.is_empty() || !self.constraints.is_empty() {
3128 f.write_str(" (")?;
3129 NewLine.fmt(f)?;
3130 Indent(DisplayCommaSeparated(&self.columns)).fmt(f)?;
3131 if !self.columns.is_empty() && !self.constraints.is_empty() {
3132 f.write_str(",")?;
3133 SpaceOrNewline.fmt(f)?;
3134 }
3135 Indent(DisplayCommaSeparated(&self.constraints)).fmt(f)?;
3136 NewLine.fmt(f)?;
3137 f.write_str(")")?;
3138 } else if self.query.is_none()
3139 && self.like.is_none()
3140 && self.clone.is_none()
3141 && self.partition_of.is_none()
3142 {
3143 f.write_str(" ()")?;
3145 } else if let Some(CreateTableLikeKind::Parenthesized(like_in_columns_list)) = &self.like {
3146 write!(f, " ({like_in_columns_list})")?;
3147 }
3148 if let Some(for_values) = &self.for_values {
3149 write!(f, " {for_values}")?;
3150 }
3151
3152 if let Some(comment) = &self.comment {
3155 write!(f, " COMMENT '{comment}'")?;
3156 }
3157
3158 if self.without_rowid {
3160 write!(f, " WITHOUT ROWID")?;
3161 }
3162
3163 if let Some(CreateTableLikeKind::Plain(like)) = &self.like {
3164 write!(f, " {like}")?;
3165 }
3166
3167 if let Some(c) = &self.clone {
3168 write!(f, " CLONE {c}")?;
3169 }
3170
3171 if let Some(version) = &self.version {
3172 write!(f, " {version}")?;
3173 }
3174
3175 match &self.hive_distribution {
3176 HiveDistributionStyle::PARTITIONED { columns } => {
3177 write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;
3178 }
3179 HiveDistributionStyle::SKEWED {
3180 columns,
3181 on,
3182 stored_as_directories,
3183 } => {
3184 write!(
3185 f,
3186 " SKEWED BY ({})) ON ({})",
3187 display_comma_separated(columns),
3188 display_comma_separated(on)
3189 )?;
3190 if *stored_as_directories {
3191 write!(f, " STORED AS DIRECTORIES")?;
3192 }
3193 }
3194 _ => (),
3195 }
3196
3197 if let Some(clustered_by) = &self.clustered_by {
3198 write!(f, " {clustered_by}")?;
3199 }
3200
3201 if let Some(HiveFormat {
3202 row_format,
3203 serde_properties,
3204 storage,
3205 location,
3206 }) = &self.hive_formats
3207 {
3208 match row_format {
3209 Some(HiveRowFormat::SERDE { class }) => write!(f, " ROW FORMAT SERDE '{class}'")?,
3210 Some(HiveRowFormat::DELIMITED { delimiters }) => {
3211 write!(f, " ROW FORMAT DELIMITED")?;
3212 if !delimiters.is_empty() {
3213 write!(f, " {}", display_separated(delimiters, " "))?;
3214 }
3215 }
3216 None => (),
3217 }
3218 match storage {
3219 Some(HiveIOFormat::IOF {
3220 input_format,
3221 output_format,
3222 }) => write!(
3223 f,
3224 " STORED AS INPUTFORMAT {input_format} OUTPUTFORMAT {output_format}"
3225 )?,
3226 Some(HiveIOFormat::FileFormat { format }) if !self.external => {
3227 write!(f, " STORED AS {format}")?
3228 }
3229 _ => (),
3230 }
3231 if let Some(serde_properties) = serde_properties.as_ref() {
3232 write!(
3233 f,
3234 " WITH SERDEPROPERTIES ({})",
3235 display_comma_separated(serde_properties)
3236 )?;
3237 }
3238 if !self.external {
3239 if let Some(loc) = location {
3240 write!(f, " LOCATION '{loc}'")?;
3241 }
3242 }
3243 }
3244 if self.external {
3245 if let Some(file_format) = self.file_format {
3246 write!(f, " STORED AS {file_format}")?;
3247 }
3248 if let Some(location) = &self.location {
3249 write!(f, " LOCATION '{location}'")?;
3250 }
3251 }
3252
3253 match &self.table_options {
3254 options @ CreateTableOptions::With(_)
3255 | options @ CreateTableOptions::Plain(_)
3256 | options @ CreateTableOptions::TableProperties(_) => write!(f, " {options}")?,
3257 _ => (),
3258 }
3259
3260 if let Some(primary_key) = &self.primary_key {
3261 write!(f, " PRIMARY KEY {primary_key}")?;
3262 }
3263 if let Some(order_by) = &self.order_by {
3264 write!(f, " ORDER BY {order_by}")?;
3265 }
3266 if let Some(inherits) = &self.inherits {
3267 write!(f, " INHERITS ({})", display_comma_separated(inherits))?;
3268 }
3269 if let Some(partition_by) = self.partition_by.as_ref() {
3270 write!(f, " PARTITION BY {partition_by}")?;
3271 }
3272 if let Some(cluster_by) = self.cluster_by.as_ref() {
3273 write!(f, " CLUSTER BY {cluster_by}")?;
3274 }
3275 if let options @ CreateTableOptions::Options(_) = &self.table_options {
3276 write!(f, " {options}")?;
3277 }
3278 if let Some(external_volume) = self.external_volume.as_ref() {
3279 write!(f, " EXTERNAL_VOLUME='{external_volume}'")?;
3280 }
3281
3282 if let Some(catalog) = self.catalog.as_ref() {
3283 write!(f, " CATALOG='{catalog}'")?;
3284 }
3285
3286 if self.iceberg {
3287 if let Some(base_location) = self.base_location.as_ref() {
3288 write!(f, " BASE_LOCATION='{base_location}'")?;
3289 }
3290 }
3291
3292 if let Some(catalog_sync) = self.catalog_sync.as_ref() {
3293 write!(f, " CATALOG_SYNC='{catalog_sync}'")?;
3294 }
3295
3296 if let Some(storage_serialization_policy) = self.storage_serialization_policy.as_ref() {
3297 write!(
3298 f,
3299 " STORAGE_SERIALIZATION_POLICY={storage_serialization_policy}"
3300 )?;
3301 }
3302
3303 if self.copy_grants {
3304 write!(f, " COPY GRANTS")?;
3305 }
3306
3307 if let Some(is_enabled) = self.enable_schema_evolution {
3308 write!(
3309 f,
3310 " ENABLE_SCHEMA_EVOLUTION={}",
3311 if is_enabled { "TRUE" } else { "FALSE" }
3312 )?;
3313 }
3314
3315 if let Some(is_enabled) = self.change_tracking {
3316 write!(
3317 f,
3318 " CHANGE_TRACKING={}",
3319 if is_enabled { "TRUE" } else { "FALSE" }
3320 )?;
3321 }
3322
3323 if let Some(data_retention_time_in_days) = self.data_retention_time_in_days {
3324 write!(
3325 f,
3326 " DATA_RETENTION_TIME_IN_DAYS={data_retention_time_in_days}",
3327 )?;
3328 }
3329
3330 if let Some(max_data_extension_time_in_days) = self.max_data_extension_time_in_days {
3331 write!(
3332 f,
3333 " MAX_DATA_EXTENSION_TIME_IN_DAYS={max_data_extension_time_in_days}",
3334 )?;
3335 }
3336
3337 if let Some(default_ddl_collation) = &self.default_ddl_collation {
3338 write!(f, " DEFAULT_DDL_COLLATION='{default_ddl_collation}'",)?;
3339 }
3340
3341 if let Some(with_aggregation_policy) = &self.with_aggregation_policy {
3342 write!(f, " WITH AGGREGATION POLICY {with_aggregation_policy}",)?;
3343 }
3344
3345 if let Some(row_access_policy) = &self.with_row_access_policy {
3346 write!(f, " {row_access_policy}",)?;
3347 }
3348
3349 if let Some(storage_lifecycle_policy) = &self.with_storage_lifecycle_policy {
3350 write!(f, " {storage_lifecycle_policy}",)?;
3351 }
3352
3353 if let Some(tag) = &self.with_tags {
3354 write!(f, " WITH TAG ({})", display_comma_separated(tag.as_slice()))?;
3355 }
3356
3357 if let Some(target_lag) = &self.target_lag {
3358 write!(f, " TARGET_LAG='{target_lag}'")?;
3359 }
3360
3361 if let Some(warehouse) = &self.warehouse {
3362 write!(f, " WAREHOUSE={warehouse}")?;
3363 }
3364
3365 if let Some(refresh_mode) = &self.refresh_mode {
3366 write!(f, " REFRESH_MODE={refresh_mode}")?;
3367 }
3368
3369 if let Some(initialize) = &self.initialize {
3370 write!(f, " INITIALIZE={initialize}")?;
3371 }
3372
3373 if self.require_user {
3374 write!(f, " REQUIRE USER")?;
3375 }
3376
3377 if self.on_commit.is_some() {
3378 let on_commit = match self.on_commit {
3379 Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",
3380 Some(OnCommit::PreserveRows) => "ON COMMIT PRESERVE ROWS",
3381 Some(OnCommit::Drop) => "ON COMMIT DROP",
3382 None => "",
3383 };
3384 write!(f, " {on_commit}")?;
3385 }
3386 if self.strict {
3387 write!(f, " STRICT")?;
3388 }
3389 if let Some(backup) = self.backup {
3390 write!(f, " BACKUP {}", if backup { "YES" } else { "NO" })?;
3391 }
3392 if let Some(diststyle) = &self.diststyle {
3393 write!(f, " DISTSTYLE {diststyle}")?;
3394 }
3395 if let Some(distkey) = &self.distkey {
3396 write!(f, " DISTKEY({distkey})")?;
3397 }
3398 if let Some(sortkey) = &self.sortkey {
3399 write!(f, " SORTKEY({})", display_comma_separated(sortkey))?;
3400 }
3401 if let Some(query) = &self.query {
3402 write!(f, " AS {query}")?;
3403 }
3404 Ok(())
3405 }
3406}
3407
3408#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3414#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3415#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3416pub enum ForValues {
3417 In(Vec<Expr>),
3419 From {
3421 from: Vec<PartitionBoundValue>,
3423 to: Vec<PartitionBoundValue>,
3425 },
3426 With {
3428 modulus: u64,
3430 remainder: u64,
3432 },
3433 Default,
3435}
3436
3437impl fmt::Display for ForValues {
3438 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3439 match self {
3440 ForValues::In(values) => {
3441 write!(f, "FOR VALUES IN ({})", display_comma_separated(values))
3442 }
3443 ForValues::From { from, to } => {
3444 write!(
3445 f,
3446 "FOR VALUES FROM ({}) TO ({})",
3447 display_comma_separated(from),
3448 display_comma_separated(to)
3449 )
3450 }
3451 ForValues::With { modulus, remainder } => {
3452 write!(
3453 f,
3454 "FOR VALUES WITH (MODULUS {modulus}, REMAINDER {remainder})"
3455 )
3456 }
3457 ForValues::Default => write!(f, "DEFAULT"),
3458 }
3459 }
3460}
3461
3462#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3467#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3468#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3469pub enum PartitionBoundValue {
3470 Expr(Expr),
3472 MinValue,
3474 MaxValue,
3476}
3477
3478impl fmt::Display for PartitionBoundValue {
3479 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3480 match self {
3481 PartitionBoundValue::Expr(expr) => write!(f, "{expr}"),
3482 PartitionBoundValue::MinValue => write!(f, "MINVALUE"),
3483 PartitionBoundValue::MaxValue => write!(f, "MAXVALUE"),
3484 }
3485 }
3486}
3487
3488#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3492#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3493#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3494pub enum DistStyle {
3495 Auto,
3497 Even,
3499 Key,
3501 All,
3503}
3504
3505impl fmt::Display for DistStyle {
3506 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3507 match self {
3508 DistStyle::Auto => write!(f, "AUTO"),
3509 DistStyle::Even => write!(f, "EVEN"),
3510 DistStyle::Key => write!(f, "KEY"),
3511 DistStyle::All => write!(f, "ALL"),
3512 }
3513 }
3514}
3515
3516
3517#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3518#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3519#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3520pub struct CreateDomain {
3533 pub name: ObjectName,
3535 pub data_type: DataType,
3537 pub collation: Option<Ident>,
3539 pub default: Option<Expr>,
3541 pub constraints: Vec<TableConstraint>,
3543}
3544
3545impl fmt::Display for CreateDomain {
3546 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3547 write!(
3548 f,
3549 "CREATE DOMAIN {name} AS {data_type}",
3550 name = self.name,
3551 data_type = self.data_type
3552 )?;
3553 if let Some(collation) = &self.collation {
3554 write!(f, " COLLATE {collation}")?;
3555 }
3556 if let Some(default) = &self.default {
3557 write!(f, " DEFAULT {default}")?;
3558 }
3559 if !self.constraints.is_empty() {
3560 write!(f, " {}", display_separated(&self.constraints, " "))?;
3561 }
3562 Ok(())
3563 }
3564}
3565
3566#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3568#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3569#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3570pub enum FunctionReturnType {
3571 DataType(DataType),
3573 SetOf(DataType),
3577}
3578
3579impl fmt::Display for FunctionReturnType {
3580 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3581 match self {
3582 FunctionReturnType::DataType(data_type) => write!(f, "{data_type}"),
3583 FunctionReturnType::SetOf(data_type) => write!(f, "SETOF {data_type}"),
3584 }
3585 }
3586}
3587
3588#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3589#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3590#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3591pub struct CreateFunction {
3593 pub or_alter: bool,
3597 pub or_replace: bool,
3599 pub temporary: bool,
3601 pub if_not_exists: bool,
3603 pub name: ObjectName,
3605 pub args: Option<Vec<OperateFunctionArg>>,
3607 pub return_type: Option<FunctionReturnType>,
3609 pub function_body: Option<CreateFunctionBody>,
3617 pub behavior: Option<FunctionBehavior>,
3623 pub called_on_null: Option<FunctionCalledOnNull>,
3627 pub parallel: Option<FunctionParallel>,
3631 pub security: Option<FunctionSecurity>,
3635 pub set_params: Vec<FunctionDefinitionSetParam>,
3639 pub using: Option<CreateFunctionUsing>,
3641 pub language: Option<Ident>,
3649 pub determinism_specifier: Option<FunctionDeterminismSpecifier>,
3653 pub options: Option<Vec<SqlOption>>,
3657 pub remote_connection: Option<ObjectName>,
3667}
3668
3669impl fmt::Display for CreateFunction {
3670 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3671 write!(
3672 f,
3673 "CREATE {or_alter}{or_replace}{temp}FUNCTION {if_not_exists}{name}",
3674 name = self.name,
3675 temp = if self.temporary { "TEMPORARY " } else { "" },
3676 or_alter = if self.or_alter { "OR ALTER " } else { "" },
3677 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
3678 if_not_exists = if self.if_not_exists {
3679 "IF NOT EXISTS "
3680 } else {
3681 ""
3682 },
3683 )?;
3684 if let Some(args) = &self.args {
3685 write!(f, "({})", display_comma_separated(args))?;
3686 }
3687 if let Some(return_type) = &self.return_type {
3688 write!(f, " RETURNS {return_type}")?;
3689 }
3690 if let Some(determinism_specifier) = &self.determinism_specifier {
3691 write!(f, " {determinism_specifier}")?;
3692 }
3693 if let Some(language) = &self.language {
3694 write!(f, " LANGUAGE {language}")?;
3695 }
3696 if let Some(behavior) = &self.behavior {
3697 write!(f, " {behavior}")?;
3698 }
3699 if let Some(called_on_null) = &self.called_on_null {
3700 write!(f, " {called_on_null}")?;
3701 }
3702 if let Some(parallel) = &self.parallel {
3703 write!(f, " {parallel}")?;
3704 }
3705 if let Some(security) = &self.security {
3706 write!(f, " {security}")?;
3707 }
3708 for set_param in &self.set_params {
3709 write!(f, " {set_param}")?;
3710 }
3711 if let Some(remote_connection) = &self.remote_connection {
3712 write!(f, " REMOTE WITH CONNECTION {remote_connection}")?;
3713 }
3714 if let Some(CreateFunctionBody::AsBeforeOptions { body, link_symbol }) = &self.function_body
3715 {
3716 write!(f, " AS {body}")?;
3717 if let Some(link_symbol) = link_symbol {
3718 write!(f, ", {link_symbol}")?;
3719 }
3720 }
3721 if let Some(CreateFunctionBody::Return(function_body)) = &self.function_body {
3722 write!(f, " RETURN {function_body}")?;
3723 }
3724 if let Some(CreateFunctionBody::AsReturnExpr(function_body)) = &self.function_body {
3725 write!(f, " AS RETURN {function_body}")?;
3726 }
3727 if let Some(CreateFunctionBody::AsReturnSelect(function_body)) = &self.function_body {
3728 write!(f, " AS RETURN {function_body}")?;
3729 }
3730 if let Some(using) = &self.using {
3731 write!(f, " {using}")?;
3732 }
3733 if let Some(options) = &self.options {
3734 write!(
3735 f,
3736 " OPTIONS({})",
3737 display_comma_separated(options.as_slice())
3738 )?;
3739 }
3740 if let Some(CreateFunctionBody::AsAfterOptions(function_body)) = &self.function_body {
3741 write!(f, " AS {function_body}")?;
3742 }
3743 if let Some(CreateFunctionBody::AsBeginEnd(bes)) = &self.function_body {
3744 write!(f, " AS {bes}")?;
3745 }
3746 Ok(())
3747 }
3748}
3749
3750#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3760#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3761#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3762pub struct CreateConnector {
3763 pub name: Ident,
3765 pub if_not_exists: bool,
3767 pub connector_type: Option<String>,
3769 pub url: Option<String>,
3771 pub comment: Option<CommentDef>,
3773 pub with_dcproperties: Option<Vec<SqlOption>>,
3775}
3776
3777impl fmt::Display for CreateConnector {
3778 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3779 write!(
3780 f,
3781 "CREATE CONNECTOR {if_not_exists}{name}",
3782 if_not_exists = if self.if_not_exists {
3783 "IF NOT EXISTS "
3784 } else {
3785 ""
3786 },
3787 name = self.name,
3788 )?;
3789
3790 if let Some(connector_type) = &self.connector_type {
3791 write!(f, " TYPE '{connector_type}'")?;
3792 }
3793
3794 if let Some(url) = &self.url {
3795 write!(f, " URL '{url}'")?;
3796 }
3797
3798 if let Some(comment) = &self.comment {
3799 write!(f, " COMMENT = '{comment}'")?;
3800 }
3801
3802 if let Some(with_dcproperties) = &self.with_dcproperties {
3803 write!(
3804 f,
3805 " WITH DCPROPERTIES({})",
3806 display_comma_separated(with_dcproperties)
3807 )?;
3808 }
3809
3810 Ok(())
3811 }
3812}
3813
3814#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3819#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3820#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3821pub enum AlterSchemaOperation {
3822 SetDefaultCollate {
3824 collate: Expr,
3826 },
3827 AddReplica {
3829 replica: Ident,
3831 options: Option<Vec<SqlOption>>,
3833 },
3834 DropReplica {
3836 replica: Ident,
3838 },
3839 SetOptionsParens {
3841 options: Vec<SqlOption>,
3843 },
3844 Rename {
3846 name: ObjectName,
3848 },
3849 OwnerTo {
3851 owner: Owner,
3853 },
3854}
3855
3856impl fmt::Display for AlterSchemaOperation {
3857 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3858 match self {
3859 AlterSchemaOperation::SetDefaultCollate { collate } => {
3860 write!(f, "SET DEFAULT COLLATE {collate}")
3861 }
3862 AlterSchemaOperation::AddReplica { replica, options } => {
3863 write!(f, "ADD REPLICA {replica}")?;
3864 if let Some(options) = options {
3865 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
3866 }
3867 Ok(())
3868 }
3869 AlterSchemaOperation::DropReplica { replica } => write!(f, "DROP REPLICA {replica}"),
3870 AlterSchemaOperation::SetOptionsParens { options } => {
3871 write!(f, "SET OPTIONS ({})", display_comma_separated(options))
3872 }
3873 AlterSchemaOperation::Rename { name } => write!(f, "RENAME TO {name}"),
3874 AlterSchemaOperation::OwnerTo { owner } => write!(f, "OWNER TO {owner}"),
3875 }
3876 }
3877}
3878#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3884#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3885#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3886pub enum RenameTableNameKind {
3887 As(ObjectName),
3889 To(ObjectName),
3891}
3892
3893impl fmt::Display for RenameTableNameKind {
3894 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3895 match self {
3896 RenameTableNameKind::As(name) => write!(f, "AS {name}"),
3897 RenameTableNameKind::To(name) => write!(f, "TO {name}"),
3898 }
3899 }
3900}
3901
3902#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3903#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3904#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3905pub struct AlterSchema {
3907 pub name: ObjectName,
3909 pub if_exists: bool,
3911 pub operations: Vec<AlterSchemaOperation>,
3913}
3914
3915impl fmt::Display for AlterSchema {
3916 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3917 write!(f, "ALTER SCHEMA ")?;
3918 if self.if_exists {
3919 write!(f, "IF EXISTS ")?;
3920 }
3921 write!(f, "{}", self.name)?;
3922 for operation in &self.operations {
3923 write!(f, " {operation}")?;
3924 }
3925
3926 Ok(())
3927 }
3928}
3929
3930impl Spanned for RenameTableNameKind {
3931 fn span(&self) -> Span {
3932 match self {
3933 RenameTableNameKind::As(name) => name.span(),
3934 RenameTableNameKind::To(name) => name.span(),
3935 }
3936 }
3937}
3938
3939#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
3940#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3941#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3942pub enum TriggerObjectKind {
3944 For(TriggerObject),
3946 ForEach(TriggerObject),
3948}
3949
3950impl Display for TriggerObjectKind {
3951 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3952 match self {
3953 TriggerObjectKind::For(obj) => write!(f, "FOR {obj}"),
3954 TriggerObjectKind::ForEach(obj) => write!(f, "FOR EACH {obj}"),
3955 }
3956 }
3957}
3958
3959#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3960#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3961#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3962pub struct CreateTrigger {
3976 pub or_alter: bool,
3980 pub temporary: bool,
3997 pub or_replace: bool,
4007 pub is_constraint: bool,
4009 pub name: ObjectName,
4011 pub period: Option<TriggerPeriod>,
4040 pub period_before_table: bool,
4051 pub events: Vec<TriggerEvent>,
4053 pub table_name: ObjectName,
4055 pub referenced_table_name: Option<ObjectName>,
4058 pub referencing: Vec<TriggerReferencing>,
4060 pub trigger_object: Option<TriggerObjectKind>,
4065 pub condition: Option<Expr>,
4067 pub exec_body: Option<TriggerExecBody>,
4069 pub statements_as: bool,
4071 pub statements: Option<ConditionalStatements>,
4073 pub characteristics: Option<ConstraintCharacteristics>,
4075}
4076
4077impl Display for CreateTrigger {
4078 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4079 let CreateTrigger {
4080 or_alter,
4081 temporary,
4082 or_replace,
4083 is_constraint,
4084 name,
4085 period_before_table,
4086 period,
4087 events,
4088 table_name,
4089 referenced_table_name,
4090 referencing,
4091 trigger_object,
4092 condition,
4093 exec_body,
4094 statements_as,
4095 statements,
4096 characteristics,
4097 } = self;
4098 write!(
4099 f,
4100 "CREATE {temporary}{or_alter}{or_replace}{is_constraint}TRIGGER {name} ",
4101 temporary = if *temporary { "TEMPORARY " } else { "" },
4102 or_alter = if *or_alter { "OR ALTER " } else { "" },
4103 or_replace = if *or_replace { "OR REPLACE " } else { "" },
4104 is_constraint = if *is_constraint { "CONSTRAINT " } else { "" },
4105 )?;
4106
4107 if *period_before_table {
4108 if let Some(p) = period {
4109 write!(f, "{p} ")?;
4110 }
4111 if !events.is_empty() {
4112 write!(f, "{} ", display_separated(events, " OR "))?;
4113 }
4114 write!(f, "ON {table_name}")?;
4115 } else {
4116 write!(f, "ON {table_name} ")?;
4117 if let Some(p) = period {
4118 write!(f, "{p}")?;
4119 }
4120 if !events.is_empty() {
4121 write!(f, " {}", display_separated(events, ", "))?;
4122 }
4123 }
4124
4125 if let Some(referenced_table_name) = referenced_table_name {
4126 write!(f, " FROM {referenced_table_name}")?;
4127 }
4128
4129 if let Some(characteristics) = characteristics {
4130 write!(f, " {characteristics}")?;
4131 }
4132
4133 if !referencing.is_empty() {
4134 write!(f, " REFERENCING {}", display_separated(referencing, " "))?;
4135 }
4136
4137 if let Some(trigger_object) = trigger_object {
4138 write!(f, " {trigger_object}")?;
4139 }
4140 if let Some(condition) = condition {
4141 write!(f, " WHEN {condition}")?;
4142 }
4143 if let Some(exec_body) = exec_body {
4144 write!(f, " EXECUTE {exec_body}")?;
4145 }
4146 if let Some(statements) = statements {
4147 if *statements_as {
4148 write!(f, " AS")?;
4149 }
4150 write!(f, " {statements}")?;
4151 }
4152 Ok(())
4153 }
4154}
4155
4156#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4157#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4158#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4159pub struct DropTrigger {
4166 pub if_exists: bool,
4168 pub trigger_name: ObjectName,
4170 pub table_name: Option<ObjectName>,
4172 pub option: Option<ReferentialAction>,
4174}
4175
4176impl fmt::Display for DropTrigger {
4177 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4178 let DropTrigger {
4179 if_exists,
4180 trigger_name,
4181 table_name,
4182 option,
4183 } = self;
4184 write!(f, "DROP TRIGGER")?;
4185 if *if_exists {
4186 write!(f, " IF EXISTS")?;
4187 }
4188 match &table_name {
4189 Some(table_name) => write!(f, " {trigger_name} ON {table_name}")?,
4190 None => write!(f, " {trigger_name}")?,
4191 };
4192 if let Some(option) = option {
4193 write!(f, " {option}")?;
4194 }
4195 Ok(())
4196 }
4197}
4198
4199#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4205#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4206#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4207pub struct Truncate {
4208 pub table_names: Vec<super::TruncateTableTarget>,
4210 pub partitions: Option<Vec<Expr>>,
4212 pub table: bool,
4214 pub if_exists: bool,
4216 pub identity: Option<super::TruncateIdentityOption>,
4218 pub cascade: Option<super::CascadeOption>,
4220 pub on_cluster: Option<Ident>,
4223}
4224
4225impl fmt::Display for Truncate {
4226 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4227 let table = if self.table { "TABLE " } else { "" };
4228 let if_exists = if self.if_exists { "IF EXISTS " } else { "" };
4229
4230 write!(
4231 f,
4232 "TRUNCATE {table}{if_exists}{table_names}",
4233 table_names = display_comma_separated(&self.table_names)
4234 )?;
4235
4236 if let Some(identity) = &self.identity {
4237 match identity {
4238 super::TruncateIdentityOption::Restart => write!(f, " RESTART IDENTITY")?,
4239 super::TruncateIdentityOption::Continue => write!(f, " CONTINUE IDENTITY")?,
4240 }
4241 }
4242 if let Some(cascade) = &self.cascade {
4243 match cascade {
4244 super::CascadeOption::Cascade => write!(f, " CASCADE")?,
4245 super::CascadeOption::Restrict => write!(f, " RESTRICT")?,
4246 }
4247 }
4248
4249 if let Some(ref parts) = &self.partitions {
4250 if !parts.is_empty() {
4251 write!(f, " PARTITION ({})", display_comma_separated(parts))?;
4252 }
4253 }
4254 if let Some(on_cluster) = &self.on_cluster {
4255 write!(f, " ON CLUSTER {on_cluster}")?;
4256 }
4257 Ok(())
4258 }
4259}
4260
4261impl Spanned for Truncate {
4262 fn span(&self) -> Span {
4263 Span::union_iter(
4264 self.table_names.iter().map(|i| i.name.span()).chain(
4265 self.partitions
4266 .iter()
4267 .flat_map(|i| i.iter().map(|k| k.span())),
4268 ),
4269 )
4270 }
4271}
4272
4273#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4280#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4281#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4282pub struct Msck {
4283 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
4285 pub table_name: ObjectName,
4286 pub repair: bool,
4288 pub partition_action: Option<super::AddDropSync>,
4290}
4291
4292impl fmt::Display for Msck {
4293 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4294 write!(
4295 f,
4296 "MSCK {repair}TABLE {table}",
4297 repair = if self.repair { "REPAIR " } else { "" },
4298 table = self.table_name
4299 )?;
4300 if let Some(pa) = &self.partition_action {
4301 write!(f, " {pa}")?;
4302 }
4303 Ok(())
4304 }
4305}
4306
4307impl Spanned for Msck {
4308 fn span(&self) -> Span {
4309 self.table_name.span()
4310 }
4311}
4312
4313#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4315#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4316#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4317pub struct CreateView {
4318 pub or_alter: bool,
4322 pub or_replace: bool,
4324 pub materialized: bool,
4326 pub secure: bool,
4329 pub name: ObjectName,
4331 pub name_before_not_exists: bool,
4342 pub columns: Vec<ViewColumnDef>,
4344 pub query: Box<Query>,
4346 pub options: CreateTableOptions,
4348 pub cluster_by: Vec<Ident>,
4350 pub comment: Option<String>,
4353 pub with_no_schema_binding: bool,
4355 pub if_not_exists: bool,
4357 pub temporary: bool,
4359 pub copy_grants: bool,
4362 pub to: Option<ObjectName>,
4365 pub params: Option<CreateViewParams>,
4367}
4368
4369impl fmt::Display for CreateView {
4370 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4371 write!(
4372 f,
4373 "CREATE {or_alter}{or_replace}",
4374 or_alter = if self.or_alter { "OR ALTER " } else { "" },
4375 or_replace = if self.or_replace { "OR REPLACE " } else { "" },
4376 )?;
4377 if let Some(ref params) = self.params {
4378 params.fmt(f)?;
4379 }
4380 write!(
4381 f,
4382 "{secure}{materialized}{temporary}VIEW {if_not_and_name}{to}",
4383 if_not_and_name = if self.if_not_exists {
4384 if self.name_before_not_exists {
4385 format!("{} IF NOT EXISTS", self.name)
4386 } else {
4387 format!("IF NOT EXISTS {}", self.name)
4388 }
4389 } else {
4390 format!("{}", self.name)
4391 },
4392 secure = if self.secure { "SECURE " } else { "" },
4393 materialized = if self.materialized {
4394 "MATERIALIZED "
4395 } else {
4396 ""
4397 },
4398 temporary = if self.temporary { "TEMPORARY " } else { "" },
4399 to = self
4400 .to
4401 .as_ref()
4402 .map(|to| format!(" TO {to}"))
4403 .unwrap_or_default()
4404 )?;
4405 if self.copy_grants {
4406 write!(f, " COPY GRANTS")?;
4407 }
4408 if !self.columns.is_empty() {
4409 write!(f, " ({})", display_comma_separated(&self.columns))?;
4410 }
4411 if matches!(self.options, CreateTableOptions::With(_)) {
4412 write!(f, " {}", self.options)?;
4413 }
4414 if let Some(ref comment) = self.comment {
4415 write!(f, " COMMENT = '{}'", escape_single_quote_string(comment))?;
4416 }
4417 if !self.cluster_by.is_empty() {
4418 write!(
4419 f,
4420 " CLUSTER BY ({})",
4421 display_comma_separated(&self.cluster_by)
4422 )?;
4423 }
4424 if matches!(self.options, CreateTableOptions::Options(_)) {
4425 write!(f, " {}", self.options)?;
4426 }
4427 f.write_str(" AS")?;
4428 SpaceOrNewline.fmt(f)?;
4429 self.query.fmt(f)?;
4430 if self.with_no_schema_binding {
4431 write!(f, " WITH NO SCHEMA BINDING")?;
4432 }
4433 Ok(())
4434 }
4435}
4436
4437#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4440#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4441#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4442pub struct CreateExtension {
4443 pub name: Ident,
4445 pub if_not_exists: bool,
4447 pub cascade: bool,
4449 pub schema: Option<Ident>,
4451 pub version: Option<Ident>,
4453}
4454
4455impl fmt::Display for CreateExtension {
4456 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4457 write!(
4458 f,
4459 "CREATE EXTENSION {if_not_exists}{name}",
4460 if_not_exists = if self.if_not_exists {
4461 "IF NOT EXISTS "
4462 } else {
4463 ""
4464 },
4465 name = self.name
4466 )?;
4467 if self.cascade || self.schema.is_some() || self.version.is_some() {
4468 write!(f, " WITH")?;
4469
4470 if let Some(name) = &self.schema {
4471 write!(f, " SCHEMA {name}")?;
4472 }
4473 if let Some(version) = &self.version {
4474 write!(f, " VERSION {version}")?;
4475 }
4476 if self.cascade {
4477 write!(f, " CASCADE")?;
4478 }
4479 }
4480
4481 Ok(())
4482 }
4483}
4484
4485impl Spanned for CreateExtension {
4486 fn span(&self) -> Span {
4487 Span::empty()
4488 }
4489}
4490
4491#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4499#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4500#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4501pub struct DropExtension {
4502 pub names: Vec<Ident>,
4504 pub if_exists: bool,
4506 pub cascade_or_restrict: Option<ReferentialAction>,
4508}
4509
4510impl fmt::Display for DropExtension {
4511 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4512 write!(f, "DROP EXTENSION")?;
4513 if self.if_exists {
4514 write!(f, " IF EXISTS")?;
4515 }
4516 write!(f, " {}", display_comma_separated(&self.names))?;
4517 if let Some(cascade_or_restrict) = &self.cascade_or_restrict {
4518 write!(f, " {cascade_or_restrict}")?;
4519 }
4520 Ok(())
4521 }
4522}
4523
4524impl Spanned for DropExtension {
4525 fn span(&self) -> Span {
4526 Span::empty()
4527 }
4528}
4529
4530#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4533#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4534#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4535pub struct CreateCollation {
4536 pub if_not_exists: bool,
4538 pub name: ObjectName,
4540 pub definition: CreateCollationDefinition,
4542}
4543
4544#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4546#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4547#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4548pub enum CreateCollationDefinition {
4549 From(ObjectName),
4555 Options(Vec<SqlOption>),
4561}
4562
4563impl fmt::Display for CreateCollation {
4564 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4565 write!(
4566 f,
4567 "CREATE COLLATION {if_not_exists}{name}",
4568 if_not_exists = if self.if_not_exists {
4569 "IF NOT EXISTS "
4570 } else {
4571 ""
4572 },
4573 name = self.name
4574 )?;
4575 match &self.definition {
4576 CreateCollationDefinition::From(existing_collation) => {
4577 write!(f, " FROM {existing_collation}")
4578 }
4579 CreateCollationDefinition::Options(options) => {
4580 write!(f, " ({})", display_comma_separated(options))
4581 }
4582 }
4583 }
4584}
4585
4586impl Spanned for CreateCollation {
4587 fn span(&self) -> Span {
4588 Span::empty()
4589 }
4590}
4591
4592#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4595#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4596#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4597pub struct AlterCollation {
4598 pub name: ObjectName,
4600 pub operation: AlterCollationOperation,
4602}
4603
4604#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4606#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4607#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4608pub enum AlterCollationOperation {
4609 RenameTo {
4615 new_name: Ident,
4617 },
4618 OwnerTo(Owner),
4624 SetSchema {
4630 schema_name: ObjectName,
4632 },
4633 RefreshVersion,
4639}
4640
4641impl fmt::Display for AlterCollationOperation {
4642 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4643 match self {
4644 AlterCollationOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
4645 AlterCollationOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
4646 AlterCollationOperation::SetSchema { schema_name } => {
4647 write!(f, "SET SCHEMA {schema_name}")
4648 }
4649 AlterCollationOperation::RefreshVersion => write!(f, "REFRESH VERSION"),
4650 }
4651 }
4652}
4653
4654impl fmt::Display for AlterCollation {
4655 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4656 write!(f, "ALTER COLLATION {} {}", self.name, self.operation)
4657 }
4658}
4659
4660impl Spanned for AlterCollation {
4661 fn span(&self) -> Span {
4662 Span::empty()
4663 }
4664}
4665
4666#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4669#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4670#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4671pub enum AlterTableType {
4672 Iceberg,
4675 Dynamic,
4678 External,
4681}
4682
4683#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4685#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4686#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4687pub struct AlterTable {
4688 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
4690 pub name: ObjectName,
4691 pub if_exists: bool,
4693 pub only: bool,
4695 pub operations: Vec<AlterTableOperation>,
4697 pub location: Option<HiveSetLocation>,
4699 pub on_cluster: Option<Ident>,
4703 pub table_type: Option<AlterTableType>,
4705 pub end_token: AttachedToken,
4707}
4708
4709impl fmt::Display for AlterTable {
4710 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4711 match &self.table_type {
4712 Some(AlterTableType::Iceberg) => write!(f, "ALTER ICEBERG TABLE ")?,
4713 Some(AlterTableType::Dynamic) => write!(f, "ALTER DYNAMIC TABLE ")?,
4714 Some(AlterTableType::External) => write!(f, "ALTER EXTERNAL TABLE ")?,
4715 None => write!(f, "ALTER TABLE ")?,
4716 }
4717
4718 if self.if_exists {
4719 write!(f, "IF EXISTS ")?;
4720 }
4721 if self.only {
4722 write!(f, "ONLY ")?;
4723 }
4724 write!(f, "{} ", &self.name)?;
4725 if let Some(cluster) = &self.on_cluster {
4726 write!(f, "ON CLUSTER {cluster} ")?;
4727 }
4728 write!(f, "{}", display_comma_separated(&self.operations))?;
4729 if let Some(loc) = &self.location {
4730 write!(f, " {loc}")?
4731 }
4732 Ok(())
4733 }
4734}
4735
4736#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4738#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4739#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4740pub struct DropFunction {
4741 pub if_exists: bool,
4743 pub func_desc: Vec<FunctionDesc>,
4745 pub drop_behavior: Option<DropBehavior>,
4747}
4748
4749impl fmt::Display for DropFunction {
4750 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4751 write!(
4752 f,
4753 "DROP FUNCTION{} {}",
4754 if self.if_exists { " IF EXISTS" } else { "" },
4755 display_comma_separated(&self.func_desc),
4756 )?;
4757 if let Some(op) = &self.drop_behavior {
4758 write!(f, " {op}")?;
4759 }
4760 Ok(())
4761 }
4762}
4763
4764impl Spanned for DropFunction {
4765 fn span(&self) -> Span {
4766 Span::empty()
4767 }
4768}
4769
4770#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4773#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4774#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4775pub struct CreateOperator {
4776 pub name: ObjectName,
4778 pub function: ObjectName,
4780 pub is_procedure: bool,
4782 pub left_arg: Option<DataType>,
4784 pub right_arg: Option<DataType>,
4786 pub options: Vec<OperatorOption>,
4788}
4789
4790#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4793#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4794#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4795pub struct CreateOperatorFamily {
4796 pub name: ObjectName,
4798 pub using: Ident,
4800}
4801
4802#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4805#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4806#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4807pub struct CreateOperatorClass {
4808 pub name: ObjectName,
4810 pub default: bool,
4812 pub for_type: DataType,
4814 pub using: Ident,
4816 pub family: Option<ObjectName>,
4818 pub items: Vec<OperatorClassItem>,
4820}
4821
4822impl fmt::Display for CreateOperator {
4823 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4824 write!(f, "CREATE OPERATOR {} (", self.name)?;
4825
4826 let function_keyword = if self.is_procedure {
4827 "PROCEDURE"
4828 } else {
4829 "FUNCTION"
4830 };
4831 let mut params = vec![format!("{} = {}", function_keyword, self.function)];
4832
4833 if let Some(left_arg) = &self.left_arg {
4834 params.push(format!("LEFTARG = {}", left_arg));
4835 }
4836 if let Some(right_arg) = &self.right_arg {
4837 params.push(format!("RIGHTARG = {}", right_arg));
4838 }
4839
4840 for option in &self.options {
4841 params.push(option.to_string());
4842 }
4843
4844 write!(f, "{}", params.join(", "))?;
4845 write!(f, ")")
4846 }
4847}
4848
4849impl fmt::Display for CreateOperatorFamily {
4850 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4851 write!(
4852 f,
4853 "CREATE OPERATOR FAMILY {} USING {}",
4854 self.name, self.using
4855 )
4856 }
4857}
4858
4859impl fmt::Display for CreateOperatorClass {
4860 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4861 write!(f, "CREATE OPERATOR CLASS {}", self.name)?;
4862 if self.default {
4863 write!(f, " DEFAULT")?;
4864 }
4865 write!(f, " FOR TYPE {} USING {}", self.for_type, self.using)?;
4866 if let Some(family) = &self.family {
4867 write!(f, " FAMILY {}", family)?;
4868 }
4869 write!(f, " AS {}", display_comma_separated(&self.items))
4870 }
4871}
4872
4873#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4875#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4876#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4877pub struct OperatorArgTypes {
4878 pub left: DataType,
4880 pub right: DataType,
4882}
4883
4884impl fmt::Display for OperatorArgTypes {
4885 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4886 write!(f, "{}, {}", self.left, self.right)
4887 }
4888}
4889
4890#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4892#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4893#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4894pub enum OperatorClassItem {
4895 Operator {
4897 strategy_number: u64,
4899 operator_name: ObjectName,
4901 op_types: Option<OperatorArgTypes>,
4903 purpose: Option<OperatorPurpose>,
4905 },
4906 Function {
4908 support_number: u64,
4910 op_types: Option<Vec<DataType>>,
4912 function_name: ObjectName,
4914 argument_types: Vec<DataType>,
4916 },
4917 Storage {
4919 storage_type: DataType,
4921 },
4922}
4923
4924#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4926#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4927#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4928pub enum OperatorPurpose {
4929 ForSearch,
4931 ForOrderBy {
4933 sort_family: ObjectName,
4935 },
4936}
4937
4938impl fmt::Display for OperatorClassItem {
4939 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4940 match self {
4941 OperatorClassItem::Operator {
4942 strategy_number,
4943 operator_name,
4944 op_types,
4945 purpose,
4946 } => {
4947 write!(f, "OPERATOR {strategy_number} {operator_name}")?;
4948 if let Some(types) = op_types {
4949 write!(f, " ({types})")?;
4950 }
4951 if let Some(purpose) = purpose {
4952 write!(f, " {purpose}")?;
4953 }
4954 Ok(())
4955 }
4956 OperatorClassItem::Function {
4957 support_number,
4958 op_types,
4959 function_name,
4960 argument_types,
4961 } => {
4962 write!(f, "FUNCTION {support_number}")?;
4963 if let Some(types) = op_types {
4964 write!(f, " ({})", display_comma_separated(types))?;
4965 }
4966 write!(f, " {function_name}")?;
4967 if !argument_types.is_empty() {
4968 write!(f, "({})", display_comma_separated(argument_types))?;
4969 }
4970 Ok(())
4971 }
4972 OperatorClassItem::Storage { storage_type } => {
4973 write!(f, "STORAGE {storage_type}")
4974 }
4975 }
4976 }
4977}
4978
4979impl fmt::Display for OperatorPurpose {
4980 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4981 match self {
4982 OperatorPurpose::ForSearch => write!(f, "FOR SEARCH"),
4983 OperatorPurpose::ForOrderBy { sort_family } => {
4984 write!(f, "FOR ORDER BY {sort_family}")
4985 }
4986 }
4987 }
4988}
4989
4990#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4993#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4994#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4995pub struct DropOperator {
4996 pub if_exists: bool,
4998 pub operators: Vec<DropOperatorSignature>,
5000 pub drop_behavior: Option<DropBehavior>,
5002}
5003
5004#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5006#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5007#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5008pub struct DropOperatorSignature {
5009 pub name: ObjectName,
5011 pub left_type: Option<DataType>,
5013 pub right_type: DataType,
5015}
5016
5017impl fmt::Display for DropOperatorSignature {
5018 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5019 write!(f, "{} (", self.name)?;
5020 if let Some(left_type) = &self.left_type {
5021 write!(f, "{}", left_type)?;
5022 } else {
5023 write!(f, "NONE")?;
5024 }
5025 write!(f, ", {})", self.right_type)
5026 }
5027}
5028
5029impl fmt::Display for DropOperator {
5030 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5031 write!(f, "DROP OPERATOR")?;
5032 if self.if_exists {
5033 write!(f, " IF EXISTS")?;
5034 }
5035 write!(f, " {}", display_comma_separated(&self.operators))?;
5036 if let Some(drop_behavior) = &self.drop_behavior {
5037 write!(f, " {}", drop_behavior)?;
5038 }
5039 Ok(())
5040 }
5041}
5042
5043impl Spanned for DropOperator {
5044 fn span(&self) -> Span {
5045 Span::empty()
5046 }
5047}
5048
5049#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5052#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5053#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5054pub struct DropOperatorFamily {
5055 pub if_exists: bool,
5057 pub names: Vec<ObjectName>,
5059 pub using: Ident,
5061 pub drop_behavior: Option<DropBehavior>,
5063}
5064
5065impl fmt::Display for DropOperatorFamily {
5066 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5067 write!(f, "DROP OPERATOR FAMILY")?;
5068 if self.if_exists {
5069 write!(f, " IF EXISTS")?;
5070 }
5071 write!(f, " {}", display_comma_separated(&self.names))?;
5072 write!(f, " USING {}", self.using)?;
5073 if let Some(drop_behavior) = &self.drop_behavior {
5074 write!(f, " {}", drop_behavior)?;
5075 }
5076 Ok(())
5077 }
5078}
5079
5080impl Spanned for DropOperatorFamily {
5081 fn span(&self) -> Span {
5082 Span::empty()
5083 }
5084}
5085
5086#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5089#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5090#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5091pub struct DropOperatorClass {
5092 pub if_exists: bool,
5094 pub names: Vec<ObjectName>,
5096 pub using: Ident,
5098 pub drop_behavior: Option<DropBehavior>,
5100}
5101
5102impl fmt::Display for DropOperatorClass {
5103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5104 write!(f, "DROP OPERATOR CLASS")?;
5105 if self.if_exists {
5106 write!(f, " IF EXISTS")?;
5107 }
5108 write!(f, " {}", display_comma_separated(&self.names))?;
5109 write!(f, " USING {}", self.using)?;
5110 if let Some(drop_behavior) = &self.drop_behavior {
5111 write!(f, " {}", drop_behavior)?;
5112 }
5113 Ok(())
5114 }
5115}
5116
5117impl Spanned for DropOperatorClass {
5118 fn span(&self) -> Span {
5119 Span::empty()
5120 }
5121}
5122
5123#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5125#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5126#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5127pub enum OperatorFamilyItem {
5128 Operator {
5130 strategy_number: u64,
5132 operator_name: ObjectName,
5134 op_types: Vec<DataType>,
5136 purpose: Option<OperatorPurpose>,
5138 },
5139 Function {
5141 support_number: u64,
5143 op_types: Option<Vec<DataType>>,
5145 function_name: ObjectName,
5147 argument_types: Vec<DataType>,
5149 },
5150}
5151
5152#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5155#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5156pub enum OperatorFamilyDropItem {
5157 Operator {
5159 strategy_number: u64,
5161 op_types: Vec<DataType>,
5163 },
5164 Function {
5166 support_number: u64,
5168 op_types: Vec<DataType>,
5170 },
5171}
5172
5173impl fmt::Display for OperatorFamilyItem {
5174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5175 match self {
5176 OperatorFamilyItem::Operator {
5177 strategy_number,
5178 operator_name,
5179 op_types,
5180 purpose,
5181 } => {
5182 write!(
5183 f,
5184 "OPERATOR {strategy_number} {operator_name} ({})",
5185 display_comma_separated(op_types)
5186 )?;
5187 if let Some(purpose) = purpose {
5188 write!(f, " {purpose}")?;
5189 }
5190 Ok(())
5191 }
5192 OperatorFamilyItem::Function {
5193 support_number,
5194 op_types,
5195 function_name,
5196 argument_types,
5197 } => {
5198 write!(f, "FUNCTION {support_number}")?;
5199 if let Some(types) = op_types {
5200 write!(f, " ({})", display_comma_separated(types))?;
5201 }
5202 write!(f, " {function_name}")?;
5203 if !argument_types.is_empty() {
5204 write!(f, "({})", display_comma_separated(argument_types))?;
5205 }
5206 Ok(())
5207 }
5208 }
5209 }
5210}
5211
5212impl fmt::Display for OperatorFamilyDropItem {
5213 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5214 match self {
5215 OperatorFamilyDropItem::Operator {
5216 strategy_number,
5217 op_types,
5218 } => {
5219 write!(
5220 f,
5221 "OPERATOR {strategy_number} ({})",
5222 display_comma_separated(op_types)
5223 )
5224 }
5225 OperatorFamilyDropItem::Function {
5226 support_number,
5227 op_types,
5228 } => {
5229 write!(
5230 f,
5231 "FUNCTION {support_number} ({})",
5232 display_comma_separated(op_types)
5233 )
5234 }
5235 }
5236 }
5237}
5238
5239#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5242#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5243#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5244pub struct AlterOperatorFamily {
5245 pub name: ObjectName,
5247 pub using: Ident,
5249 pub operation: AlterOperatorFamilyOperation,
5251}
5252
5253#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5256#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5257pub enum AlterOperatorFamilyOperation {
5258 Add {
5260 items: Vec<OperatorFamilyItem>,
5262 },
5263 Drop {
5265 items: Vec<OperatorFamilyDropItem>,
5267 },
5268 RenameTo {
5270 new_name: ObjectName,
5272 },
5273 OwnerTo(Owner),
5275 SetSchema {
5277 schema_name: ObjectName,
5279 },
5280}
5281
5282impl fmt::Display for AlterOperatorFamily {
5283 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5284 write!(
5285 f,
5286 "ALTER OPERATOR FAMILY {} USING {}",
5287 self.name, self.using
5288 )?;
5289 write!(f, " {}", self.operation)
5290 }
5291}
5292
5293impl fmt::Display for AlterOperatorFamilyOperation {
5294 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5295 match self {
5296 AlterOperatorFamilyOperation::Add { items } => {
5297 write!(f, "ADD {}", display_comma_separated(items))
5298 }
5299 AlterOperatorFamilyOperation::Drop { items } => {
5300 write!(f, "DROP {}", display_comma_separated(items))
5301 }
5302 AlterOperatorFamilyOperation::RenameTo { new_name } => {
5303 write!(f, "RENAME TO {new_name}")
5304 }
5305 AlterOperatorFamilyOperation::OwnerTo(owner) => {
5306 write!(f, "OWNER TO {owner}")
5307 }
5308 AlterOperatorFamilyOperation::SetSchema { schema_name } => {
5309 write!(f, "SET SCHEMA {schema_name}")
5310 }
5311 }
5312 }
5313}
5314
5315impl Spanned for AlterOperatorFamily {
5316 fn span(&self) -> Span {
5317 Span::empty()
5318 }
5319}
5320
5321#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5324#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5325#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5326pub struct AlterOperatorClass {
5327 pub name: ObjectName,
5329 pub using: Ident,
5331 pub operation: AlterOperatorClassOperation,
5333}
5334
5335#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5337#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5338#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5339pub enum AlterOperatorClassOperation {
5340 RenameTo {
5343 new_name: ObjectName,
5345 },
5346 OwnerTo(Owner),
5348 SetSchema {
5351 schema_name: ObjectName,
5353 },
5354}
5355
5356impl fmt::Display for AlterOperatorClass {
5357 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5358 write!(f, "ALTER OPERATOR CLASS {} USING {}", self.name, self.using)?;
5359 write!(f, " {}", self.operation)
5360 }
5361}
5362
5363impl fmt::Display for AlterOperatorClassOperation {
5364 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5365 match self {
5366 AlterOperatorClassOperation::RenameTo { new_name } => {
5367 write!(f, "RENAME TO {new_name}")
5368 }
5369 AlterOperatorClassOperation::OwnerTo(owner) => {
5370 write!(f, "OWNER TO {owner}")
5371 }
5372 AlterOperatorClassOperation::SetSchema { schema_name } => {
5373 write!(f, "SET SCHEMA {schema_name}")
5374 }
5375 }
5376 }
5377}
5378
5379impl Spanned for AlterOperatorClass {
5380 fn span(&self) -> Span {
5381 Span::empty()
5382 }
5383}
5384
5385#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5387#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5388#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5389pub struct AlterFunction {
5390 pub kind: AlterFunctionKind,
5392 pub function: FunctionDesc,
5394 pub aggregate_order_by: Option<Vec<OperateFunctionArg>>,
5398 pub aggregate_star: bool,
5402 pub operation: AlterFunctionOperation,
5404}
5405
5406#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5408#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5409#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5410pub enum AlterFunctionKind {
5411 Function,
5413 Aggregate,
5415 Procedure,
5417}
5418
5419impl fmt::Display for AlterFunctionKind {
5420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5421 match self {
5422 Self::Function => write!(f, "FUNCTION"),
5423 Self::Aggregate => write!(f, "AGGREGATE"),
5424 Self::Procedure => write!(f, "PROCEDURE"),
5425 }
5426 }
5427}
5428
5429#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5431#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5432#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5433pub enum AlterFunctionOperation {
5434 RenameTo {
5436 new_name: Ident,
5438 },
5439 OwnerTo(Owner),
5441 SetSchema {
5443 schema_name: ObjectName,
5445 },
5446 DependsOnExtension {
5448 no: bool,
5450 extension_name: ObjectName,
5452 },
5453 Actions {
5455 actions: Vec<AlterFunctionAction>,
5457 restrict: bool,
5459 },
5460}
5461
5462#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5464#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5465#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5466pub enum AlterFunctionAction {
5467 CalledOnNull(FunctionCalledOnNull),
5469 Behavior(FunctionBehavior),
5471 Leakproof(bool),
5473 Security {
5475 external: bool,
5477 security: FunctionSecurity,
5479 },
5480 Parallel(FunctionParallel),
5482 Cost(Expr),
5484 Rows(Expr),
5486 Support(ObjectName),
5488 Set(FunctionDefinitionSetParam),
5491 Reset(ResetConfig),
5493}
5494
5495impl fmt::Display for AlterFunction {
5496 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5497 write!(f, "ALTER {} ", self.kind)?;
5498 match self.kind {
5499 AlterFunctionKind::Function | AlterFunctionKind::Procedure => {
5500 write!(f, "{} ", self.function)?;
5501 }
5502 AlterFunctionKind::Aggregate => {
5503 write!(f, "{}(", self.function.name)?;
5504 if self.aggregate_star {
5505 write!(f, "*")?;
5506 } else {
5507 if let Some(args) = &self.function.args {
5508 write!(f, "{}", display_comma_separated(args))?;
5509 }
5510 if let Some(order_by_args) = &self.aggregate_order_by {
5511 if self
5512 .function
5513 .args
5514 .as_ref()
5515 .is_some_and(|args| !args.is_empty())
5516 {
5517 write!(f, " ")?;
5518 }
5519 write!(f, "ORDER BY {}", display_comma_separated(order_by_args))?;
5520 }
5521 }
5522 write!(f, ") ")?;
5523 }
5524 }
5525 write!(f, "{}", self.operation)
5526 }
5527}
5528
5529impl fmt::Display for AlterFunctionOperation {
5530 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5531 match self {
5532 AlterFunctionOperation::RenameTo { new_name } => {
5533 write!(f, "RENAME TO {new_name}")
5534 }
5535 AlterFunctionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5536 AlterFunctionOperation::SetSchema { schema_name } => {
5537 write!(f, "SET SCHEMA {schema_name}")
5538 }
5539 AlterFunctionOperation::DependsOnExtension { no, extension_name } => {
5540 if *no {
5541 write!(f, "NO DEPENDS ON EXTENSION {extension_name}")
5542 } else {
5543 write!(f, "DEPENDS ON EXTENSION {extension_name}")
5544 }
5545 }
5546 AlterFunctionOperation::Actions { actions, restrict } => {
5547 write!(f, "{}", display_separated(actions, " "))?;
5548 if *restrict {
5549 write!(f, " RESTRICT")?;
5550 }
5551 Ok(())
5552 }
5553 }
5554 }
5555}
5556
5557impl fmt::Display for AlterFunctionAction {
5558 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5559 match self {
5560 AlterFunctionAction::CalledOnNull(called_on_null) => write!(f, "{called_on_null}"),
5561 AlterFunctionAction::Behavior(behavior) => write!(f, "{behavior}"),
5562 AlterFunctionAction::Leakproof(leakproof) => {
5563 if *leakproof {
5564 write!(f, "LEAKPROOF")
5565 } else {
5566 write!(f, "NOT LEAKPROOF")
5567 }
5568 }
5569 AlterFunctionAction::Security { external, security } => {
5570 if *external {
5571 write!(f, "EXTERNAL ")?;
5572 }
5573 write!(f, "{security}")
5574 }
5575 AlterFunctionAction::Parallel(parallel) => write!(f, "{parallel}"),
5576 AlterFunctionAction::Cost(execution_cost) => write!(f, "COST {execution_cost}"),
5577 AlterFunctionAction::Rows(result_rows) => write!(f, "ROWS {result_rows}"),
5578 AlterFunctionAction::Support(support_function) => {
5579 write!(f, "SUPPORT {support_function}")
5580 }
5581 AlterFunctionAction::Set(set_param) => write!(f, "{set_param}"),
5582 AlterFunctionAction::Reset(reset_config) => match reset_config {
5583 ResetConfig::ALL => write!(f, "RESET ALL"),
5584 ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
5585 },
5586 }
5587 }
5588}
5589
5590impl Spanned for AlterFunction {
5591 fn span(&self) -> Span {
5592 Span::empty()
5593 }
5594}
5595
5596#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5600#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5601#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5602pub struct CreatePolicy {
5603 pub name: Ident,
5605 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5607 pub table_name: ObjectName,
5608 pub policy_type: Option<CreatePolicyType>,
5610 pub command: Option<CreatePolicyCommand>,
5612 pub to: Option<Vec<Owner>>,
5614 pub using: Option<Expr>,
5616 pub with_check: Option<Expr>,
5618}
5619
5620impl fmt::Display for CreatePolicy {
5621 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5622 write!(
5623 f,
5624 "CREATE POLICY {name} ON {table_name}",
5625 name = self.name,
5626 table_name = self.table_name,
5627 )?;
5628 if let Some(ref policy_type) = self.policy_type {
5629 write!(f, " AS {policy_type}")?;
5630 }
5631 if let Some(ref command) = self.command {
5632 write!(f, " FOR {command}")?;
5633 }
5634 if let Some(ref to) = self.to {
5635 write!(f, " TO {}", display_comma_separated(to))?;
5636 }
5637 if let Some(ref using) = self.using {
5638 write!(f, " USING ({using})")?;
5639 }
5640 if let Some(ref with_check) = self.with_check {
5641 write!(f, " WITH CHECK ({with_check})")?;
5642 }
5643 Ok(())
5644 }
5645}
5646
5647#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5653#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5654#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5655pub enum CreatePolicyType {
5656 Permissive,
5658 Restrictive,
5660}
5661
5662impl fmt::Display for CreatePolicyType {
5663 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5664 match self {
5665 CreatePolicyType::Permissive => write!(f, "PERMISSIVE"),
5666 CreatePolicyType::Restrictive => write!(f, "RESTRICTIVE"),
5667 }
5668 }
5669}
5670
5671#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5677#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5678#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5679pub enum CreatePolicyCommand {
5680 All,
5682 Select,
5684 Insert,
5686 Update,
5688 Delete,
5690}
5691
5692impl fmt::Display for CreatePolicyCommand {
5693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5694 match self {
5695 CreatePolicyCommand::All => write!(f, "ALL"),
5696 CreatePolicyCommand::Select => write!(f, "SELECT"),
5697 CreatePolicyCommand::Insert => write!(f, "INSERT"),
5698 CreatePolicyCommand::Update => write!(f, "UPDATE"),
5699 CreatePolicyCommand::Delete => write!(f, "DELETE"),
5700 }
5701 }
5702}
5703
5704#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5708#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5709#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5710pub struct DropPolicy {
5711 pub if_exists: bool,
5713 pub name: Ident,
5715 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5717 pub table_name: ObjectName,
5718 pub drop_behavior: Option<DropBehavior>,
5720}
5721
5722impl fmt::Display for DropPolicy {
5723 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5724 write!(
5725 f,
5726 "DROP POLICY {if_exists}{name} ON {table_name}",
5727 if_exists = if self.if_exists { "IF EXISTS " } else { "" },
5728 name = self.name,
5729 table_name = self.table_name
5730 )?;
5731 if let Some(ref behavior) = self.drop_behavior {
5732 write!(f, " {behavior}")?;
5733 }
5734 Ok(())
5735 }
5736}
5737
5738impl From<CreatePolicy> for crate::ast::Statement {
5739 fn from(v: CreatePolicy) -> Self {
5740 crate::ast::Statement::CreatePolicy(v)
5741 }
5742}
5743
5744impl From<DropPolicy> for crate::ast::Statement {
5745 fn from(v: DropPolicy) -> Self {
5746 crate::ast::Statement::DropPolicy(v)
5747 }
5748}
5749
5750#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5757#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5758#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5759pub struct AlterPolicy {
5760 pub name: Ident,
5762 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5764 pub table_name: ObjectName,
5765 pub operation: AlterPolicyOperation,
5767}
5768
5769impl fmt::Display for AlterPolicy {
5770 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5771 write!(
5772 f,
5773 "ALTER POLICY {name} ON {table_name}{operation}",
5774 name = self.name,
5775 table_name = self.table_name,
5776 operation = self.operation
5777 )
5778 }
5779}
5780
5781impl From<AlterPolicy> for crate::ast::Statement {
5782 fn from(v: AlterPolicy) -> Self {
5783 crate::ast::Statement::AlterPolicy(v)
5784 }
5785}
5786
5787#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5791#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5792#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5793pub enum FdwRoutineClause {
5794 Function(ObjectName),
5796 NoFunction,
5798}
5799
5800#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5804#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5805#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5806pub struct CreateForeignDataWrapper {
5807 pub name: Ident,
5809 pub handler: Option<FdwRoutineClause>,
5811 pub validator: Option<FdwRoutineClause>,
5813 pub options: Option<Vec<CreateServerOption>>,
5815}
5816
5817impl fmt::Display for CreateForeignDataWrapper {
5818 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5819 write!(f, "CREATE FOREIGN DATA WRAPPER {}", self.name)?;
5820 if let Some(handler) = &self.handler {
5821 match handler {
5822 FdwRoutineClause::Function(name) => write!(f, " HANDLER {name}")?,
5823 FdwRoutineClause::NoFunction => write!(f, " NO HANDLER")?,
5824 }
5825 }
5826 if let Some(validator) = &self.validator {
5827 match validator {
5828 FdwRoutineClause::Function(name) => write!(f, " VALIDATOR {name}")?,
5829 FdwRoutineClause::NoFunction => write!(f, " NO VALIDATOR")?,
5830 }
5831 }
5832 if let Some(options) = &self.options {
5833 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
5834 }
5835 Ok(())
5836 }
5837}
5838
5839impl From<CreateForeignDataWrapper> for crate::ast::Statement {
5840 fn from(v: CreateForeignDataWrapper) -> Self {
5841 crate::ast::Statement::CreateForeignDataWrapper(v)
5842 }
5843}
5844
5845#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5849#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5850#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5851pub struct CreateForeignTable {
5852 #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
5854 pub name: ObjectName,
5855 pub if_not_exists: bool,
5857 pub columns: Vec<ColumnDef>,
5859 pub server_name: Ident,
5861 pub options: Option<Vec<CreateServerOption>>,
5863}
5864
5865impl fmt::Display for CreateForeignTable {
5866 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5867 write!(
5868 f,
5869 "CREATE FOREIGN TABLE {if_not_exists}{name} ({columns}) SERVER {server_name}",
5870 if_not_exists = if self.if_not_exists { "IF NOT EXISTS " } else { "" },
5871 name = self.name,
5872 columns = display_comma_separated(&self.columns),
5873 server_name = self.server_name,
5874 )?;
5875 if let Some(options) = &self.options {
5876 write!(f, " OPTIONS ({})", display_comma_separated(options))?;
5877 }
5878 Ok(())
5879 }
5880}
5881
5882impl From<CreateForeignTable> for crate::ast::Statement {
5883 fn from(v: CreateForeignTable) -> Self {
5884 crate::ast::Statement::CreateForeignTable(v)
5885 }
5886}
5887
5888#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5891#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5892#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5893pub struct CreateAggregate {
5894 pub or_replace: bool,
5896 pub name: ObjectName,
5898 pub args: Vec<DataType>,
5900 pub options: Vec<CreateAggregateOption>,
5903}
5904
5905impl fmt::Display for CreateAggregate {
5906 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5907 write!(f, "CREATE")?;
5908 if self.or_replace {
5909 write!(f, " OR REPLACE")?;
5910 }
5911 write!(f, " AGGREGATE {}", self.name)?;
5912 write!(f, " ({})", display_comma_separated(&self.args))?;
5913 write!(f, " (")?;
5914 for (i, option) in self.options.iter().enumerate() {
5915 if i > 0 {
5916 write!(f, ", ")?;
5917 }
5918 write!(f, "{option}")?;
5919 }
5920 write!(f, ")")
5921 }
5922}
5923
5924impl From<CreateAggregate> for crate::ast::Statement {
5925 fn from(v: CreateAggregate) -> Self {
5926 crate::ast::Statement::CreateAggregate(v)
5927 }
5928}
5929
5930#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5934#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5935#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5936pub enum CreateAggregateOption {
5937 Sfunc(ObjectName),
5939 Stype(DataType),
5941 Sspace(u64),
5943 Finalfunc(ObjectName),
5945 FinalfuncExtra,
5947 FinalfuncModify(AggregateModifyKind),
5949 Combinefunc(ObjectName),
5951 Serialfunc(ObjectName),
5953 Deserialfunc(ObjectName),
5955 Initcond(Value),
5957 Msfunc(ObjectName),
5959 Minvfunc(ObjectName),
5961 Mstype(DataType),
5963 Msspace(u64),
5965 Mfinalfunc(ObjectName),
5967 MfinalfuncExtra,
5969 MfinalfuncModify(AggregateModifyKind),
5971 Minitcond(Value),
5973 Sortop(ObjectName),
5975 Parallel(FunctionParallel),
5977 Hypothetical,
5979}
5980
5981impl fmt::Display for CreateAggregateOption {
5982 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5983 match self {
5984 Self::Sfunc(name) => write!(f, "SFUNC = {name}"),
5985 Self::Stype(data_type) => write!(f, "STYPE = {data_type}"),
5986 Self::Sspace(size) => write!(f, "SSPACE = {size}"),
5987 Self::Finalfunc(name) => write!(f, "FINALFUNC = {name}"),
5988 Self::FinalfuncExtra => write!(f, "FINALFUNC_EXTRA"),
5989 Self::FinalfuncModify(kind) => write!(f, "FINALFUNC_MODIFY = {kind}"),
5990 Self::Combinefunc(name) => write!(f, "COMBINEFUNC = {name}"),
5991 Self::Serialfunc(name) => write!(f, "SERIALFUNC = {name}"),
5992 Self::Deserialfunc(name) => write!(f, "DESERIALFUNC = {name}"),
5993 Self::Initcond(cond) => write!(f, "INITCOND = {cond}"),
5994 Self::Msfunc(name) => write!(f, "MSFUNC = {name}"),
5995 Self::Minvfunc(name) => write!(f, "MINVFUNC = {name}"),
5996 Self::Mstype(data_type) => write!(f, "MSTYPE = {data_type}"),
5997 Self::Msspace(size) => write!(f, "MSSPACE = {size}"),
5998 Self::Mfinalfunc(name) => write!(f, "MFINALFUNC = {name}"),
5999 Self::MfinalfuncExtra => write!(f, "MFINALFUNC_EXTRA"),
6000 Self::MfinalfuncModify(kind) => write!(f, "MFINALFUNC_MODIFY = {kind}"),
6001 Self::Minitcond(cond) => write!(f, "MINITCOND = {cond}"),
6002 Self::Sortop(name) => write!(f, "SORTOP = {name}"),
6003 Self::Parallel(parallel) => {
6004 let kind = match parallel {
6005 FunctionParallel::Safe => "SAFE",
6006 FunctionParallel::Restricted => "RESTRICTED",
6007 FunctionParallel::Unsafe => "UNSAFE",
6008 };
6009 write!(f, "PARALLEL = {kind}")
6010 }
6011 Self::Hypothetical => write!(f, "HYPOTHETICAL"),
6012 }
6013 }
6014}
6015
6016#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6020#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6021#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6022pub enum AggregateModifyKind {
6023 ReadOnly,
6025 Shareable,
6027 ReadWrite,
6029}
6030
6031impl fmt::Display for AggregateModifyKind {
6032 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6033 match self {
6034 Self::ReadOnly => write!(f, "READ_ONLY"),
6035 Self::Shareable => write!(f, "SHAREABLE"),
6036 Self::ReadWrite => write!(f, "READ_WRITE"),
6037 }
6038 }
6039}
6040
6041#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6046#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6047#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6048pub struct CreateTextSearchConfiguration {
6049 pub name: ObjectName,
6051 pub options: Vec<SqlOption>,
6053}
6054
6055impl fmt::Display for CreateTextSearchConfiguration {
6056 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6057 write!(
6058 f,
6059 "CREATE TEXT SEARCH CONFIGURATION {name} ({options})",
6060 name = self.name,
6061 options = display_comma_separated(&self.options),
6062 )
6063 }
6064}
6065
6066impl From<CreateTextSearchConfiguration> for crate::ast::Statement {
6067 fn from(v: CreateTextSearchConfiguration) -> Self {
6068 crate::ast::Statement::CreateTextSearchConfiguration(v)
6069 }
6070}
6071
6072#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6077#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6078#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6079pub struct CreateTextSearchDictionary {
6080 pub name: ObjectName,
6082 pub options: Vec<SqlOption>,
6084}
6085
6086impl fmt::Display for CreateTextSearchDictionary {
6087 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6088 write!(
6089 f,
6090 "CREATE TEXT SEARCH DICTIONARY {name} ({options})",
6091 name = self.name,
6092 options = display_comma_separated(&self.options),
6093 )
6094 }
6095}
6096
6097impl From<CreateTextSearchDictionary> for crate::ast::Statement {
6098 fn from(v: CreateTextSearchDictionary) -> Self {
6099 crate::ast::Statement::CreateTextSearchDictionary(v)
6100 }
6101}
6102
6103#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6108#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6109#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6110pub struct CreateTextSearchParser {
6111 pub name: ObjectName,
6113 pub options: Vec<SqlOption>,
6115}
6116
6117impl fmt::Display for CreateTextSearchParser {
6118 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6119 write!(
6120 f,
6121 "CREATE TEXT SEARCH PARSER {name} ({options})",
6122 name = self.name,
6123 options = display_comma_separated(&self.options),
6124 )
6125 }
6126}
6127
6128impl From<CreateTextSearchParser> for crate::ast::Statement {
6129 fn from(v: CreateTextSearchParser) -> Self {
6130 crate::ast::Statement::CreateTextSearchParser(v)
6131 }
6132}
6133
6134#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6139#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6140#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6141pub struct CreateTextSearchTemplate {
6142 pub name: ObjectName,
6144 pub options: Vec<SqlOption>,
6146}
6147
6148impl fmt::Display for CreateTextSearchTemplate {
6149 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6150 write!(
6151 f,
6152 "CREATE TEXT SEARCH TEMPLATE {name} ({options})",
6153 name = self.name,
6154 options = display_comma_separated(&self.options),
6155 )
6156 }
6157}
6158
6159impl From<CreateTextSearchTemplate> for crate::ast::Statement {
6160 fn from(v: CreateTextSearchTemplate) -> Self {
6161 crate::ast::Statement::CreateTextSearchTemplate(v)
6162 }
6163}
6164
6165#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6169#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6170#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6171pub struct AlterDomain {
6172 pub name: ObjectName,
6174 pub operation: AlterDomainOperation,
6176}
6177
6178#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6181#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6182pub enum AlterDomainOperation {
6183 AddConstraint {
6185 constraint: TableConstraint,
6187 not_valid: bool,
6189 },
6190 DropConstraint {
6192 if_exists: bool,
6194 name: Ident,
6196 drop_behavior: Option<DropBehavior>,
6198 },
6199 RenameConstraint {
6201 old_name: Ident,
6203 new_name: Ident,
6205 },
6206 OwnerTo(Owner),
6208 RenameTo {
6210 new_name: Ident,
6212 },
6213 SetSchema {
6215 schema_name: ObjectName,
6217 },
6218 SetDefault {
6220 default: Expr,
6222 },
6223 DropDefault,
6225 ValidateConstraint {
6227 name: Ident,
6229 },
6230}
6231
6232impl fmt::Display for AlterDomain {
6233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6234 write!(f, "ALTER DOMAIN {} {}", self.name, self.operation)
6235 }
6236}
6237
6238impl fmt::Display for AlterDomainOperation {
6239 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6240 match self {
6241 AlterDomainOperation::AddConstraint {
6242 constraint,
6243 not_valid,
6244 } => {
6245 write!(f, "ADD {constraint}")?;
6246 if *not_valid {
6247 write!(f, " NOT VALID")?;
6248 }
6249 Ok(())
6250 }
6251 AlterDomainOperation::DropConstraint {
6252 if_exists,
6253 name,
6254 drop_behavior,
6255 } => {
6256 write!(f, "DROP CONSTRAINT")?;
6257 if *if_exists {
6258 write!(f, " IF EXISTS")?;
6259 }
6260 write!(f, " {name}")?;
6261 if let Some(behavior) = drop_behavior {
6262 write!(f, " {behavior}")?;
6263 }
6264 Ok(())
6265 }
6266 AlterDomainOperation::RenameConstraint { old_name, new_name } => {
6267 write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
6268 }
6269 AlterDomainOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6270 AlterDomainOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6271 AlterDomainOperation::SetSchema { schema_name } => {
6272 write!(f, "SET SCHEMA {schema_name}")
6273 }
6274 AlterDomainOperation::SetDefault { default } => write!(f, "SET DEFAULT {default}"),
6275 AlterDomainOperation::DropDefault => write!(f, "DROP DEFAULT"),
6276 AlterDomainOperation::ValidateConstraint { name } => {
6277 write!(f, "VALIDATE CONSTRAINT {name}")
6278 }
6279 }
6280 }
6281}
6282
6283#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6287#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6288#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6289pub enum PublicationTarget {
6290 AllTables,
6292 Tables(Vec<ObjectName>),
6294 TablesInSchema(Vec<Ident>),
6296}
6297
6298impl fmt::Display for PublicationTarget {
6299 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6300 match self {
6301 PublicationTarget::AllTables => write!(f, "FOR ALL TABLES"),
6302 PublicationTarget::Tables(tables) => {
6303 write!(f, "FOR TABLE {}", display_comma_separated(tables))
6304 }
6305 PublicationTarget::TablesInSchema(schemas) => {
6306 write!(
6307 f,
6308 "FOR TABLES IN SCHEMA {}",
6309 display_comma_separated(schemas)
6310 )
6311 }
6312 }
6313 }
6314}
6315
6316impl From<AlterDomain> for crate::ast::Statement {
6317 fn from(a: AlterDomain) -> Self {
6318 crate::ast::Statement::AlterDomain(a)
6319 }
6320}
6321
6322#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6327#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6328pub struct AlterTrigger {
6329 pub name: Ident,
6331 pub table_name: ObjectName,
6333 pub operation: AlterTriggerOperation,
6335}
6336
6337#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6339#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6340#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6341pub enum AlterTriggerOperation {
6342 RenameTo {
6344 new_name: Ident,
6346 },
6347}
6348
6349impl fmt::Display for AlterTrigger {
6350 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6351 write!(
6352 f,
6353 "ALTER TRIGGER {} ON {} {}",
6354 self.name, self.table_name, self.operation
6355 )
6356 }
6357}
6358
6359impl fmt::Display for AlterTriggerOperation {
6360 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6361 match self {
6362 AlterTriggerOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6363 }
6364 }
6365}
6366
6367impl From<AlterTrigger> for crate::ast::Statement {
6368 fn from(a: AlterTrigger) -> Self {
6369 crate::ast::Statement::AlterTrigger(a)
6370 }
6371}
6372
6373#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6377#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6378#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6379pub struct AlterExtension {
6380 pub name: Ident,
6382 pub operation: AlterExtensionOperation,
6384}
6385
6386#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6388#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6389#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6390pub enum AlterExtensionOperation {
6391 UpdateTo {
6393 version: Option<Ident>,
6395 },
6396 SetSchema {
6398 schema_name: ObjectName,
6400 },
6401 OwnerTo(Owner),
6403 RenameTo {
6405 new_name: Ident,
6407 },
6408}
6409
6410impl fmt::Display for AlterExtension {
6411 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6412 write!(f, "ALTER EXTENSION {} {}", self.name, self.operation)
6413 }
6414}
6415
6416impl fmt::Display for AlterExtensionOperation {
6417 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6418 match self {
6419 AlterExtensionOperation::UpdateTo { version } => {
6420 write!(f, "UPDATE")?;
6421 if let Some(v) = version {
6422 write!(f, " TO {v}")?;
6423 }
6424 Ok(())
6425 }
6426 AlterExtensionOperation::SetSchema { schema_name } => {
6427 write!(f, "SET SCHEMA {schema_name}")
6428 }
6429 AlterExtensionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6430 AlterExtensionOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6431 }
6432 }
6433}
6434
6435impl From<AlterExtension> for crate::ast::Statement {
6436 fn from(a: AlterExtension) -> Self {
6437 crate::ast::Statement::AlterExtension(a)
6438 }
6439}
6440
6441#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6446#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6447#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6448pub struct CreatePublication {
6449 pub name: Ident,
6451 pub target: Option<PublicationTarget>,
6453 pub with_options: Vec<SqlOption>,
6455}
6456
6457impl fmt::Display for CreatePublication {
6458 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6459 write!(f, "CREATE PUBLICATION {}", self.name)?;
6460 if let Some(target) = &self.target {
6461 write!(f, " {target}")?;
6462 }
6463 if !self.with_options.is_empty() {
6464 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
6465 }
6466 Ok(())
6467 }
6468}
6469
6470impl From<CreatePublication> for crate::ast::Statement {
6471 fn from(v: CreatePublication) -> Self {
6472 crate::ast::Statement::CreatePublication(v)
6473 }
6474}
6475
6476#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6481#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6482#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6483pub struct CreateSubscription {
6484 pub name: Ident,
6486 pub connection: Value,
6488 pub publications: Vec<Ident>,
6490 pub with_options: Vec<SqlOption>,
6492}
6493
6494impl fmt::Display for CreateSubscription {
6495 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6496 write!(
6497 f,
6498 "CREATE SUBSCRIPTION {name} CONNECTION {connection} PUBLICATION {publications}",
6499 name = self.name,
6500 connection = self.connection,
6501 publications = display_comma_separated(&self.publications),
6502 )?;
6503 if !self.with_options.is_empty() {
6504 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
6505 }
6506 Ok(())
6507 }
6508}
6509
6510impl From<CreateSubscription> for crate::ast::Statement {
6511 fn from(v: CreateSubscription) -> Self {
6512 crate::ast::Statement::CreateSubscription(v)
6513 }
6514}
6515
6516#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6520#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6521#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6522pub enum CastFunctionKind {
6523 WithFunction {
6525 function_name: ObjectName,
6527 argument_types: Vec<DataType>,
6530 },
6531 WithoutFunction,
6533 WithInout,
6535}
6536
6537impl fmt::Display for CastFunctionKind {
6538 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6539 match self {
6540 CastFunctionKind::WithFunction {
6541 function_name,
6542 argument_types,
6543 } => {
6544 write!(f, "WITH FUNCTION {function_name}")?;
6545 if !argument_types.is_empty() {
6546 write!(f, "({})", display_comma_separated(argument_types))?;
6547 }
6548 Ok(())
6549 }
6550 CastFunctionKind::WithoutFunction => write!(f, "WITHOUT FUNCTION"),
6551 CastFunctionKind::WithInout => write!(f, "WITH INOUT"),
6552 }
6553 }
6554}
6555
6556#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6561#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6562#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6563pub enum StatisticsKind {
6564 NDistinct,
6566 Dependencies,
6568 Mcv,
6570}
6571
6572impl fmt::Display for StatisticsKind {
6573 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6574 match self {
6575 StatisticsKind::NDistinct => write!(f, "ndistinct"),
6576 StatisticsKind::Dependencies => write!(f, "dependencies"),
6577 StatisticsKind::Mcv => write!(f, "mcv"),
6578 }
6579 }
6580}
6581
6582#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6586#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6587#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6588pub enum SecurityLabelObjectKind {
6589 Table,
6591 Column,
6593 Database,
6595 Domain,
6597 Function,
6599 Role,
6601 Schema,
6603 Sequence,
6605 Type,
6607 View,
6609 MaterializedView,
6611}
6612
6613impl fmt::Display for SecurityLabelObjectKind {
6614 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6615 match self {
6616 SecurityLabelObjectKind::Table => write!(f, "TABLE"),
6617 SecurityLabelObjectKind::Column => write!(f, "COLUMN"),
6618 SecurityLabelObjectKind::Database => write!(f, "DATABASE"),
6619 SecurityLabelObjectKind::Domain => write!(f, "DOMAIN"),
6620 SecurityLabelObjectKind::Function => write!(f, "FUNCTION"),
6621 SecurityLabelObjectKind::Role => write!(f, "ROLE"),
6622 SecurityLabelObjectKind::Schema => write!(f, "SCHEMA"),
6623 SecurityLabelObjectKind::Sequence => write!(f, "SEQUENCE"),
6624 SecurityLabelObjectKind::Type => write!(f, "TYPE"),
6625 SecurityLabelObjectKind::View => write!(f, "VIEW"),
6626 SecurityLabelObjectKind::MaterializedView => write!(f, "MATERIALIZED VIEW"),
6627 }
6628 }
6629}
6630
6631#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6635#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6636#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6637pub enum CastContext {
6638 Explicit,
6640 Assignment,
6642 Implicit,
6644}
6645
6646impl fmt::Display for CastContext {
6647 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6648 match self {
6649 CastContext::Explicit => Ok(()),
6650 CastContext::Assignment => write!(f, " AS ASSIGNMENT"),
6651 CastContext::Implicit => write!(f, " AS IMPLICIT"),
6652 }
6653 }
6654}
6655
6656#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6661#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6662#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6663pub struct CreateCast {
6664 pub source_type: DataType,
6666 pub target_type: DataType,
6668 pub function_kind: CastFunctionKind,
6670 pub cast_context: CastContext,
6672}
6673
6674impl fmt::Display for CreateCast {
6675 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6676 write!(
6677 f,
6678 "CREATE CAST ({source} AS {target}) {function_kind}{context}",
6679 source = self.source_type,
6680 target = self.target_type,
6681 function_kind = self.function_kind,
6682 context = self.cast_context,
6683 )
6684 }
6685}
6686
6687impl From<CreateCast> for crate::ast::Statement {
6688 fn from(v: CreateCast) -> Self {
6689 crate::ast::Statement::CreateCast(v)
6690 }
6691}
6692
6693#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6698#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6699#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6700pub struct CreateConversion {
6701 pub name: ObjectName,
6703 pub is_default: bool,
6705 pub source_encoding: String,
6707 pub destination_encoding: String,
6709 pub function_name: ObjectName,
6711}
6712
6713impl fmt::Display for CreateConversion {
6714 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6715 write!(f, "CREATE")?;
6716 if self.is_default {
6717 write!(f, " DEFAULT")?;
6718 }
6719 write!(
6720 f,
6721 " CONVERSION {name} FOR '{source}' TO '{destination}' FROM {function}",
6722 name = self.name,
6723 source = self.source_encoding,
6724 destination = self.destination_encoding,
6725 function = self.function_name,
6726 )
6727 }
6728}
6729
6730impl From<CreateConversion> for crate::ast::Statement {
6731 fn from(v: CreateConversion) -> Self {
6732 crate::ast::Statement::CreateConversion(v)
6733 }
6734}
6735
6736#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6741#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6742#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6743pub struct CreateLanguage {
6744 pub name: Ident,
6746 pub or_replace: bool,
6748 pub trusted: bool,
6750 pub procedural: bool,
6752 pub handler: Option<ObjectName>,
6754 pub inline_handler: Option<ObjectName>,
6756 pub validator: Option<ObjectName>,
6758}
6759
6760impl fmt::Display for CreateLanguage {
6761 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6762 write!(f, "CREATE")?;
6763 if self.or_replace {
6764 write!(f, " OR REPLACE")?;
6765 }
6766 if self.trusted {
6767 write!(f, " TRUSTED")?;
6768 }
6769 if self.procedural {
6770 write!(f, " PROCEDURAL")?;
6771 }
6772 write!(f, " LANGUAGE {}", self.name)?;
6773 if let Some(handler) = &self.handler {
6774 write!(f, " HANDLER {handler}")?;
6775 }
6776 if let Some(inline) = &self.inline_handler {
6777 write!(f, " INLINE {inline}")?;
6778 }
6779 if let Some(validator) = &self.validator {
6780 write!(f, " VALIDATOR {validator}")?;
6781 }
6782 Ok(())
6783 }
6784}
6785
6786#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6791#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6792#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6793pub struct CreateStatistics {
6794 pub if_not_exists: bool,
6796 pub name: ObjectName,
6798 pub kinds: Vec<StatisticsKind>,
6800 pub on: Vec<Expr>,
6802 pub from: ObjectName,
6804}
6805
6806impl fmt::Display for CreateStatistics {
6807 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6808 write!(f, "CREATE STATISTICS")?;
6809 if self.if_not_exists {
6810 write!(f, " IF NOT EXISTS")?;
6811 }
6812 write!(f, " {}", self.name)?;
6813 if !self.kinds.is_empty() {
6814 write!(f, " ({})", display_comma_separated(&self.kinds))?;
6815 }
6816 write!(f, " ON {}", display_comma_separated(&self.on))?;
6817 write!(f, " FROM {}", self.from)?;
6818 Ok(())
6819 }
6820}
6821
6822#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6827#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6828#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6829pub struct SecurityLabel {
6830 pub provider: Option<Ident>,
6832 pub object_kind: SecurityLabelObjectKind,
6834 pub object_name: ObjectName,
6836 pub label: Option<Value>,
6838}
6839
6840impl fmt::Display for SecurityLabel {
6841 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6842 write!(f, "SECURITY LABEL")?;
6843 if let Some(provider) = &self.provider {
6844 write!(f, " FOR {provider}")?;
6845 }
6846 write!(f, " ON {} {}", self.object_kind, self.object_name)?;
6847 write!(f, " IS ")?;
6848 match &self.label {
6849 Some(label) => write!(f, "{label}"),
6850 None => write!(f, "NULL"),
6851 }
6852 }
6853}
6854
6855impl From<SecurityLabel> for crate::ast::Statement {
6856 fn from(v: SecurityLabel) -> Self {
6857 crate::ast::Statement::SecurityLabel(v)
6858 }
6859}
6860
6861#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6865#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6866#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6867pub enum UserMappingUser {
6868 Ident(Ident),
6870 User,
6872 CurrentRole,
6874 CurrentUser,
6876 Public,
6878}
6879
6880impl fmt::Display for UserMappingUser {
6881 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6882 match self {
6883 UserMappingUser::Ident(ident) => write!(f, "{ident}"),
6884 UserMappingUser::User => write!(f, "USER"),
6885 UserMappingUser::CurrentRole => write!(f, "CURRENT_ROLE"),
6886 UserMappingUser::CurrentUser => write!(f, "CURRENT_USER"),
6887 UserMappingUser::Public => write!(f, "PUBLIC"),
6888 }
6889 }
6890}
6891
6892#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6897#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6898#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6899pub struct CreateUserMapping {
6900 pub if_not_exists: bool,
6902 pub user: UserMappingUser,
6904 pub server_name: Ident,
6906 pub options: Option<Vec<CreateServerOption>>,
6908}
6909
6910impl fmt::Display for CreateUserMapping {
6911 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6912 write!(f, "CREATE USER MAPPING")?;
6913 if self.if_not_exists {
6914 write!(f, " IF NOT EXISTS")?;
6915 }
6916 write!(f, " FOR {} SERVER {}", self.user, self.server_name)?;
6917 if let Some(options) = &self.options {
6918 write!(
6919 f,
6920 " OPTIONS ({})",
6921 display_comma_separated(options)
6922 )?;
6923 }
6924 Ok(())
6925 }
6926}
6927
6928impl From<CreateLanguage> for crate::ast::Statement {
6929 fn from(v: CreateLanguage) -> Self {
6930 crate::ast::Statement::CreateLanguage(v)
6931 }
6932}
6933
6934#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6938#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6939#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6940pub enum RuleEvent {
6941 Select,
6943 Insert,
6945 Update,
6947 Delete,
6949}
6950
6951impl fmt::Display for RuleEvent {
6952 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6953 match self {
6954 RuleEvent::Select => write!(f, "SELECT"),
6955 RuleEvent::Insert => write!(f, "INSERT"),
6956 RuleEvent::Update => write!(f, "UPDATE"),
6957 RuleEvent::Delete => write!(f, "DELETE"),
6958 }
6959 }
6960}
6961
6962impl From<CreateStatistics> for crate::ast::Statement {
6963 fn from(v: CreateStatistics) -> Self {
6964 crate::ast::Statement::CreateStatistics(v)
6965 }
6966}
6967
6968#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6973#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6974#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6975pub enum AccessMethodType {
6976 Index,
6978 Table,
6980}
6981
6982impl fmt::Display for AccessMethodType {
6983 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6984 match self {
6985 AccessMethodType::Index => write!(f, "INDEX"),
6986 AccessMethodType::Table => write!(f, "TABLE"),
6987 }
6988 }
6989}
6990
6991#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6995#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6996#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6997pub enum RuleAction {
6998 Nothing,
7000 Statements(Vec<crate::ast::Statement>),
7002}
7003
7004impl fmt::Display for RuleAction {
7005 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7006 match self {
7007 RuleAction::Nothing => write!(f, "NOTHING"),
7008 RuleAction::Statements(stmts) => {
7009 if stmts.len() == 1 {
7010 write!(f, "{}", stmts[0])
7011 } else {
7012 write!(f, "(")?;
7013 for (i, stmt) in stmts.iter().enumerate() {
7014 if i > 0 {
7015 write!(f, "; ")?;
7016 }
7017 write!(f, "{stmt}")?;
7018 }
7019 write!(f, ")")
7020 }
7021 }
7022 }
7023 }
7024}
7025
7026#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7031#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7032#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7033pub struct CreateRule {
7034 pub name: Ident,
7036 pub event: RuleEvent,
7038 pub table: ObjectName,
7040 pub condition: Option<Expr>,
7042 pub instead: bool,
7044 pub action: RuleAction,
7046}
7047
7048impl fmt::Display for CreateRule {
7049 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7050 write!(
7051 f,
7052 "CREATE RULE {name} AS ON {event} TO {table}",
7053 name = self.name,
7054 event = self.event,
7055 table = self.table,
7056 )?;
7057 if let Some(condition) = &self.condition {
7058 write!(f, " WHERE {condition}")?;
7059 }
7060 write!(f, " DO")?;
7061 if self.instead {
7062 write!(f, " INSTEAD")?;
7063 } else {
7064 write!(f, " ALSO")?;
7065 }
7066 write!(f, " {}", self.action)
7067 }
7068}
7069
7070impl From<CreateRule> for crate::ast::Statement {
7071 fn from(v: CreateRule) -> Self {
7072 crate::ast::Statement::CreateRule(v)
7073 }
7074}
7075
7076#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7081#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7082#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7083pub struct CreateAccessMethod {
7084 pub name: Ident,
7086 pub method_type: AccessMethodType,
7088 pub handler: ObjectName,
7090}
7091
7092impl fmt::Display for CreateAccessMethod {
7093 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7094 write!(
7095 f,
7096 "CREATE ACCESS METHOD {name} TYPE {method_type} HANDLER {handler}",
7097 name = self.name,
7098 method_type = self.method_type,
7099 handler = self.handler,
7100 )
7101 }
7102}
7103
7104impl From<CreateAccessMethod> for crate::ast::Statement {
7105 fn from(v: CreateAccessMethod) -> Self {
7106 crate::ast::Statement::CreateAccessMethod(v)
7107 }
7108}
7109
7110#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7115#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7116#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7117pub enum EventTriggerEvent {
7118 DdlCommandStart,
7120 DdlCommandEnd,
7122 TableRewrite,
7124 SqlDrop,
7126}
7127
7128impl fmt::Display for EventTriggerEvent {
7129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7130 match self {
7131 EventTriggerEvent::DdlCommandStart => write!(f, "ddl_command_start"),
7132 EventTriggerEvent::DdlCommandEnd => write!(f, "ddl_command_end"),
7133 EventTriggerEvent::TableRewrite => write!(f, "table_rewrite"),
7134 EventTriggerEvent::SqlDrop => write!(f, "sql_drop"),
7135 }
7136 }
7137}
7138
7139#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7144#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7145#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7146pub struct CreateEventTrigger {
7147 pub name: Ident,
7149 pub event: EventTriggerEvent,
7151 pub when_tags: Option<Vec<Value>>,
7153 pub execute: ObjectName,
7155 pub is_procedure: bool,
7157}
7158
7159impl fmt::Display for CreateEventTrigger {
7160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7161 write!(f, "CREATE EVENT TRIGGER {} ON {}", self.name, self.event)?;
7162 if let Some(tags) = &self.when_tags {
7163 write!(f, " WHEN TAG IN ({})", display_comma_separated(tags))?;
7164 }
7165 let func_kw = if self.is_procedure {
7166 "PROCEDURE"
7167 } else {
7168 "FUNCTION"
7169 };
7170 write!(f, " EXECUTE {func_kw} {}()", self.execute)?;
7171 Ok(())
7172 }
7173}
7174
7175impl From<CreateUserMapping> for crate::ast::Statement {
7176 fn from(v: CreateUserMapping) -> Self {
7177 crate::ast::Statement::CreateUserMapping(v)
7178 }
7179}
7180
7181#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7186#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7187#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7188pub struct CreateTablespace {
7189 pub name: Ident,
7191 pub owner: Option<Ident>,
7193 pub location: Value,
7195 pub with_options: Vec<SqlOption>,
7197}
7198
7199impl fmt::Display for CreateTablespace {
7200 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7201 write!(f, "CREATE TABLESPACE {}", self.name)?;
7202 if let Some(owner) = &self.owner {
7203 write!(f, " OWNER {owner}")?;
7204 }
7205 write!(f, " LOCATION {}", self.location)?;
7206 if !self.with_options.is_empty() {
7207 write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
7208 }
7209 Ok(())
7210 }
7211}
7212
7213impl From<CreateEventTrigger> for crate::ast::Statement {
7214 fn from(v: CreateEventTrigger) -> Self {
7215 crate::ast::Statement::CreateEventTrigger(v)
7216 }
7217}
7218
7219#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7226#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7227#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7228pub struct TransformElement {
7229 pub is_from: bool,
7231 pub function: ObjectName,
7233 pub arg_types: Vec<DataType>,
7235}
7236
7237impl fmt::Display for TransformElement {
7238 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7239 let direction = if self.is_from { "FROM" } else { "TO" };
7240 write!(
7241 f,
7242 "{direction} SQL WITH FUNCTION {}({})",
7243 self.function,
7244 display_comma_separated(&self.arg_types),
7245 )
7246 }
7247}
7248
7249#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7254#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7255#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7256pub struct CreateTransform {
7257 pub or_replace: bool,
7259 pub type_name: DataType,
7261 pub language: Ident,
7263 pub elements: Vec<TransformElement>,
7265}
7266
7267impl fmt::Display for CreateTransform {
7268 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7269 write!(f, "CREATE")?;
7270 if self.or_replace {
7271 write!(f, " OR REPLACE")?;
7272 }
7273 write!(
7274 f,
7275 " TRANSFORM FOR {} LANGUAGE {} ({})",
7276 self.type_name,
7277 self.language,
7278 display_comma_separated(&self.elements),
7279 )
7280 }
7281}
7282
7283impl From<CreateTransform> for crate::ast::Statement {
7284 fn from(v: CreateTransform) -> Self {
7285 crate::ast::Statement::CreateTransform(v)
7286 }
7287}
7288
7289impl From<CreateTablespace> for crate::ast::Statement {
7290 fn from(v: CreateTablespace) -> Self {
7291 crate::ast::Statement::CreateTablespace(v)
7292 }
7293}