use super::{SqliteQueryBuilder, json};
use crate::backend::bound_sql::bound;
use crate::backend::dialect::{
ArrayOperator, BoundSql, JsonContainment, JsonExistence, JsonPathStep, LONG_LIST, SqlDialect,
all_elements, any_element, array_contained_by, builder_dialect, document, existence_steps,
in_list, json_path_text, json_text, negated, unknown_when_null,
};
use crate::{Expr, Value, error::RenderError};
const ELEMENT: &str = "tideorm_element";
impl SqlDialect for SqliteQueryBuilder {
builder_dialect!(SqliteQueryBuilder);
fn json_equals(&self, column: &str, value: &serde_json::Value, negate: bool) -> BoundSql {
let equal = json::sqlite_json_equals(column, value);
if negate {
bound!(format!("({column} IS NOT NULL AND NOT "), equal, ")")
} else {
equal
}
}
fn json_containment(
&self,
column: &str,
value: &serde_json::Value,
containment: JsonContainment,
) -> BoundSql {
match containment {
JsonContainment::Contains => json::sqlite_json_contains(column, value),
JsonContainment::ContainedBy => json::sqlite_json_contained_by(column, value),
}
}
fn json_exists(
&self,
column: &str,
existence: JsonExistence,
target: &str,
negate: bool,
) -> Result<BoundSql, RenderError> {
let steps = existence_steps(existence, target, "SQLite")?;
let sql = unknown_when_null(
column,
bound!(
format!("json_type({column}, "),
Value::String(Some(json_path_text(&steps))),
") IS NOT NULL"
),
);
Ok(negated(sql, negate))
}
fn array_predicate(
&self,
column: &str,
operator: ArrayOperator,
elements: Vec<Value>,
_null_allowed: bool,
) -> BoundSql {
let checks: Vec<BoundSql> = elements
.iter()
.map(|element| json::sqlite_json_element_equals(ELEMENT, &document(element)))
.collect();
let rows = format!("json_each({column}) AS {ELEMENT}");
let has_elements = |checks: Vec<BoundSql>| {
checks
.into_iter()
.map(|equal| bound!(format!("EXISTS (SELECT 1 FROM {rows} WHERE "), equal, ")"))
.collect()
};
let sql = match operator {
ArrayOperator::Contains => all_elements(column, has_elements(checks)),
ArrayOperator::Overlaps => any_element(has_elements(checks)),
ArrayOperator::ContainedBy => {
let offending = (!checks.is_empty())
.then(|| bound!("NOT (", BoundSql::join(checks, " OR "), ")"));
array_contained_by(column, &rows, offending)
}
};
bound!(format!("(json_type({column}) = 'array' AND ("), sql, "))")
}
fn array_append(&self, column: &str, element: Value) -> BoundSql {
let text = json_text(&document(&element));
bound!(
format!("CASE WHEN {column} IS NULL THEN json_array(json("),
text.clone(),
format!(
")) WHEN json_type({column}) = 'array' THEN json_insert({column}, '$[#]', json("
),
text.clone(),
format!(")) ELSE json_array(json({column}), json("),
text,
")) END"
)
}
fn array_remove(&self, column: &str, source: &str, element: Value) -> BoundSql {
let equal = json::sqlite_json_element_equals(ELEMENT, &document(&element));
let kept = format!("json({ELEMENT}.json -> {ELEMENT}.fullkey)");
bound!(
format!(
"CASE WHEN {column} IS NULL THEN NULL WHEN json_type({column}) <> 'array' THEN {column} \
ELSE (SELECT json_group_array({kept} ORDER BY {ELEMENT}.key) \
FROM json_each({source}) AS {ELEMENT} WHERE NOT "
),
equal,
") END"
)
}
fn json_set(&self, column: &str, path: &[JsonPathStep], value: &serde_json::Value) -> BoundSql {
let text = |steps: &[JsonPathStep], suffix: &str| {
Value::String(Some(format!("{}{suffix}", json_path_text(steps))))
};
let Some((last, parent)) = path.split_last() else {
return BoundSql::sql(column);
};
let document = json_text(value);
match last {
JsonPathStep::Member(_) => bound!(
format!("CASE WHEN json_type({column}, "),
text(parent, ""),
format!(") IS NOT 'object' THEN {column} ELSE json_set({column}, "),
text(path, ""),
", json(",
document,
")) END"
),
JsonPathStep::Element(index) => bound!(
format!("CASE WHEN json_type({column}, "),
text(parent, ""),
format!(") IS NOT 'array' THEN {column} WHEN json_array_length({column}, "),
text(parent, ""),
format!(") > {index} THEN json_set({column}, "),
text(path, ""),
", json(",
document.clone(),
format!(")) ELSE json_insert({column}, "),
text(parent, "[#]"),
", json(",
document,
")) END"
),
}
}
fn open_limit(&self) -> Option<&'static str> {
Some("-1")
}
fn exact_key(&self, column: &str) -> String {
format!("CAST({column} AS BLOB)")
}
fn literal_match(
&self,
column: &str,
literal: &str,
leading: bool,
trailing: bool,
) -> Result<BoundSql, RenderError> {
if literal.contains('\0') {
return Err(RenderError::Unsupported(
"SQLite literal text filters cannot represent a NUL character".into(),
));
}
let test = match (leading, trailing) {
(true, true) => "instr(hay, needle) > 0",
(false, true) => "substr(hay, 1, length(needle)) = needle",
(true, false) => "length(needle) = 0 OR substr(hay, -length(needle)) = needle",
(false, false) => "hay = needle",
};
Ok(bound!(
format!(
"(SELECT CASE WHEN hay IS NOT NULL THEN ({test}) END FROM \
(SELECT CAST(CASE WHEN 'a' LIKE 'A' THEN lower(raw_hay) ELSE raw_hay END AS BLOB) AS hay, \
CAST(CASE WHEN 'a' LIKE 'A' THEN lower(raw_needle) ELSE raw_needle END AS BLOB) AS needle \
FROM (SELECT {column} AS raw_hay, "
),
Value::String(Some(literal.to_owned())),
" AS raw_needle)))"
))
}
fn in_list(&self, column: Expr, values: Vec<Value>, negated: bool) -> Expr {
if values.len() > LONG_LIST
&& let Some(texts) = values
.iter()
.map(|value| match value {
Value::String(Some(text)) => Some(text.as_str()),
_ => None,
})
.collect::<Option<Vec<_>>>()
{
let operator = if negated { " NOT IN" } else { " IN" };
return bound!(
format!("{operator} (SELECT value FROM json_each("),
Value::String(Some(serde_json::to_string(&texts).unwrap_or_default())),
"))"
)
.following(column);
}
in_list(column, values, negated)
}
}