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

Cleanup IndexBuilder trait methods #426

Merged
merged 4 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions src/backend/index_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,22 @@ use crate::*;

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) {
self.prepare_index_prefix(create, sql);
write!(sql, "KEY ").unwrap();

self.prepare_index_name(&create.index.name, sql);
if create.index.name.is_some() {
write!(sql, "CONSTRAINT ").unwrap();
self.prepare_index_name(&create.index.name, sql);
write!(sql, " ").unwrap();
}

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

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

/// Translate [`IndexCreateStatement`] into SQL statement.
fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
write!(sql, "CREATE ").unwrap();
self.prepare_index_prefix(create, sql);
write!(sql, "INDEX ").unwrap();

self.prepare_index_name(&create.index.name, sql);

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

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

self.prepare_index_columns(&create.index.columns, sql);
}
fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter);

/// Translate [`TableRef`] into SQL statement.
fn prepare_table_ref_index_stmt(&self, table_ref: &TableRef, sql: &mut SqlWriter);
Expand Down
12 changes: 0 additions & 12 deletions src/backend/postgres/index.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
use super::*;

impl IndexBuilder for PostgresQueryBuilder {
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();
}

self.prepare_index_prefix(create, sql);

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

fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
write!(sql, "CREATE ").unwrap();
self.prepare_index_prefix(create, sql);
Expand Down
11 changes: 0 additions & 11 deletions src/backend/sqlite/index.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
use super::*;

impl IndexBuilder for SqliteQueryBuilder {
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);

self.prepare_index_prefix(create, sql);

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

fn prepare_index_create_statement(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
write!(sql, "CREATE ").unwrap();
self.prepare_index_prefix(create, sql);
Expand Down
53 changes: 53 additions & 0 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,59 @@ fn create_with_primary_unique_index() {
);
}

#[test]
fn create_with_unique_index_constraint() {
assert_eq!(
Table::create()
.table(Char::Table)
.if_not_exists()
.col(
ColumnDef::new(Char::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Char::FontSize).integer().not_null())
.col(ColumnDef::new(Char::Character).string().not_null())
.col(ColumnDef::new(Char::SizeW).integer().not_null())
.col(ColumnDef::new(Char::SizeH).integer().not_null())
.col(
ColumnDef::new(Char::FontId)
.integer()
.default(Value::Int(None)),
)
.foreign_key(
ForeignKey::create()
.from(Char::Table, Char::FontId)
.to(Font::Table, Font::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("idx-sizehw")
.table(Char::Table)
.col(Char::SizeH)
.col(Char::SizeW)
.unique(),
)
.to_string(SqliteQueryBuilder),
vec![
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" text NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
r#"CONSTRAINT "idx-sizehw" UNIQUE ("size_h", "size_w"),"#,
r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id") ON DELETE CASCADE ON UPDATE CASCADE"#,
r#")"#,
].join(" ")
);
}

#[test]
fn drop_1() {
assert_eq!(
Expand Down