Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql create table add table and colunm comment #622

Merged
merged 16 commits into from
Apr 4, 2023
Merged
15 changes: 15 additions & 0 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use super::*;

impl TableBuilder for MysqlQueryBuilder {
fn prepare_table_opt(&self, create: &TableCreateStatement, sql: &mut dyn SqlWriter) {
// comment
if let Some(comment) = &create.comment {
// self.quote()
write!(sql, " COMMENT '{comment}'").unwrap();
Caisin marked this conversation as resolved.
Show resolved Hide resolved
}
self.prepare_table_opt_def(create, sql)
}

fn prepare_column_def(&self, column_def: &ColumnDef, sql: &mut dyn SqlWriter) {
column_def.name.prepare(sql.as_writer(), self.quote());

Expand Down Expand Up @@ -191,4 +200,10 @@ impl TableBuilder for MysqlQueryBuilder {
self.prepare_table_ref_table_stmt(to_name, sql);
}
}

/// column comment
fn column_comment(&self, comment: &str, sql: &mut dyn SqlWriter) {
let comment = comment.replace('\'', "\\'");
write!(sql, "COMMENT '{comment}'").unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a standard way to escape string

}
}
4 changes: 4 additions & 0 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ impl TableBuilder for PostgresQueryBuilder {
if let ColumnSpec::AutoIncrement = column_spec {
continue;
}
if let ColumnSpec::Comment(_) = column_spec {
continue;
}
write!(sql, " ").unwrap();
self.prepare_column_spec(column_spec, sql);
}
Expand Down Expand Up @@ -167,6 +170,7 @@ impl TableBuilder for PostgresQueryBuilder {
ColumnSpec::Check(check) => self.prepare_check_constraint(check, sql),
ColumnSpec::Generated { .. } => {}
ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(),
ColumnSpec::Comment(_) => {}
}
false
});
Expand Down
3 changes: 3 additions & 0 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ impl TableBuilder for SqliteQueryBuilder {
is_auto_increment = true;
continue;
}
if let ColumnSpec::Comment(_) = column_spec {
continue;
}
write!(sql, " ").unwrap();
self.prepare_column_spec(column_spec, sql);
}
Expand Down
9 changes: 9 additions & 0 deletions src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,23 @@ pub trait TableBuilder:
self.prepare_generated_column(expr, *stored, sql)
}
ColumnSpec::Extra(string) => write!(sql, "{string}").unwrap(),
ColumnSpec::Comment(comment) => self.column_comment(comment, sql),
}
}

/// column comment
fn column_comment(&self, _comment: &str, _sql: &mut dyn SqlWriter) {}

/// The keyword for setting a column to be auto increment.
fn column_spec_auto_increment_keyword(&self) -> &str;

/// Translate [`TableOpt`] into SQL statement.
fn prepare_table_opt(&self, create: &TableCreateStatement, sql: &mut dyn SqlWriter) {
self.prepare_table_opt_def(create, sql)
}
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved

/// Default function
fn prepare_table_opt_def(&self, create: &TableCreateStatement, sql: &mut dyn SqlWriter) {
for table_opt in create.options.iter() {
write!(sql, " ").unwrap();
write!(
Expand Down
10 changes: 10 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub enum ColumnSpec {
Check(SimpleExpr),
Generated { expr: SimpleExpr, stored: bool },
Extra(String),
Comment(String),
}

// All interval fields
Expand Down Expand Up @@ -638,6 +639,15 @@ impl ColumnDef {
self
}

/// MySQL only.
pub fn comment<T>(&mut self, string: T) -> &mut Self
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
where
T: Into<String>,
{
self.spec.push(ColumnSpec::Comment(string.into()));
self
}

pub fn get_column_name(&self) -> String {
self.name.to_string()
}
Expand Down
22 changes: 19 additions & 3 deletions src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use crate::{
/// let table = Table::create()
/// .table(Char::Table)
/// .if_not_exists()
/// .comment("table comment")
/// .col(ColumnDef::new(Char::Id).integer().not_null().auto_increment().primary_key())
/// .col(ColumnDef::new(Char::FontSize).integer().not_null())
/// .col(ColumnDef::new(Char::FontSize).integer().not_null().comment("font size"))
/// .col(ColumnDef::new(Char::Character).string().not_null())
/// .col(ColumnDef::new(Char::SizeW).integer().not_null())
/// .col(ColumnDef::new(Char::SizeH).integer().not_null())
Expand All @@ -36,15 +37,15 @@ use crate::{
/// [
/// r#"CREATE TABLE IF NOT EXISTS `character` ("#,
/// r#"`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,"#,
/// r#"`font_size` int NOT NULL,"#,
/// r#"`font_size` int NOT NULL COMMENT 'font size',"#,
/// r#"`character` varchar(255) NOT NULL,"#,
/// r#"`size_w` int NOT NULL,"#,
/// r#"`size_h` int NOT NULL,"#,
/// r#"`font_id` int DEFAULT NULL,"#,
/// r#"CONSTRAINT `FK_2e303c3a712662f1fc2a4d0aad6`"#,
/// r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)"#,
/// r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
/// r#")"#,
/// r#") COMMENT 'table comment'"#,
/// ].join(" ")
/// );
/// assert_eq!(
Expand Down Expand Up @@ -88,6 +89,7 @@ pub struct TableCreateStatement {
pub(crate) foreign_keys: Vec<ForeignKeyCreateStatement>,
pub(crate) if_not_exists: bool,
pub(crate) check: Vec<SimpleExpr>,
pub(crate) comment: Option<String>,
pub(crate) extra: Option<String>,
}

Expand Down Expand Up @@ -124,6 +126,15 @@ impl TableCreateStatement {
self
}
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved

/// Set table comment
pub fn comment<T>(&mut self, comment: T) -> &mut Self
where
T: Into<String>,
{
self.comment = Some(comment.into());
self
}

/// Add a new table column
pub fn col(&mut self, column: &mut ColumnDef) -> &mut Self {
let mut column = column.take();
Expand Down Expand Up @@ -270,6 +281,10 @@ impl TableCreateStatement {
self.columns.as_ref()
}

pub fn get_comment(&self) -> Option<&String> {
self.comment.as_ref()
}

pub fn get_foreign_key_create_stmts(&self) -> &Vec<ForeignKeyCreateStatement> {
self.foreign_keys.as_ref()
}
Expand Down Expand Up @@ -334,6 +349,7 @@ impl TableCreateStatement {
foreign_keys: std::mem::take(&mut self.foreign_keys),
if_not_exists: self.if_not_exists,
check: std::mem::take(&mut self.check),
comment: std::mem::take(&mut self.comment),
extra: std::mem::take(&mut self.extra),
}
}
Expand Down