Skip to content

Commit 2ebe18a

Browse files
authored
Support COLLATION keywork on CREATE TABLE (#424)
* support table definition's collate * add table collate test
1 parent b5f3711 commit 2ebe18a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/ast/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ pub enum Statement {
758758
like: Option<ObjectName>,
759759
engine: Option<String>,
760760
default_charset: Option<String>,
761+
collation: Option<String>,
761762
},
762763
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
763764
CreateVirtualTable {
@@ -1208,6 +1209,7 @@ impl fmt::Display for Statement {
12081209
like,
12091210
default_charset,
12101211
engine,
1212+
collation,
12111213
} => {
12121214
// We want to allow the following options
12131215
// Empty column list, allowed by PostgreSQL:
@@ -1339,6 +1341,9 @@ impl fmt::Display for Statement {
13391341
if let Some(default_charset) = default_charset {
13401342
write!(f, " DEFAULT CHARSET={}", default_charset)?;
13411343
}
1344+
if let Some(collation) = collation {
1345+
write!(f, " COLLATE={}", collation)?;
1346+
}
13421347
Ok(())
13431348
}
13441349
Statement::CreateVirtualTable {

src/parser.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,7 @@ impl<'a> Parser<'a> {
15601560
like: None,
15611561
default_charset: None,
15621562
engine: None,
1563+
collation: None,
15631564
})
15641565
}
15651566

@@ -1754,6 +1755,16 @@ impl<'a> Parser<'a> {
17541755
None
17551756
};
17561757

1758+
let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
1759+
self.expect_token(&Token::Eq)?;
1760+
match self.next_token() {
1761+
Token::Word(w) => Some(w.value),
1762+
unexpected => self.expected("identifier", unexpected)?,
1763+
}
1764+
} else {
1765+
None
1766+
};
1767+
17571768
Ok(Statement::CreateTable {
17581769
name: table_name,
17591770
temporary,
@@ -1773,6 +1784,7 @@ impl<'a> Parser<'a> {
17731784
like,
17741785
engine,
17751786
default_charset,
1787+
collation,
17761788
})
17771789
}
17781790

tests/sqlparser_mysql.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,32 @@ fn parse_create_table_engine_default_charset() {
211211
}
212212
}
213213

214+
#[test]
215+
fn parse_create_table_collate() {
216+
let sql = "CREATE TABLE foo (id INT(11)) COLLATE=utf8mb4_0900_ai_ci";
217+
match mysql().verified_stmt(sql) {
218+
Statement::CreateTable {
219+
name,
220+
columns,
221+
collation,
222+
..
223+
} => {
224+
assert_eq!(name.to_string(), "foo");
225+
assert_eq!(
226+
vec![ColumnDef {
227+
name: Ident::new("id"),
228+
data_type: DataType::Int(Some(11)),
229+
collation: None,
230+
options: vec![],
231+
},],
232+
columns
233+
);
234+
assert_eq!(collation, Some("utf8mb4_0900_ai_ci".to_string()));
235+
}
236+
_ => unreachable!(),
237+
}
238+
}
239+
214240
#[test]
215241
fn parse_create_table_comment_character_set() {
216242
let sql = "CREATE TABLE foo (s TEXT CHARACTER SET utf8mb4 COMMENT 'comment')";

0 commit comments

Comments
 (0)