alter table tests + fixes

This commit is contained in:
pedrocarlo
2025-05-27 19:06:16 -03:00
parent 7fb3d40ec2
commit 43b1d4f5da
2 changed files with 96 additions and 11 deletions

View File

@@ -306,8 +306,8 @@ impl ToSqlString for ast::Operator {
impl ToSqlString for ast::Type {
fn to_sql_string<C: super::ToSqlContext>(&self, context: &C) -> String {
let mut ret = self.name.clone();
ret.push(' ');
if let Some(size) = &self.size {
ret.push(' ');
ret.push('(');
ret.push_str(&size.to_sql_string(context));
ret.push(')');
@@ -346,7 +346,10 @@ impl ToSqlString for ast::Distinctness {
impl ToSqlString for ast::QualifiedName {
fn to_sql_string<C: super::ToSqlContext>(&self, _context: &C) -> String {
let mut ret = String::new();
// TODO: skip DB Name
if let Some(db_name) = &self.db_name {
ret.push_str(&db_name.0);
ret.push('.');
}
if let Some(alias) = &self.alias {
ret.push_str(&alias.0);
ret.push('.');

View File

@@ -22,11 +22,14 @@ impl ToSqlString for ast::ColumnDefinition {
"".to_string()
},
if !self.constraints.is_empty() {
self.constraints
.iter()
.map(|constraint| constraint.to_sql_string(context))
.collect::<Vec<_>>()
.join(" ")
format!(
" {}",
self.constraints
.iter()
.map(|constraint| constraint.to_sql_string(context))
.collect::<Vec<_>>()
.join(" ")
)
} else {
"".to_string()
}
@@ -38,7 +41,7 @@ impl ToSqlString for ast::NamedColumnConstraint {
fn to_sql_string<C: crate::to_sql_string::ToSqlContext>(&self, context: &C) -> String {
let mut ret = Vec::new();
if let Some(name) = &self.name {
ret.push(format!("CONSTRAINT {}", name.0));
ret.push(format!("CONSTRAINT {} ", name.0));
}
ret.push(self.constraint.to_sql_string(context));
ret.join(" ")
@@ -71,10 +74,15 @@ impl ToSqlString for ast::ColumnConstraint {
}
),
Self::Generated { expr, typ } => {
// Don't need to add the generated part
format!(
"GENERATED ALWAYS AS ({}){}",
"AS ({}){}",
expr.to_sql_string(context),
if let Some(typ) = typ { &typ.0 } else { "" }
if let Some(typ) = typ {
format!(" {}", &typ.0)
} else {
"".to_string()
}
)
}
Self::NotNull {
@@ -128,7 +136,7 @@ impl ToSqlString for ast::ForeignKeyClause {
self.tbl_name.0,
if let Some(columns) = &self.columns {
format!(
" ({})",
"({})",
columns
.iter()
.map(|cols| cols.to_sql_string(context))
@@ -200,3 +208,77 @@ impl ToSqlString for ast::DeferSubclause {
)
}
}
#[cfg(test)]
mod tests {
use crate::to_sql_string_test;
to_sql_string_test!(
test_alter_table_rename,
"ALTER TABLE t RENAME TO new_table_name"
);
to_sql_string_test!(
test_alter_table_add_column,
"ALTER TABLE t ADD COLUMN c INTEGER"
);
to_sql_string_test!(
test_alter_table_add_column_with_default,
"ALTER TABLE t ADD COLUMN c TEXT DEFAULT 'value'"
);
to_sql_string_test!(
test_alter_table_add_column_not_null_default,
"ALTER TABLE t ADD COLUMN c REAL NOT NULL DEFAULT 0.0"
);
to_sql_string_test!(
test_alter_table_add_column_unique,
"ALTER TABLE t ADD COLUMN c TEXT UNIQUE",
ignore = "ParserError = Cannot add a UNIQUE column"
);
to_sql_string_test!(
test_alter_table_rename_column,
"ALTER TABLE t RENAME COLUMN old_name TO new_name"
);
to_sql_string_test!(test_alter_table_drop_column, "ALTER TABLE t DROP COLUMN c");
to_sql_string_test!(
test_alter_table_add_column_check,
"ALTER TABLE t ADD COLUMN c INTEGER CHECK (c > 0)"
);
to_sql_string_test!(
test_alter_table_add_column_foreign_key,
"ALTER TABLE t ADD COLUMN c INTEGER REFERENCES t2(id) ON DELETE CASCADE"
);
to_sql_string_test!(
test_alter_table_add_column_collate,
"ALTER TABLE t ADD COLUMN c TEXT COLLATE NOCASE"
);
to_sql_string_test!(
test_alter_table_add_column_primary_key,
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY",
ignore = "ParserError = Cannot add a PRIMARY KEY column"
);
to_sql_string_test!(
test_alter_table_add_column_primary_key_autoincrement,
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY AUTOINCREMENT",
ignore = "ParserError = Cannot add a PRIMARY KEY column"
);
to_sql_string_test!(
test_alter_table_add_generated_column,
"ALTER TABLE t ADD COLUMN c_generated AS (a + b) STORED"
);
to_sql_string_test!(
test_alter_table_add_column_schema,
"ALTER TABLE schema_name.t ADD COLUMN c INTEGER"
);
}