sz-orm-sqlx 10.13.3

sqlx adapter: MySQL, PostgreSQL, SQLite backend via sqlx crate
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
//! T5.2 + v9.2.0 M1: sqlx/any.rs MySQL #[ignore] 集成测试
//!
//! 需要本机 MySQL 9.6: mysql://root:test123@127.0.0.1:3306/sz_orm_test
//! 运行: cargo test -p sz-orm-sqlx --test any_mysql_test -- --ignored

use std::sync::OnceLock;

use sz_orm_core::{Connection, Value};
use sz_orm_sqlx::{AnyBackend, AnyPool};

static DSN_CELL: OnceLock<String> = OnceLock::new();

fn dsn() -> &'static str {
    DSN_CELL.get_or_init(|| {
        std::env::var("SZ_ORM_MYSQL_URL")
            .unwrap_or_else(|_| "mysql://root:test123@127.0.0.1:3306/sz_orm_test".to_string())
    })
}

#[tokio::test]
#[ignore]
async fn test_any_backend_mysql_connect() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    assert_eq!(pool.backend(), AnyBackend::MySql);
}

#[tokio::test]
#[ignore]
async fn test_any_connection_mysql_create_insert_query() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS any_test_t")
        .await
        .unwrap();
    conn.execute("CREATE TABLE any_test_t (id INT PRIMARY KEY, name VARCHAR(255) NOT NULL)")
        .await
        .unwrap();
    conn.execute("INSERT INTO any_test_t (id, name) VALUES (1, 'alice')")
        .await
        .unwrap();
    let rows = conn
        .query("SELECT id, name FROM any_test_t WHERE id = 1")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    conn.execute("DROP TABLE IF EXISTS any_test_t")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_any_connection_mysql_dialect_placeholder() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let dialect = pool.dialect();
    assert_eq!(dialect.db_type(), sz_orm_core::DbType::MySQL);
}

#[tokio::test]
#[ignore]
async fn test_any_connection_mysql_transaction() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS any_tx_test")
        .await
        .unwrap();
    conn.execute("CREATE TABLE any_tx_test (id INT PRIMARY KEY)")
        .await
        .unwrap();
    conn.begin_transaction().await.unwrap();
    conn.execute("INSERT INTO any_tx_test (id) VALUES (1)")
        .await
        .unwrap();
    conn.rollback().await.unwrap();
    let rows = conn.query("SELECT id FROM any_tx_test").await.unwrap();
    assert_eq!(rows.len(), 0);
    conn.execute("DROP TABLE IF EXISTS any_tx_test")
        .await
        .unwrap();
}

// ===================== T1: execute 全分支(4 tests) =====================

#[tokio::test]
#[ignore]
async fn test_mysql_execute_after_close_returns_error() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.close().await.unwrap();
    let result = conn.execute("SELECT 1").await;
    assert!(result.is_err());
    assert!(!conn.is_connected());
}

#[tokio::test]
#[ignore]
async fn test_mysql_execute_error_marks_disconnected() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let result = conn
        .execute("INSERT INTO nonexistent_table_v920 VALUES (1)")
        .await;
    assert!(result.is_err());
}

#[tokio::test]
#[ignore]
async fn test_mysql_execute_rows_affected() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_affected")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_affected (id INT PRIMARY KEY)")
        .await
        .unwrap();
    let affected = conn
        .execute("INSERT INTO t_v920_affected (id) VALUES (1), (2), (3)")
        .await
        .unwrap();
    assert_eq!(affected, 3);
    conn.execute("DROP TABLE IF EXISTS t_v920_affected")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_execute_needs_raw_sql_path() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_raw")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_raw (id INT PRIMARY KEY)")
        .await
        .unwrap();
    conn.begin_transaction().await.unwrap();
    conn.execute("INSERT INTO t_v920_raw (id) VALUES (1)")
        .await
        .unwrap();
    conn.rollback().await.unwrap();
    let rows = conn.query("SELECT id FROM t_v920_raw").await.unwrap();
    assert_eq!(rows.len(), 0);
    conn.execute("DROP TABLE IF EXISTS t_v920_raw")
        .await
        .unwrap();
}

