tideorm 0.9.14

A developer-friendly ORM for Rust with clean, expressive syntax
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
use crate::config::DatabaseType;
use crate::internal::Value;
use crate::internal::sql_safety;

mod previews_and_arrays;

pub(crate) use previews_and_arrays::*;

fn escape_sql_literal(db_type: DatabaseType, value: &str) -> String {
    sql_safety::escape_sql_literal_for_db(db_type, value)
}

fn escape_mysql_literal(value: &str) -> String {
    escape_sql_literal(DatabaseType::MySQL, value)
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BoundSql {
    pub sql: String,
    pub values: Vec<Value>,
}

impl BoundSql {
    fn new(sql: String, values: Vec<Value>) -> Self {
        Self { sql, values }
    }
}

fn json_text_value(text: String) -> Value {
    Value::String(Some(text))
}

fn json_scalar_parameter(value: &serde_json::Value) -> Value {
    json_text_value(
        serde_json::to_string(value).expect("serializing scalar predicate value should not fail"),
    )
}

fn json_native_parameter(value: &serde_json::Value) -> Value {
    Value::Json(Some(Box::new(value.clone())))
}

fn sqlite_json_compare_parameter(value: &serde_json::Value) -> Value {
    match value {
        serde_json::Value::String(text) => Value::String(Some(text.clone())),
        serde_json::Value::Null => Value::String(Some("null".to_string())),
        serde_json::Value::Bool(boolean) => Value::Bool(Some(*boolean)),
        serde_json::Value::Number(number) => {
            if let Some(integer) = number.as_i64() {
                Value::BigInt(Some(integer))
            } else if let Some(float) = number.as_f64() {
                Value::Double(Some(float))
            } else {
                Value::String(Some(number.to_string()))
            }
        }
        serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
            Value::String(Some(value.to_string()))
        }
    }
}

fn json_string_contents(value: &str) -> String {
    let json = serde_json::to_string(value).expect("serializing JSON path segment should not fail");
    json[1..json.len() - 1].to_string()
}

pub(crate) fn canonical_json_member_path(key: &str) -> String {
    format!("$.\"{}\"", json_string_contents(key))
}

pub(crate) fn json_contains_bound(
    db_type: DatabaseType,
    column_sql: &str,
    value: &serde_json::Value,
) -> BoundSql {
    match db_type {
        DatabaseType::Postgres => BoundSql::new(
            format!("{} @> $1", column_sql),
            vec![json_native_parameter(value)],
        ),
        DatabaseType::MySQL | DatabaseType::MariaDB => BoundSql::new(
            format!("JSON_CONTAINS({}, CAST(? AS JSON))", column_sql),
            vec![json_scalar_parameter(value)],
        ),
        DatabaseType::SQLite => BoundSql::new(
            format!(
                "EXISTS (SELECT 1 FROM json_each({}) WHERE value = ?)",
                column_sql
            ),
            vec![sqlite_json_compare_parameter(value)],
        ),
    }
}

pub(crate) fn json_contained_by_bound(
    db_type: DatabaseType,
    column_sql: &str,
    value: &serde_json::Value,
) -> BoundSql {
    match db_type {
        DatabaseType::Postgres => BoundSql::new(
            format!("{} <@ $1", column_sql),
            vec![json_native_parameter(value)],
        ),
        DatabaseType::MySQL | DatabaseType::MariaDB => BoundSql::new(
            format!("JSON_CONTAINS(CAST(? AS JSON), {})", column_sql),
            vec![json_scalar_parameter(value)],
        ),
        DatabaseType::SQLite => BoundSql::new(
            format!(
                "json_type({}) IS NOT NULL AND ? LIKE '%' || {} || '%'",
                column_sql, column_sql
            ),
            vec![json_scalar_parameter(value)],
        ),
    }
}

pub(crate) fn json_key_exists_bound(
    db_type: DatabaseType,
    column_sql: &str,
    key: &str,
) -> BoundSql {
    match db_type {
        DatabaseType::Postgres => BoundSql::new(
            format!("{} ? $1", column_sql),
            vec![Value::String(Some(key.to_string()))],
        ),
        DatabaseType::MySQL | DatabaseType::MariaDB => BoundSql::new(
            format!("JSON_CONTAINS_PATH({}, 'one', ?)", column_sql),
            vec![Value::String(Some(canonical_json_member_path(key)))],
        ),
        DatabaseType::SQLite => BoundSql::new(
            format!("json_extract({}, ?) IS NOT NULL", column_sql),
            vec![Value::String(Some(canonical_json_member_path(key)))],
        ),
    }
}

pub(crate) fn json_key_not_exists_bound(
    db_type: DatabaseType,
    column_sql: &str,
    key: &str,
) -> BoundSql {
    match db_type {
        DatabaseType::Postgres => BoundSql::new(
            format!("NOT ({} ? $1)", column_sql),
            vec![Value::String(Some(key.to_string()))],
        ),
        DatabaseType::MySQL | DatabaseType::MariaDB => BoundSql::new(
            format!("NOT JSON_CONTAINS_PATH({}, 'one', ?)", column_sql),
            vec![Value::String(Some(canonical_json_member_path(key)))],
        ),
        DatabaseType::SQLite => BoundSql::new(
            format!("json_extract({}, ?) IS NULL", column_sql),
            vec![Value::String(Some(canonical_json_member_path(key)))],
        ),
    }
}

