medoo_rs 0.1.0

Query builder dinámico multi-backend (Postgres/MySQL/SQLite) inspirado en Medoo (PHP). Núcleo sin dependencias, pool async opcional.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use crate::backend::Backend;
use crate::cond::{render_cond, Binder, Cond};
use crate::error::{QueryError, Result};
use crate::ident;
use crate::log::{LogCategory, Query};
use crate::value::{IntoValue, Value};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderDir {
    Asc,
    Desc,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinKind {
    Inner,
    Left,
    Right,
    Full,
    Cross,
}

impl JoinKind {
    fn as_sql(&self) -> &'static str {
        match self {
            JoinKind::Inner => "INNER JOIN",
            JoinKind::Left => "LEFT JOIN",
            JoinKind::Right => "RIGHT JOIN",
            JoinKind::Full => "FULL JOIN",
            JoinKind::Cross => "CROSS JOIN",
        }
    }
}

#[derive(Debug, Clone)]
enum JoinTarget {
    Table(String),
    /// LATERAL (subquery) AS alias. Soportado en PG y MySQL 8.0.14+.
    Lateral { sub: Box<SelectQuery>, alias: String },
}

#[derive(Debug, Clone)]
struct Join {
    kind: JoinKind,
    target: JoinTarget,
    on: Option<String>, // None para CROSS JOIN
}

#[derive(Debug, Clone)]
struct Cte {
    name: String,
    query: Box<SelectQuery>,
}

#[derive(Debug, Clone)]
pub struct SelectQuery {
    backend: Backend,
    table: String,
    columns: Vec<String>, // vacío => "*"
    distinct: bool,
    joins: Vec<Join>,
    wheres: Vec<Cond>,
    group_by: Vec<String>,
    having: Vec<Cond>,
    order_by: Vec<(String, OrderDir)>,
    limit: Option<u64>,
    offset: Option<u64>,
    ctes: Vec<Cte>,
    cte_recursive: bool,
}

impl SelectQuery {
    pub fn new(backend: Backend, table: &str) -> Self {
        Self {
            backend,
            table: table.to_string(),
            columns: Vec::new(),
            distinct: false,
            joins: Vec::new(),
            wheres: Vec::new(),
            group_by: Vec::new(),
            having: Vec::new(),
            order_by: Vec::new(),
            limit: None,
            offset: None,
            ctes: Vec::new(),
            cte_recursive: false,
        }
    }

    pub fn columns<I: IntoIterator<Item = S>, S: Into<String>>(mut self, cols: I) -> Self {
        self.columns = cols.into_iter().map(|s| s.into()).collect();
        self
    }

    pub fn distinct(mut self) -> Self {
        self.distinct = true;
        self
    }

    pub fn where_eq<C: Into<String>, V: IntoValue>(mut self, col: C, v: V) -> Self {
        self.wheres.push(Cond::eq(col, v));
        self
    }

    pub fn where_op<C: Into<String>, V: IntoValue>(mut self, col: C, op: &str, v: V) -> Self {
        match Cond::op(col, op, v) {
            Ok(c) => self.wheres.push(c),
            Err(e) => panic!("medoo_rs: where_op operador inválido en literal: {}", e),
        }
        self
    }

    /// Versión non-panic: retorna `Result<Self, QueryError>`. Usá esta
    /// cuando el operador viene de input dinámico (config, query string).
    pub fn try_where_op<C: Into<String>, V: IntoValue>(
        mut self,
        col: C,
        op: &str,
        v: V,
    ) -> Result<Self> {
        self.wheres.push(Cond::op(col, op, v)?);
        Ok(self)
    }

    /// Versión non-panic de `where_eq`. `where_eq` ya no panicea hoy
    /// (siempre construye un `Cond::Cmp`), pero se incluye por simetría
    /// y para retornar `Result` cuando se compone en cadenas fallibles.
    pub fn try_where_eq<C: Into<String>, V: IntoValue>(
        mut self,
        col: C,
        v: V,
    ) -> Result<Self> {
        self.wheres.push(Cond::eq(col, v));
        Ok(self)
    }

