Skip to content

Commit

Permalink
issues-347 Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrivosheev committed Sep 13, 2022
1 parent 9e92589 commit 2a84a16
Show file tree
Hide file tree
Showing 21 changed files with 233 additions and 252 deletions.
2 changes: 1 addition & 1 deletion src/backend/index_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub trait IndexBuilder: QuotedBuilder + TableRefBuilder {
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);

Expand Down
14 changes: 7 additions & 7 deletions src/backend/mysql/foreign_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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,
Expand Down Expand Up @@ -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 dyn SqlWriter) {
match table_ref {
TableRef::Table(_) => self.prepare_table_ref_iden(table_ref, sql),
_ => panic!("Not supported"),
}
}
}
16 changes: 8 additions & 8 deletions src/backend/mysql/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ impl IndexBuilder for MysqlQueryBuilder {
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_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 {
Expand All @@ -48,6 +54,7 @@ impl IndexBuilder for MysqlQueryBuilder {
self.prepare_table_ref_index_stmt(table, sql);
}
}

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) {
Expand Down Expand Up @@ -77,11 +84,4 @@ impl IndexBuilder for MysqlQueryBuilder {
write!(sql, "FULLTEXT ").unwrap();
}
}

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"),
}
}
}
66 changes: 33 additions & 33 deletions src/backend/mysql/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
use super::*;

impl QueryBuilder for MysqlQueryBuilder {
fn prepare_returning(&self, _returning: &Option<ReturningClause>, _sql: &mut dyn SqlWriter) {}
fn values_list_tuple_prefix(&self) -> &str {
"ROW"
}

fn prepare_select_distinct(&self, select_distinct: &SelectDistinct, sql: &mut dyn SqlWriter) {
match select_distinct {
SelectDistinct::All => write!(sql, "ALL").unwrap(),
SelectDistinct::Distinct => write!(sql, "DISTINCT").unwrap(),
SelectDistinct::DistinctRow => write!(sql, "DISTINCTROW").unwrap(),
_ => {}
};
}

fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut dyn SqlWriter) {
query.prepare_statement(self, sql);
}

fn prepare_with_clause_recursive_options(&self, _: &WithClause, _: &mut dyn SqlWriter) {
// MySQL doesn't support sql recursive with query 'SEARCH' and 'CYCLE' options.
}

fn prepare_with_query_clause_materialization(
&self,
_: &CommonTableExpression,
_: &mut dyn SqlWriter,
) {
// MySQL doesn't support declaring materialization in SQL for with query.
}

fn prepare_order_expr(&self, order_expr: &OrderExpr, sql: &mut dyn SqlWriter) {
match order_expr.nulls {
Expand All @@ -17,25 +44,13 @@ impl QueryBuilder for MysqlQueryBuilder {
}
if !matches!(order_expr.order, Order::Field(_)) {
self.prepare_simple_expr(&order_expr.expr, sql);
write!(sql, " ").unwrap();
}
write!(sql, " ").unwrap();
self.prepare_order(order_expr, sql);
}

fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut dyn SqlWriter) {
query.prepare_statement(self, sql);
}

fn prepare_with_clause_recursive_options(&self, _: &WithClause, _: &mut dyn SqlWriter) {
// MySQL doesn't support sql recursive with query 'SEARCH' and 'CYCLE' options.
}

fn prepare_with_query_clause_materialization(
&self,
_: &CommonTableExpression,
_: &mut dyn SqlWriter,
) {
// MySQL doesn't support declaring materialization in SQL for with query.
fn prepare_value(&self, value: &Value, sql: &mut dyn SqlWriter) {
sql.push_param(value.clone(), self as _);
}

fn prepare_on_conflict_target(&self, _: &Option<OnConflictTarget>, _: &mut dyn SqlWriter) {
Expand All @@ -56,24 +71,9 @@ impl QueryBuilder for MysqlQueryBuilder {
write!(sql, ")").unwrap();
}

fn prepare_returning(&self, _returning: &Option<ReturningClause>, _sql: &mut dyn SqlWriter) {}

fn insert_default_keyword(&self) -> &str {
"()"
}

fn prepare_select_distinct(&self, select_distinct: &SelectDistinct, sql: &mut dyn SqlWriter) {
match select_distinct {
SelectDistinct::All => write!(sql, "ALL").unwrap(),
SelectDistinct::Distinct => write!(sql, "DISTINCT").unwrap(),
SelectDistinct::DistinctRow => write!(sql, "DISTINCTROW").unwrap(),
_ => {}
};
}

fn values_list_tuple_prefix(&self) -> &str {
"ROW"
}

fn prepare_value(&self, value: &Value, sql: &mut dyn SqlWriter) {
sql.push_param(value.clone(), self as _);
}
}
13 changes: 2 additions & 11 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,8 @@ impl TableBuilder for MysqlQueryBuilder {
}
}

