Skip to content

Commit

Permalink
Merge pull request #436 from ikrivosheev/feature/issues-347_sqlwriter…
Browse files Browse the repository at this point in the history
…_trait

issues-347 Add SqlWriter trait
  • Loading branch information
tyt2y3 authored Sep 18, 2022
2 parents 94831a1 + c9c0bef commit 6cf7932
Show file tree
Hide file tree
Showing 40 changed files with 813 additions and 1,301 deletions.
12 changes: 6 additions & 6 deletions src/backend/foreign_key_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait ForeignKeyBuilder: QuotedBuilder + TableRefBuilder {
fn prepare_foreign_key_create_statement(
&self,
create: &ForeignKeyCreateStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
) {
self.prepare_foreign_key_create_statement_internal(create, sql, Mode::Alter)
}
Expand All @@ -21,7 +21,7 @@ pub trait ForeignKeyBuilder: QuotedBuilder + TableRefBuilder {
fn prepare_foreign_key_drop_statement(
&self,
drop: &ForeignKeyDropStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
) {
self.prepare_foreign_key_drop_statement_internal(drop, sql, Mode::Alter)
}
Expand All @@ -30,7 +30,7 @@ pub trait ForeignKeyBuilder: QuotedBuilder + TableRefBuilder {
fn prepare_foreign_key_action(
&self,
foreign_key_action: &ForeignKeyAction,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
) {
write!(
sql,
Expand All @@ -47,14 +47,14 @@ pub trait ForeignKeyBuilder: QuotedBuilder + TableRefBuilder {
}

/// Translate [`TableRef`] into SQL statement.
fn prepare_table_ref_fk_stmt(&self, table_ref: &TableRef, sql: &mut SqlWriter);
fn prepare_table_ref_fk_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter);

#[doc(hidden)]
/// Internal function to factor foreign key drop in table and outside.
fn prepare_foreign_key_drop_statement_internal(
&self,
drop: &ForeignKeyDropStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
mode: Mode,
);

Expand All @@ -63,7 +63,7 @@ pub trait ForeignKeyBuilder: QuotedBuilder + TableRefBuilder {
fn prepare_foreign_key_create_statement_internal(
&self,
create: &ForeignKeyCreateStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
mode: Mode,
);
}
42 changes: 20 additions & 22 deletions src/backend/index_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ pub trait IndexBuilder: QuotedBuilder + TableRefBuilder {
/// Translate [`IndexCreateStatement`] into SQL expression.
/// This is the default implementation for `PostgresQueryBuilder` and `SqliteQueryBuilder`.
/// `MysqlQueryBuilder` overrides this default implementation.
fn prepare_table_index_expression(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
if create.index.name.is_some() {
write!(sql, "CONSTRAINT ").unwrap();
self.prepare_index_name(&create.index.name, sql);
write!(sql, " ").unwrap();
fn prepare_table_index_expression(
&self,
create: &IndexCreateStatement,
sql: &mut dyn SqlWriter,
) {
if let Some(name) = &create.index.name {
write!(sql, "CONSTRAINT {}{}{} ", self.quote(), name, self.quote()).unwrap();
}

self.prepare_index_prefix(create, sql);
Expand All @@ -17,39 +19,43 @@ pub trait IndexBuilder: QuotedBuilder + TableRefBuilder {
}

/// Translate [`IndexCreateStatement`] into SQL statement.
fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter);
fn prepare_index_create_statement(
&self,
create: &IndexCreateStatement,
sql: &mut dyn SqlWriter,
);

/// Translate [`TableRef`] into SQL statement.
fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut SqlWriter);
fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter);

/// Translate [`IndexDropStatement`] into SQL statement.
fn prepare_index_drop_statement(&self, drop: &IndexDropStatement, sql: &mut SqlWriter);
fn prepare_index_drop_statement(&self, drop: &IndexDropStatement, sql: &mut dyn SqlWriter);

#[doc(hidden)]
/// Write the index type (Btree, hash, ...).
fn prepare_index_type(&self, _col_index_type: &Option<IndexType>, _sql: &mut SqlWriter) {}
fn prepare_index_type(&self, _col_index_type: &Option<IndexType>, _sql: &mut dyn SqlWriter) {}

#[doc(hidden)]
/// Write the index prefix (primary, unique, ...).
fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut SqlWriter);
fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut dyn SqlWriter);

#[doc(hidden)]
/// Write the column index prefix.
fn write_column_index_prefix(&self, col_prefix: &Option<u32>, sql: &mut SqlWriter) {
fn write_column_index_prefix(&self, col_prefix: &Option<u32>, sql: &mut dyn SqlWriter) {
if let Some(prefix) = col_prefix {
write!(sql, " ({})", prefix).unwrap();
}
}

#[doc(hidden)]
/// Write the column index prefix.
fn prepare_index_columns(&self, columns: &[IndexColumn], sql: &mut SqlWriter) {
write!(sql, " (").unwrap();
fn prepare_index_columns(&self, columns: &[IndexColumn], sql: &mut dyn SqlWriter) {
write!(sql, "(").unwrap();
columns.iter().fold(true, |first, col| {
if !first {
write!(sql, ", ").unwrap();
}
col.name.prepare(sql, self.quote());
col.name.prepare(sql.as_writer(), self.quote());
self.write_column_index_prefix(&col.prefix, sql);
if let Some(order) = &col.order {
match order {
Expand All @@ -61,12 +67,4 @@ pub trait IndexBuilder: QuotedBuilder + TableRefBuilder {
});
write!(sql, ")").unwrap();
}

#[doc(hidden)]
/// Write index name.
fn prepare_index_name(&self, name: &Option<String>, sql: &mut SqlWriter) {
if let Some(name) = name {
write!(sql, "{}{}{}", self.quote(), name, self.quote()).unwrap();
}
}
}
24 changes: 12 additions & 12 deletions src/backend/mysql/foreign_key.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use super::*;

impl ForeignKeyBuilder for MysqlQueryBuilder {
fn prepare_table_ref_fk_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter) {
match table_ref {
TableRef::Table(_) => self.prepare_table_ref_iden(table_ref, sql),
_ => panic!("Not supported"),
}
}

fn prepare_foreign_key_drop_statement_internal(
&self,
drop: &ForeignKeyDropStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
mode: Mode,
) {
if mode == Mode::Alter {
Expand All @@ -24,7 +31,7 @@ impl ForeignKeyBuilder for MysqlQueryBuilder {
fn prepare_foreign_key_create_statement_internal(
&self,
create: &ForeignKeyCreateStatement,
sql: &mut SqlWriter,
sql: &mut dyn SqlWriter,
mode: Mode,
) {
if mode == Mode::Alter {
Expand All @@ -45,12 +52,12 @@ impl ForeignKeyBuilder for MysqlQueryBuilder {
}
write!(sql, " FOREIGN KEY ").unwrap();

write!(sql, " (").unwrap();
write!(sql, "(").unwrap();
create.foreign_key.columns.iter().fold(true, |first, col| {
if !first {
write!(sql, ", ").unwrap();
}
col.prepare(sql, self.quote());
col.prepare(sql.as_writer(), self.quote());
false
});
write!(sql, ")").unwrap();
Expand All @@ -70,7 +77,7 @@ impl ForeignKeyBuilder for MysqlQueryBuilder {
if !first {
write!(sql, ", ").unwrap();
}
col.prepare(sql, self.quote());
col.prepare(sql.as_writer(), self.quote());
false
});
write!(sql, ")").unwrap();
Expand All @@ -85,11 +92,4 @@ impl ForeignKeyBuilder for MysqlQueryBuilder {
self.prepare_foreign_key_action(foreign_key_action, sql);
}
}

fn prepare_table_ref_fk_stmt(&self, table_ref: &TableRef, sql: &mut SqlWriter) {
match table_ref {
TableRef::Table(_) => self.prepare_table_ref_iden(table_ref, sql),
_ => panic!("Not supported"),
}
}
}
60 changes: 40 additions & 20 deletions src/backend/mysql/index.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
use super::*;

impl IndexBuilder for MysqlQueryBuilder {
fn prepare_table_index_expression(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
fn prepare_table_index_expression(
&self,
create: &IndexCreateStatement,
sql: &mut dyn SqlWriter,
) {
self.prepare_index_prefix(create, sql);
write!(sql, "KEY ").unwrap();
write!(sql, " KEY ").unwrap();

self.prepare_index_name(&create.index.name, sql);
if let Some(name) = &create.index.name {
write!(sql, "{}{}{} ", self.quote(), name, self.quote()).unwrap();
}

self.prepare_index_type(&create.index_type, sql);
if matches!(create.index_type, Some(IndexType::FullText)) {
write!(sql, " ").unwrap();
}

self.prepare_index_columns(&create.index.columns, sql);
}

fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
fn prepare_index_create_statement(
&self,
create: &IndexCreateStatement,
sql: &mut dyn SqlWriter,
) {
write!(sql, "CREATE ").unwrap();
self.prepare_index_prefix(create, sql);
write!(sql, "INDEX ").unwrap();
if create.unique || create.primary || matches!(create.index_type, Some(IndexType::FullText))
{
write!(sql, " INDEX ").unwrap();
} else {
write!(sql, "INDEX ").unwrap();
}

self.prepare_index_name(&create.index.name, sql);
if let Some(name) = &create.index.name {
write!(sql, "{}{}{}", self.quote(), name, self.quote()).unwrap();
}

write!(sql, " ON ").unwrap();
if let Some(table) = &create.table {
self.prepare_table_ref_index_stmt(table, sql);
}

write!(sql, " ").unwrap();
self.prepare_index_columns(&create.index.columns, sql);

self.prepare_index_type(&create.index_type, sql);
}

fn prepare_index_drop_statement(&self, drop: &IndexDropStatement, sql: &mut SqlWriter) {
fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter) {
match table_ref {
TableRef::Table(_) => self.prepare_table_ref_iden(table_ref, sql),
_ => panic!("Not supported"),
}
}
fn prepare_index_drop_statement(&self, drop: &IndexDropStatement, sql: &mut dyn SqlWriter) {
write!(sql, "DROP INDEX ").unwrap();
if let Some(name) = &drop.index.name {
write!(sql, "`{}`", name).unwrap();
Expand All @@ -40,7 +66,8 @@ impl IndexBuilder for MysqlQueryBuilder {
self.prepare_table_ref_index_stmt(table, sql);
}
}
fn prepare_index_type(&self, col_index_type: &Option<IndexType>, sql: &mut SqlWriter) {

fn prepare_index_type(&self, col_index_type: &Option<IndexType>, sql: &mut dyn SqlWriter) {
if let Some(index_type) = col_index_type {
if !matches!(index_type, IndexType::FullText) {
write!(
Expand All @@ -58,22 +85,15 @@ impl IndexBuilder for MysqlQueryBuilder {
}
}

fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut dyn SqlWriter) {
if create.primary {
write!(sql, "PRIMARY ").unwrap();
write!(sql, "PRIMARY").unwrap();
}
if create.unique {
write!(sql, "UNIQUE ").unwrap();
write!(sql, "UNIQUE").unwrap();
}
if matches!(create.index_type, Some(IndexType::FullText)) {
write!(sql, "FULLTEXT ").unwrap();
}
}

fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut SqlWriter) {
match table_ref {
TableRef::Table(_) => self.prepare_table_ref_iden(table_ref, sql),
_ => panic!("Not supported"),
write!(sql, "FULLTEXT").unwrap();
}
}
}
Loading

0 comments on commit 6cf7932

Please sign in to comment.