Skip to content

Commit 15c4539

Browse files
MySQL: Share column primary-key parsing
1 parent 72ce6a8 commit 15c4539

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎src/parser/mod.rs‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10179,20 +10179,6 @@ impl<'a> Parser<'a> {
1017910179
} else {
1018010180
Ok(Some(ColumnOption::Ephemeral(Some(self.parse_expr()?))))
1018110181
}
10182-
} else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
10183-
let characteristics = self.parse_constraint_characteristics()?;
10184-
Ok(Some(
10185-
PrimaryKeyConstraint {
10186-
name: None,
10187-
index_name: None,
10188-
index_type: None,
10189-
columns: vec![],
10190-
include: vec![],
10191-
index_options: vec![],
10192-
characteristics,
10193-
}
10194-
.into(),
10195-
))
1019610182
} else if self.parse_keyword(Keyword::UNIQUE) {
1019710183
let index_type_display =
1019810184
if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) {
@@ -10215,9 +10201,9 @@ impl<'a> Parser<'a> {
1021510201
}
1021610202
.into(),
1021710203
))
10218-
} else if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) {
10219-
// In MySQL, `KEY` in a column definition is shorthand for `PRIMARY KEY`.
10220-
// See: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
10204+
} else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
10205+
|| (self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY))
10206+
{
1022110207
let characteristics = self.parse_constraint_characteristics()?;
1022210208
Ok(Some(
1022310209
PrimaryKeyConstraint {

‎tests/sqlparser_common.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22099,3 +22099,46 @@ fn parse_qualified_literal_prefix_requires_adjacent_period() {
2209922099
dialects.tokenizes_to(sql, vec![token]);
2210022100
}
2210122101
}
22102+
22103+
#[test]
22104+
fn column_key_shorthand_preserves_characteristics_and_unique() {
22105+
let dialects = all_dialects_where(|d| d.supports_key_column_option());
22106+
let Statement::CreateTable(table) = dialects.one_statement_parses_to(
22107+
"CREATE TABLE t (id INT KEY DEFERRABLE INITIALLY DEFERRED UNIQUE KEY)",
22108+
"CREATE TABLE t (id INT PRIMARY KEY DEFERRABLE INITIALLY DEFERRED UNIQUE KEY)",
22109+
) else {
22110+
panic!("Expected CREATE TABLE");
22111+
};
22112+
let [ColumnOptionDef {
22113+
option: ColumnOption::PrimaryKey(primary),
22114+
..
22115+
}, ColumnOptionDef {
22116+
option: ColumnOption::Unique(unique),
22117+
..
22118+
}] = table.columns[0].options.as_slice()
22119+
else {
22120+
panic!("Expected primary and unique column options");
22121+
};
22122+
assert_eq!(
22123+
primary.characteristics,
22124+
Some(ConstraintCharacteristics {
22125+
deferrable: Some(true),
22126+
initially: Some(DeferrableInitial::Deferred),
22127+
enforced: None,
22128+
})
22129+
);
22130+
assert_eq!(unique.index_type_display, KeyOrIndexDisplay::Key);
22131+
}
22132+
22133+
#[test]
22134+
fn unsupported_key_column_option_preserves_keyword() {
22135+
for dialect in all_dialects_where(|d| !d.supports_key_column_option()).dialects {
22136+
let mut parser = Parser::new(dialect.as_ref()).try_with_sql("KEY").unwrap();
22137+
assert_eq!(
22138+
parser.parse_optional_column_option(),
22139+
Ok(None),
22140+
"{dialect:?}"
22141+
);
22142+
assert!(parser.parse_keyword(Keyword::KEY), "{dialect:?}");
22143+
}
22144+
}

0 commit comments

Comments
 (0)