@@ -31,10 +31,10 @@ use sqlparser_derive::{Visit, VisitMut};
3131use crate :: ast:: helpers:: attached_token:: AttachedToken ;
3232use crate :: ast:: table_constraints:: TableConstraint ;
3333use crate :: ast:: value:: escape_single_quote_string;
34- use crate :: ast:: { CreateViewParams , FunctionDesc , HiveSetLocation } ;
3534use crate :: ast:: {
36- display_comma_separated, display_separated, table_constraints:: ForeignKeyConstraint , ArgMode ,
37- CommentDef , ConditionalStatements , CreateFunctionBody , CreateFunctionUsing ,
35+ display_comma_separated, display_separated,
36+ table_constraints:: { ForeignKeyConstraint , PrimaryKeyConstraint , UniqueConstraint } ,
37+ ArgMode , CommentDef , ConditionalStatements , CreateFunctionBody , CreateFunctionUsing ,
3838 CreateTableLikeKind , CreateTableOptions , DataType , Expr , FileFormat , FunctionBehavior ,
3939 FunctionCalledOnNull , FunctionDeterminismSpecifier , FunctionParallel , HiveDistributionStyle ,
4040 HiveFormat , HiveIOFormat , HiveRowFormat , Ident , InitializeKind , MySQLColumnPosition ,
@@ -43,6 +43,7 @@ use crate::ast::{
4343 StorageSerializationPolicy , TableVersion , Tag , TriggerEvent , TriggerExecBody , TriggerObject ,
4444 TriggerPeriod , TriggerReferencing , Value , ValueWithSpan , WrappedCollection ,
4545} ;
46+ use crate :: ast:: { CreateViewParams , FunctionDesc , HiveSetLocation } ;
4647use crate :: display_utils:: { DisplayCommaSeparated , Indent , NewLine , SpaceOrNewline } ;
4748use crate :: keywords:: Keyword ;
4849use crate :: tokenizer:: { Span , Token } ;
@@ -56,6 +57,22 @@ pub struct IndexColumn {
5657 pub operator_class : Option < Ident > ,
5758}
5859
60+ impl From < Ident > for IndexColumn {
61+ fn from ( c : Ident ) -> Self {
62+ Self {
63+ column : OrderByExpr :: from ( c) ,
64+ operator_class : None ,
65+ }
66+ }
67+ }
68+
69+ impl < ' a > From < & ' a str > for IndexColumn {
70+ fn from ( c : & ' a str ) -> Self {
71+ let ident = Ident :: new ( c) ;
72+ ident. into ( )
73+ }
74+ }
75+
5976impl fmt:: Display for IndexColumn {
6077 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
6178 write ! ( f, "{}" , self . column) ?;
@@ -1555,12 +1572,10 @@ pub enum ColumnOption {
15551572 ///
15561573 /// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/create/table#default_values)
15571574 Alias ( Expr ) ,
1558-
1559- /// `{ PRIMARY KEY | UNIQUE } [<constraint_characteristics>]`
1560- Unique {
1561- is_primary : bool ,
1562- characteristics : Option < ConstraintCharacteristics > ,
1563- } ,
1575+ /// `PRIMARY KEY [<constraint_characteristics>]`
1576+ PrimaryKey ( PrimaryKeyConstraint ) ,
1577+ /// `UNIQUE [<constraint_characteristics>]`
1578+ Unique ( UniqueConstraint ) ,
15641579 /// A referential integrity constraint (`REFERENCES <foreign_table> (<referred_columns>)
15651580 /// [ MATCH { FULL | PARTIAL | SIMPLE } ]
15661581 /// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
@@ -1644,6 +1659,17 @@ impl From<ForeignKeyConstraint> for ColumnOption {
16441659 ColumnOption :: ForeignKey ( fk)
16451660 }
16461661}
1662+ impl From < UniqueConstraint > for ColumnOption {
1663+ fn from ( c : UniqueConstraint ) -> Self {
1664+ ColumnOption :: Unique ( c)
1665+ }
1666+ }
1667+
1668+ impl From < PrimaryKeyConstraint > for ColumnOption {
1669+ fn from ( c : PrimaryKeyConstraint ) -> Self {
1670+ ColumnOption :: PrimaryKey ( c)
1671+ }
1672+ }
16471673
16481674impl fmt:: Display for ColumnOption {
16491675 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -1661,12 +1687,16 @@ impl fmt::Display for ColumnOption {
16611687 }
16621688 }
16631689 Alias ( expr) => write ! ( f, "ALIAS {expr}" ) ,
1664- Unique {
1665- is_primary,
1666- characteristics,
1667- } => {
1668- write ! ( f, "{}" , if * is_primary { "PRIMARY KEY" } else { "UNIQUE" } ) ?;
1669- if let Some ( characteristics) = characteristics {
1690+ PrimaryKey ( constraint) => {
1691+ write ! ( f, "PRIMARY KEY" ) ?;
1692+ if let Some ( characteristics) = & constraint. characteristics {
1693+ write ! ( f, " {characteristics}" ) ?;
1694+ }
1695+ Ok ( ( ) )
1696+ }
1697+ Unique ( constraint) => {
1698+ write ! ( f, "UNIQUE" ) ?;
1699+ if let Some ( characteristics) = & constraint. characteristics {
16701700 write ! ( f, " {characteristics}" ) ?;
16711701 }
16721702 Ok ( ( ) )
0 commit comments