    pub fn where_in<C: Into<String>, V: IntoValue, I: IntoIterator<Item = V>>(
        mut self,
        col: C,
        vals: I,
    ) -> Self {
        self.wheres.push(Cond::r#in(col, vals));
        self
    }

    pub fn where_between<C: Into<String>, A: IntoValue, B: IntoValue>(
        mut self,
        col: C,
        lo: A,
        hi: B,
    ) -> Self {
        self.wheres.push(Cond::Between {
            col: col.into(),
            lo: lo.into_value(),
            hi: hi.into_value(),
        });
        self
    }

    /// `WHERE col BETWEEN lo_col AND hi_col` (ambos inclusive).
    /// Útil para "fecha entre fecha_inicio y fecha_fin".
    pub fn where_between_cols<C, L, H>(self, col: C, lo_col: L, hi_col: H) -> Self
    where C: Into<String>, L: Into<String>, H: Into<String> {
        self.where_between_cols_with(col, lo_col, hi_col, true, true)
    }

    /// Versión con flags de inclusividad por límite. `false` deja `<` o `>`.
    pub fn where_between_cols_with<C, L, H>(
        mut self,
        col: C,
        lo_col: L,
        hi_col: H,
        inclusive_lo: bool,
        inclusive_hi: bool,
    ) -> Self
    where C: Into<String>, L: Into<String>, H: Into<String> {
        self.wheres.push(Cond::BetweenCols {
            col: col.into(),
            lo_col: lo_col.into(),
            hi_col: hi_col.into(),
            inclusive_lo,
            inclusive_hi,
        });
        self
    }

    /// `WHERE lo_col <= val AND val <= hi_col` — un valor entre dos
    /// columnas. Útil para "el día de hoy cae entre fecha_desde y
    /// fecha_hasta de la promo".
    pub fn where_value_in_range<V, L, H>(self, val: V, lo_col: L, hi_col: H) -> Self
    where V: IntoValue, L: Into<String>, H: Into<String> {
        self.where_value_in_range_with(val, lo_col, hi_col, true, true)
    }

    pub fn where_value_in_range_with<V, L, H>(
        mut self,
        val: V,
        lo_col: L,
        hi_col: H,
        inclusive_lo: bool,
        inclusive_hi: bool,
    ) -> Self
    where V: IntoValue, L: Into<String>, H: Into<String> {
        self.wheres.push(Cond::ValueInRange {
            val: val.into_value(),
            lo_col: lo_col.into(),
            hi_col: hi_col.into(),
            inclusive_lo,
            inclusive_hi,
        });
        self
    }

    /// `col LIKE 'pattern'`. Pasá `%`/`_` en el pattern como necesites.
    pub fn where_like<C: Into<String>, P: Into<String>>(mut self, col: C, pattern: P) -> Self {
        self.wheres.push(Cond::Cmp {
            col: col.into(),
            op: crate::cond::Operator::Like,
            val: Value::Text(pattern.into()),
        });
        self
    }
    /// `col NOT LIKE 'pattern'`.
    pub fn where_not_like<C: Into<String>, P: Into<String>>(mut self, col: C, pattern: P) -> Self {
        self.wheres.push(Cond::Cmp {
            col: col.into(),
            op: crate::cond::Operator::NotLike,
            val: Value::Text(pattern.into()),
        });
        self
    }
    /// `col LIKE 'prefix%'`. Auto-escapa `%` y `_` del prefix.
    pub fn where_starts_with<C: Into<String>, P: AsRef<str>>(self, col: C, prefix: P) -> Self {
        let p = format!("{}%", escape_like(prefix.as_ref()));
        self.where_like(col, p)
    }
    /// `col LIKE '%suffix'`.
    pub fn where_ends_with<C: Into<String>, P: AsRef<str>>(self, col: C, suffix: P) -> Self {
        let p = format!("%{}", escape_like(suffix.as_ref()));
        self.where_like(col, p)
    }
    /// `col LIKE '%needle%'`.
    pub fn where_contains<C: Into<String>, P: AsRef<str>>(self, col: C, needle: P) -> Self {
        let p = format!("%{}%", escape_like(needle.as_ref()));
        self.where_like(col, p)
    }
    /// PG `ILIKE` (case-insensitive). En MySQL/SQLite cae a
    /// `LOWER(col) LIKE LOWER(?)` para resultar equivalente.
    pub fn where_ilike<C: Into<String>, P: Into<String>>(mut self, col: C, pattern: P) -> Self {
        let pat = pattern.into();
        match self.backend {
            Backend::Postgres => {
                let raw = format!("{} ILIKE ?", crate::ident::quote(self.backend, &col.into()).unwrap_or_default());
                self.wheres.push(Cond::Raw { sql: raw, params: vec![Value::Text(pat)] });
            }
            _ => {
                let qcol = crate::ident::quote(self.backend, &col.into()).unwrap_or_default();
                let raw = format!("LOWER({}) LIKE LOWER(?)", qcol);
                self.wheres.push(Cond::Raw { sql: raw, params: vec![Value::Text(pat)] });
            }
        }
        self
    }

    pub fn where_null<C: Into<String>>(mut self, col: C) -> Self {
        self.wheres.push(Cond::IsNull { col: col.into(), negate: false });
        self
    }

    pub fn where_not_null<C: Into<String>>(mut self, col: C) -> Self {
        self.wheres.push(Cond::IsNull { col: col.into(), negate: true });
        self
    }

    /// `WHERE JSON_UNQUOTE(JSON_EXTRACT(col,'$.path')) <op> ?` (MySQL)
    /// — equivalente Postgres `col #>> '{path}' <op> ?` y SQLite
    /// `json_extract(col,'$.path') <op> ?`.
    pub fn where_json<C: Into<String>, V: IntoValue>(
        mut self,
        col: C,
        path: &str,
        op: &str,
        v: V,
    ) -> Self {
        match crate::cond::Cond::json_get(col, path, op, v) {
            Ok(c) => self.wheres.push(c),
            Err(e) => panic!("medoo_rs: where_json inválido: {}", e),
        }
        self
    }

    /// `JSON_CONTAINS(col, ?)` / `col @> ?::jsonb`. SQLite no soportado.
    pub fn where_json_contains<C: Into<String>>(mut self, col: C, json: Value) -> Self {
        self.wheres.push(crate::cond::Cond::json_contains(col, json));
        self
    }

    /// `WHERE col IN (SELECT ...)`.
    pub fn where_in_subquery<C: Into<String>>(mut self, col: C, sub: SelectQuery) -> Self {
        self.wheres.push(crate::cond::Cond::in_subquery(col, sub));
        self
    }
    /// `WHERE col NOT IN (SELECT ...)`.
    pub fn where_not_in_subquery<C: Into<String>>(mut self, col: C, sub: SelectQuery) -> Self {
        self.wheres.push(crate::cond::Cond::not_in_subquery(col, sub));
        self
    }
    /// `WHERE EXISTS (SELECT ...)`.
    pub fn where_exists(mut self, sub: SelectQuery) -> Self {
        self.wheres.push(crate::cond::Cond::exists(sub));
        self
    }
    /// `WHERE NOT EXISTS (SELECT ...)`.
    pub fn where_not_exists(mut self, sub: SelectQuery) -> Self {
        self.wheres.push(crate::cond::Cond::not_exists(sub));
        self
    }
    /// `WHERE col <op> (SELECT v)` — comparación con subquery escalar.
    /// El operador acepta los mismos formatos que `where_op`.
    pub fn where_scalar<C: Into<String>>(mut self, col: C, op: &str, sub: SelectQuery) -> Self {
        match crate::cond::Cond::scalar_cmp(col, op, sub) {
            Ok(c) => self.wheres.push(c),
            Err(e) => panic!("medoo_rs: where_scalar operador inválido: {}", e),
        }
        self
    }

    /// Agrega un CTE: `WITH <name> AS (<sub>) SELECT ...`. Se pueden
    /// encadenar varios; salen separados por coma.
    pub fn with<N: Into<String>>(mut self, name: N, sub: SelectQuery) -> Self {
        self.ctes.push(Cte { name: name.into(), query: Box::new(sub) });
        self
    }
    /// Marca los CTEs como `WITH RECURSIVE`. Soportado en PG/SQLite/
    /// MySQL 8+/MariaDB 10.2+.
    pub fn with_recursive_flag(mut self) -> Self {
        self.cte_recursive = true;
        self
    }

    pub fn where_raw<S: Into<String>>(mut self, sql: S, params: Vec<Value>) -> Self {
        self.wheres.push(Cond::Raw { sql: sql.into(), params });
        self
    }

    pub fn where_cond(mut self, c: Cond) -> Self {
        self.wheres.push(c);
        self
    }

    pub fn or_where(mut self, conds: Vec<Cond>) -> Self {
        self.wheres.push(Cond::Or(conds));
        self
    }

    pub fn join_on<T: Into<String>, O: Into<String>>(mut self, kind: JoinKind, table: T, on: O) -> Self {
        self.joins.push(Join {
            kind,
            target: JoinTarget::Table(table.into()),
            on: Some(on.into()),
        });
        self
    }
    pub fn inner_join<T: Into<String>, O: Into<String>>(self, t: T, on: O) -> Self {
        self.join_on(JoinKind::Inner, t, on)
    }
    pub fn left_join<T: Into<String>, O: Into<String>>(self, t: T, on: O) -> Self {
        self.join_on(JoinKind::Left, t, on)
    }
    pub fn right_join<T: Into<String>, O: Into<String>>(self, t: T, on: O) -> Self {
        self.join_on(JoinKind::Right, t, on)
    }
    pub fn cross_join<T: Into<String>>(mut self, t: T) -> Self {
        self.joins.push(Join {
            kind: JoinKind::Cross,
            target: JoinTarget::Table(t.into()),
            on: None,
        });
        self
    }

    /// `INNER JOIN LATERAL (subquery) AS <alias> ON <cond>` (PG / MySQL 8.0.14+).
    pub fn inner_join_lateral<A: Into<String>, O: Into<String>>(
        mut self,
        sub: SelectQuery,
        alias: A,
        on: O,
    ) -> Self {
        self.joins.push(Join {
            kind: JoinKind::Inner,
            target: JoinTarget::Lateral { sub: Box::new(sub), alias: alias.into() },
            on: Some(on.into()),
        });
        self
    }
    /// `LEFT JOIN LATERAL (subquery) AS <alias> ON <cond>`.
    pub fn left_join_lateral<A: Into<String>, O: Into<String>>(
        mut self,
        sub: SelectQuery,
        alias: A,
        on: O,
    ) -> Self {
        self.joins.push(Join {
            kind: JoinKind::Left,
            target: JoinTarget::Lateral { sub: Box::new(sub), alias: alias.into() },
            on: Some(on.into()),
        });
        self
    }
    /// `CROSS JOIN LATERAL (subquery) AS <alias>` — sin ON.
    pub fn cross_join_lateral<A: Into<String>>(mut self, sub: SelectQuery, alias: A) -> Self {
        self.joins.push(Join {
            kind: JoinKind::Cross,
            target: JoinTarget::Lateral { sub: Box::new(sub), alias: alias.into() },
            on: None,
        });
        self
    }

    pub fn order_by<C: Into<String>>(mut self, col: C, dir: OrderDir) -> Self {
        self.order_by.push((col.into(), dir));
        self
    }
    pub fn order_asc<C: Into<String>>(self, col: C) -> Self {
        self.order_by(col, OrderDir::Asc)
    }
    pub fn order_desc<C: Into<String>>(self, col: C) -> Self {
        self.order_by(col, OrderDir::Desc)
    }

    pub fn group_by<C: Into<String>>(mut self, col: C) -> Self {
        self.group_by.push(col.into());
        self
    }

    pub fn having(mut self, c: Cond) -> Self {
        self.having.push(c);
        self
    }

    pub fn limit(mut self, n: u64) -> Self {
        self.limit = Some(n);
        self
    }

    pub fn offset(mut self, n: u64) -> Self {
        self.offset = Some(n);
        self
    }

    pub fn to_sql(&self) -> Result<(String, Vec<Value>)> {
        let mut b = Binder::new(self.backend);
        let sql = self.render_into(&mut b)?;
        Ok((sql, b.into_params()))
    }

    /// Renderiza la query usando un Binder externo (para subqueries).
    /// Los placeholders se asignan en orden global con los del outer.
    pub(crate) fn render_into(&self, b: &mut Binder) -> Result<String> {
        let mut sql = String::new();
        if !self.ctes.is_empty() {
            sql.push_str(if self.cte_recursive { "WITH RECURSIVE " } else { "WITH " });
            let mut parts = Vec::with_capacity(self.ctes.len());
            for cte in &self.ctes {
                ident::validate(&cte.name)?;
                let inner = cte.query.render_into(b)?;
                parts.push(format!(
                    "{} AS ({})",
                    self.backend.quote_ident(&cte.name),
                    inner
                ));
            }
            sql.push_str(&parts.join(", "));
            sql.push(' ');
        }
        sql.push_str("SELECT ");
        if self.distinct {
            sql.push_str("DISTINCT ");
        }
        if self.columns.is_empty() {
            sql.push('*');
        } else {
            let cols: Result<Vec<String>> = self
                .columns
                .iter()
                .map(|c| render_column(self.backend, c))
                .collect();
            sql.push_str(&cols?.join(", "));
        }
        sql.push_str(" FROM ");
        sql.push_str(&ident::quote(self.backend, &self.table)?);

        for j in &self.joins {
            sql.push(' ');
            sql.push_str(j.kind.as_sql());
            sql.push(' ');
            match &j.target {
                JoinTarget::Table(t) => {
                    sql.push_str(&ident::quote(self.backend, t)?);
                }
                JoinTarget::Lateral { sub, alias } => {
                    ident::validate(alias)?;
                    sql.push_str("LATERAL (");
                    sql.push_str(&sub.render_into(b)?);
                    sql.push_str(") AS ");
                    sql.push_str(&self.backend.quote_ident(alias));
                }
            }
            if let Some(on) = &j.on {
                sql.push_str(" ON ");
                sql.push_str(&render_join_on(self.backend, on)?);
            }
        }

        if !self.wheres.is_empty() {
            sql.push_str(" WHERE ");
            let parts: Result<Vec<String>> = self.wheres.iter().map(|c| render_cond(c, b)).collect();
            sql.push_str(&parts?.join(" AND "));
        }

        if !self.group_by.is_empty() {
            sql.push_str(" GROUP BY ");
            let parts: Result<Vec<String>> = self
                .group_by
                .iter()
                .map(|c| ident::quote(self.backend, c))
                .collect();
            sql.push_str(&parts?.join(", "));
        }

        if !self.having.is_empty() {
            sql.push_str(" HAVING ");
            let parts: Result<Vec<String>> = self.having.iter().map(|c| render_cond(c, b)).collect();
            sql.push_str(&parts?.join(" AND "));
        }

        if !self.order_by.is_empty() {
            sql.push_str(" ORDER BY ");
            let parts: Result<Vec<String>> = self
                .order_by
                .iter()
                .map(|(c, d)| {
                    let qc = ident::quote(self.backend, c)?;
                    Ok(format!("{} {}", qc, if matches!(d, OrderDir::Asc) { "ASC" } else { "DESC" }))
                })
                .collect();
            sql.push_str(&parts?.join(", "));
        }

        if let Some(l) = self.limit {
            sql.push_str(&format!(" LIMIT {}", l));
        }
        if let Some(o) = self.offset {
            sql.push_str(&format!(" OFFSET {}", o));
        }

        Ok(sql)
    }
}

/// Escapa `%`, `_` y `\` en input de usuario para `starts_with` /
/// `ends_with` / `contains`. Usa `\` como escape char (default LIKE).
fn escape_like(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '%' | '_' | '\\' => {
                out.push('\\');
                out.push(c);
            }
            _ => out.push(c),
        }
    }
    out
}