fn prepare_column_spec(&self, column_spec: &ColumnSpec, sql: &mut dyn SqlWriter) {
match column_spec {
ColumnSpec::Null => write!(sql, "NULL"),
ColumnSpec::NotNull => write!(sql, "NOT NULL"),
ColumnSpec::Default(value) => write!(sql, "DEFAULT {}", self.value_to_string(value)),
ColumnSpec::AutoIncrement => write!(sql, "AUTO_INCREMENT"),
ColumnSpec::UniqueKey => write!(sql, "UNIQUE"),
ColumnSpec::PrimaryKey => write!(sql, "PRIMARY KEY"),
ColumnSpec::Extra(string) => write!(sql, "{}", string),
}
.unwrap()
fn column_spec_auto_increment_keyword(&self) -> &str {
"AUTO_INCREMENT"
}

fn prepare_table_alter_statement(&self, alter: &TableAlterStatement, sql: &mut dyn SqlWriter) {
Expand Down
20 changes: 10 additions & 10 deletions src/backend/postgres/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ impl IndexBuilder for PostgresQueryBuilder {
}

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

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

fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter) {
match table_ref {
TableRef::Table(_) | TableRef::SchemaTable(_, _) => {
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(table) = &drop.table {
Expand Down Expand Up @@ -83,13 +92,4 @@ impl IndexBuilder for PostgresQueryBuilder {
write!(sql, "UNIQUE ").unwrap();
}
}

fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut dyn SqlWriter) {
match table_ref {
TableRef::Table(_) | TableRef::SchemaTable(_, _) => {
self.prepare_table_ref_iden(table_ref, sql)
}
_ => panic!("Not supported"),
}
}
}
80 changes: 40 additions & 40 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@ impl QueryBuilder for PostgresQueryBuilder {
("$", true)
}

fn if_null_function(&self) -> &str {
"COALESCE"
fn prepare_simple_expr(&self, simple_expr: &SimpleExpr, sql: &mut dyn SqlWriter) {
match simple_expr {
SimpleExpr::AsEnum(type_name, expr) => {
let simple_expr = expr.clone().cast_as(SeaRc::clone(type_name));
self.prepare_simple_expr_common(&simple_expr, sql);
}
_ => QueryBuilder::prepare_simple_expr_common(self, simple_expr, sql),
}
}

fn write_string_quoted(&self, string: &str, buffer: &mut String) {
let escaped = self.escape_string(string);
let string = if escaped.find('\\').is_some() {
"E'".to_owned() + &escaped + "'"
} else {
"'".to_owned() + &escaped + "'"
fn prepare_select_distinct(&self, select_distinct: &SelectDistinct, sql: &mut dyn SqlWriter) {
match select_distinct {
SelectDistinct::All => write!(sql, "ALL").unwrap(),
SelectDistinct::Distinct => write!(sql, "DISTINCT").unwrap(),
SelectDistinct::DistinctOn(cols) => {
write!(sql, "DISTINCT ON (").unwrap();
cols.iter().fold(true, |first, c| {
if !first {
write!(sql, ", ").unwrap();
}
c.prepare(sql.as_writer(), self.quote());
false
});
write!(sql, ")").unwrap();
}
_ => {}
};
write!(buffer, "{}", string).unwrap()
}

fn prepare_bin_oper(&self, bin_oper: &BinOper, sql: &mut dyn SqlWriter) {
Expand All @@ -30,6 +45,10 @@ impl QueryBuilder for PostgresQueryBuilder {
}
}

fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut dyn SqlWriter) {
query.prepare_statement(self, sql);
}

fn prepare_function(&self, function: &Function, sql: &mut dyn SqlWriter) {
match function {
Function::PgFunction(function) => write!(
Expand All @@ -56,21 +75,11 @@ impl QueryBuilder for PostgresQueryBuilder {
}
}

fn prepare_simple_expr(&self, simple_expr: &SimpleExpr, sql: &mut dyn SqlWriter) {
match simple_expr {
SimpleExpr::AsEnum(type_name, expr) => {
let simple_expr = expr.clone().cast_as(SeaRc::clone(type_name));
self.prepare_simple_expr_common(&simple_expr, sql);
}
_ => QueryBuilder::prepare_simple_expr_common(self, simple_expr, sql),
}
}

fn prepare_order_expr(&self, order_expr: &OrderExpr, sql: &mut dyn SqlWriter) {
if !matches!(order_expr.order, Order::Field(_)) {
self.prepare_simple_expr(&order_expr.expr, sql);
write!(sql, " ").unwrap();
}
write!(sql, " ").unwrap();
self.prepare_order(order_expr, sql);
match order_expr.nulls {
None => (),
Expand All @@ -79,30 +88,21 @@ impl QueryBuilder for PostgresQueryBuilder {
}
}

fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut dyn SqlWriter) {
query.prepare_statement(self, sql);
fn prepare_value(&self, value: &Value, sql: &mut dyn SqlWriter) {
sql.push_param(value.clone(), self as _);
}

fn prepare_select_distinct(&self, select_distinct: &SelectDistinct, sql: &mut dyn SqlWriter) {
match select_distinct {
SelectDistinct::All => write!(sql, "ALL").unwrap(),
SelectDistinct::Distinct => write!(sql, "DISTINCT").unwrap(),
SelectDistinct::DistinctOn(cols) => {
write!(sql, "DISTINCT ON (").unwrap();
cols.iter().fold(true, |first, c| {
if !first {
write!(sql, ", ").unwrap();
}
c.prepare(sql.as_writer(), self.quote());
false
});
write!(sql, ")").unwrap();
}
_ => {}
fn write_string_quoted(&self, string: &str, buffer: &mut String) {
let escaped = self.escape_string(string);
let string = if escaped.find('\\').is_some() {
"E'".to_owned() + &escaped + "'"
} else {
"'".to_owned() + &escaped + "'"
};
write!(buffer, "{}", string).unwrap()
}

fn prepare_value(&self, value: &Value, sql: &mut dyn SqlWriter) {
sql.push_param(value.clone(), self as _);
fn if_null_function(&self) -> &str {
"COALESCE"
}
}
17 changes: 3 additions & 14 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,8 @@ impl TableBuilder for PostgresQueryBuilder {
.unwrap()
}

fn prepare_column_spec(&self, column_spec: &ColumnSpec, sql: &mut dyn SqlWriter) {
match column_spec {
ColumnSpec::Null => write!(sql, "NULL"),
ColumnSpec::NotNull => write!(sql, "NOT NULL"),
ColumnSpec::Default(value) => {
write!(sql, "DEFAULT {}", self.value_to_string(value))
}
ColumnSpec::AutoIncrement => write!(sql, ""),
ColumnSpec::UniqueKey => write!(sql, "UNIQUE"),
ColumnSpec::PrimaryKey => write!(sql, "PRIMARY KEY"),
ColumnSpec::Extra(string) => write!(sql, "{}", string),
}
.unwrap()
fn column_spec_auto_increment_keyword(&self) -> &str {
""
}

fn prepare_table_alter_statement(&self, alter: &TableAlterStatement, sql: &mut dyn SqlWriter) {
Expand Down Expand Up @@ -161,7 +150,7 @@ impl TableBuilder for PostgresQueryBuilder {
if column_def.types.is_some() {
write!(sql, ", ALTER COLUMN ").unwrap();
} else {
write!(sql, " ALTER COLUMN ").unwrap();
write!(sql, "ALTER COLUMN ").unwrap();
}
column_def.name.prepare(sql.as_writer(), self.quote());
match column_spec {
Expand Down
Loading

0 comments on commit 2a84a16

Please sign in to comment.