pub(crate) fn json_path_exists_bound(
    db_type: DatabaseType,
    column_sql: &str,
    path: &str,
) -> Option<BoundSql> {
    match db_type {
        DatabaseType::Postgres => Some(BoundSql::new(
            format!("{} @? ($1::jsonpath)", column_sql),
            vec![Value::String(Some(path.to_string()))],
        )),
        DatabaseType::MySQL | DatabaseType::MariaDB => {
            normalize_mysql_sqlite_json_path(path).map(|normalized| {
                BoundSql::new(
                    format!("JSON_CONTAINS_PATH({}, 'one', ?)", column_sql),
                    vec![Value::String(Some(normalized))],
                )
            })
        }
        DatabaseType::SQLite => normalize_mysql_sqlite_json_path(path).map(|normalized| {
            BoundSql::new(
                format!("json_extract({}, ?) IS NOT NULL", column_sql),
                vec![Value::String(Some(normalized))],
            )
        }),
    }
}

pub(crate) fn json_path_not_exists_bound(
    db_type: DatabaseType,
    column_sql: &str,
    path: &str,
) -> Option<BoundSql> {
    match db_type {
        DatabaseType::Postgres => Some(BoundSql::new(
            format!("NOT ({} @? ($1::jsonpath))", column_sql),
            vec![Value::String(Some(path.to_string()))],
        )),
        DatabaseType::MySQL | DatabaseType::MariaDB => {
            normalize_mysql_sqlite_json_path(path).map(|normalized| {
                BoundSql::new(
                    format!("NOT JSON_CONTAINS_PATH({}, 'one', ?)", column_sql),
                    vec![Value::String(Some(normalized))],
                )
            })
        }
        DatabaseType::SQLite => normalize_mysql_sqlite_json_path(path).map(|normalized| {
            BoundSql::new(
                format!("json_extract({}, ?) IS NULL", column_sql),
                vec![Value::String(Some(normalized))],
            )
        }),
    }
}

pub(crate) fn normalize_mysql_sqlite_json_path(path: &str) -> Option<String> {
    let chars: Vec<char> = path.chars().collect();
    if chars.first().copied() != Some('$') {
        return None;
    }

    let mut index = 1;
    let mut normalized = String::from("$");

    while index < chars.len() {
        match chars[index] {
            '.' => {
                index += 1;
                if index >= chars.len() {
                    return None;
                }

                let segment = if chars[index] == '"' || chars[index] == '\'' {
                    parse_quoted_json_path_segment(&chars, &mut index)?
                } else {
                    let start = index;
                    while index < chars.len() && chars[index] != '.' && chars[index] != '[' {
                        index += 1;
                    }
                    let segment: String = chars[start..index].iter().collect();
                    if !is_safe_identifier_segment(&segment) {
                        return None;
                    }
                    segment
                };

                normalized.push_str(&format!(".\"{}\"", json_string_contents(&segment)));
            }
            '[' => {
                index += 1;
                if index >= chars.len() {
                    return None;
                }

                if chars[index].is_ascii_digit() {
                    let start = index;
                    while index < chars.len() && chars[index].is_ascii_digit() {
                        index += 1;
                    }
                    if index >= chars.len() || chars[index] != ']' {
                        return None;
                    }
                    normalized.push('[');
                    normalized.extend(chars[start..index].iter());
                    normalized.push(']');
                    index += 1;
                } else if chars[index] == '"' || chars[index] == '\'' {
                    let segment = parse_quoted_json_path_segment(&chars, &mut index)?;
                    if index >= chars.len() || chars[index] != ']' {
                        return None;
                    }
                    normalized.push_str(&format!(".\"{}\"", json_string_contents(&segment)));
                    index += 1;
                } else {
                    return None;
                }
            }
            _ => return None,
        }
    }

    Some(normalized)
}

fn parse_quoted_json_path_segment(chars: &[char], index: &mut usize) -> Option<String> {
    let quote = chars.get(*index).copied()?;
    *index += 1;

    let mut segment = String::new();
    while *index < chars.len() {
        match chars[*index] {
            '\\' => {
                *index += 1;
                let escaped = chars.get(*index).copied()?;
                segment.push(escaped);
                *index += 1;
            }
            ch if ch == quote => {
                *index += 1;
                return Some(segment);
            }
            ch => {
                segment.push(ch);
                *index += 1;
            }
        }
    }

    None
}

pub(crate) fn invalid_json_path_predicate(exists: bool) -> String {
    let _ = exists;
    "0 = 1".to_string()
}

fn sql_array_value_to_json(value: &str) -> serde_json::Value {
    let trimmed = value.trim();
    if trimmed.starts_with('\'') && trimmed.ends_with('\'') && trimmed.len() >= 2 {
        return serde_json::Value::String(trimmed[1..trimmed.len() - 1].replace("''", "'"));
    }

    match trimmed {
        "null" | "NULL" => serde_json::Value::Null,
        "true" | "TRUE" => serde_json::Value::Bool(true),
        "false" | "FALSE" => serde_json::Value::Bool(false),
        _ => serde_json::from_str(trimmed)
            .unwrap_or_else(|_| serde_json::Value::String(trimmed.to_string())),
    }
}