fn render_column(backend: Backend, c: &str) -> Result<String> {
    if c == "*" {
        return Ok("*".to_string());
    }
    let (expr, alias) = match split_alias(c) {
        Some((e, a)) => (e, Some(a)),
        None => (c, None),
    };
    let expr_sql = if expr.contains('(') {
        // raw fragment (agregado u operador); pasa sin quotar pero rechaza
        // ';' o '--' por defensa básica.
        if expr.contains(';') || expr.contains("--") {
            return Err(QueryError::InvalidIdentifier(expr.to_string()));
        }
        expr.to_string()
    } else {
        ident::quote(backend, expr)?
    };
    if let Some(a) = alias {
        ident::validate(a)?;
        Ok(format!("{} AS {}", expr_sql, backend.quote_ident(a)))
    } else {
        Ok(expr_sql)
    }
}

fn split_alias(c: &str) -> Option<(&str, &str)> {
    // soporta "expr AS alias" (case insensitive) o "expr alias" no — exigimos AS para ser explícitos.
    let lower = c.to_ascii_lowercase();
    let idx = lower.find(" as ")?;
    let expr = c[..idx].trim();
    let alias = c[idx + 4..].trim();
    if expr.is_empty() || alias.is_empty() {
        return None;
    }
    Some((expr, alias))
}

impl Query for SelectQuery {
    fn category(&self) -> LogCategory {
        LogCategory::READ
    }
    fn build_sql(&self) -> Result<(String, Vec<Value>)> {
        self.to_sql()
    }
}

/// Renderiza una cláusula ON segura: acepta `tabla.col = otra.col`,
/// encadenadas con `AND`, o el literal `true`/`1=1` para joins lateral
/// sin condición. No permite literales ni operadores fuera de `=`.
fn render_join_on(backend: Backend, on: &str) -> Result<String> {
    ident::render_eq_join(backend, on)
}