// ===================== T2: query 全类型转换(10 tests) =====================

#[tokio::test]
#[ignore]
async fn test_mysql_query_int_types() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_int")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_int (a TINYINT, b SMALLINT, c INT, d BIGINT)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_int VALUES (127, 32767, 2147483647, 9223372036854775807)")
        .await
        .unwrap();
    let rows = conn
        .query("SELECT a, b, c, d FROM t_v920_int")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    let row = &rows[0];
    assert!(!row.get("a").unwrap().is_null());
    assert!(!row.get("b").unwrap().is_null());
    assert!(!row.get("c").unwrap().is_null());
    assert!(!row.get("d").unwrap().is_null());
    conn.execute("DROP TABLE IF EXISTS t_v920_int")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_unsigned_types() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_unsigned")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_unsigned (a INT UNSIGNED, b BIGINT UNSIGNED)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_unsigned VALUES (4294967295, 18446744073709551615)")
        .await
        .unwrap();
    let rows = conn
        .query("SELECT a, b FROM t_v920_unsigned")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    conn.execute("DROP TABLE IF EXISTS t_v920_unsigned")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_float_double() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_float")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_float (a FLOAT, b DOUBLE)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_float VALUES (3.14, 2.718281828459045)")
        .await
        .unwrap();
    let rows = conn.query("SELECT a, b FROM t_v920_float").await.unwrap();
    assert_eq!(rows.len(), 1);
    let row = &rows[0];
    assert!(!row.get("a").unwrap().is_null());
    assert!(!row.get("b").unwrap().is_null());
    conn.execute("DROP TABLE IF EXISTS t_v920_float")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_varchar_text() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_str")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_str (a VARCHAR(50), b TEXT, c CHAR(3))")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_str VALUES ('hello', 'world text', 'abc')")
        .await
        .unwrap();
    let rows = conn.query("SELECT a, b, c FROM t_v920_str").await.unwrap();
    assert_eq!(rows.len(), 1);
    let row = &rows[0];
    assert!(row.get("a").unwrap().is_string());
    assert!(row.get("b").unwrap().is_string());
    assert!(row.get("c").unwrap().is_string());
    conn.execute("DROP TABLE IF EXISTS t_v920_str")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_blob_binary() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_blob")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_blob (a BLOB, b VARBINARY(100))")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_blob VALUES (x'deadbeef', x'cafe')")
        .await
        .unwrap();
    let rows = conn.query("SELECT a, b FROM t_v920_blob").await.unwrap();
    assert_eq!(rows.len(), 1);
    let row = &rows[0];
    assert!(row.get("a").unwrap().is_bytes());
    assert!(row.get("b").unwrap().is_bytes());
    conn.execute("DROP TABLE IF EXISTS t_v920_blob")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_decimal() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_dec")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_dec (a DECIMAL(20,2))")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_dec VALUES (123456789.99)")
        .await
        .unwrap();
    let rows = conn.query("SELECT a FROM t_v920_dec").await.unwrap();
    assert_eq!(rows.len(), 1);
    // R2:DECIMAL 必须解码为任意精度 Decimal 字符串(不允许 f64/rust_decimal 截断)
    match rows[0].get("a").unwrap() {
        Value::Decimal(s) => assert_eq!(s, "123456789.99"),
        other => panic!("expected Value::Decimal, got {other:?}"),
    }
    conn.execute("DROP TABLE IF EXISTS t_v920_dec")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_decimal_38_digit_is_exact() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_dec38")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_dec38 (a DECIMAL(38,0))")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_dec38 VALUES (12345678901234567890123456789012345678)")
        .await
        .unwrap();
    let rows = conn.query("SELECT a FROM t_v920_dec38").await.unwrap();
    assert_eq!(rows.len(), 1);
    // R2:38 位有效数字逐位保真
    match rows[0].get("a").unwrap() {
        Value::Decimal(s) => assert_eq!(s, "12345678901234567890123456789012345678"),
        other => panic!("expected Value::Decimal, got {other:?}"),
    }
    conn.execute("DROP TABLE IF EXISTS t_v920_dec38")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_date_datetime_time() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_date")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_date (a DATE, b DATETIME, c TIME)")
        .await
        .unwrap();
    conn.execute(
        "INSERT INTO t_v920_date VALUES ('2026-09-27', '2026-09-27 12:30:00', '08:00:00')",
    )
    .await
    .unwrap();
    let rows = conn.query("SELECT a, b, c FROM t_v920_date").await.unwrap();
    assert_eq!(rows.len(), 1);
    // R1:时间类型不得静默返回 NULL,必须按 chrono 解码为对应 Value 变体
    match rows[0].get("a").unwrap() {
        Value::Date(s) => assert_eq!(s, "2026-09-27"),
        other => panic!("expected Value::Date, got {other:?}"),
    }
    match rows[0].get("b").unwrap() {
        Value::DateTime(s) => assert_eq!(s, "2026-09-27 12:30:00"),
        other => panic!("expected Value::DateTime, got {other:?}"),
    }
    match rows[0].get("c").unwrap() {
        Value::Time(s) => assert_eq!(s, "08:00:00"),
        other => panic!("expected Value::Time, got {other:?}"),
    }
    conn.execute("DROP TABLE IF EXISTS t_v920_date")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_null_values() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_null")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_null (a INT, b VARCHAR(50))")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_null VALUES (NULL, NULL)")
        .await
        .unwrap();
    let rows = conn.query("SELECT a, b FROM t_v920_null").await.unwrap();
    assert_eq!(rows.len(), 1);
    let row = &rows[0];
    assert!(row.get("a").unwrap().is_null());
    assert!(row.get("b").unwrap().is_null());
    conn.execute("DROP TABLE IF EXISTS t_v920_null")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_empty_result() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_empty")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_empty (id INT PRIMARY KEY)")
        .await
        .unwrap();
    let rows = conn
        .query("SELECT id FROM t_v920_empty WHERE id > 100")
        .await
        .unwrap();
    assert_eq!(rows.len(), 0);
    conn.execute("DROP TABLE IF EXISTS t_v920_empty")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_query_with_params() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_params")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_params (id INT PRIMARY KEY, name VARCHAR(50))")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_params (id, name) VALUES (?, ?)",
        &[Value::I32(1), Value::String("test".to_string())],
    )
    .await
    .unwrap();
    let rows = conn
        .query_with_params(
            "SELECT id, name FROM t_v920_params WHERE id = ?",
            &[Value::I32(1)],
        )
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("name").unwrap().is_string());
    conn.execute("DROP TABLE IF EXISTS t_v920_params")
        .await
        .unwrap();
}

