Skip to content

Commit 2944e00

Browse files
author
mashuai
committed
fix: use parse_comma_separated to pass columns
1 parent 9c0d5f8 commit 2944e00

File tree

4 files changed

+16
-33
lines changed

4 files changed

+16
-33
lines changed

src/ast/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ pub enum Statement {
481481
/// index name
482482
name: ObjectName,
483483
table_name: ObjectName,
484-
columns: Vec<ObjectName>,
485-
if_unique: bool,
484+
columns: Vec<Ident>,
485+
unique: bool,
486486
if_not_exists: bool,
487487
},
488488
/// ALTER TABLE
@@ -668,13 +668,13 @@ impl fmt::Display for Statement {
668668
name,
669669
table_name,
670670
columns,
671-
if_unique,
671+
unique,
672672
if_not_exists,
673673
} => {
674674
write!(
675675
f,
676676
"CREATE{}INDEX{}{} ON {}({}",
677-
if *if_unique { " UNIQUE " } else { " " },
677+
if *unique { " UNIQUE " } else { " " },
678678
if *if_not_exists {
679679
" IF NOT EXISTS "
680680
} else {

src/dialect/keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ macro_rules! define_keywords {
5050
}
5151

5252
define_keywords!(
53-
INDEX,
5453
ABS,
5554
ADD,
5655
ASC,
@@ -421,7 +420,8 @@ define_keywords!(
421420
WORK,
422421
YEAR,
423422
ZONE,
424-
END_EXEC = "END-EXEC"
423+
END_EXEC = "END-EXEC",
424+
INDEX
425425
);
426426

427427
/// These keywords can't be used as a table alias, so that `FROM table_name alias`

src/parser.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ impl Parser {
938938
})
939939
}
940940

941-
pub fn parse_create_index(&mut self, if_unique: bool) -> Result<Statement, ParserError> {
941+
pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
942942
let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]);
943943
let index_name = self.parse_object_name()?;
944944
self.expect_keyword("ON")?;
@@ -948,7 +948,7 @@ impl Parser {
948948
name: index_name,
949949
table_name,
950950
columns,
951-
if_unique,
951+
unique,
952952
if_not_exists,
953953
})
954954
}
@@ -1386,26 +1386,10 @@ impl Parser {
13861386
Ok(ObjectName(idents))
13871387
}
13881388

1389-
pub fn parse_index_column(&mut self) -> Result<Vec<ObjectName>, ParserError> {
1389+
pub fn parse_index_column(&mut self) -> Result<Vec<Ident>, ParserError> {
13901390
self.expect_token(&Token::LParen)?;
1391-
let mut object_names = vec![];
1392-
loop {
1393-
let mut idents = vec![];
1394-
loop {
1395-
match self.peek_token() {
1396-
None | Some(Token::Comma) | Some(Token::RParen) => break,
1397-
_ => idents.push(self.parse_identifier()?),
1398-
}
1399-
}
1400-
object_names.push(ObjectName(idents));
1401-
let comma = self.consume_token(&Token::Comma);
1402-
if self.consume_token(&Token::RParen) {
1403-
// allow a trailing comma, even though it's not in standard
1404-
break;
1405-
} else if !comma {
1406-
return self.expected("',' or ')' after column definition", self.peek_token());
1407-
}
1408-
}
1391+
let object_names = self.parse_comma_separated(Parser::parse_identifier)?;
1392+
self.expect_token(&Token::RParen)?;
14091393
Ok(object_names)
14101394
}
14111395

tests/sqlparser_common.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,21 +2633,20 @@ fn ensure_multiple_dialects_are_tested() {
26332633

26342634
#[test]
26352635
fn parse_create_index() {
2636-
let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name);";
2637-
let ident_vec = vec![Ident::new("name")];
2638-
let vec: Vec<ObjectName> = vec![ObjectName(ident_vec)];
2636+
let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age);";
2637+
let ident_vec = vec![Ident::new("name"), Ident::new("age")];
26392638
match verified_stmt(sql) {
26402639
Statement::CreateIndex {
26412640
name,
26422641
table_name,
26432642
columns,
2644-
if_unique,
2643+
unique,
26452644
if_not_exists,
26462645
} => {
26472646
assert_eq!("idx_name", name.to_string());
26482647
assert_eq!("test", table_name.to_string());
2649-
assert_eq!(vec, columns);
2650-
assert_eq!(true, if_unique);
2648+
assert_eq!(ident_vec, columns);
2649+
assert_eq!(true, unique);
26512650
assert_eq!(true, if_not_exists)
26522651
}
26532652
_ => unreachable!(),

0 commit comments

Comments
 (0)