an5-adapters 0.1.0

Standalone database adapter runtime for AN5 ORM (Rust) — connection handling, dialect-aware CRUD, aggregates and vector search.
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
//! WHERE and ORDER BY builders for the AN5 Rust adapter.
//!
//! Accepts the same ORM-style `serde_json::Value` payloads as the Go and
//! TypeScript adapters, so `{"name": {"contains": "Jo"}}` behaves the same
//! everywhere.

use serde_json::{Map, Value};

use super::dialect::Dialect;

/// Comparison operators understood inside a filter object.
const OPERATOR_KEYS: [&str; 12] = [
    "equals", "in", "notIn", "contains", "startsWith", "endsWith", "not", "gte", "lte", "gt",
    "lt", "mode",
];

/// A bound SQL argument.
pub type SqlArg = serde_json::Value;

/// Accumulated SQL fragment plus its ordered bind arguments.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Where {
    pub clause: String,
    pub args: Vec<SqlArg>,
}

impl Where {
    pub fn is_empty(&self) -> bool {
        self.clause.is_empty()
    }
}

fn is_operator_map(v: &Value) -> bool {
    match v.as_object() {
        Some(m) => OPERATOR_KEYS.iter().any(|k| m.contains_key(*k)),
        None => false,
    }
}

fn as_object(v: &Value) -> Option<&Map<String, Value>> {
    match v {
        Value::Object(m) => Some(m),
        _ => None,
    }
}

fn is_list(v: &Value) -> bool {
    v.is_array()
}

fn to_list(v: &Value) -> Vec<Value> {
    match v {
        Value::Array(items) => items.clone(),
        other => vec![other.clone()],
    }
}

fn to_string_value(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}

/// Flattens relation-style nested where objects (`user` -> `{email: ...}`).
fn clean_where(where_obj: &Map<String, Value>) -> Map<String, Value> {
    let mut clean = Map::new();
    for (k, v) in where_obj {
        if k.contains('_') {
            match as_object(v) {
                Some(inner) if !is_operator_map(v) => {
                    for (nk, nv) in inner {
                        clean.insert(nk.clone(), nv.clone());
                    }
                }
                _ => {
                    clean.insert(k.clone(), v.clone());
                }
            }
        } else {
            clean.insert(k.clone(), v.clone());
        }
    }
    clean
}

/// Build a WHERE clause with 1-based placeholders from `ph`.
pub fn build_where(where_obj: &Value, dialect: Dialect, ph: &dyn Fn(usize) -> String) -> Where {
    let mut out = Where::default();
    if let Some(obj) = as_object(where_obj) {
        let cleaned = clean_where(obj);
        out.clause = build_where_rec(&cleaned, dialect, &mut out.args, ph);
    }
    out
}

fn build_where_rec(
    where_obj: &Map<String, Value>,
    dialect: Dialect,
    args: &mut Vec<SqlArg>,
    ph: &dyn Fn(usize) -> String,
) -> String {
    if where_obj.is_empty() {
        return String::new();
    }
    let mut conditions: Vec<String> = Vec::new();

    // Deterministic key order so the generated SQL is stable.
    let mut keys: Vec<&String> = where_obj.keys().collect();
    keys.sort();

    for key in keys {
        let value = &where_obj[key];

        match key.as_str() {
            "AND" | "OR" if is_list(value) => {
                let joiner = if key == "AND" { " AND " } else { " OR " };
                let mut subs: Vec<String> = Vec::new();
                for item in to_list(value) {
                    if let Some(m) = as_object(&item) {
                        let s = build_where_rec(m, dialect, args, ph);
                        if !s.is_empty() {
                            subs.push(s);
                        }
                    }
                }
                if !subs.is_empty() {
                    conditions.push(format!("({})", subs.join(joiner)));
                }
                continue;
            }
            "NOT" => {
                let mut subs: Vec<String> = Vec::new();
                for item in to_list(value) {
                    if let Some(m) = as_object(&item) {
                        let s = build_where_rec(m, dialect, args, ph);
                        if !s.is_empty() {
                            subs.push(s);
                        }
                    }
                }
                if !subs.is_empty() {
                    conditions.push(format!("NOT ({})", subs.join(" AND ")));
                }
                continue;
            }
            "AND" | "OR" => continue,
            _ => {}
        }

        conditions.extend(build_field_condition(key, value, dialect, args, ph));
    }

    conditions.join(" AND ")
}

