Skip to main content

no_mistakes_postgres_sql/statement_facts/
iteration.rs

1/// One `LIMIT` / `FETCH FIRST` row cap, at the position of its count.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct SqlLimitFact {
4    pub line: usize,
5    pub column: usize,
6    pub value: SqlLimitValue,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum SqlLimitValue {
11    /// An integer literal. `FETCH FIRST ROW ONLY` counts as `Literal(1)`.
12    Literal(u64),
13    /// A placeholder, an expression, or a `FETCH FIRST n PERCENT`.
14    Other,
15}
16
17/// A limited `SELECT` over one base table, ordered by plain columns of that table: the shape of
18/// a page of a keyset walk. Only the WHERE conjuncts decide whether the walk is selective.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct SqlSweepFact {
21    pub line: usize,
22    /// The column where the table is written, which decides the owning operand of recovered SQL.
23    pub column: usize,
24    /// The base relation, as written and normalized.
25    pub table: String,
26    /// The parts of that name, so a quoted dot is not read as a schema qualifier.
27    pub table_parts: Vec<String>,
28    /// The ORDER BY columns, in order.
29    pub order_columns: Vec<String>,
30    /// Top-level `AND` conjuncts of WHERE (none when there is no WHERE).
31    pub conjuncts: Vec<SqlConjunctFact>,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct SqlConjunctFact {
36    /// The conjunct's SQL tokens, folding only unquoted words and separating tokens.
37    pub text: String,
38    /// The columns it compares with a bind parameter as a keyset cursor (`id > $1`,
39    /// `(a, b) > ($1, $2)`, or `($1 IS NULL OR id > $1)`); empty for any other predicate.
40    pub cursor_columns: Vec<String>,
41    /// Which side of those columns the bind bounds; `None` when the conjunct is not a cursor.
42    pub cursor_bound: Option<SqlCursorBound>,
43    /// The cursor can be switched off by a NULL bind (`($1 IS NULL OR id > $1)`), so it does not
44    /// bound the walk unconditionally.
45    pub cursor_optional: bool,
46    /// `TRUE` or `1 = 1`: a conjunct a query builder seeds a WHERE with, which selects nothing.
47    pub constant_true: bool,
48    /// A bind-only condition independent of every row, without calls or subqueries.
49    pub bind_guard: bool,
50}
51
52/// The direction of a cursor comparison: `id > $1` bounds `id` from below, `id < $1` from above.
53/// A range (`id >= $1 AND id < $2`) has both, which narrows the walk.
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum SqlCursorBound {
56    Lower,
57    Upper,
58}