1use std::cmp::Ordering;
17use std::fmt::Write as _;
18
19use crate::{Value, ident};
20
21pub trait RowView {
25 fn col(&self, column: &str) -> Option<Value>;
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35pub enum Quant {
36 Any,
37 All,
38 None,
39 Empty,
41 CountGe(usize),
43}
44
45#[derive(Clone, Debug, PartialEq)]
48pub enum Pred {
49 Always,
50 Eq(&'static str, Value),
51 Ne(&'static str, Value),
52 Lt(&'static str, Value),
53 Le(&'static str, Value),
54 Gt(&'static str, Value),
55 Ge(&'static str, Value),
56 Contains(&'static str, String),
58 ContainsCi(&'static str, String),
62 StartsWith(&'static str, String),
65 StartsWithCi(&'static str, String),
67 Between(&'static str, Value, Value),
68 In(&'static str, Vec<Value>),
71 NotIn(&'static str, Vec<Value>),
73 IdIn(Vec<u64>),
75 Related {
80 owner: &'static str,
82 field: &'static str,
84 target: &'static str,
85 quant: Quant,
86 inner: Box<Pred>,
87 },
88 And(Box<Pred>, Box<Pred>),
89 Or(Box<Pred>, Box<Pred>),
90 Not(Box<Pred>),
91 Raw(String, Vec<Value>),
94 Matches {
97 columns: &'static [&'static str],
98 query: String,
99 },
100 Within {
104 lat: &'static str,
105 lon: &'static str,
106 min_lat: f64,
107 max_lat: f64,
108 min_lon: f64,
109 max_lon: f64,
110 },
111}
112
113impl Pred {
114 #[expect(
117 clippy::match_same_arms,
118 reason = "each empty arm carries the explanation of why that predicate reads no local column"
119 )]
120 pub fn columns(&self, out: &mut Vec<&'static str>) {
121 let push = |c: &'static str, out: &mut Vec<&'static str>| {
122 if !out.contains(&c) {
123 out.push(c);
124 }
125 };
126 match self {
127 Pred::Always | Pred::Raw(..) => {}
128 Pred::Eq(c, _)
129 | Pred::Ne(c, _)
130 | Pred::Lt(c, _)
131 | Pred::Le(c, _)
132 | Pred::Gt(c, _)
133 | Pred::Ge(c, _)
134 | Pred::Contains(c, _)
135 | Pred::ContainsCi(c, _)
136 | Pred::StartsWith(c, _)
137 | Pred::StartsWithCi(c, _)
138 | Pred::In(c, _)
139 | Pred::NotIn(c, _)
140 | Pred::Between(c, _, _) => push(c, out),
141 Pred::IdIn(_) => {}
144 Pred::Related { .. } => {}
149 Pred::And(a, b) | Pred::Or(a, b) => {
150 a.columns(out);
151 b.columns(out);
152 }
153 Pred::Not(a) => a.columns(out),
154 Pred::Matches { columns, .. } => {
155 for c in *columns {
156 push(c, out);
157 }
158 }
159 Pred::Within { lat, lon, .. } => {
160 push(lat, out);
161 push(lon, out);
162 }
163 }
164 }
165
166 fn related_deps(&self, out: &mut Vec<RelatedDep>) {
170 match self {
171 Pred::Related {
172 owner,
173 field,
174 target,
175 inner,
176 ..
177 } => {
178 let mut columns = Vec::new();
179 inner.columns(&mut columns);
180 out.push(RelatedDep {
181 owner,
182 field,
183 target_table: target,
184 columns,
185 });
186 inner.related_deps(out);
187 }
188 Pred::And(a, b) | Pred::Or(a, b) => {
189 a.related_deps(out);
190 b.related_deps(out);
191 }
192 Pred::Not(a) => a.related_deps(out),
193 _ => {}
194 }
195 }
196
197 pub fn contains_raw(&self) -> bool {
199 match self {
200 Pred::Raw(..) => true,
201 Pred::And(a, b) | Pred::Or(a, b) => a.contains_raw() || b.contains_raw(),
202 Pred::Not(a) => a.contains_raw(),
203 _ => false,
204 }
205 }
206
207 pub(crate) fn contains_fold(&self) -> bool {
210 match self {
211 Pred::ContainsCi(..) | Pred::StartsWithCi(..) => true,
212 Pred::And(a, b) | Pred::Or(a, b) => a.contains_fold() || b.contains_fold(),
213 Pred::Not(a) => a.contains_fold(),
214 Pred::Related { inner, .. } => inner.contains_fold(),
215 _ => false,
216 }
217 }
218
219 pub(crate) fn contains_related(&self) -> bool {
221 match self {
222 Pred::Related { .. } => true,
223 Pred::And(a, b) | Pred::Or(a, b) => a.contains_related() || b.contains_related(),
224 Pred::Not(a) => a.contains_related(),
225 _ => false,
226 }
227 }
228
229 pub fn eval(&self, key: u64, row: &dyn RowView) -> bool {
235 self.eval3(key, row) == Some(true)
236 }
237
238 #[expect(
245 clippy::match_same_arms,
246 reason = "each constant arm carries the explanation of why that predicate is not evaluated here"
247 )]
248 pub fn eval3(&self, key: u64, row: &dyn RowView) -> Option<bool> {
249 match self {
250 Pred::Always => Some(true),
251 Pred::Raw(..) | Pred::Matches { .. } => Some(true),
253 Pred::Related { .. } => Some(true),
256
257 Pred::Eq(c, Value::Null) => Some(matches!(row.col(c), Some(Value::Null) | None)),
259 Pred::Ne(c, Value::Null) => Some(!matches!(row.col(c), Some(Value::Null) | None)),
260
261 Pred::Eq(c, v) => defined(row.col(c)).map(|a| a == *v),
262 Pred::Ne(c, v) => defined(row.col(c)).map(|a| a != *v),
263 Pred::Lt(c, v) => defined(row.col(c)).map(|a| compare_values(&a, v) == Ordering::Less),
264 Pred::Le(c, v) => {
265 defined(row.col(c)).map(|a| compare_values(&a, v) != Ordering::Greater)
266 }
267 Pred::Gt(c, v) => {
268 defined(row.col(c)).map(|a| compare_values(&a, v) == Ordering::Greater)
269 }
270 Pred::Ge(c, v) => defined(row.col(c)).map(|a| compare_values(&a, v) != Ordering::Less),
271 Pred::Between(c, lo, hi) => defined(row.col(c)).map(|a| {
272 compare_values(&a, lo) != Ordering::Less
273 && compare_values(&a, hi) != Ordering::Greater
274 }),
275
276 Pred::Contains(c, needle) => text(row.col(c)).map(|t| t.contains(needle.as_str())),
277 Pred::ContainsCi(c, needle) => {
278 text(row.col(c)).map(|t| t.to_lowercase().contains(&needle.to_lowercase()))
279 }
280 Pred::StartsWith(c, prefix) => text(row.col(c)).map(|t| t.starts_with(prefix.as_str())),
281 Pred::StartsWithCi(c, prefix) => {
282 text(row.col(c)).map(|t| t.to_lowercase().starts_with(&prefix.to_lowercase()))
283 }
284
285 Pred::In(c, set) => defined(row.col(c)).map(|a| in_set(set, &a)),
286 Pred::NotIn(c, set) => defined(row.col(c)).map(|a| !in_set(set, &a)),
287 Pred::IdIn(ids) => Some(ids.binary_search(&key).is_ok()),
289
290 Pred::And(a, b) => match (a.eval3(key, row), b.eval3(key, row)) {
292 (Some(false), _) | (_, Some(false)) => Some(false),
293 (Some(true), Some(true)) => Some(true),
294 _ => None,
295 },
296 Pred::Or(a, b) => match (a.eval3(key, row), b.eval3(key, row)) {
297 (Some(true), _) | (_, Some(true)) => Some(true),
298 (Some(false), Some(false)) => Some(false),
299 _ => None,
300 },
301 Pred::Not(a) => a.eval3(key, row).map(|v| !v),
302
303 Pred::Within {
304 lat,
305 lon,
306 min_lat,
307 max_lat,
308 min_lon,
309 max_lon,
310 } => {
311 let in_range = |c: &str, lo: f64, hi: f64| match defined(row.col(c)) {
312 Some(Value::Real(v)) => Some(v >= lo && v <= hi),
313 Some(Value::Int(v)) => Some((v as f64) >= lo && (v as f64) <= hi),
314 Some(_) => Some(false),
315 None => None,
316 };
317 match (
318 in_range(lat, *min_lat, *max_lat),
319 in_range(lon, *min_lon, *max_lon),
320 ) {
321 (Some(false), _) | (_, Some(false)) => Some(false),
322 (Some(true), Some(true)) => Some(true),
323 _ => None,
324 }
325 }
326 }
327 }
328}
329
330impl std::ops::BitAnd for Pred {
331 type Output = Pred;
332 fn bitand(self, rhs: Pred) -> Pred {
333 Pred::And(Box::new(self), Box::new(rhs))
334 }
335}
336
337impl std::ops::BitOr for Pred {
338 type Output = Pred;
339 fn bitor(self, rhs: Pred) -> Pred {
340 Pred::Or(Box::new(self), Box::new(rhs))
341 }
342}
343
344impl std::ops::Not for Pred {
345 type Output = Pred;
346 fn not(self) -> Pred {
347 Pred::Not(Box::new(self))
348 }
349}
350
351#[derive(Clone, Debug)]
358pub(crate) enum RelSql {
359 Children {
362 target_key: String,
363 fk_col: String,
364 owner_key: String,
365 },
366 Referent { target_key: String, fk_col: String },
369 Join {
371 join_table: String,
372 owner_col: String,
373 target_col: String,
374 owner_key: String,
375 target_key: String,
376 },
377 Linked {
381 local_col: String,
382 remote_col: String,
383 target_key: String,
384 },
385}
386
387pub(crate) trait SqlIndex {
390 fn relation(&self, owner: &str, field: &str) -> Option<RelSql>;
392 fn key_of(&self, table: &str) -> Option<String>;
394 fn table_sql(&self, table: &str) -> String;
396 fn fts_of(&self, table: &str) -> Option<FtsSql>;
398 fn fts_key_of(&self, _table: &str) -> &'static str {
399 "rowid"
400 }
401 fn geo_of(&self, table: &str, lat: &str, lon: &str) -> Option<String>;
404 fn unicode_fold(&self) -> bool;
406}
407
408#[derive(Clone, Debug)]
412pub(crate) struct FtsSql {
413 pub from: String,
414 pub match_col: String,
415}
416
417#[derive(Clone, Debug, PartialEq)]
420pub(crate) enum CompileErr {
421 NeedsFold,
423 FoldInsideRelation,
427 Unwired(&'static str, &'static str),
429 NoFts,
431}
432
433impl CompileErr {
434 pub(crate) fn message(&self) -> String {
435 match self {
436 CompileErr::NeedsFold | CompileErr::FoldInsideRelation => {
437 "case-insensitive predicate needs the driver's day_fold function".into()
438 }
439 CompileErr::Unwired(owner, field) => {
440 format!("`{owner}.{field}` is not a wired relation of this container")
441 }
442 CompileErr::NoFts => "matches()/rank() needs a #[model(fts(…))] declaration".into(),
443 }
444 }
445}
446
447#[derive(Clone, Debug)]
449pub(crate) struct SqlQuery {
450 pub sql: String,
451 pub params: Vec<Value>,
452}
453
454pub(crate) fn compile_fetch(
458 table: &str,
459 fetch: &Fetch,
460 idx: &dyn SqlIndex,
461) -> Result<SqlQuery, CompileErr> {
462 let key = ident(&idx.key_of(table).ok_or(CompileErr::Unwired("", ""))?);
463 let t = idx.table_sql(table);
464 let mut params = Vec::new();
465 let mut aliases = 0usize;
466 let by_rank = fetch.sort.iter().any(|s| s.by_rank);
467
468 let (from, rank_pred);
472 if by_rank {
473 let fts = idx.fts_of(table).ok_or(CompileErr::NoFts)?;
474 let Some(q) = find_match_query(&fetch.pred) else {
475 return Err(CompileErr::NoFts);
476 };
477 params.push(Value::Text(q));
478 let fts_key = idx.fts_key_of(table);
479 from = format!(
480 "{t} JOIN {} AS day_rank ON day_rank.{fts_key} = {t}.{key} AND day_rank.{} MATCH ?",
481 fts.from, fts.match_col
482 );
483 rank_pred = true;
484 } else {
485 from = t.clone();
486 rank_pred = false;
487 }
488
489 let ctx = SqlCtx {
490 idx,
491 skip_matches: rank_pred,
492 };
493 let where_clause = pred_sql(
494 &fetch.pred,
495 table,
496 &t,
497 &key,
498 &ctx,
499 &mut params,
500 &mut aliases,
501 )?;
502
503 let mut sql = format!("SELECT {t}.{key} FROM {from}");
504 if where_clause != "1" {
505 let _ = write!(sql, " WHERE {where_clause}");
506 }
507 let mut orders: Vec<String> = Vec::new();
508 for s in &fetch.sort {
509 if s.by_rank {
510 orders.push("day_rank.rank".into());
511 } else {
512 orders.push(format!(
513 "{t}.{} {}",
514 ident(s.column),
515 if s.ascending { "ASC" } else { "DESC" }
516 ));
517 }
518 }
519 orders.push(format!("{t}.{key} ASC"));
522 let _ = write!(sql, " ORDER BY {}", orders.join(", "));
523 if let Some(n) = fetch.limit {
524 let _ = write!(sql, " LIMIT {n}");
525 }
526 Ok(SqlQuery { sql, params })
527}
528
529pub(crate) fn compile_count(
533 table: &str,
534 fetch: &Fetch,
535 idx: &dyn SqlIndex,
536) -> Result<SqlQuery, CompileErr> {
537 let key = ident(&idx.key_of(table).ok_or(CompileErr::Unwired("", ""))?);
538 let t = idx.table_sql(table);
539 let mut params = Vec::new();
540 let mut aliases = 0usize;
541 let ctx = SqlCtx {
542 idx,
543 skip_matches: false,
544 };
545 let where_clause = pred_sql(
546 &fetch.pred,
547 table,
548 &t,
549 &key,
550 &ctx,
551 &mut params,
552 &mut aliases,
553 )?;
554 let mut sql = format!("SELECT COUNT(*) FROM {t}");
555 if where_clause != "1" {
556 let _ = write!(sql, " WHERE {where_clause}");
557 }
558 Ok(SqlQuery { sql, params })
559}
560
561pub(crate) fn compile_fallback(
566 table: &str,
567 fetch: &Fetch,
568 idx: &dyn SqlIndex,
569) -> Result<(SqlQuery, Vec<&'static str>), CompileErr> {
570 if fetch.pred.contains_related() && fetch.pred.contains_fold() {
571 return Err(CompileErr::FoldInsideRelation);
574 }
575 let key = ident(&idx.key_of(table).ok_or(CompileErr::Unwired("", ""))?);
576 let t = idx.table_sql(table);
577 let mut params = Vec::new();
578 let mut aliases = 0usize;
579 let ctx = SqlCtx {
580 idx,
581 skip_matches: false,
582 };
583
584 let mut conjuncts = Vec::new();
587 split_and(&fetch.pred, &mut conjuncts);
588 let mut clauses = Vec::new();
589 for c in conjuncts {
590 if !c.contains_fold() {
591 clauses.push(pred_sql(
592 c,
593 table,
594 &t,
595 &key,
596 &ctx,
597 &mut params,
598 &mut aliases,
599 )?);
600 }
601 }
602
603 let mut deps = Vec::new();
604 fetch.pred.columns(&mut deps);
605 for s in &fetch.sort {
606 if !s.by_rank && !deps.contains(&s.column) {
607 deps.push(s.column);
608 }
609 }
610 let cols = if deps.is_empty() {
611 format!("{t}.{key}")
612 } else {
613 format!(
614 "{t}.{key}, {}",
615 deps.iter()
616 .map(|c| format!("{t}.{}", ident(c)))
617 .collect::<Vec<_>>()
618 .join(", ")
619 )
620 };
621 let mut sql = format!("SELECT {cols} FROM {t}");
622 let clauses: Vec<String> = clauses.into_iter().filter(|c| c != "1").collect();
623 if !clauses.is_empty() {
624 let _ = write!(sql, " WHERE {}", clauses.join(" AND "));
625 }
626 let mut orders: Vec<String> = fetch
627 .sort
628 .iter()
629 .filter(|s| !s.by_rank)
630 .map(|s| {
631 format!(
632 "{t}.{} {}",
633 ident(s.column),
634 if s.ascending { "ASC" } else { "DESC" }
635 )
636 })
637 .collect();
638 orders.push(format!("{t}.{key} ASC"));
639 let _ = write!(sql, " ORDER BY {}", orders.join(", "));
640 Ok((SqlQuery { sql, params }, deps))
641}
642
643fn split_and<'p>(pred: &'p Pred, out: &mut Vec<&'p Pred>) {
645 match pred {
646 Pred::And(a, b) => {
647 split_and(a, out);
648 split_and(b, out);
649 }
650 p => out.push(p),
651 }
652}
653
654struct SqlCtx<'a> {
655 idx: &'a dyn SqlIndex,
656 skip_matches: bool,
659}
660
661fn pred_sql(
666 pred: &Pred,
667 table: &str,
668 alias: &str,
669 key: &str,
670 ctx: &SqlCtx<'_>,
671 params: &mut Vec<Value>,
672 aliases: &mut usize,
673) -> Result<String, CompileErr> {
674 let col = |c: &str| format!("{alias}.{}", ident(c));
676 Ok(match pred {
677 Pred::Always => "1".into(),
678 Pred::Eq(c, Value::Null) => format!("{} IS NULL", col(c)),
679 Pred::Ne(c, Value::Null) => format!("{} IS NOT NULL", col(c)),
680 Pred::Eq(c, v) => {
681 params.push(v.clone());
682 format!("{} = ?", col(c))
683 }
684 Pred::Ne(c, v) => {
685 params.push(v.clone());
686 format!("{} <> ?", col(c))
687 }
688 Pred::Lt(c, v) => {
689 params.push(v.clone());
690 format!("{} < ?", col(c))
691 }
692 Pred::Le(c, v) => {
693 params.push(v.clone());
694 format!("{} <= ?", col(c))
695 }
696 Pred::Gt(c, v) => {
697 params.push(v.clone());
698 format!("{} > ?", col(c))
699 }
700 Pred::Ge(c, v) => {
701 params.push(v.clone());
702 format!("{} >= ?", col(c))
703 }
704 Pred::Contains(c, s) => {
705 params.push(Value::Text(s.clone()));
706 format!("instr({}, ?) > 0", col(c))
707 }
708 Pred::ContainsCi(c, s) => {
709 if !ctx.idx.unicode_fold() {
710 return Err(CompileErr::NeedsFold);
711 }
712 params.push(Value::Text(s.to_lowercase()));
713 format!("instr(day_fold({}), ?) > 0", col(c))
714 }
715 Pred::StartsWith(c, prefix) => {
716 params.push(Value::Int(prefix.chars().count() as i64));
720 params.push(Value::Text(prefix.clone()));
721 format!("substr({}, 1, ?) = ?", col(c))
722 }
723 Pred::StartsWithCi(c, prefix) => {
724 if !ctx.idx.unicode_fold() {
725 return Err(CompileErr::NeedsFold);
726 }
727 let folded = prefix.to_lowercase();
728 params.push(Value::Int(folded.chars().count() as i64));
729 params.push(Value::Text(folded));
730 format!("substr(day_fold({}), 1, ?) = ?", col(c))
731 }
732 Pred::In(c, set) | Pred::NotIn(c, set) => {
733 if set.is_empty() {
739 return Ok(if matches!(pred, Pred::In(..)) {
740 "0".into()
741 } else {
742 format!("{} IS NOT NULL", col(c))
743 });
744 }
745 let negate = if matches!(pred, Pred::NotIn(..)) {
746 "NOT "
747 } else {
748 ""
749 };
750 let marks = vec!["?"; set.len()].join(", ");
751 params.extend(set.iter().cloned());
752 format!("{} {negate}IN ({marks})", col(c))
753 }
754 Pred::IdIn(ids) => {
755 if ids.is_empty() {
756 return Ok("0".into());
757 }
758 for id in ids {
759 params.push(crate::key_param(*id));
760 }
761 let marks = vec!["?"; ids.len()].join(", ");
762 format!("{alias}.{key} IN ({marks})")
763 }
764 Pred::Between(c, lo, hi) => {
765 params.push(lo.clone());
766 params.push(hi.clone());
767 format!("{} BETWEEN ? AND ?", col(c))
768 }
769 Pred::And(a, b) => format!(
770 "({} AND {})",
771 pred_sql(a, table, alias, key, ctx, params, aliases)?,
772 pred_sql(b, table, alias, key, ctx, params, aliases)?
773 ),
774 Pred::Or(a, b) => format!(
775 "({} OR {})",
776 pred_sql(a, table, alias, key, ctx, params, aliases)?,
777 pred_sql(b, table, alias, key, ctx, params, aliases)?
778 ),
779 Pred::Not(a) => format!(
780 "NOT ({})",
781 pred_sql(a, table, alias, key, ctx, params, aliases)?
782 ),
783 Pred::Raw(sql, args) => {
784 params.extend(args.iter().cloned());
785 format!("({sql})")
786 }
787 Pred::Matches { query, .. } => {
788 if ctx.skip_matches {
789 "1".into()
791 } else {
792 let fts = ctx.idx.fts_of(table).ok_or(CompileErr::NoFts)?;
793 params.push(Value::Text(query.clone()));
794 let fts_key = ctx.idx.fts_key_of(table);
795 format!(
796 "{alias}.{key} IN (SELECT {fts_key} FROM {} WHERE {} MATCH ?)",
797 fts.from, fts.match_col
798 )
799 }
800 }
801 Pred::Within {
802 lat,
803 lon,
804 min_lat,
805 max_lat,
806 min_lon,
807 max_lon,
808 } => {
809 let exact = {
810 params.push(Value::Real(*min_lat));
811 params.push(Value::Real(*max_lat));
812 params.push(Value::Real(*min_lon));
813 params.push(Value::Real(*max_lon));
814 format!(
815 "({} BETWEEN ? AND ? AND {} BETWEEN ? AND ?)",
816 col(lat),
817 col(lon)
818 )
819 };
820 match ctx.idx.geo_of(table, lat, lon) {
824 Some(geo) => {
825 params.push(Value::Real(*min_lat));
826 params.push(Value::Real(*max_lat));
827 params.push(Value::Real(*min_lon));
828 params.push(Value::Real(*max_lon));
829 format!(
830 "({alias}.{key} IN (SELECT {key} FROM {geo} WHERE \
831 max_lat >= ? AND min_lat <= ? AND max_lon >= ? AND min_lon <= ?) \
832 AND {exact})"
833 )
834 }
835 None => exact,
836 }
837 }
838 Pred::Related {
839 owner,
840 field,
841 target,
842 quant,
843 inner,
844 } => {
845 let rel = ctx
846 .idx
847 .relation(owner, field)
848 .ok_or(CompileErr::Unwired(owner, field))?;
849 let r = format!("day_r{}", *aliases);
850 *aliases += 1;
851 let target_sql = ctx.idx.table_sql(target);
852 let (from, tie, inner_alias, inner_key) = match &rel {
853 RelSql::Children {
854 target_key,
855 fk_col,
856 owner_key,
857 } => (
858 format!("{target_sql} AS {r}"),
859 format!("{r}.{} = {}", ident(fk_col), col(owner_key)),
860 r.clone(),
861 ident(target_key),
862 ),
863 RelSql::Referent { target_key, fk_col } => (
864 format!("{target_sql} AS {r}"),
865 format!("{r}.{} = {}", ident(target_key), col(fk_col)),
866 r.clone(),
867 ident(target_key),
868 ),
869 RelSql::Linked {
870 local_col,
871 remote_col,
872 target_key,
873 } => (
874 format!("{target_sql} AS {r}"),
875 format!("{r}.{} = {}", ident(remote_col), col(local_col)),
876 r.clone(),
877 ident(target_key),
878 ),
879 RelSql::Join {
880 join_table,
881 owner_col,
882 target_col,
883 owner_key,
884 target_key,
885 } => {
886 let j = format!("day_j{}", *aliases);
887 *aliases += 1;
888 (
889 format!(
890 "{} AS {j} JOIN {target_sql} AS {r} ON {r}.{} = {j}.{}",
891 ctx.idx.table_sql(join_table),
892 ident(target_key),
893 ident(target_col)
894 ),
895 format!("{j}.{} = {}", ident(owner_col), col(owner_key)),
896 r.clone(),
897 ident(target_key),
898 )
899 }
900 };
901 match quant {
902 Quant::Empty => format!("NOT EXISTS (SELECT 1 FROM {from} WHERE {tie})"),
903 Quant::CountGe(n) => {
904 params.push(Value::Int(*n as i64));
905 format!("(SELECT COUNT(*) FROM {from} WHERE {tie}) >= ?")
906 }
907 Quant::Any | Quant::None | Quant::All => {
908 let inner_sql = pred_sql(
909 inner,
910 target,
911 &inner_alias,
912 &inner_key,
913 ctx,
914 params,
915 aliases,
916 )?;
917 match quant {
918 Quant::Any => {
919 format!("EXISTS (SELECT 1 FROM {from} WHERE {tie} AND ({inner_sql}))")
920 }
921 Quant::None => format!(
922 "NOT EXISTS (SELECT 1 FROM {from} WHERE {tie} AND ({inner_sql}))"
923 ),
924 Quant::All => format!(
928 "NOT EXISTS (SELECT 1 FROM {from} WHERE {tie} AND ({inner_sql}) IS NOT TRUE)"
929 ),
930 _ => unreachable!(),
931 }
932 }
933 }
934 }
935 })
936}
937
938fn defined(v: Option<Value>) -> Option<Value> {
946 match v {
947 Some(Value::Null) | None => None,
948 Some(v) => Some(v),
949 }
950}
951
952fn text(v: Option<Value>) -> Option<String> {
956 match defined(v) {
957 Some(Value::Text(t)) => Some(t),
958 _ => None,
959 }
960}
961
962fn in_set(set: &[Value], v: &Value) -> bool {
966 let start = set.partition_point(|x| compare_values(x, v) == Ordering::Less);
967 set[start..]
968 .iter()
969 .take_while(|x| compare_values(x, v) == Ordering::Equal)
970 .any(|x| x == v)
971}
972
973pub fn compare_values(a: &Value, b: &Value) -> Ordering {
977 fn class(v: &Value) -> u8 {
978 match v {
979 Value::Null => 0,
980 Value::Int(_) | Value::Real(_) => 1,
981 Value::Text(_) => 2,
982 Value::Blob(_) => 3,
983 }
984 }
985 match (a, b) {
986 (Value::Int(x), Value::Int(y)) => x.cmp(y),
987 (Value::Real(x), Value::Real(y)) => x.total_cmp(y),
988 (Value::Int(x), Value::Real(y)) => (*x as f64).total_cmp(y),
989 (Value::Real(x), Value::Int(y)) => x.total_cmp(&(*y as f64)),
990 (Value::Text(x), Value::Text(y)) => x.cmp(y),
991 (Value::Blob(x), Value::Blob(y)) => x.cmp(y),
992 _ => class(a).cmp(&class(b)),
993 }
994}
995
996pub struct Col<V: 'static> {
1004 pub column: &'static str,
1005 pub field: &'static str,
1009 pub owner: &'static str,
1012 encode: fn(&V) -> Value,
1013}
1014
1015impl<V> Clone for Col<V> {
1016 fn clone(&self) -> Self {
1017 *self
1018 }
1019}
1020impl<V> Copy for Col<V> {}
1021
1022pub struct RelationCol<P: 'static, T: 'static> {
1026 field: &'static str,
1027 owner: &'static str,
1028 target: &'static str,
1029 _p: std::marker::PhantomData<fn() -> (P, T)>,
1030}
1031
1032impl<P, T> Clone for RelationCol<P, T> {
1033 fn clone(&self) -> Self {
1034 *self
1035 }
1036}
1037impl<P, T> Copy for RelationCol<P, T> {}
1038
1039impl<P, T> RelationCol<P, T> {
1040 pub const fn new(field: &'static str, owner: &'static str, target: &'static str) -> Self {
1041 RelationCol {
1042 field,
1043 owner,
1044 target,
1045 _p: std::marker::PhantomData,
1046 }
1047 }
1048
1049 fn build(self, quant: Quant, inner: Pred) -> Pred {
1050 Pred::Related {
1051 owner: self.owner,
1052 field: self.field,
1053 target: self.target,
1054 quant,
1055 inner: Box::new(inner),
1056 }
1057 }
1058
1059 pub fn any(self, inner: Pred) -> Pred {
1061 self.build(Quant::Any, inner)
1062 }
1063
1064 pub fn none(self, inner: Pred) -> Pred {
1066 self.build(Quant::None, inner)
1067 }
1068
1069 pub fn all(self, inner: Pred) -> Pred {
1072 self.build(Quant::All, inner)
1073 }
1074
1075 pub fn is_empty(self) -> Pred {
1077 self.build(Quant::Empty, Pred::Always)
1078 }
1079
1080 pub fn count_ge(self, n: usize) -> Pred {
1082 self.build(Quant::CountGe(n), Pred::Always)
1083 }
1084}
1085
1086pub fn encode_column<T: crate::ColumnValue>(v: &T) -> Value {
1088 v.to_sqlite_value()
1089}
1090
1091impl<V> Col<V> {
1092 pub const fn new(
1093 column: &'static str,
1094 field: &'static str,
1095 owner: &'static str,
1096 encode: fn(&V) -> Value,
1097 ) -> Col<V> {
1098 Col {
1099 column,
1100 field,
1101 owner,
1102 encode,
1103 }
1104 }
1105 fn enc(&self, v: &V) -> Value {
1106 (self.encode)(v)
1107 }
1108 pub fn eq(self, v: impl std::borrow::Borrow<V>) -> Pred {
1109 Pred::Eq(self.column, self.enc(v.borrow()))
1110 }
1111 pub fn ne(self, v: impl std::borrow::Borrow<V>) -> Pred {
1112 Pred::Ne(self.column, self.enc(v.borrow()))
1113 }
1114 pub fn lt(self, v: impl std::borrow::Borrow<V>) -> Pred {
1115 Pred::Lt(self.column, self.enc(v.borrow()))
1116 }
1117 pub fn le(self, v: impl std::borrow::Borrow<V>) -> Pred {
1118 Pred::Le(self.column, self.enc(v.borrow()))
1119 }
1120 pub fn gt(self, v: impl std::borrow::Borrow<V>) -> Pred {
1121 Pred::Gt(self.column, self.enc(v.borrow()))
1122 }
1123 pub fn ge(self, v: impl std::borrow::Borrow<V>) -> Pred {
1124 Pred::Ge(self.column, self.enc(v.borrow()))
1125 }
1126 pub fn is_in(self, values: impl IntoIterator<Item = impl std::borrow::Borrow<V>>) -> Pred {
1129 Pred::In(self.column, self.encode_set(values))
1130 }
1131
1132 pub fn not_in(self, values: impl IntoIterator<Item = impl std::borrow::Borrow<V>>) -> Pred {
1136 Pred::NotIn(self.column, self.encode_set(values))
1137 }
1138
1139 fn encode_set(
1141 self,
1142 values: impl IntoIterator<Item = impl std::borrow::Borrow<V>>,
1143 ) -> Vec<Value> {
1144 let mut set: Vec<Value> = values.into_iter().map(|v| self.enc(v.borrow())).collect();
1145 set.sort_by(compare_values);
1146 set.dedup();
1147 set
1148 }
1149
1150 pub fn is_null(self) -> Pred {
1152 Pred::Eq(self.column, Value::Null)
1153 }
1154
1155 pub fn is_not_null(self) -> Pred {
1157 Pred::Ne(self.column, Value::Null)
1158 }
1159
1160 pub fn between(self, lo: impl std::borrow::Borrow<V>, hi: impl std::borrow::Borrow<V>) -> Pred {
1161 Pred::Between(self.column, self.enc(lo.borrow()), self.enc(hi.borrow()))
1162 }
1163 pub fn asc(self) -> Sort {
1164 Sort::asc(self.column)
1165 }
1166 pub fn desc(self) -> Sort {
1167 Sort::desc(self.column)
1168 }
1169}
1170
1171#[derive(Clone, Copy)]
1173pub struct FtsRef {
1174 pub columns: &'static [&'static str],
1175}
1176
1177impl FtsRef {
1178 pub fn search(self, text: &str) -> Pred {
1181 let terms = text
1182 .split_whitespace()
1183 .map(|term| format!("\"{}\"", term.replace('"', "\"\"")))
1184 .collect::<Vec<_>>();
1185 if terms.is_empty() {
1186 Pred::Always
1187 } else {
1188 self.matches(terms.join(" AND "))
1189 }
1190 }
1191
1192 pub fn matches(self, query: impl Into<String>) -> Pred {
1195 Pred::Matches {
1196 columns: self.columns,
1197 query: query.into(),
1198 }
1199 }
1200}
1201
1202#[derive(Clone, Copy)]
1204pub struct GeoRef {
1205 pub lat: &'static str,
1206 pub lon: &'static str,
1207}
1208
1209#[derive(Clone, Copy, Debug, PartialEq)]
1211pub struct GeoRect {
1212 pub min_lat: f64,
1213 pub max_lat: f64,
1214 pub min_lon: f64,
1215 pub max_lon: f64,
1216}
1217
1218impl GeoRef {
1219 pub fn within(self, r: GeoRect) -> Pred {
1222 Pred::Within {
1223 lat: self.lat,
1224 lon: self.lon,
1225 min_lat: r.min_lat,
1226 max_lat: r.max_lat,
1227 min_lon: r.min_lon,
1228 max_lon: r.max_lon,
1229 }
1230 }
1231}
1232
1233pub fn rank() -> Sort {
1235 Sort {
1236 column: "",
1237 ascending: true,
1238 by_rank: true,
1239 }
1240}
1241
1242impl Col<String> {
1243 pub fn contains(self, needle: impl Into<String>) -> Pred {
1245 Pred::Contains(self.column, needle.into())
1246 }
1247 pub fn starts_with(self, prefix: impl Into<String>) -> Pred {
1249 Pred::StartsWith(self.column, prefix.into())
1250 }
1251
1252 pub fn starts_with_ci(self, prefix: impl Into<String>) -> Pred {
1254 Pred::StartsWithCi(self.column, prefix.into())
1255 }
1256
1257 pub fn contains_ci(self, needle: impl Into<String>) -> Pred {
1259 Pred::ContainsCi(self.column, needle.into())
1260 }
1261}
1262
1263#[derive(Clone, Debug, PartialEq)]
1264pub struct Sort {
1265 pub column: &'static str,
1266 pub ascending: bool,
1267 pub by_rank: bool,
1270}
1271
1272impl Sort {
1273 pub fn asc(column: &'static str) -> Sort {
1274 Sort {
1275 column,
1276 ascending: true,
1277 by_rank: false,
1278 }
1279 }
1280 pub fn desc(column: &'static str) -> Sort {
1281 Sort {
1282 column,
1283 ascending: false,
1284 by_rank: false,
1285 }
1286 }
1287}
1288
1289#[derive(Clone, Debug, PartialEq)]
1291pub struct Fetch {
1292 pub pred: Pred,
1293 pub sort: Vec<Sort>,
1294 pub limit: Option<usize>,
1295}
1296
1297impl Default for Fetch {
1298 fn default() -> Fetch {
1299 Fetch {
1300 pred: Pred::Always,
1301 sort: Vec::new(),
1302 limit: None,
1303 }
1304 }
1305}
1306
1307impl Fetch {
1308 pub fn new() -> Fetch {
1309 Fetch::default()
1310 }
1311 #[must_use]
1312 pub fn filter(mut self, p: Pred) -> Fetch {
1313 self.pred = match self.pred {
1314 Pred::Always => p,
1315 prior => prior & p,
1316 };
1317 self
1318 }
1319 #[must_use]
1320 pub fn sort(mut self, s: Sort) -> Fetch {
1321 self.sort.push(s);
1322 self
1323 }
1324 #[must_use]
1325 pub fn limit(mut self, n: usize) -> Fetch {
1326 self.limit = Some(n);
1327 self
1328 }
1329
1330 pub fn dependencies(&self) -> Deps {
1333 let mut local = Vec::new();
1334 self.pred.columns(&mut local);
1335 for s in &self.sort {
1336 if !s.by_rank && !local.contains(&s.column) {
1337 local.push(s.column);
1338 }
1339 }
1340 let mut related = Vec::new();
1341 self.pred.related_deps(&mut related);
1342 Deps { local, related }
1343 }
1344}
1345
1346#[derive(Clone, Copy, PartialEq, Eq, Debug)]
1348pub enum Delta {
1349 Insert(usize, u64),
1350 Remove(usize, u64),
1351 Move(usize, usize, u64),
1352}
1353
1354#[derive(Clone, Debug, Default, PartialEq)]
1360pub struct Deps {
1361 pub local: Vec<&'static str>,
1363 pub related: Vec<RelatedDep>,
1365}
1366
1367#[derive(Clone, Debug, PartialEq)]
1369pub struct RelatedDep {
1370 pub owner: &'static str,
1372 pub field: &'static str,
1374 pub target_table: &'static str,
1375 pub columns: Vec<&'static str>,
1378}
1379
1380impl Deps {
1381 pub fn touches_local(&self, column: &str) -> bool {
1383 self.local.contains(&column)
1384 }
1385
1386 pub fn touches_related(&self, table: &str, column: &str) -> bool {
1388 self.related
1389 .iter()
1390 .any(|r| r.target_table == table && r.columns.contains(&column))
1391 }
1392
1393 pub fn related_tables(&self) -> Vec<&'static str> {
1396 let mut out: Vec<&'static str> = Vec::new();
1397 for r in &self.related {
1398 if !out.contains(&r.target_table) {
1399 out.push(r.target_table);
1400 }
1401 }
1402 out
1403 }
1404}
1405
1406#[derive(Clone, PartialEq, Debug)]
1408pub enum SetChange {
1409 Same,
1411 Deltas(Vec<Delta>),
1415 Reload,
1417}
1418
1419pub struct ResultSet {
1422 ids: Vec<u64>,
1423 fetch: Fetch,
1424 deps: Deps,
1425}
1426
1427impl ResultSet {
1428 pub fn new(fetch: Fetch) -> ResultSet {
1429 ResultSet {
1430 ids: Vec::new(),
1431 deps: fetch.dependencies(),
1432 fetch,
1433 }
1434 }
1435
1436 pub fn ids(&self) -> &[u64] {
1437 &self.ids
1438 }
1439
1440 pub fn fetch(&self) -> &Fetch {
1441 &self.fetch
1442 }
1443
1444 pub fn deps(&self) -> &Deps {
1446 &self.deps
1447 }
1448
1449 pub fn reset(&mut self, ids: Vec<u64>) {
1451 self.ids = ids;
1452 }
1453
1454 pub fn adopt(&mut self, new: Vec<u64>) -> SetChange {
1459 if self.ids == new {
1460 return SetChange::Same;
1461 }
1462 let old = std::mem::replace(&mut self.ids, new);
1463 let new = &self.ids;
1464
1465 let new_set: std::collections::HashSet<u64> = new.iter().copied().collect();
1466 let old_set: std::collections::HashSet<u64> = old.iter().copied().collect();
1467 let retained_old: Vec<u64> = old
1468 .iter()
1469 .copied()
1470 .filter(|k| new_set.contains(k))
1471 .collect();
1472 let retained_new: Vec<u64> = new
1473 .iter()
1474 .copied()
1475 .filter(|k| old_set.contains(k))
1476 .collect();
1477
1478 let mut deltas: Vec<Delta> = Vec::new();
1479 for (i, k) in old.iter().enumerate().rev() {
1481 if !new_set.contains(k) {
1482 deltas.push(Delta::Remove(i, *k));
1483 }
1484 }
1485 let mut scratch = retained_old.clone();
1486
1487 if retained_old != retained_new {
1488 let mismatch = retained_old
1492 .iter()
1493 .copied()
1494 .zip(retained_new.iter().copied())
1495 .find(|(a, b)| a != b);
1496 let mut moved = false;
1497 if let Some((a, b)) = mismatch {
1498 for k in [a, b] {
1499 let from = scratch.iter().position(|x| *x == k);
1500 let to = retained_new.iter().position(|x| *x == k);
1501 if let (Some(from), Some(to)) = (from, to) {
1502 let mut trial = scratch.clone();
1503 let key = trial.remove(from);
1504 trial.insert(to, key);
1505 if trial == retained_new {
1506 deltas.push(Delta::Move(from, to, k));
1507 scratch = trial;
1508 moved = true;
1509 break;
1510 }
1511 }
1512 }
1513 }
1514 if !moved {
1515 return SetChange::Reload;
1516 }
1517 }
1518
1519 for (i, k) in new.iter().enumerate() {
1521 if !old_set.contains(k) {
1522 deltas.push(Delta::Insert(i, *k));
1523 if i <= scratch.len() {
1524 scratch.insert(i, *k);
1525 } else {
1526 return SetChange::Reload;
1527 }
1528 }
1529 }
1530
1531 if scratch == *new {
1533 SetChange::Deltas(deltas)
1534 } else {
1535 SetChange::Reload
1536 }
1537 }
1538}
1539
1540pub(crate) fn find_match_query(pred: &Pred) -> Option<String> {
1541 match pred {
1542 Pred::Matches { query, .. } => Some(query.clone()),
1543 Pred::And(a, b) | Pred::Or(a, b) => find_match_query(a).or_else(|| find_match_query(b)),
1544 Pred::Not(a) => find_match_query(a),
1545 _ => None,
1546 }
1547}