Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
7phs committed Oct 2, 2024
1 parent 7088132 commit 3b95e3b
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 57 deletions.
48 changes: 32 additions & 16 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,25 +1067,24 @@ pub enum Identity {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct IdentityProperty {
pub parameters: Option<IdentityParameters>,
pub parameters: Option<IdentityFormat>,
pub order: Option<IdentityOrder>,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct IdentityParameters {
pub format: IdentityParametersFormat,
pub seed: Expr,
pub increment: Expr,
pub enum IdentityFormat {
FunctionCall(IdentityParameters),
StartAndIncrement(IdentityParameters),
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum IdentityParametersFormat {
FunctionCall,
StartIncrement,
pub struct IdentityParameters {
pub seed: Expr,
pub increment: Expr,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -1113,14 +1112,18 @@ impl fmt::Display for Identity {
}
}

impl fmt::Display for IdentityParameters {
impl fmt::Display for IdentityFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.format {
IdentityParametersFormat::FunctionCall => {
write!(f, "({}, {})", self.seed, self.increment)
match self {
IdentityFormat::FunctionCall(parameters) => {
write!(f, "({}, {})", parameters.seed, parameters.increment)
}
IdentityParametersFormat::StartIncrement => {
write!(f, " START {} INCREMENT {}", self.seed, self.increment)
IdentityFormat::StartAndIncrement(parameters) => {
write!(
f,
" START {} INCREMENT {}",
parameters.seed, parameters.increment
)
}
}
}
Expand Down Expand Up @@ -1157,7 +1160,7 @@ impl fmt::Display for ColumnPolicy {
ColumnPolicy::MaskingPolicy(property) => ("MASKING POLICY", property),
ColumnPolicy::ProjectionPolicy(property) => ("PROJECTION POLICY", property),
};
write!(f, "{command} {}", property.policy_name)?;
write!(f, "WITH {command} {}", property.policy_name)?;
if let Some(using_columns) = &property.using_columns {
write!(f, "USING ({})", display_comma_separated(using_columns))?;
}
Expand Down Expand Up @@ -1243,7 +1246,20 @@ pub enum ColumnOption {
/// [MS SQL Server]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
Identity(Identity),
/// Snowflake specific: an option of specifying security masking or projection policy to set on a column.
/// Syntax:
/// ```sql
/// [ WITH ] MASKING POLICY <policy_name> [ USING ( <col_name> , <cond_col1> , ... ) ]
/// [ WITH ] PROJECTION POLICY <policy_name>
/// ```
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
Policy(ColumnPolicy),
/// Snowflake specific: Specifies the tag name and the tag string value.
/// Syntax:
/// ```sql
/// [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] )
/// ```
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
Tags(Vec<Tag>),
}

Expand Down Expand Up @@ -1353,7 +1369,7 @@ impl fmt::Display for ColumnOption {
write!(f, "{parameters}")
}
Tags(tags) => {
write!(f, "{}", display_comma_separated(tags))
write!(f, "WITH TAG ({})", display_comma_separated(tags))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub use self::dcl::{AlterRoleOperation, ResetConfig, RoleOption, SetConfigValue,
pub use self::ddl::{
AlterColumnOperation, AlterIndexOperation, AlterTableOperation, ClusteredBy, ColumnDef,
ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics,
Deduplicate, DeferrableInitial, GeneratedAs, GeneratedExpressionMode, Identity, IdentityOrder,
IdentityParameters, IdentityParametersFormat, IdentityProperty, IndexOption, IndexType,
KeyOrIndexDisplay, Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
Deduplicate, DeferrableInitial, GeneratedAs, GeneratedExpressionMode, Identity, IdentityFormat,
IdentityOrder, IdentityParameters, IdentityProperty, IndexOption, IndexType, KeyOrIndexDisplay,
Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
Expand Down
17 changes: 7 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6190,11 +6190,10 @@ impl<'a> Parser<'a> {
let increment = self.parse_number()?;
self.expect_token(&Token::RParen)?;

Some(IdentityParameters {
format: IdentityParametersFormat::FunctionCall,
Some(IdentityFormat::FunctionCall(IdentityParameters {
seed,
increment,
})
}))
} else {
None
};
Expand Down Expand Up @@ -6246,21 +6245,19 @@ impl<'a> Parser<'a> {
let increment = self.parse_number()?;
self.expect_token(&Token::RParen)?;

Some(IdentityParameters {
format: IdentityParametersFormat::FunctionCall,
Some(IdentityFormat::FunctionCall(IdentityParameters {
seed,
increment,
})
}))
} else if self.parse_keyword(Keyword::START) {
let seed = self.parse_number()?;
self.expect_keyword(Keyword::INCREMENT)?;
let increment = self.parse_number()?;

Some(IdentityParameters {
format: IdentityParametersFormat::StartIncrement,
Some(IdentityFormat::StartAndIncrement(IdentityParameters {
seed,
increment,
})
}))
} else {
None
};
Expand Down Expand Up @@ -6300,7 +6297,7 @@ impl<'a> Parser<'a> {
let policy_name = self.parse_identifier(false)?;
let using_columns = if self.parse_keyword(Keyword::USING) {
self.expect_token(&Token::LParen)?;
let columns = self.parse_comma_separated(Self::parse_identifier)?;
let columns = self.parse_comma_separated(|p| p.parse_identifier(false))?;
self.expect_token(&Token::RParen)?;
Some(columns)
} else {
Expand Down
15 changes: 6 additions & 9 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,19 +938,16 @@ fn parse_create_table_with_identity_column() {
ColumnOptionDef {
name: None,
#[cfg(not(feature = "bigdecimal"))]
option: ColumnOption::Identity(IdentityProperty {
format: IdentityPropertyCommand::Identity,
parameters: Some(IdentityParameters {
format: IdentityParametersFormat::FunctionCall,
option: ColumnOption::Identity(Identity::Identity(IdentityProperty {
parameters: Some(IdentityFormat::FunctionCall(IdentityParameters {
seed: Expr::Value(Value::Number("1".to_string(), false)),
increment: Expr::Value(Value::Number("1".to_string(), false)),
}),
})),
order: None,
}),
})),
#[cfg(feature = "bigdecimal")]
option: ColumnOption::Identity(Identity::Identity(IdentityProperty {
parameters: Some(IdentityParameters {
format: IdentityParametersFormat::FunctionCall,
parameters: Some(IdentityFormat::FunctionCall(IdentityParameters {
seed: Expr::Value(Value::Number(
bigdecimal::BigDecimal::from(1),
false,
Expand All @@ -959,7 +956,7 @@ fn parse_create_table_with_identity_column() {
bigdecimal::BigDecimal::from(1),
false,
)),
}),
})),
order: None,
})),
},
Expand Down
Loading

0 comments on commit 3b95e3b

Please sign in to comment.