rust-store-core 5.2.1

One data engine for MongoDB, MySQL, PostgreSQL and SQLite: a MongoDB-style GQL query language, permission checks, computed columns, command planning and SQL dialect translation. Pure logic, no IO.
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
//! JSON 列基础件:`object`/`array` 字段在 SQL 后端**落单列存 JSON 文本**
//! (MySQL `JSON` / PG `jsonb` / SQLite `TEXT`+JSON1)的方言表达式生成。
//!
//! 这是 U3(对象点号路径过滤)/ U4(对象点号路径排序)在 `standard` 档下推的地基;
//! 依据执行文档 §5 步骤 8 与不足清单 #3。契约:core 不写 DDL(铁律 6),
//! 列类型由示例 DDL 与 introspection 对齐,此处只产出表达式。

use serde_json::json;

use rust_store_core::dialect::{restore_rows_json, translate, Backend};
use rust_store_core::schema::Registry;

fn reg() -> Registry {
    let mut r = Registry::new();
    r.register(&json!({
        "name": "Course",
        "collection": "courses",
        "timestamps": false,
        "fields": {
            "title": { "type": "string" },
            "tags": { "type": "array" },
            "meta": { "type": "object", "fields": {
                "level": { "type": "string" },
                "seo": { "type": "object", "fields": { "title": { "type": "string" } } }
            } }
        },
        "relations": {}
    }))
    .unwrap();
    r
}

#[test]
fn json_type_name_per_backend() {
    assert_eq!(Backend::Mysql.json_type_name(), "JSON");
    assert_eq!(Backend::Postgres.json_type_name(), "jsonb");
    assert_eq!(Backend::Sqlite.json_type_name(), "TEXT");
}

#[test]
fn json_extract_scalar_multi_segment() {
    assert_eq!(
        Backend::Mysql
            .json_extract_scalar("`meta`", &["a", "b"])
            .unwrap(),
        "JSON_UNQUOTE(JSON_EXTRACT(`meta`, '$.a.b'))"
    );
    assert_eq!(
        Backend::Postgres
            .json_extract_scalar("\"meta\"", &["a", "b"])
            .unwrap(),
        "(\"meta\" #>> '{a,b}')"
    );
    assert_eq!(
        Backend::Sqlite
            .json_extract_scalar("\"meta\"", &["a", "b"])
            .unwrap(),
        "json_extract(\"meta\", '$.a.b')"
    );
}

#[test]
fn json_extract_scalar_single_segment() {
    assert_eq!(
        Backend::Mysql
            .json_extract_scalar("`meta`", &["title"])
            .unwrap(),
        "JSON_UNQUOTE(JSON_EXTRACT(`meta`, '$.title'))"
    );
    assert_eq!(
        Backend::Postgres
            .json_extract_scalar("\"meta\"", &["title"])
            .unwrap(),
        "(\"meta\" #>> '{title}')"
    );
    assert_eq!(
        Backend::Sqlite
            .json_extract_scalar("\"meta\"", &["title"])
            .unwrap(),
        "json_extract(\"meta\", '$.title')"
    );
}

/// N1 注入防护:段含 `'` / `\` / 空格 / `$` 等非法字符 → 三后端一律显式 `Err`(禁静默改写)
#[test]
fn json_extract_scalar_rejects_illegal_segment() {
    for backend in [Backend::Mysql, Backend::Postgres, Backend::Sqlite] {
        for bad in ["a'b", "a\\b", "a b", "$x", "a{b", "a,b", ""] {
            let err = backend
                .json_extract_scalar("`meta`", &[bad])
                .expect_err("非法路径段应显式 Err(禁静默改写)");
            assert!(
                err.starts_with("ERR_GQL_PARSE:"),
                "错误前缀应为 ERR_GQL_PARSE:(实际 {err})"
            );
        }
    }
}

/// U3:对象点号路径过滤 → 各方言 JSON 标量提取下推(standard 档放开)
#[test]
fn u3_object_dotted_filter_pushes_json_extract() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "meta.seo.title": "x" },
        "projection": { "_id": 1, "title": 1 }
    });
    let sqlite = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sqlite["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("json_extract(t.\"meta\", '$.seo.title')"),
        "SQLite U3: {text}"
    );
    assert!(sqlite["unsupported"].as_array().unwrap().is_empty());

    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("(t.\"meta\" #>> '{seo,title}')"),
        "PG U3: {text}"
    );

    let my = translate(Backend::Mysql, &cmd, &reg()).unwrap();
    let text = my["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("JSON_UNQUOTE(JSON_EXTRACT(t.`meta`, '$.seo.title'))"),
        "MySQL U3: {text}"
    );
}