fn build_field_condition(
    key: &str,
    value: &Value,
    dialect: Dialect,
    args: &mut Vec<SqlArg>,
    ph: &dyn Fn(usize) -> String,
) -> Vec<String> {
    let col = dialect.quote_identifier(key);

    if value.is_null() {
        return vec![format!("{} IS NULL", col)];
    }

    let map = match as_object(value) {
        Some(m) => m,
        None => {
            let p = push_arg(args, value.clone(), ph);
            return vec![format!("{} = {}", col, p)];
        }
    };

    let mut parts: Vec<String> = Vec::new();
    let mut op_keys: Vec<&String> = map.keys().collect();
    op_keys.sort();

    for op in op_keys {
        let val = &map[op];
        let p = |args: &mut Vec<SqlArg>| push_arg(args, val.clone(), ph);
        match op.as_str() {
            "not" => {
                if val.is_null() {
                    parts.push(format!("{} IS NOT NULL", col));
                } else if is_operator_map(val) {
                    let mut nested = Map::new();
                    nested.insert(key.to_string(), val.clone());
                    let cond = build_where_rec(&nested, dialect, args, ph);
                    if !cond.is_empty() {
                        parts.push(format!("NOT ({})", cond));
                    }
                } else {
                    let phs = p(args);
                    parts.push(format!("{} <> {}", col, phs));
                }
            }
            "equals" => {
                if val.is_null() {
                    parts.push(format!("{} IS NULL", col));
                } else {
                    let phs = p(args);
                    parts.push(format!("{} = {}", col, phs));
                }
            }
            "contains" => {
                let phs = p(args);
                parts.push(format!("{} LIKE {}", col, phs));
                wrap_last_arg(args, |a| format!("%{}%", to_string_value(a)));
            }
            "startsWith" => {
                let phs = p(args);
                parts.push(format!("{} LIKE {}", col, phs));
                wrap_last_arg(args, |a| format!("{}%", to_string_value(a)));
            }
            "endsWith" => {
                let phs = p(args);
                parts.push(format!("{} LIKE {}", col, phs));
                wrap_last_arg(args, |a| format!("%{}", to_string_value(a)));
            }
            "gte" => {
                let phs = p(args);
                parts.push(format!("{} >= {}", col, phs));
            }
            "lte" => {
                let phs = p(args);
                parts.push(format!("{} <= {}", col, phs));
            }
            "gt" => {
                let phs = p(args);
                parts.push(format!("{} > {}", col, phs));
            }
            "lt" => {
                let phs = p(args);
                parts.push(format!("{} < {}", col, phs));
            }
            "in" => {
                let items = to_list(val);
                if items.is_empty() {
                    parts.push("1=0".to_string());
                } else {
                    let phs: Vec<String> = items
                        .iter()
                        .map(|it| push_arg(args, it.clone(), ph))
                        .collect();
                    parts.push(format!("{} IN ({})", col, phs.join(", ")));
                }
            }
            "notIn" => {
                let items = to_list(val);
                if items.is_empty() {
                    parts.push("1=1".to_string());
                } else {
                    let phs: Vec<String> = items
                        .iter()
                        .map(|it| push_arg(args, it.clone(), ph))
                        .collect();
                    parts.push(format!("{} NOT IN ({})", col, phs.join(", ")));
                }
            }
            // `mode: "insensitive"` is accepted and ignored: collation is a
            // database concern, matching the Go adapter's tolerance.
            _ => {}
        }
    }

    parts
}

