Implement ALTER TABLE ADD COLUMN and RENAME (#203)

Based on sqlite grammar
https://www.sqlite.org/lang_altertable.html
This commit is contained in:
mz 2020-06-17 03:52:37 +08:00 committed by GitHub
parent fab6e28271
commit faeb7d440a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 27 deletions

View file

@ -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()
);