/// U4:对象点号路径排序 → 各方言 JSON 标量提取进 ORDER BY(standard 档放开)
#[test]
fn u4_object_dotted_sort_pushes_json_extract() {
    let cmd = json!({
        "kind": "aggregate", "collection": "courses",
        "pipeline": [ { "$sort": { "meta.level": 1 } }, { "$project": { "_id": 1 } } ]
    });
    let sqlite = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sqlite["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("ORDER BY json_extract(t.\"meta\", '$.level') ASC"),
        "SQLite U4: {text}"
    );
    assert!(sqlite["unsupported"].as_array().unwrap().is_empty());

    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("ORDER BY (t.\"meta\" #>> '{level}') ASC"),
        "PG U4: {text}"
    );
}

/// 写入:object 字段序列化为 JSON 文本参数(跨后端落 JSON 列)
#[test]
fn insert_object_field_serialized_as_json_text() {
    let cmd = json!({
        "kind": "insertOne", "collection": "courses",
        "doc": { "_id": "c1", "title": "t", "meta": { "level": "a" } }
    });
    let out = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let params = out["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params
            .iter()
            .any(|p| p.as_str() == Some("{\"level\":\"a\"}")),
        "object 字段应序列化为 JSON 文本: {params:?}"
    );
}

/// U1:数组字段过滤 → 各方言「数组包含元素」谓词下推(standard 档放开)
#[test]
fn u1_array_contains_pushes_native_predicate() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": "python" },
        "projection": { "_id": 1 }
    });
    // MySQL:JSON_CONTAINS + 值的 JSON 字面量参数
    let my = translate(Backend::Mysql, &cmd, &reg()).unwrap();
    let text = my["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("JSON_CONTAINS(t.`tags`, CAST(? AS JSON))"),
        "MySQL U1: {text}"
    );
    let params = my["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some("\"python\"")),
        "MySQL U1 参数应为值的 JSON 字面量: {params:?}"
    );

    // PG:@> + 单元素数组参数
    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(text.contains("t.\"tags\" @> $1::jsonb"), "PG U1: {text}");
    let params = pg["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some("[\"python\"]")),
        "PG U1 参数应为单元素数组: {params:?}"
    );

    // SQLite:json_each 子查询 + 标量原值参数
    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("EXISTS (SELECT 1 FROM json_each(t.\"tags\") WHERE json_each.value = ?)"),
        "SQLite U1: {text}"
    );
    let params = sq["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some("python")),
        "SQLite U1 参数应为标量原值: {params:?}"
    );
}

/// U1:数组整体等值(值本身是数组)→ 各方言 JSON 整值等值下推
#[test]
fn u1_array_whole_equality_pushes_json_value_eq() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": ["a", "b"] },
        "projection": { "_id": 1 }
    });
    let my = translate(Backend::Mysql, &cmd, &reg()).unwrap();
    let text = my["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("t.`tags` = CAST(? AS JSON)"),
        "MySQL U1 整体等值: {text}"
    );

    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("t.\"tags\" = $1::jsonb"),
        "PG U1 整体等值: {text}"
    );

    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("json(t.\"tags\") = json(?)"),
        "SQLite U1 整体等值: {text}"
    );
}

/// U2:对象字段整值等值 → 各方言 JSON 整值等值下推,且**必须告警**(跨后端键序差异,禁静默)
#[test]
fn u2_object_equality_pushes_json_value_eq_and_warns() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "meta": { "level": "a" } },
        "projection": { "_id": 1 }
    });
    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("json(t.\"meta\") = json(?)"),
        "SQLite U2: {text}"
    );
    let params = sq["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params
            .iter()
            .any(|p| p.as_str() == Some("{\"level\":\"a\"}")),
        "U2 参数应为对象 JSON 文本: {params:?}"
    );
    let warnings = sq["warnings"].as_array().unwrap();
    assert!(
        warnings
            .iter()
            .any(|w| w.as_str().unwrap_or("").contains("键序")),
        "U2 必须产出跨后端键序差异告警(禁静默): {warnings:?}"
    );

    // PG / MySQL 同样下推整值等值
    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(text.contains("t.\"meta\" = $1::jsonb"), "PG U2: {text}");
}