// ===================== T3: bulk_insert 路径(2 tests) =====================

#[tokio::test]
#[ignore]
async fn test_mysql_bulk_insert_via_execute_with_params() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_bulk")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_bulk (id INT PRIMARY KEY, name VARCHAR(50))")
        .await
        .unwrap();
    let affected = conn
        .execute_with_params(
            "INSERT INTO t_v920_bulk (id, name) VALUES (?, ?), (?, ?)",
            &[
                Value::I32(1),
                Value::String("a".to_string()),
                Value::I32(2),
                Value::String("b".to_string()),
            ],
        )
        .await
        .unwrap();
    assert_eq!(affected, 2);
    let rows = conn.query("SELECT id FROM t_v920_bulk").await.unwrap();
    assert_eq!(rows.len(), 2);
    conn.execute("DROP TABLE IF EXISTS t_v920_bulk")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_mysql_execute_with_params_empty_falls_back_to_execute() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_noparams")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_noparams (id INT PRIMARY KEY)")
        .await
        .unwrap();
    let affected = conn
        .execute_with_params("INSERT INTO t_v920_noparams (id) VALUES (42)", &[])
        .await
        .unwrap();
    assert_eq!(affected, 1);
    conn.execute("DROP TABLE IF EXISTS t_v920_noparams")
        .await
        .unwrap();
}