1#[cfg(not(feature = "std"))]
17use alloc::{boxed::Box, string::ToString, vec::Vec};
18use core::fmt;
19
20#[cfg(feature = "serde")]
21use serde::{Deserialize, Serialize};
22
23use super::{FormatEncodeOptions, SqlOption};
24use crate::ast::{
25 DataType, Expr, Ident, ObjectName, SecretRefValue, SetVariableValue, Value,
26 display_comma_separated, display_separated,
27};
28use crate::tokenizer::Token;
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32pub enum AlterDatabaseOperation {
33 ChangeOwner { new_owner_name: Ident },
34 RenameDatabase { database_name: ObjectName },
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Hash)]
38#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
39pub enum AlterSchemaOperation {
40 ChangeOwner { new_owner_name: Ident },
41 RenameSchema { schema_name: ObjectName },
42 SwapRenameSchema { target_schema: ObjectName },
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Hash)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48pub enum AlterTableOperation {
49 AddConstraint(TableConstraint),
51 AddColumn {
53 column_def: ColumnDef,
54 },
55 DropConstraint {
57 name: Ident,
58 },
59 DropColumn {
61 column_name: Ident,
62 if_exists: bool,
63 cascade: bool,
64 },
65 RenameColumn {
67 old_column_name: Ident,
68 new_column_name: Ident,
69 },
70 RenameTable {
72 table_name: ObjectName,
73 },
74 ChangeColumn {
76 old_name: Ident,
77 new_name: Ident,
78 data_type: DataType,
79 options: Vec<ColumnOption>,
80 },
81 RenameConstraint {
85 old_name: Ident,
86 new_name: Ident,
87 },
88 AlterColumn {
90 column_name: Ident,
91 op: AlterColumnOperation,
92 },
93 ChangeOwner {
95 new_owner_name: Ident,
96 },
97 SetSchema {
99 new_schema_name: ObjectName,
100 },
101 SetParallelism {
103 parallelism: SetVariableValue,
104 deferred: bool,
105 },
106 RefreshSchema,
107 SetSourceRateLimit {
109 rate_limit: i32,
110 },
111 SetBackfillRateLimit {
113 rate_limit: i32,
114 },
115 SetDmlRateLimit {
117 rate_limit: i32,
118 },
119 SwapRenameTable {
121 target_table: ObjectName,
122 },
123 DropConnector,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Hash)]
128#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
129pub enum AlterIndexOperation {
130 RenameIndex {
131 index_name: ObjectName,
132 },
133 SetParallelism {
135 parallelism: SetVariableValue,
136 deferred: bool,
137 },
138}
139
140#[derive(Debug, Clone, PartialEq, Eq, Hash)]
141#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142pub enum AlterViewOperation {
143 RenameView {
144 view_name: ObjectName,
145 },
146 ChangeOwner {
147 new_owner_name: Ident,
148 },
149 SetSchema {
150 new_schema_name: ObjectName,
151 },
152 SetParallelism {
154 parallelism: SetVariableValue,
155 deferred: bool,
156 },
157 SetResourceGroup {
160 resource_group: Option<SetVariableValue>,
161 deferred: bool,
162 },
163 SetBackfillRateLimit {
165 rate_limit: i32,
166 },
167 SwapRenameView {
169 target_view: ObjectName,
170 },
171}
172
173#[derive(Debug, Clone, PartialEq, Eq, Hash)]
174#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
175pub enum AlterSinkOperation {
176 RenameSink {
177 sink_name: ObjectName,
178 },
179 ChangeOwner {
180 new_owner_name: Ident,
181 },
182 SetSchema {
183 new_schema_name: ObjectName,
184 },
185 SetParallelism {
187 parallelism: SetVariableValue,
188 deferred: bool,
189 },
190 SwapRenameSink {
192 target_sink: ObjectName,
193 },
194 SetSinkRateLimit {
195 rate_limit: i32,
196 },
197 SetSinkProps {
198 changed_props: Vec<SqlOption>,
199 },
200}
201
202#[derive(Debug, Clone, PartialEq, Eq, Hash)]
203#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
204pub enum AlterSubscriptionOperation {
205 RenameSubscription { subscription_name: ObjectName },
206 ChangeOwner { new_owner_name: Ident },
207 SetSchema { new_schema_name: ObjectName },
208 SwapRenameSubscription { target_subscription: ObjectName },
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, Hash)]
212#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
213pub enum AlterSourceOperation {
214 RenameSource {
215 source_name: ObjectName,
216 },
217 AddColumn {
218 column_def: ColumnDef,
219 },
220 ChangeOwner {
221 new_owner_name: Ident,
222 },
223 SetSchema {
224 new_schema_name: ObjectName,
225 },
226 FormatEncode {
227 format_encode: FormatEncodeOptions,
228 },
229 RefreshSchema,
230 SetSourceRateLimit {
231 rate_limit: i32,
232 },
233 SwapRenameSource {
234 target_source: ObjectName,
235 },
236 SetParallelism {
238 parallelism: SetVariableValue,
239 deferred: bool,
240 },
241}
242
243#[derive(Debug, Clone, PartialEq, Eq, Hash)]
244#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
245pub enum AlterFunctionOperation {
246 SetSchema { new_schema_name: ObjectName },
247}
248
249#[derive(Debug, Clone, PartialEq, Eq, Hash)]
250#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
251pub enum AlterConnectionOperation {
252 SetSchema { new_schema_name: ObjectName },
253 ChangeOwner { new_owner_name: Ident },
254}
255
256#[derive(Debug, Clone, PartialEq, Eq, Hash)]
257#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
258pub enum AlterSecretOperation {
259 ChangeCredential { new_credential: Value },
260}
261
262#[derive(Debug, Clone, PartialEq, Eq, Hash)]
263#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
264pub enum AlterFragmentOperation {
265 AlterBackfillRateLimit { rate_limit: i32 },
266}
267
268impl fmt::Display for AlterDatabaseOperation {
269 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270 match self {
271 AlterDatabaseOperation::ChangeOwner { new_owner_name } => {
272 write!(f, "OWNER TO {}", new_owner_name)
273 }
274 AlterDatabaseOperation::RenameDatabase { database_name } => {
275 write!(f, "RENAME TO {}", database_name)
276 }
277 }
278 }
279}
280
281impl fmt::Display for AlterSchemaOperation {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 match self {
284 AlterSchemaOperation::ChangeOwner { new_owner_name } => {
285 write!(f, "OWNER TO {}", new_owner_name)
286 }
287 AlterSchemaOperation::RenameSchema { schema_name } => {
288 write!(f, "RENAME TO {}", schema_name)
289 }
290 AlterSchemaOperation::SwapRenameSchema { target_schema } => {
291 write!(f, "SWAP WITH {}", target_schema)
292 }
293 }
294 }
295}
296
297impl fmt::Display for AlterTableOperation {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 match self {
300 AlterTableOperation::AddConstraint(c) => write!(f, "ADD {}", c),
301 AlterTableOperation::AddColumn { column_def } => {
302 write!(f, "ADD COLUMN {}", column_def)
303 }
304 AlterTableOperation::AlterColumn { column_name, op } => {
305 write!(f, "ALTER COLUMN {} {}", column_name, op)
306 }
307 AlterTableOperation::DropConstraint { name } => write!(f, "DROP CONSTRAINT {}", name),
308 AlterTableOperation::DropColumn {
309 column_name,
310 if_exists,
311 cascade,
312 } => write!(
313 f,
314 "DROP COLUMN {}{}{}",
315 if *if_exists { "IF EXISTS " } else { "" },
316 column_name,
317 if *cascade { " CASCADE" } else { "" }
318 ),
319 AlterTableOperation::RenameColumn {
320 old_column_name,
321 new_column_name,
322 } => write!(
323 f,
324 "RENAME COLUMN {} TO {}",
325 old_column_name, new_column_name
326 ),
327 AlterTableOperation::RenameTable { table_name } => {
328 write!(f, "RENAME TO {}", table_name)
329 }
330 AlterTableOperation::ChangeColumn {
331 old_name,
332 new_name,
333 data_type,
334 options,
335 } => {
336 write!(f, "CHANGE COLUMN {} {} {}", old_name, new_name, data_type)?;
337 if options.is_empty() {
338 Ok(())
339 } else {
340 write!(f, " {}", display_separated(options, " "))
341 }
342 }
343 AlterTableOperation::RenameConstraint { old_name, new_name } => {
344 write!(f, "RENAME CONSTRAINT {} TO {}", old_name, new_name)
345 }
346 AlterTableOperation::ChangeOwner { new_owner_name } => {
347 write!(f, "OWNER TO {}", new_owner_name)
348 }
349 AlterTableOperation::SetSchema { new_schema_name } => {
350 write!(f, "SET SCHEMA {}", new_schema_name)
351 }
352 AlterTableOperation::SetParallelism {
353 parallelism,
354 deferred,
355 } => {
356 write!(
357 f,
358 "SET PARALLELISM TO {}{}",
359 parallelism,
360 if *deferred { " DEFERRED" } else { "" }
361 )
362 }
363 AlterTableOperation::RefreshSchema => {
364 write!(f, "REFRESH SCHEMA")
365 }
366 AlterTableOperation::SetSourceRateLimit { rate_limit } => {
367 write!(f, "SET SOURCE_RATE_LIMIT TO {}", rate_limit)
368 }
369 AlterTableOperation::SetBackfillRateLimit { rate_limit } => {
370 write!(f, "SET BACKFILL_RATE_LIMIT TO {}", rate_limit)
371 }
372 AlterTableOperation::SetDmlRateLimit { rate_limit } => {
373 write!(f, "SET DML_RATE_LIMIT TO {}", rate_limit)
374 }
375 AlterTableOperation::SwapRenameTable { target_table } => {
376 write!(f, "SWAP WITH {}", target_table)
377 }
378 AlterTableOperation::DropConnector => {
379 write!(f, "DROP CONNECTOR")
380 }
381 }
382 }
383}
384
385impl fmt::Display for AlterIndexOperation {
386 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
387 match self {
388 AlterIndexOperation::RenameIndex { index_name } => {
389 write!(f, "RENAME TO {index_name}")
390 }
391 AlterIndexOperation::SetParallelism {
392 parallelism,
393 deferred,
394 } => {
395 write!(
396 f,
397 "SET PARALLELISM TO {}{}",
398 parallelism,
399 if *deferred { " DEFERRED" } else { "" }
400 )
401 }
402 }
403 }
404}
405
406impl fmt::Display for AlterViewOperation {
407 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
408 match self {
409 AlterViewOperation::RenameView { view_name } => {
410 write!(f, "RENAME TO {view_name}")
411 }
412 AlterViewOperation::ChangeOwner { new_owner_name } => {
413 write!(f, "OWNER TO {}", new_owner_name)
414 }
415 AlterViewOperation::SetSchema { new_schema_name } => {
416 write!(f, "SET SCHEMA {}", new_schema_name)
417 }
418 AlterViewOperation::SetParallelism {
419 parallelism,
420 deferred,
421 } => {
422 write!(
423 f,
424 "SET PARALLELISM TO {}{}",
425 parallelism,
426 if *deferred { " DEFERRED" } else { "" }
427 )
428 }
429 AlterViewOperation::SetBackfillRateLimit { rate_limit } => {
430 write!(f, "SET BACKFILL_RATE_LIMIT TO {}", rate_limit)
431 }
432 AlterViewOperation::SwapRenameView { target_view } => {
433 write!(f, "SWAP WITH {}", target_view)
434 }
435 AlterViewOperation::SetResourceGroup {
436 resource_group,
437 deferred,
438 } => {
439 let deferred = if *deferred { " DEFERRED" } else { "" };
440
441 if let Some(resource_group) = resource_group {
442 write!(f, "SET RESOURCE_GROUP TO {} {}", resource_group, deferred)
443 } else {
444 write!(f, "RESET RESOURCE_GROUP {}", deferred)
445 }
446 }
447 }
448 }
449}
450
451impl fmt::Display for AlterSinkOperation {
452 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
453 match self {
454 AlterSinkOperation::RenameSink { sink_name } => {
455 write!(f, "RENAME TO {sink_name}")
456 }
457 AlterSinkOperation::ChangeOwner { new_owner_name } => {
458 write!(f, "OWNER TO {}", new_owner_name)
459 }
460 AlterSinkOperation::SetSchema { new_schema_name } => {
461 write!(f, "SET SCHEMA {}", new_schema_name)
462 }
463 AlterSinkOperation::SetParallelism {
464 parallelism,
465 deferred,
466 } => {
467 write!(
468 f,
469 "SET PARALLELISM TO {}{}",
470 parallelism,
471 if *deferred { " DEFERRED" } else { "" }
472 )
473 }
474 AlterSinkOperation::SwapRenameSink { target_sink } => {
475 write!(f, "SWAP WITH {}", target_sink)
476 }
477 AlterSinkOperation::SetSinkRateLimit { rate_limit } => {
478 write!(f, "SET SINK_RATE_LIMIT TO {}", rate_limit)
479 }
480 AlterSinkOperation::SetSinkProps { changed_props } => {
481 write!(
482 f,
483 "CONNECTOR WITH ({})",
484 display_comma_separated(changed_props)
485 )
486 }
487 }
488 }
489}
490
491impl fmt::Display for AlterSubscriptionOperation {
492 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493 match self {
494 AlterSubscriptionOperation::RenameSubscription { subscription_name } => {
495 write!(f, "RENAME TO {subscription_name}")
496 }
497 AlterSubscriptionOperation::ChangeOwner { new_owner_name } => {
498 write!(f, "OWNER TO {}", new_owner_name)
499 }
500 AlterSubscriptionOperation::SetSchema { new_schema_name } => {
501 write!(f, "SET SCHEMA {}", new_schema_name)
502 }
503 AlterSubscriptionOperation::SwapRenameSubscription {
504 target_subscription,
505 } => {
506 write!(f, "SWAP WITH {}", target_subscription)
507 }
508 }
509 }
510}
511
512impl fmt::Display for AlterSourceOperation {
513 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
514 match self {
515 AlterSourceOperation::RenameSource { source_name } => {
516 write!(f, "RENAME TO {source_name}")
517 }
518 AlterSourceOperation::AddColumn { column_def } => {
519 write!(f, "ADD COLUMN {column_def}")
520 }
521 AlterSourceOperation::ChangeOwner { new_owner_name } => {
522 write!(f, "OWNER TO {}", new_owner_name)
523 }
524 AlterSourceOperation::SetSchema { new_schema_name } => {
525 write!(f, "SET SCHEMA {}", new_schema_name)
526 }
527 AlterSourceOperation::FormatEncode { format_encode } => {
528 write!(f, "{format_encode}")
529 }
530 AlterSourceOperation::RefreshSchema => {
531 write!(f, "REFRESH SCHEMA")
532 }
533 AlterSourceOperation::SetSourceRateLimit { rate_limit } => {
534 write!(f, "SET SOURCE_RATE_LIMIT TO {}", rate_limit)
535 }
536 AlterSourceOperation::SwapRenameSource { target_source } => {
537 write!(f, "SWAP WITH {}", target_source)
538 }
539 AlterSourceOperation::SetParallelism {
540 parallelism,
541 deferred,
542 } => {
543 write!(
544 f,
545 "SET PARALLELISM TO {}{}",
546 parallelism,
547 if *deferred { " DEFERRED" } else { "" }
548 )
549 }
550 }
551 }
552}
553
554impl fmt::Display for AlterFunctionOperation {
555 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
556 match self {
557 AlterFunctionOperation::SetSchema { new_schema_name } => {
558 write!(f, "SET SCHEMA {new_schema_name}")
559 }
560 }
561 }
562}
563
564impl fmt::Display for AlterConnectionOperation {
565 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
566 match self {
567 AlterConnectionOperation::SetSchema { new_schema_name } => {
568 write!(f, "SET SCHEMA {new_schema_name}")
569 }
570 AlterConnectionOperation::ChangeOwner { new_owner_name } => {
571 write!(f, "OWNER TO {new_owner_name}")
572 }
573 }
574 }
575}
576
577impl fmt::Display for AlterSecretOperation {
578 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
579 match self {
580 AlterSecretOperation::ChangeCredential { new_credential } => {
581 write!(f, "AS {new_credential}")
582 }
583 }
584 }
585}
586
587#[derive(Debug, Clone, PartialEq, Eq, Hash)]
589#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
590pub enum AlterColumnOperation {
591 SetNotNull,
593 DropNotNull,
595 SetDefault { value: Expr },
597 DropDefault,
599 SetDataType {
601 data_type: DataType,
602 using: Option<Expr>,
604 },
605}
606
607impl fmt::Display for AlterColumnOperation {
608 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
609 match self {
610 AlterColumnOperation::SetNotNull => write!(f, "SET NOT NULL",),
611 AlterColumnOperation::DropNotNull => write!(f, "DROP NOT NULL",),
612 AlterColumnOperation::SetDefault { value } => {
613 write!(f, "SET DEFAULT {}", value)
614 }
615 AlterColumnOperation::DropDefault => {
616 write!(f, "DROP DEFAULT")
617 }
618 AlterColumnOperation::SetDataType { data_type, using } => {
619 if let Some(expr) = using {
620 write!(f, "SET DATA TYPE {} USING {}", data_type, expr)
621 } else {
622 write!(f, "SET DATA TYPE {}", data_type)
623 }
624 }
625 }
626 }
627}
628
629impl fmt::Display for AlterFragmentOperation {
630 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
631 match self {
632 AlterFragmentOperation::AlterBackfillRateLimit { rate_limit } => {
633 write!(f, "SET BACKFILL_RATE_LIMIT TO {}", rate_limit)
634 }
635 }
636 }
637}
638
639#[derive(Debug, Clone, PartialEq, Eq, Hash)]
642#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
643pub struct SourceWatermark {
644 pub column: Ident,
645 pub expr: Expr,
646}
647
648impl fmt::Display for SourceWatermark {
649 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
650 write!(f, "WATERMARK FOR {} AS {}", self.column, self.expr,)
651 }
652}
653
654#[derive(Debug, Clone, PartialEq, Eq, Hash)]
657#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
658pub enum TableConstraint {
659 Unique {
661 name: Option<Ident>,
662 columns: Vec<Ident>,
663 is_primary: bool,
665 },
666 ForeignKey {
672 name: Option<Ident>,
673 columns: Vec<Ident>,
674 foreign_table: ObjectName,
675 referred_columns: Vec<Ident>,
676 on_delete: Option<ReferentialAction>,
677 on_update: Option<ReferentialAction>,
678 },
679 Check {
681 name: Option<Ident>,
682 expr: Box<Expr>,
683 },
684}
685
686impl fmt::Display for TableConstraint {
687 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
688 match self {
689 TableConstraint::Unique {
690 name,
691 columns,
692 is_primary,
693 } => write!(
694 f,
695 "{}{} ({})",
696 display_constraint_name(name),
697 if *is_primary { "PRIMARY KEY" } else { "UNIQUE" },
698 display_comma_separated(columns)
699 ),
700 TableConstraint::ForeignKey {
701 name,
702 columns,
703 foreign_table,
704 referred_columns,
705 on_delete,
706 on_update,
707 } => {
708 write!(
709 f,
710 "{}FOREIGN KEY ({}) REFERENCES {}({})",
711 display_constraint_name(name),
712 display_comma_separated(columns),
713 foreign_table,
714 display_comma_separated(referred_columns),
715 )?;
716 if let Some(action) = on_delete {
717 write!(f, " ON DELETE {}", action)?;
718 }
719 if let Some(action) = on_update {
720 write!(f, " ON UPDATE {}", action)?;
721 }
722 Ok(())
723 }
724 TableConstraint::Check { name, expr } => {
725 write!(f, "{}CHECK ({})", display_constraint_name(name), expr)
726 }
727 }
728 }
729}
730
731#[derive(Debug, Clone, PartialEq, Eq, Hash)]
733#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
734pub struct ColumnDef {
735 pub name: Ident,
736 pub data_type: Option<DataType>,
737 pub collation: Option<ObjectName>,
738 pub options: Vec<ColumnOptionDef>,
739}
740
741impl ColumnDef {
742 pub fn new(
743 name: Ident,
744 data_type: DataType,
745 collation: Option<ObjectName>,
746 options: Vec<ColumnOptionDef>,
747 ) -> Self {
748 ColumnDef {
749 name,
750 data_type: Some(data_type),
751 collation,
752 options,
753 }
754 }
755
756 pub fn is_generated(&self) -> bool {
757 self.options
758 .iter()
759 .any(|option| matches!(option.option, ColumnOption::GeneratedColumns(_)))
760 }
761}
762
763impl fmt::Display for ColumnDef {
764 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
765 write!(
766 f,
767 "{} {}",
768 self.name,
769 if let Some(data_type) = &self.data_type {
770 data_type.to_string()
771 } else {
772 "None".to_owned()
773 }
774 )?;
775 for option in &self.options {
776 write!(f, " {}", option)?;
777 }
778 Ok(())
779 }
780}
781
782#[derive(Debug, Clone, PartialEq, Eq, Hash)]
799#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
800pub struct ColumnOptionDef {
801 pub name: Option<Ident>,
802 pub option: ColumnOption,
803}
804
805impl fmt::Display for ColumnOptionDef {
806 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
807 write!(f, "{}{}", display_constraint_name(&self.name), self.option)
808 }
809}
810
811#[derive(Debug, Clone, PartialEq, Eq, Hash)]
814#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
815pub enum ColumnOption {
816 Null,
818 NotNull,
820 DefaultValue(Expr),
822 DefaultValueInternal {
825 persisted: Box<[u8]>,
827 expr: Option<Expr>,
831 },
832 Unique { is_primary: bool },
834 ForeignKey {
840 foreign_table: ObjectName,
841 referred_columns: Vec<Ident>,
842 on_delete: Option<ReferentialAction>,
843 on_update: Option<ReferentialAction>,
844 },
845 Check(Expr),
847 DialectSpecific(Vec<Token>),
851 GeneratedColumns(Expr),
853}
854
855impl fmt::Display for ColumnOption {
856 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
857 use ColumnOption::*;
858 match self {
859 Null => write!(f, "NULL"),
860 NotNull => write!(f, "NOT NULL"),
861 DefaultValue(expr) => write!(f, "DEFAULT {}", expr),
862 DefaultValueInternal { persisted: _, expr } => {
863 if let Some(expr) = expr {
864 write!(f, "DEFAULT {}", expr)
865 } else {
866 write!(f, "DEFAULT INTERNAL")
867 }
868 }
869 Unique { is_primary } => {
870 write!(f, "{}", if *is_primary { "PRIMARY KEY" } else { "UNIQUE" })
871 }
872 ForeignKey {
873 foreign_table,
874 referred_columns,
875 on_delete,
876 on_update,
877 } => {
878 write!(f, "REFERENCES {}", foreign_table)?;
879 if !referred_columns.is_empty() {
880 write!(f, " ({})", display_comma_separated(referred_columns))?;
881 }
882 if let Some(action) = on_delete {
883 write!(f, " ON DELETE {}", action)?;
884 }
885 if let Some(action) = on_update {
886 write!(f, " ON UPDATE {}", action)?;
887 }
888 Ok(())
889 }
890 Check(expr) => write!(f, "CHECK ({})", expr),
891 DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
892 GeneratedColumns(expr) => write!(f, "AS {}", expr),
893 }
894 }
895}
896
897fn display_constraint_name(name: &'_ Option<Ident>) -> impl fmt::Display + '_ {
898 struct ConstraintName<'a>(&'a Option<Ident>);
899 impl fmt::Display for ConstraintName<'_> {
900 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
901 if let Some(name) = self.0 {
902 write!(f, "CONSTRAINT {} ", name)?;
903 }
904 Ok(())
905 }
906 }
907 ConstraintName(name)
908}
909
910#[derive(Debug, Clone, PartialEq, Eq, Hash)]
915#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
916pub enum ReferentialAction {
917 Restrict,
918 Cascade,
919 SetNull,
920 NoAction,
921 SetDefault,
922}
923
924impl fmt::Display for ReferentialAction {
925 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
926 f.write_str(match self {
927 ReferentialAction::Restrict => "RESTRICT",
928 ReferentialAction::Cascade => "CASCADE",
929 ReferentialAction::SetNull => "SET NULL",
930 ReferentialAction::NoAction => "NO ACTION",
931 ReferentialAction::SetDefault => "SET DEFAULT",
932 })
933 }
934}
935
936#[derive(Debug, Clone, PartialEq, Eq, Hash)]
938#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
939pub struct WebhookSourceInfo {
940 pub secret_ref: Option<SecretRefValue>,
941 pub signature_expr: Expr,
942 pub wait_for_persistence: bool,
943}