Skip to content

Commit c72c0c6

Browse files
committed
Add support for ASC and DESC in CREATE TABLE column constraints for SQLite.
See: https://sqlite.org/lang_createtable.html
1 parent 1e0460a commit c72c0c6

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@ impl Dialect for GenericDialect {
103103
fn supports_limit_comma(&self) -> bool {
104104
true
105105
}
106+
107+
fn supports_asc_desc_in_column_definition(&self) -> bool {
108+
true
109+
}
106110
}

src/dialect/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ pub trait Dialect: Debug + Any {
557557
fn supports_explain_with_utility_options(&self) -> bool {
558558
false
559559
}
560+
561+
fn supports_asc_desc_in_column_definition(&self) -> bool {
562+
false
563+
}
560564
}
561565

562566
/// This represents the operators for which precedence must be defined

src/dialect/sqlite.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,8 @@ impl Dialect for SQLiteDialect {
7777
fn supports_limit_comma(&self) -> bool {
7878
true
7979
}
80+
81+
fn supports_asc_desc_in_column_definition(&self) -> bool {
82+
true
83+
}
8084
}

src/parser/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6175,6 +6175,20 @@ impl<'a> Parser<'a> {
61756175
Ok(Some(ColumnOption::DialectSpecific(vec![
61766176
Token::make_keyword("AUTOINCREMENT"),
61776177
])))
6178+
} else if self.parse_keyword(Keyword::ASC)
6179+
&& self.dialect.supports_asc_desc_in_column_definition()
6180+
{
6181+
// Support ASC for SQLite
6182+
Ok(Some(ColumnOption::DialectSpecific(vec![
6183+
Token::make_keyword("ASC"),
6184+
])))
6185+
} else if self.parse_keyword(Keyword::DESC)
6186+
&& self.dialect.supports_asc_desc_in_column_definition()
6187+
{
6188+
// Support DESC for SQLite
6189+
Ok(Some(ColumnOption::DialectSpecific(vec![
6190+
Token::make_keyword("DESC"),
6191+
])))
61786192
} else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
61796193
&& dialect_of!(self is MySqlDialect | GenericDialect)
61806194
{

tests/sqlparser_sqlite.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,43 @@ fn parse_create_table_auto_increment() {
237237
}
238238
}
239239

240+
#[test]
241+
fn parse_create_table_primary_key_asc_desc() {
242+
let expected_column_def = |kind| ColumnDef {
243+
name: "bar".into(),
244+
data_type: DataType::Int(None),
245+
collation: None,
246+
options: vec![
247+
ColumnOptionDef {
248+
name: None,
249+
option: ColumnOption::Unique {
250+
is_primary: true,
251+
characteristics: None,
252+
},
253+
},
254+
ColumnOptionDef {
255+
name: None,
256+
option: ColumnOption::DialectSpecific(vec![Token::make_keyword(kind)]),
257+
},
258+
],
259+
};
260+
261+
let sql = "CREATE TABLE foo (bar INT PRIMARY KEY ASC)";
262+
match sqlite_and_generic().verified_stmt(sql) {
263+
Statement::CreateTable(CreateTable { columns, .. }) => {
264+
assert_eq!(vec![expected_column_def("ASC")], columns);
265+
}
266+
_ => unreachable!(),
267+
}
268+
let sql = "CREATE TABLE foo (bar INT PRIMARY KEY DESC)";
269+
match sqlite_and_generic().verified_stmt(sql) {
270+
Statement::CreateTable(CreateTable { columns, .. }) => {
271+
assert_eq!(vec![expected_column_def("DESC")], columns);
272+
}
273+
_ => unreachable!(),
274+
}
275+
}
276+
240277
#[test]
241278
fn parse_create_sqlite_quote() {
242279
let sql = "CREATE TABLE `PRIMARY` (\"KEY\" INT, [INDEX] INT)";

0 commit comments

Comments
 (0)