use openehr_store::schema::Table;
use openehr_store::{ColTy, Dialect, Placeholder};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SqliteDialect;
impl Dialect for SqliteDialect {
fn name(&self) -> &'static str {
"SQLite"
}
#[allow(clippy::match_same_arms)]
fn col_sql(&self, ty: ColTy) -> String {
match ty {
ColTy::Id(_) | ColTy::Text(_) | ColTy::LongText | ColTy::Instant => "TEXT",
ColTy::Json => "TEXT",
ColTy::InstantUtc | ColTy::Int => "INTEGER",
ColTy::Bool => "INTEGER",
ColTy::Digest => "BLOB",
}
.to_owned()
}
fn quote(&self, identifier: &str) -> String {
format!("\"{}\"", identifier.replace('"', "\"\""))
}
fn placeholder(&self) -> Placeholder {
Placeholder::Question
}
fn append_only_sql(&self, table: &Table) -> Vec<String> {
let name = self.quote(table.name);
["UPDATE", "DELETE"]
.into_iter()
.map(|op| {
format!(
"CREATE TRIGGER IF NOT EXISTS {} BEFORE {op} ON {name} BEGIN \
SELECT RAISE(ABORT, '{} is append-only (openEHR V8.10)'); END",
self.quote(&format!("trg_{}_no_{}", table.name, op.to_lowercase())),
table.name
)
})
.collect()
}
}