|
@@ -3,17 +3,17 @@ use crate::entity::{Entity, Index};
|
|
pub fn sql_for_table<T: Entity>() -> (String, String) {
|
|
pub fn sql_for_table<T: Entity>() -> (String, String) {
|
|
let all_columns = T::columns();
|
|
let all_columns = T::columns();
|
|
|
|
|
|
- let mut columns = vec!["id integer primary key".to_owned()];
|
|
|
|
|
|
+ let mut columns = vec!["`id` integer primary key".to_owned()];
|
|
|
|
|
|
// skip the id column type
|
|
// skip the id column type
|
|
for col in all_columns.iter().skip(1) {
|
|
for col in all_columns.iter().skip(1) {
|
|
columns.push(format!(
|
|
columns.push(format!(
|
|
- "\"{}\" {}{}",
|
|
|
|
|
|
+ "`{}` {}{}",
|
|
col.name(),
|
|
col.name(),
|
|
col.sql_type(),
|
|
col.sql_type(),
|
|
if col.fk_table_name().is_some() {
|
|
if col.fk_table_name().is_some() {
|
|
format!(
|
|
format!(
|
|
- " REFERENCES \"{}\"(\"{}\") ON DELETE CASCADE",
|
|
|
|
|
|
+ " REFERENCES `{}`(`{}`) ON DELETE CASCADE",
|
|
col.fk_table_name().unwrap(),
|
|
col.fk_table_name().unwrap(),
|
|
col.fk_column_name().unwrap()
|
|
col.fk_column_name().unwrap()
|
|
)
|
|
)
|
|
@@ -35,15 +35,15 @@ pub fn sql_for_table<T: Entity>() -> (String, String) {
|
|
|
|
|
|
pub fn sql_for_index<I: Index>() -> (String, String) {
|
|
pub fn sql_for_index<I: Index>() -> (String, String) {
|
|
(
|
|
(
|
|
- format!("DROP INDEX IF EXISTS \"{}\"", I::index_name()),
|
|
|
|
|
|
+ format!("DROP INDEX IF EXISTS `{}`", I::index_name()),
|
|
format!(
|
|
format!(
|
|
- "CREATE {}INDEX \"{}\" ON \"{}\" ({})",
|
|
|
|
|
|
+ "CREATE {}INDEX `{}` ON `{}` ({})",
|
|
if I::unique() { "UNIQUE " } else { "" },
|
|
if I::unique() { "UNIQUE " } else { "" },
|
|
I::index_name(),
|
|
I::index_name(),
|
|
I::table_name(),
|
|
I::table_name(),
|
|
I::column_names()
|
|
I::column_names()
|
|
.iter()
|
|
.iter()
|
|
- .map(|x| format!("\"{}\"", x))
|
|
|
|
|
|
+ .map(|x| format!("`{}`", x))
|
|
.collect::<Vec<_>>()
|
|
.collect::<Vec<_>>()
|
|
.join(",")
|
|
.join(",")
|
|
),
|
|
),
|
|
@@ -73,15 +73,15 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<Empty>(),
|
|
super::sql_for_table::<Empty>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "empty""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "empty" (id integer primary key)"#.to_owned()
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `empty`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `empty` (`id` integer primary key)"#.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<Single>(),
|
|
super::sql_for_table::<Single>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "single""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "single" (id integer primary key,"e" integer)"#
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `single`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `single` (`id` integer primary key,`e` integer)"#
|
|
.to_owned()
|
|
.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
@@ -89,8 +89,8 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<Reference>(),
|
|
super::sql_for_table::<Reference>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "reference""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "reference" (id integer primary key,"e" integer)"#
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `reference`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `reference` (`id` integer primary key,`e` integer)"#
|
|
.to_owned()
|
|
.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
@@ -110,8 +110,8 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<UnitNewtype>(),
|
|
super::sql_for_table::<UnitNewtype>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "unit_newtype""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "unit_newtype" (id integer primary key,"newtype" integer)"#
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `unit_newtype`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `unit_newtype` (`id` integer primary key,`newtype` integer)"#
|
|
.to_owned()
|
|
.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
@@ -131,8 +131,8 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<NonUnitNewtype>(),
|
|
super::sql_for_table::<NonUnitNewtype>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "non_unit_newtype""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "non_unit_newtype" (id integer primary key,"newtype" blob)"#.to_owned()
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `non_unit_newtype`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `non_unit_newtype` (`id` integer primary key,`newtype` blob)"#.to_owned()
|
|
)
|
|
)
|
|
)
|
|
)
|
|
}
|
|
}
|
|
@@ -149,8 +149,8 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<Child>(),
|
|
super::sql_for_table::<Child>(),
|
|
(
|
|
(
|
|
- r#"DROP TABLE IF EXISTS "child""#.to_owned(),
|
|
|
|
- r#"CREATE TABLE IF NOT EXISTS "child" (id integer primary key,"parent_id" integer REFERENCES "single"("id") ON DELETE CASCADE)"#.to_owned()
|
|
|
|
|
|
+ r#"DROP TABLE IF EXISTS `child`"#.to_owned(),
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `child` (`id` integer primary key,`parent_id` integer REFERENCES `single`(`id`) ON DELETE CASCADE)"#.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
}
|
|
}
|
|
@@ -170,15 +170,15 @@ mod test {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_index::<ValueIndex>(),
|
|
super::sql_for_index::<ValueIndex>(),
|
|
(
|
|
(
|
|
- r#"DROP INDEX IF EXISTS "value_index""#.to_owned(),
|
|
|
|
- r#"CREATE INDEX "value_index" ON "key_value" ("value")"#.to_owned()
|
|
|
|
|
|
+ r#"DROP INDEX IF EXISTS `value_index`"#.to_owned(),
|
|
|
|
+ r#"CREATE INDEX `value_index` ON `key_value` (`value`)"#.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_index::<UniqueValueIndex>(),
|
|
super::sql_for_index::<UniqueValueIndex>(),
|
|
(
|
|
(
|
|
- r#"DROP INDEX IF EXISTS "unique_value_index""#.to_owned(),
|
|
|
|
- r#"CREATE UNIQUE INDEX "unique_value_index" ON "key_value" ("value")"#.to_owned()
|
|
|
|
|
|
+ r#"DROP INDEX IF EXISTS `unique_value_index`"#.to_owned(),
|
|
|
|
+ r#"CREATE UNIQUE INDEX `unique_value_index` ON `key_value` (`value`)"#.to_owned()
|
|
)
|
|
)
|
|
)
|
|
)
|
|
}
|
|
}
|
|
@@ -194,7 +194,7 @@ mod test {
|
|
fn test_vec() {
|
|
fn test_vec() {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<VecTest>().1,
|
|
super::sql_for_table::<VecTest>().1,
|
|
- r#"CREATE TABLE IF NOT EXISTS "vec_test" (id integer primary key,"e" integer,"test" blob)"#.to_owned()
|
|
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `vec_test` (`id` integer primary key,`e` integer,`test` blob)"#.to_owned()
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -218,7 +218,7 @@ mod test {
|
|
fn test_enum() {
|
|
fn test_enum() {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
super::sql_for_table::<EnumContainer>().1,
|
|
super::sql_for_table::<EnumContainer>().1,
|
|
- r#"CREATE TABLE IF NOT EXISTS "enum_container" (id integer primary key,"before" integer,"e" text,"after" integer)"#.to_owned()
|
|
|
|
|
|
+ r#"CREATE TABLE IF NOT EXISTS `enum_container` (`id` integer primary key,`before` integer,`e` text,`after` integer)"#.to_owned()
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|