|
@@ -132,7 +132,7 @@ impl<'de> serde::de::SeqAccess<'de> for CreateDeserializer<'de> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-pub fn sql_for<T: crate::model::Entity>() -> (String, String) {
|
|
|
|
|
|
+pub fn sql_for_table<T: crate::model::Entity>() -> (String, String) {
|
|
let elength = Rc::new(Cell::new(0));
|
|
let elength = Rc::new(Cell::new(0));
|
|
|
|
|
|
let mut cd = CreateDeserializer {
|
|
let mut cd = CreateDeserializer {
|
|
@@ -186,6 +186,27 @@ pub fn sql_for<T: crate::model::Entity>() -> (String, String) {
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+pub fn sql_for_index<
|
|
|
|
+ I: super::Index<T>,
|
|
|
|
+ T: super::Entity<Column = C>,
|
|
|
|
+ C: super::EntityColumns<Entity = T>,
|
|
|
|
+>() -> (String, String) {
|
|
|
|
+ (
|
|
|
|
+ format!("DROP INDEX IF EXISTS \"{}\"", I::index_name()),
|
|
|
|
+ format!(
|
|
|
|
+ "CREATE {}INDEX \"{}\" ON \"{}\" ({})",
|
|
|
|
+ if I::unique() { "UNIQUE " } else { "" },
|
|
|
|
+ I::index_name(),
|
|
|
|
+ T::table_name(),
|
|
|
|
+ I::columns()
|
|
|
|
+ .iter()
|
|
|
|
+ .map(|x| format!("\"{}\"", T::name(x.clone())))
|
|
|
|
+ .collect::<Vec<_>>()
|
|
|
|
+ .join(",")
|
|
|
|
+ ),
|
|
|
|
+ )
|
|
|
|
+}
|
|
|
|
+
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
mod test {
|
|
mod test {
|
|
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
@@ -207,14 +228,14 @@ mod test {
|
|
#[test]
|
|
#[test]
|
|
fn example_sql_for() {
|
|
fn example_sql_for() {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
- super::sql_for::<Empty>(),
|
|
|
|
|
|
+ super::sql_for_table::<Empty>(),
|
|
(
|
|
(
|
|
r#"DROP TABLE IF EXISTS "empty""#.to_owned(),
|
|
r#"DROP TABLE IF EXISTS "empty""#.to_owned(),
|
|
r#"CREATE TABLE IF NOT EXISTS "empty" (id integer primary key)"#.to_owned()
|
|
r#"CREATE TABLE IF NOT EXISTS "empty" (id integer primary key)"#.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
assert_eq!(
|
|
assert_eq!(
|
|
- super::sql_for::<Single>(),
|
|
|
|
|
|
+ super::sql_for_table::<Single>(),
|
|
(
|
|
(
|
|
r#"DROP TABLE IF EXISTS "single""#.to_owned(),
|
|
r#"DROP TABLE IF EXISTS "single""#.to_owned(),
|
|
r#"CREATE TABLE IF NOT EXISTS "single" (id integer primary key,"e" integer)"#
|
|
r#"CREATE TABLE IF NOT EXISTS "single" (id integer primary key,"e" integer)"#
|
|
@@ -223,7 +244,7 @@ mod test {
|
|
);
|
|
);
|
|
|
|
|
|
assert_eq!(
|
|
assert_eq!(
|
|
- super::sql_for::<Reference>(),
|
|
|
|
|
|
+ super::sql_for_table::<Reference>(),
|
|
(
|
|
(
|
|
r#"DROP TABLE IF EXISTS "reference""#.to_owned(),
|
|
r#"DROP TABLE IF EXISTS "reference""#.to_owned(),
|
|
r#"CREATE TABLE IF NOT EXISTS "reference" (id integer primary key,"e" integer)"#
|
|
r#"CREATE TABLE IF NOT EXISTS "reference" (id integer primary key,"e" integer)"#
|
|
@@ -244,7 +265,7 @@ mod test {
|
|
#[test]
|
|
#[test]
|
|
fn unit_newtype_struct() {
|
|
fn unit_newtype_struct() {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
- super::sql_for::<UnitNewtype>(),
|
|
|
|
|
|
+ super::sql_for_table::<UnitNewtype>(),
|
|
(
|
|
(
|
|
r#"DROP TABLE IF EXISTS "unit_newtype""#.to_owned(),
|
|
r#"DROP TABLE IF EXISTS "unit_newtype""#.to_owned(),
|
|
r#"CREATE TABLE IF NOT EXISTS "unit_newtype" (id integer primary key,"newtype" integer)"#
|
|
r#"CREATE TABLE IF NOT EXISTS "unit_newtype" (id integer primary key,"newtype" integer)"#
|
|
@@ -265,7 +286,7 @@ mod test {
|
|
#[test]
|
|
#[test]
|
|
#[should_panic]
|
|
#[should_panic]
|
|
fn nonunit_newtype_struct() {
|
|
fn nonunit_newtype_struct() {
|
|
- super::sql_for::<NonUnitNewtype>();
|
|
|
|
|
|
+ super::sql_for_table::<NonUnitNewtype>();
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
#[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
@@ -278,11 +299,39 @@ mod test {
|
|
#[test]
|
|
#[test]
|
|
fn test_foreign_key() {
|
|
fn test_foreign_key() {
|
|
assert_eq!(
|
|
assert_eq!(
|
|
- super::sql_for::<Child>(),
|
|
|
|
|
|
+ super::sql_for_table::<Child>(),
|
|
(
|
|
(
|
|
r#"DROP TABLE IF EXISTS "child""#.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"))"#.to_owned()
|
|
r#"CREATE TABLE IF NOT EXISTS "child" (id integer primary key,"parent_id" integer references "single"("id"))"#.to_owned()
|
|
)
|
|
)
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ #[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
|
|
+ #[microrm_internal]
|
|
|
|
+ pub struct KeyValue {
|
|
|
|
+ key: String,
|
|
|
|
+ value: String,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ microrm_macros::make_index_internal!(ValueIndex, KeyValueColumns::Value);
|
|
|
|
+ microrm_macros::make_index_internal!(!UniqueValueIndex, KeyValueColumns::Value);
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn test_indexes() {
|
|
|
|
+ assert_eq!(
|
|
|
|
+ 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()
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+ assert_eq!(
|
|
|
|
+ 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()
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
+ }
|
|
}
|
|
}
|