/// U1:数组字段 `$exists` 走 `__present` 哨兵(与标量字段同源)
#[test]
fn u1_array_exists_uses_present_sentinel() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": { "$exists": false } },
        "projection": { "_id": 1 }
    });
    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("NOT (COALESCE(t.__present, ',') LIKE '%,tags,%')"),
        "U1 $exists 应走 __present 哨兵: {text}"
    );
}

/// 无法语义等价翻译的取值组合 → 显式 Err(禁静默丢弃条件)
#[test]
fn json_field_unsupported_operator_is_error() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": { "$gt": "a" } },
        "projection": { "_id": 1 }
    });
    let err = translate(Backend::Sqlite, &cmd, &reg()).expect_err("U1 $gt 应显式报错");
    assert!(err.contains("$gt"), "错误信息应含操作符: {err}");
}

/// accessKeys:数组字段 `$in`(ctx 清单 → 交集命中)→ 各方言集合重叠谓词
#[test]
fn access_in_array_uses_overlap_per_dialect() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": { "$in": ["GEO", "LEO"] } },
        "projection": { "_id": 1 }
    });
    const LIST_JSON: &str = "[\"GEO\",\"LEO\"]";

    // MySQL:JSON_OVERLAPS + 清单 JSON 文本参数
    let my = translate(Backend::Mysql, &cmd, &reg()).unwrap();
    let text = my["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains("JSON_OVERLAPS(t.`tags`, CAST(? AS JSON))"),
        "MySQL $in: {text}"
    );
    let params = my["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some(LIST_JSON)),
        "MySQL $in 参数应为清单 JSON 文本: {params:?}"
    );

    // PG:&& + 清单 jsonb 参数
    let pg = translate(Backend::Postgres, &cmd, &reg()).unwrap();
    let text = pg["stmts"][0]["text"].as_str().unwrap();
    assert!(text.contains("t.\"tags\" && $1::jsonb"), "PG $in: {text}");
    let params = pg["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some(LIST_JSON)),
        "PG $in 参数应为清单 JSON 文本: {params:?}"
    );

    // SQLite:json_each 子查询交集 + 清单 JSON 文本参数
    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(
        text.contains(
            "EXISTS (SELECT 1 FROM json_each(t.\"tags\") WHERE json_each.value IN \
             (SELECT value FROM json_each(?)))"
        ),
        "SQLite $in: {text}"
    );
    let params = sq["stmts"][0]["params"].as_array().unwrap();
    assert!(
        params.iter().any(|p| p.as_str() == Some(LIST_JSON)),
        "SQLite $in 参数应为清单 JSON 文本: {params:?}"
    );
}

/// accessKeys:空清单 `$in` → 恒假,不消耗占位符(读路径空结果 / 写路径全拒)
#[test]
fn access_in_empty_list_is_always_false() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "tags": { "$in": [] } },
        "projection": { "_id": 1 }
    });
    let sq = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let text = sq["stmts"][0]["text"].as_str().unwrap();
    assert!(text.contains("WHERE 0"), "空清单应翻译为恒假: {text}");
    let params = sq["stmts"][0]["params"].as_array().unwrap();
    assert!(params.is_empty(), "空清单不应消耗占位符: {params:?}");
}

/// accessKeys:对象字段不支持 `$in`(无对应语义)→ 显式 Err
#[test]
fn access_in_object_field_is_error() {
    let cmd = json!({
        "kind": "find", "collection": "courses",
        "filter": { "meta": { "$in": ["a"] } },
        "projection": { "_id": 1 }
    });
    let err = translate(Backend::Sqlite, &cmd, &reg()).expect_err("对象字段 $in 应显式报错");
    assert!(err.contains("$in"), "错误信息应说明 $in 不适用: {err}");
}

/// 读取:JSON 列(文本)还原为嵌套对象(跨后端对齐 Mongo)
#[test]
fn json_column_restores_nested_object() {
    let cmd = json!({
        "kind": "find", "collection": "courses", "filter": {},
        "projection": { "_id": 1, "title": 1, "meta": 1 }
    });
    let out = translate(Backend::Sqlite, &cmd, &reg()).unwrap();
    let shape = &out["stmts"][0]["rowShape"];
    let rows = json!([
        { "_id": "c1", "title": "t",
          "meta": "{\"level\":\"a\",\"seo\":{\"title\":\"z\"}}",
          "__present": ",title,meta," }
    ]);
    let restored = restore_rows_json(shape, &rows).unwrap();
    assert_eq!(
        restored,
        json!([{ "_id": "c1", "title": "t",
                 "meta": { "level": "a", "seo": { "title": "z" } } }])
    );
}