Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
7phs committed Sep 20, 2024
1 parent 5687466 commit 5613dfe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
16 changes: 15 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,20 @@ impl fmt::Display for ColumnOptionDef {
}
}

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

impl fmt::Display for IdentityProperty {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}, {}", self.seed, self.increment)
}
}

/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
/// TABLE` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -1126,7 +1140,7 @@ pub enum ColumnOption {
/// IDENTITY [ (seed , increment) ]
/// ```
/// [MS SQL Server]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property
Identity(Option<SqlOption>),
Identity(Option<IdentityProperty>),
}

impl fmt::Display for ColumnOption {
Expand Down
14 changes: 2 additions & 12 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub use self::dcl::{AlterRoleOperation, ResetConfig, RoleOption, SetConfigValue,
pub use self::ddl::{
AlterColumnOperation, AlterIndexOperation, AlterTableOperation, ClusteredBy, ColumnDef,
ColumnOption, ColumnOptionDef, ConstraintCharacteristics, Deduplicate, DeferrableInitial,
GeneratedAs, GeneratedExpressionMode, IndexOption, IndexType, KeyOrIndexDisplay, Owner,
Partition, ProcedureParam, ReferentialAction, TableConstraint,
GeneratedAs, GeneratedExpressionMode, IdentityProperty, IndexOption, IndexType,
KeyOrIndexDisplay, Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
Expand Down Expand Up @@ -5818,13 +5818,6 @@ pub enum SqlOption {
range_direction: Option<PartitionRangeDirection>,
for_values: Vec<Expr>,
},
/// MS SQL Server specific: Optional parameters of identity column
/// E.g.
///
/// IDENTITY(1, 2)
///
/// [MS SQL Server]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property
Identity { seed: Value, increment: Value },
}

impl fmt::Display for SqlOption {
Expand Down Expand Up @@ -5856,9 +5849,6 @@ impl fmt::Display for SqlOption {
display_comma_separated(for_values)
)
}
SqlOption::Identity { seed, increment } => {
write!(f, "{}, {}", seed, increment)
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6046,17 +6046,17 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::IDENTITY)
&& dialect_of!(self is MsSqlDialect | GenericDialect)
{
let parameters = if self.expect_token(&Token::LParen).is_ok() {
let seed = self.parse_number_value()?;
let property = if self.consume_token(&Token::LParen) {
let seed = self.parse_number()?;
self.expect_token(&Token::Comma)?;
let increment = self.parse_number_value()?;
let increment = self.parse_number()?;
self.expect_token(&Token::RParen)?;

Some(SqlOption::Identity { seed, increment })
Some(IdentityProperty { seed, increment })
} else {
None
};
Ok(Some(ColumnOption::Identity(parameters)))
Ok(Some(ColumnOption::Identity(property)))
} else {
Ok(None)
}
Expand Down
15 changes: 9 additions & 6 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,17 @@ fn parse_create_table_with_identity_column() {
ColumnOptionDef {
name: None,
#[cfg(not(feature = "bigdecimal"))]
option: ColumnOption::Identity(Some(SqlOption::Identity {
seed: Value::Number("1".to_string(), false),
increment: Value::Number("1".to_string(), false),
option: ColumnOption::Identity(Some(IdentityProperty {
seed: Expr::Value(Value::Number("1".to_string(), false)),
increment: Expr::Value(Value::Number("1".to_string(), false)),
})),
#[cfg(feature = "bigdecimal")]
option: ColumnOption::Identity(Some(SqlOption::Identity {
seed: Value::Number(bigdecimal::BigDecimal::from(1), false),
increment: Value::Number(bigdecimal::BigDecimal::from(1), false),
option: ColumnOption::Identity(Some(IdentityProperty {
seed: Expr::Value(Value::Number(bigdecimal::BigDecimal::from(1), false)),
increment: Expr::Value(Value::Number(
bigdecimal::BigDecimal::from(1),
false,
)),
})),
},
ColumnOptionDef {
Expand Down

0 comments on commit 5613dfe

Please sign in to comment.