Skip to content

Commit

Permalink
Merge pull request SeaQL#227 from 05storm26/master
Browse files Browse the repository at this point in the history
Fix UNIQUE table index expression syntax for sqlite
  • Loading branch information
tyt2y3 authored Mar 13, 2022
2 parents f5bd7ac + 5cc8c5b commit 66fdf06
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/backend/sqlite/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ impl IndexBuilder for SqliteQueryBuilder {
self.prepare_index_name(&create.index.name, sql);

self.prepare_index_prefix(create, sql);
write!(sql, "KEY ").unwrap();

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

self.prepare_index_columns(&create.index.columns, sql);
}
Expand All @@ -27,8 +24,6 @@ impl IndexBuilder for SqliteQueryBuilder {
table.prepare(sql, self.quote());
}

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

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

Expand All @@ -49,9 +44,8 @@ impl IndexBuilder for SqliteQueryBuilder {

fn prepare_index_prefix(&self, create: &IndexCreateStatement, sql: &mut SqlWriter) {
if create.primary {
write!(sql, "PRIMARY ").unwrap();
}
if create.unique {
write!(sql, "PRIMARY KEY ").unwrap();
} else if create.unique {
write!(sql, "UNIQUE ").unwrap();
}
}
Expand Down
92 changes: 92 additions & 0 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,98 @@ fn create_3() {
);
}

#[test]
fn create_with_unique_index() {
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().unique().col(Char::SizeH).col(Char::SizeW))
.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#"UNIQUE ("size_h", "size_w"),"#,
r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id") ON DELETE CASCADE ON UPDATE CASCADE"#,
r#")"#,
]
.join(" ")
);
}

#[test]
fn create_with_primary_unique_index() {
assert_eq!(
Table::create()
.table(Char::Table)
.if_not_exists()
.col(
ColumnDef::new(Char::Id)
.integer()
.not_null()
)
.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().unique().primary().col(Char::SizeH).col(Char::SizeW))
.to_string(SqliteQueryBuilder),
vec![
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL,"#,
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#"PRIMARY KEY ("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

0 comments on commit 66fdf06

Please sign in to comment.