From 8a7cc7669d4b5c187a1b76d6422a66078821b179 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Tue, 27 May 2025 21:35:44 -0300 Subject: [PATCH] impl ToSqlString for CREATE TABLE stmt --- .../src/to_sql_string/stmt/alter_table.rs | 2 +- .../src/to_sql_string/stmt/create_table.rs | 115 ++++++++++++++++++ .../src/to_sql_string/stmt/mod.rs | 15 +++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 vendored/sqlite3-parser/src/to_sql_string/stmt/create_table.rs diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/alter_table.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/alter_table.rs index 47919bc27..af5169800 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/alter_table.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/alter_table.rs @@ -65,7 +65,7 @@ impl ToSqlString for ast::ColumnConstraint { clause, deref_clause, } => format!( - "{}{}", + "FOREIGN KEY {}{}", clause.to_sql_string(context), if let Some(deref) = deref_clause { deref.to_sql_string(context) diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/create_table.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/create_table.rs new file mode 100644 index 000000000..8ef7d9570 --- /dev/null +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/create_table.rs @@ -0,0 +1,115 @@ +use crate::{ast, to_sql_string::ToSqlString}; + +impl ToSqlString for ast::CreateTableBody { + fn to_sql_string(&self, context: &C) -> String { + match self { + Self::AsSelect(select) => format!("AS {}", select.to_sql_string(context)), + Self::ColumnsAndConstraints { + columns, + constraints, + options, + } => { + format!( + "({}) {}{}", + columns + .iter() + .map(|(_, col)| col.to_sql_string(context)) + .collect::>() + .join(", "), + constraints + .as_ref() + .map_or("".to_string(), |constraints| format!( + " {}", + constraints + .iter() + .map(|constraint| constraint.to_sql_string(context)) + .collect::>() + .join(", ") + )), + options.to_sql_string(context) + ) + } + } + } +} + +impl ToSqlString for ast::NamedTableConstraint { + fn to_sql_string(&self, context: &C) -> String { + if let Some(name) = &self.name { + format!("{} {}", name.0, self.constraint.to_sql_string(context)) + } else { + format!("{}", self.constraint.to_sql_string(context)) + } + } +} + +impl ToSqlString for ast::TableConstraint { + fn to_sql_string(&self, context: &C) -> String { + match self { + Self::Check(expr) => format!("CHECK ({})", expr.to_sql_string(context)), + Self::ForeignKey { + columns, + clause, + deref_clause, + } => format!( + "FOREIGN KEY ({}){}{}", + columns + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", "), + clause.to_sql_string(context), + if let Some(deref) = deref_clause { + deref.to_sql_string(context) + } else { + "".to_string() + } + ), + Self::PrimaryKey { + columns, + auto_increment, + conflict_clause, + } => format!( + "PRIMARY KEY ({}){}{}", + columns + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", "), + conflict_clause.map_or("".to_string(), |conflict| format!( + " {}", + conflict.to_sql_string(context) + )), + auto_increment.then_some(" AUTOINCREMENT").unwrap_or("") + ), + Self::Unique { + columns, + conflict_clause, + } => format!( + "UNIQUE ({}){}", + columns + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", "), + conflict_clause.map_or("".to_string(), |conflict| format!( + " {}", + conflict.to_sql_string(context) + )) + ), + } + } +} + +impl ToSqlString for ast::TableOptions { + fn to_sql_string(&self, _context: &C) -> String { + if *self == Self::NONE { + "" + } else if *self == Self::STRICT { + "STRICT" + } else { + "WITHOUT ROWID" + } + .to_string() + } +} diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs index ee6a05fc9..8efa00722 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs @@ -3,6 +3,7 @@ use crate::ast; use super::ToSqlString; mod alter_table; +mod create_table; mod select; impl ToSqlString for ast::Stmt { @@ -75,6 +76,20 @@ impl ToSqlString for ast::Stmt { )) ) } + Self::CreateTable { + temporary, + if_not_exists, + tbl_name, + body, + } => { + format!( + "CREATE{} TABLE {}{} {}", + temporary.then_some(" TEMP").unwrap_or(""), + if_not_exists.then_some("IF NOT EXISTS ").unwrap_or(""), + tbl_name.to_sql_string(context), + body.to_sql_string(context) + ) + } Self::Select(select) => select.to_sql_string(context), _ => todo!(), }