fn mysql_json_array_literal(values: &[String]) -> String {
    let json = serde_json::to_string(
        &values
            .iter()
            .map(|value| sql_array_value_to_json(value))
            .collect::<Vec<_>>(),
    )
    .expect("serializing JSON array should not fail");
    escape_mysql_literal(&json)
}

fn mysql_json_scalar_literal(value: &str) -> String {
    let json = serde_json::to_string(&sql_array_value_to_json(value))
        .expect("serializing JSON scalar should not fail");
    escape_mysql_literal(&json)
}

fn is_safe_identifier_segment(segment: &str) -> bool {
    sql_safety::is_safe_identifier_segment(segment)
}

pub(crate) fn validate_raw_sql_fragment(kind: &str, sql: &str) -> std::result::Result<(), String> {
    sql_safety::validate_raw_sql_fragment(kind, sql)
}

pub(crate) fn validate_having_sql_fragment(
    kind: &str,
    sql: &str,
) -> std::result::Result<(), String> {
    sql_safety::validate_having_sql_fragment(kind, sql)
}

pub(crate) fn validate_subquery_sql(sql: &str) -> std::result::Result<(), String> {
    sql_safety::validate_subquery_sql(sql)
}

pub(crate) fn validate_compound_subquery_sql(sql: &str) -> std::result::Result<(), String> {
    sql_safety::validate_compound_subquery_sql(sql)
}

pub(crate) fn validate_identifier(kind: &str, value: &str) -> std::result::Result<(), String> {
    sql_safety::validate_identifier(kind, value)
}

pub(crate) fn validate_identifier_reference(
    kind: &str,
    value: &str,
) -> std::result::Result<(), String> {
    sql_safety::validate_identifier_reference(kind, value)
}

pub(crate) fn validate_join_column(value: &str) -> std::result::Result<(), String> {
    sql_safety::validate_join_column(value)
}

/// Get the identifier quote character for the database
#[cfg(test)]
pub(crate) fn quote_char(db_type: DatabaseType) -> char {
    sql_safety::quote_char(db_type)
}

/// Quote an identifier (column or table name)
pub fn quote_ident(db_type: DatabaseType, name: &str) -> String {
    sql_safety::quote_ident(db_type, name)
}

/// Quote a simple identifier reference like `column` or `table.column`.
pub fn format_identifier_reference(db_type: DatabaseType, value: &str) -> Option<String> {
    sql_safety::format_identifier_reference(db_type, value)
}

/// Format a trusted column/expression slot for rendering paths that intentionally
/// allow raw SQL expressions after higher-level validation.
pub(crate) fn format_column_or_trusted_expression(
    db_type: DatabaseType,
    column_or_expression: &str,
) -> String {
    let trimmed = column_or_expression.trim();
    format_identifier_reference(db_type, trimmed).unwrap_or_else(|| trimmed.to_string())
}

/// Format a column identifier for the database.
///
/// This helper is intentionally strict: if the input is not a simple
/// identifier reference like `column` or `table.column`, it is quoted as a
/// single identifier instead of being passed through as raw SQL. Call
/// `format_column_or_trusted_expression()` only from rendering paths that
/// intentionally support validated raw expressions.
pub fn format_column(db_type: DatabaseType, column: &str) -> String {
    let trimmed = column.trim();
    format_identifier_reference(db_type, trimmed).unwrap_or_else(|| quote_ident(db_type, trimmed))
}

/// Generate aggregate function with proper casting for the database
pub fn cast_to_float(db_type: DatabaseType, expr: &str) -> String {
    match db_type {
        DatabaseType::Postgres => format!("CAST({} AS FLOAT8)", expr),
        DatabaseType::MySQL | DatabaseType::MariaDB => format!("CAST({} AS DOUBLE)", expr),
        DatabaseType::SQLite => format!("CAST({} AS REAL)", expr),
    }
}

/// Generate = ANY(array) expression (PostgreSQL optimization for IN)
pub fn eq_any(db_type: DatabaseType, column: &str, values: &[String]) -> String {
    match db_type {
        DatabaseType::Postgres => {
            format!("{} = ANY(ARRAY[{}])", column, values.join(","))
        }
        DatabaseType::MySQL | DatabaseType::MariaDB | DatabaseType::SQLite => {
            format!("{} IN ({})", column, values.join(","))
        }
    }
}

/// Generate <> ALL(array) expression (PostgreSQL optimization for NOT IN)
pub fn ne_all(db_type: DatabaseType, column: &str, values: &[String]) -> String {
    match db_type {
        DatabaseType::Postgres => {
            format!("{} <> ALL(ARRAY[{}])", column, values.join(","))
        }
        DatabaseType::MySQL | DatabaseType::MariaDB | DatabaseType::SQLite => {
            format!("{} NOT IN ({})", column, values.join(","))
        }
    }
}