fn push_arg(args: &mut Vec<SqlArg>, value: SqlArg, ph: &dyn Fn(usize) -> String) -> String {
    let n = args.len() + 1;
    args.push(value);
    ph(n)
}

/// Rewrites the most recently pushed arg, used by the LIKE operators which need
/// to wrap the value in wildcards after the placeholder has been allocated.
fn wrap_last_arg(args: &mut Vec<SqlArg>, f: impl Fn(&SqlArg) -> String) {
    if let Some(last) = args.last_mut() {
        let wrapped = f(last);
        *last = Value::String(wrapped);
    }
}

fn normalize_sort_direction(dir: &Value) -> &'static str {
    match dir.as_str() {
        Some(s) if s.eq_ignore_ascii_case("desc") => "DESC",
        _ => "ASC",
    }
}

/// Build an ORDER BY clause from a map, list of maps, or raw string.
pub fn build_order_by(order_by: &Value, dialect: Dialect) -> String {
    let mut entries: Vec<&Map<String, Value>> = Vec::new();

    match order_by {
        Value::String(s) => {
            if s.trim().is_empty() {
                return String::new();
            }
            return format!("ORDER BY {}", s);
        }
        Value::Array(items) => {
            for item in items {
                if let Some(m) = as_object(item) {
                    entries.push(m);
                }
            }
        }
        Value::Object(m) => entries.push(m),
        _ => return String::new(),
    }

    let mut parts: Vec<String> = Vec::new();
    for entry in entries {
        let mut keys: Vec<&String> = entry.keys().collect();
        keys.sort();
        for key in keys {
            parts.push(format!(
                "{} {}",
                dialect.quote_identifier(key),
                normalize_sort_direction(&entry[key])
            ));
        }
    }

    if parts.is_empty() {
        return String::new();
    }
    format!("ORDER BY {}", parts.join(", "))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn q(n: usize) -> String {
        format!("?{}", n)
    }

    #[test]
    fn equality_produces_placeholder() {
        let w = build_where(&json!({ "id": "abc" }), Dialect::Mssql, &q);
        assert_eq!(w.clause, "[id] = ?1");
        assert_eq!(w.args, vec![json!("abc")]);
    }

    #[test]
    fn null_uses_is_null() {
        let w = build_where(&json!({ "name": null }), Dialect::Mssql, &q);
        assert_eq!(w.clause, "[name] IS NULL");
        assert!(w.args.is_empty());
    }

    #[test]
    fn contains_wraps_wildcards() {
        let w = build_where(
            &json!({ "email": { "contains": "@example.com" } }),
            Dialect::Mssql,
            &q,
        );
        assert_eq!(w.clause, "[email] LIKE ?1");
        assert_eq!(w.args, vec![json!("%@example.com%")]);
    }

    #[test]
    fn in_expands_each_item() {
        let w = build_where(
            &json!({ "status": { "in": ["a", "b"] } }),
            Dialect::Mssql,
            &q,
        );
        assert_eq!(w.clause, "[status] IN (?1, ?2)");
        assert_eq!(w.args, vec![json!("a"), json!("b")]);
    }

    #[test]
    fn postgres_placeholders_are_numbered() {
        let ph = |n: usize| format!("${}", n);
        let w = build_where(&json!({ "id": "x" }), Dialect::Postgres, &ph);
        assert_eq!(w.clause, "\"id\" = $1");
    }

    #[test]
    fn or_blocks_are_grouped() {
        let w = build_where(
            &json!({ "OR": [{ "a": 1 }, { "b": 2 }] }),
            Dialect::Mssql,
            &q,
        );
        assert_eq!(w.clause, "([a] = ?1 OR [b] = ?2)");
    }

    #[test]
    fn order_by_defaults_to_asc() {
        let sql = build_order_by(&json!({ "name": "desc" }), Dialect::Mssql);
        assert_eq!(sql, "ORDER BY [name] DESC");
        let sql = build_order_by(&json!({ "name": "anything" }), Dialect::Mssql);
        assert_eq!(sql, "ORDER BY [name] ASC");
    }
}