mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-31 11:17:23 +00:00
Implement ALTER TABLE ADD COLUMN and RENAME (#203)
Based on sqlite grammar https://www.sqlite.org/lang_altertable.html
This commit is contained in:
parent
fab6e28271
commit
faeb7d440a
5 changed files with 117 additions and 27 deletions
|
@ -1313,6 +1313,51 @@ fn parse_create_table_empty() {
|
|||
let _ = verified_stmt("CREATE TABLE t ()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_alter_table() {
|
||||
let add_column = "ALTER TABLE tab ADD COLUMN foo TEXT";
|
||||
match verified_stmt(add_column) {
|
||||
Statement::AlterTable {
|
||||
name,
|
||||
operation: AlterTableOperation::AddColumn { column_def },
|
||||
} => {
|
||||
assert_eq!("tab", name.to_string());
|
||||
assert_eq!("foo", column_def.name.to_string());
|
||||
assert_eq!("TEXT", column_def.data_type.to_string());
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let rename_table = "ALTER TABLE tab RENAME TO new_tab";
|
||||
match verified_stmt(rename_table) {
|
||||
Statement::AlterTable {
|
||||
name,
|
||||
operation: AlterTableOperation::RenameTable { table_name },
|
||||
} => {
|
||||
assert_eq!("tab", name.to_string());
|
||||
assert_eq!("new_tab", table_name.to_string())
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let rename_column = "ALTER TABLE tab RENAME COLUMN foo TO new_foo";
|
||||
match verified_stmt(rename_column) {
|
||||
Statement::AlterTable {
|
||||
name,
|
||||
operation:
|
||||
AlterTableOperation::RenameColumn {
|
||||
old_column_name,
|
||||
new_column_name,
|
||||
},
|
||||
} => {
|
||||
assert_eq!("tab", name.to_string());
|
||||
assert_eq!(old_column_name.to_string(), "foo");
|
||||
assert_eq!(new_column_name.to_string(), "new_foo");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_alter_table_constraints() {
|
||||
check_one("CONSTRAINT address_pkey PRIMARY KEY (address_id)");
|
||||
|
@ -1347,9 +1392,7 @@ fn parse_alter_table_constraints() {
|
|||
fn parse_bad_constraint() {
|
||||
let res = parse_sql_statements("ALTER TABLE tab ADD");
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"Expected a constraint in ALTER TABLE .. ADD, found: EOF".to_string()
|
||||
),
|
||||
ParserError::ParserError("Expected identifier, found: EOF".to_string()),
|
||||
res.unwrap_err()
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue