navree-orm 1.0.0

Navree ORM: active-record models, a typed query builder, dirty tracking and pagination on PostgreSQL/TimescaleDB, MySQL and SQLite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! The query builders: [`Builder<M>`] for models, [`TableBuilder`] for any
//! table, with [`WhereGroup`] and [`JoinClause`] for nested conditions.
//!
//! Builder methods take `self` and return `Self`, so queries chain;
//! terminal methods (`get`, `first`, `count`, `update`, ...) take the
//! executor ([`Db`](navree_db::Db), [`Uow`](navree_db::Uow), ...) first and
//! run the query.

use std::fmt;
use std::marker::PhantomData;

mod bulk;
pub(crate) mod clauses;
mod fetch;
pub(crate) mod filters;
mod join;
mod table;
mod write;

pub use bulk::ConflictUpdate;
pub use clauses::{HasQuery, IntoSelect};
pub use fetch::Selectable;
pub use filters::{HasWheres, WhereGroup};
pub use join::JoinClause;
pub use table::{TableBuilder, table};

use crate::attributes::extras::ExtraCasts;
use crate::column::{Filter, IntoQuery};
use crate::error::Result;
use crate::ir::{ColumnRef, Expr, InValues, QueryIr, SelectItem, Where, WhereClause};
use crate::model::Model;
use crate::scope::{GlobalScope, ScopeChoices, Trashed, group_scope_wheres};
use crate::sql::{Dialect, Statement, render_select};
use crate::value::Field;

use self::clauses::clauses;
use self::filters::filters;

/// A query of model `M`: the where family, selects, joins, groups,
/// orders, limits, scopes, and the retrieval, aggregate, pagination and
/// mass-write methods that run it.
///
/// ```no_run
/// # use navree_orm::__doc_models::*;
/// # async fn demo(db: Db, me: i64, page: PageQuery) -> Result<()> {
/// let won = Deal::query()
///     .where_(Deal::STAGE.eq(Stage::Won))
///     .where_group(|g| g.where_(Deal::OWNER_ID.eq(me)).or_where_null(Deal::OWNER_ID))
///     .latest()
///     .paginate(&db, page)
///     .await?;
/// # Ok(()) }
/// ```
///
/// A query selects the model's columns (qualified by its table, so joins
/// stay unambiguous) unless `select` replaces them; columns added with
/// `add_select`, `select_raw`, `select_sub` and friends come back as the
/// records' [extras](crate::Record::extras). Global scopes (and the
/// soft-delete scope) apply when the query runs.
pub struct Builder<M: Model> {
    pub(crate) ir: QueryIr,
    pub(crate) scopes: ScopeChoices,
    pub(crate) scopes_applied: bool,
    /// The relations the query loads into its records (and its depth in
    /// existence subqueries); see [`crate::relations`].
    pub(crate) eager: crate::relations::eager::EagerState,
    /// Query-time casts of extra columns (`with_casts`).
    pub(crate) extra_casts: ExtraCasts,
    _model: PhantomData<fn() -> M>,
}

impl<M: Model> Builder<M> {
    /// A query of every record of `M`.
    pub fn new() -> Self {
        Builder {
            ir: QueryIr::new(M::meta().table()),
            scopes: ScopeChoices::default(),
            scopes_applied: false,
            eager: crate::relations::eager::EagerState::default(),
            extra_casts: ExtraCasts::default(),
            _model: PhantomData,
        }
    }

    filters!();
    clauses!();

    /// Only the record with key `key`.
    pub fn where_key(self, key: impl Into<M::Key>) -> Self {
        let key = key.into();
        self.where_(Filter::compare(
            key_expr::<M>(),
            crate::ir::Operator::Eq,
            Expr::Value(key.to_value()),
        ))
    }

    /// Every record but the one with key `key`.
    pub fn where_key_not(self, key: impl Into<M::Key>) -> Self {
        let key = key.into();
        self.where_(Filter::compare(
            key_expr::<M>(),
            crate::ir::Operator::Ne,
            Expr::Value(key.to_value()),
        ))
    }

    /// Only the records with these keys (none for an empty list).
    pub fn where_key_in(self, keys: impl IntoIterator<Item = impl Into<M::Key>>) -> Self {
        let values = keys
            .into_iter()
            .map(|key| Expr::Value(key.into().to_value()))
            .collect();
        self.where_(Filter(Where::In {
            expr: key_expr::<M>(),
            values: InValues::List(values),
            not: false,
        }))
    }

    /// Every record but the ones with these keys.
    pub fn where_key_not_in(self, keys: impl IntoIterator<Item = impl Into<M::Key>>) -> Self {
        let values = keys
            .into_iter()
            .map(|key| Expr::Value(key.into().to_value()))
            .collect();
        self.where_(Filter(Where::In {
            expr: key_expr::<M>(),
            values: InValues::List(values),
            not: true,
        }))
    }

    /// Applies local scope `scope` (a function of the builder). The
    /// conditions it adds are grouped when they (or the ones before them)
    /// contain an `OR`, so a scope never changes the meaning of the
    /// conditions around it. `#[scopes]` turns scope functions into builder
    /// methods; this is the plain form.
    ///
    /// ```no_run
    /// # use navree_orm::__doc_models::*;
    /// # async fn demo(db: Db) -> Result<()> {
    /// fn open(query: Builder<Deal>) -> Builder<Deal> {
    ///     query.where_null(Deal::CLOSED_AT)
    /// }
    ///
    /// let open_deals = Deal::query().scope(open).get(&db).await?;
    /// # Ok(()) }
    /// ```
    pub fn scope(mut self, scope: impl FnOnce(Self) -> Self) -> Self {
        let before = self.ir.wheres.len();
        self = scope(self);
        group_scope_wheres(&mut self.ir, before);
        self
    }

    /// Reads the model's rows from `query` (a derived table) instead of its
    /// table: `FROM (subquery) AS <the model's table>`. The derived table
    /// takes the table's name, so typed columns, keys and scopes keep
    /// working; the subquery must return the model's columns. Useful for a
    /// union of model queries, or a window over rows before filtering.
    ///
    /// ```
    /// # use navree_orm::__doc_models::*;
    /// let either = Deal::query()
    ///     .where_(Deal::STAGE.eq(Stage::Won))
    ///     .union(Deal::query().where_(Deal::AMOUNT_CENTS.gte(100_000)));
    /// let sql = Deal::query().from_sub(either).order_by_desc(Deal::AMOUNT_CENTS).limit(10).to_sql(Dialect::Postgres)?;
    /// assert!(sql.sql.ends_with(r#") AS "deals" ORDER BY "deals"."amount_cents" DESC LIMIT $3"#));
    /// # Ok::<(), navree_orm::OrmError>(())
    /// ```
    pub fn from_sub(mut self, query: impl IntoQuery) -> Self {
        self.ir.table = crate::ir::TableRef::subquery(query.into_query(), M::meta().table());
        self
    }

    /// Leaves out global scope `S` for this query.
    pub fn without_global_scope<S: GlobalScope<M>>(mut self) -> Self {
        self.scopes.ids.push(std::any::TypeId::of::<S>());
        self
    }

    /// Leaves out every global scope (the soft-delete scope included;
    /// `without_trashed()` and `only_trashed()` still apply).
    pub fn without_global_scopes(mut self) -> Self {
        self.scopes.all = true;
        self
    }

    /// Includes soft-deleted records (removes the soft-delete scope). Does
    /// nothing for a model without soft deletes.
    pub fn with_trashed(mut self) -> Self {
        self.scopes.trashed = Trashed::Included;
        self
    }

    /// Newest first by the model's creation timestamp. Fails when the query
    /// runs if the model keeps none; use [`latest_by`](Self::latest_by).
    pub fn latest(self) -> Self {
        match M::meta().created_at() {
            Some(column) => self.latest_by(ColumnRef::new(Some(M::meta().table()), column)),
            None => self.with_error(format!(
                "{} keeps no creation timestamp; use latest_by(column)",
                M::meta().name()
            )),
        }
    }

    /// Oldest first by the model's creation timestamp. Fails when the query
    /// runs if the model keeps none; use [`oldest_by`](Self::oldest_by).
    pub fn oldest(self) -> Self {
        match M::meta().created_at() {
            Some(column) => self.oldest_by(ColumnRef::new(Some(M::meta().table()), column)),
            None => self.with_error(format!(
                "{} keeps no creation timestamp; use oldest_by(column)",
                M::meta().name()
            )),
        }
    }

    /// Where the query asks to run: the connection it names, the model's
    /// connection, and whether its reads go to the writer.
    pub(crate) fn route(&self) -> crate::Route<'_> {
        self.ir.route(M::meta().connection_name())
    }

    /// Records a problem found while building; the query fails with it
    /// when it runs.
    pub(crate) fn with_error(mut self, message: String) -> Self {
        self.ir.errors.push(message);
        self
    }

    /// The query as built so far, without global scopes and default
    /// columns (for inspection; [`into_ir`](Self::into_ir) gives what
    /// runs).
    pub fn ir(&self) -> &QueryIr {
        &self.ir
    }

    /// The query that runs: global scopes applied and the model's columns
    /// selected when nothing else is.
    pub fn into_ir(self) -> QueryIr {
        let mut builder = self.apply_global_scopes();
        if builder.ir.columns.is_empty() {
            builder.ir.columns = model_columns::<M>(builder.ir.table.qualifier());
        }
        builder.ir
    }

    /// The statement the query compiles to for `dialect` (global scopes
    /// applied), without running it.
    ///
    /// ```
    /// # use navree_orm::__doc_models::*;
    /// # fn main() -> Result<()> {
    /// let sql = Company::query().where_(Company::ACTIVE.eq(true)).to_sql(Dialect::Postgres)?;
    /// assert_eq!(
    ///     sql.sql,
    ///     r#"SELECT "companies"."id", "companies"."name", "companies"."active" FROM "companies" WHERE "companies"."active" = $1"#
    /// );
    /// assert_eq!(sql.values, [Value::from(true)]);
    /// # Ok(()) }
    /// ```
    pub fn to_sql(&self, dialect: Dialect) -> Result<Statement> {
        render_select(&self.clone().into_ir(), dialect)
    }

    /// The builder with its global scopes applied (once): the model's
    /// scopes the query keeps, each grouped, then the query's own choice
    /// of trashed rows (`without_trashed`, `only_trashed`).
    pub(crate) fn apply_global_scopes(mut self) -> Self {
        if self.scopes_applied {
            return self;
        }
        self.scopes_applied = true;
        for scope in crate::global_scopes::scopes_of::<M>() {
            if self.scopes.removes(scope.as_ref()) {
                continue;
            }
            let before = self.ir.wheres.len();
            self = scope.apply(self);
            group_scope_wheres(&mut self.ir, before);
        }
        if let Some(deleted_at) = M::meta().deleted_at() {
            let not = match self.scopes.trashed {
                Trashed::Excluded => false,
                Trashed::Only => true,
                Trashed::Hidden | Trashed::Included => return self,
            };
            let before = self.ir.wheres.len();
            self = self.where_(Filter(Where::Null {
                expr: Expr::Column(ColumnRef::new(Some(M::meta().table()), deleted_at)),
                not,
            }));
            group_scope_wheres(&mut self.ir, before);
        }
        self
    }
}

/// `query` reading `items` (`value`, `pluck`) instead of its select list.
/// A query with unions keeps its select list (every part must line up with
/// the first) and reads `items` from the union as a derived table named
/// like its table, in the union's order.
pub(crate) fn reading_values(
    mut query: QueryIr,
    items: Vec<SelectItem>,
    default_columns: Vec<SelectItem>,
) -> QueryIr {
    if query.unions.is_empty() {
        query.columns = items;
        return query;
    }
    let qualifier = query.table.qualifier().to_owned();
    if query.columns.is_empty() {
        query.columns = default_columns;
    }
    let mut outer = QueryIr::new(qualifier.clone());
    // The union's order sorts the values; inside, it only picks the rows
    // under a limit or an offset.
    outer.orders = query
        .orders
        .iter()
        .map(|order| match order {
            crate::ir::Order::By {
                expr: Expr::Column(column),
                direction,
                nulls,
            } => crate::ir::Order::By {
                expr: Expr::Column(ColumnRef {
                    table: Some(qualifier.clone().into()),
                    ..column.clone()
                }),
                direction: *direction,
                nulls: *nulls,
            },
            other => other.clone(),
        })
        .collect();
    if query.limit.is_none() && query.offset.is_none() {
        query.orders.clear();
    }
    // Where and how long the query runs stay with it.
    outer.connection = query.connection.clone();
    outer.use_writer = query.use_writer;
    outer.timeout = query.timeout;
    outer.table = crate::ir::TableRef::subquery(query, qualifier);
    outer.columns = items;
    outer
}

/// Where a read of `query` runs: a locking read where the executor writes
/// (`&Db` joins the task's unit of work, beginning its transaction, since
/// a lock outside a transaction ends with the statement); others where it
/// reads.
pub(crate) fn read_access(query: &QueryIr) -> crate::exec::Access {
    if query.lock.is_some() {
        crate::exec::Access::Write
    } else {
        crate::exec::Access::Read
    }
}

/// Where an operation of model `M` that has no query runs (a record's
/// save, delete or refresh): on the model's connection.
pub(crate) fn model_route<M: Model>() -> crate::Route<'static> {
    crate::Route::new().for_model(M::meta().connection_name())
}

/// The model's key column, qualified by its table.
pub(crate) fn key_expr<M: Model>() -> Expr {
    let meta = M::meta();
    Expr::Column(ColumnRef::new(Some(meta.table()), meta.key_column()))
}

/// The model's columns, qualified by `table`.
pub(crate) fn model_columns<M: Model>(table: &str) -> Vec<SelectItem> {
    M::meta()
        .all_columns()
        .iter()
        .map(|column| SelectItem {
            expr: Expr::Column(ColumnRef::new(Some(table.to_owned()), column.name())),
            alias: None,
        })
        .collect()
}

impl<M: Model> Default for Builder<M> {
    fn default() -> Self {
        Builder::new()
    }
}

impl<M: Model> Clone for Builder<M> {
    fn clone(&self) -> Self {
        Builder {
            ir: self.ir.clone(),
            scopes: self.scopes.clone(),
            scopes_applied: self.scopes_applied,
            eager: self.eager.clone(),
            extra_casts: self.extra_casts.clone(),
            _model: PhantomData,
        }
    }
}

impl<M: Model> fmt::Debug for Builder<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("Builder");
        debug
            .field("model", &M::meta().name())
            .field("query", &self.ir);
        if !self.eager.tree.is_empty() {
            debug.field("with", &self.eager.tree.paths());
        }
        debug.finish_non_exhaustive()
    }
}

impl<M: Model> HasWheres for Builder<M> {
    fn wheres_mut(&mut self) -> &mut Vec<WhereClause> {
        &mut self.ir.wheres
    }
}

impl<M: Model> HasQuery for Builder<M> {
    fn query_mut(&mut self) -> &mut QueryIr {
        &mut self.ir
    }

    fn default_select(&self) -> Vec<SelectItem> {
        model_columns::<M>(self.ir.table.qualifier())
    }
}

/// A model query as a subquery (`where_in_sub`, `where_exists`,
/// `select_sub`, ...), its global scopes applied.
impl<M: Model> IntoQuery for Builder<M> {
    fn into_query(self) -> QueryIr {
        self.into_ir()
    }
}