sz-orm-sqlx 10.13.1

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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! T5.2 + v9.2.0 M1: sqlx/any.rs PostgreSQL #[ignore] 集成测试
//!
//! 需要本机 PostgreSQL 18: postgres://postgres:test123@127.0.0.1:5432/sz_orm_test
//! 运行: cargo test -p sz-orm-sqlx --test any_pg_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_PG_URL").unwrap_or_else(|_| {
            "postgres://postgres:test123@127.0.0.1:5432/sz_orm_test".to_string()
        })
    })
}

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

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

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

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

// ===================== T4: execute/query 全分支(14 tests) =====================

#[tokio::test]
#[ignore]
async fn test_pg_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_pg_execute_error_on_nonexistent_table() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let result = conn
        .execute("INSERT INTO nonexistent_v920 VALUES (1)")
        .await;
    assert!(result.is_err());
}

#[tokio::test]
#[ignore]
async fn test_pg_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_pg_execute_needs_raw_sql_begin_commit() {
    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.commit().await.unwrap();
    let rows = conn.query("SELECT id FROM t_v920_raw").await.unwrap();
    assert_eq!(rows.len(), 1);
    conn.execute("DROP TABLE IF EXISTS t_v920_raw")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_query_basic_select() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn.query("SELECT 1 AS one, 'hello' AS txt").await.unwrap();
    assert_eq!(rows.len(), 1);
    assert!(!rows[0].get("one").unwrap().is_null());
    assert!(rows[0].get("txt").unwrap().is_string());
}

#[tokio::test]
#[ignore]
async fn test_pg_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_pg_query_with_params_dollar_placeholders() {
    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 TEXT)")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_params (id, name) VALUES ($1, $2)",
        &[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 = $1",
            &[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();
}

#[tokio::test]
#[ignore]
async fn test_pg_execute_with_params_various_types() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_multi")
        .await
        .unwrap();
    conn.execute(
        "CREATE TABLE t_v920_multi (id INT, val BIGINT, flag BOOLEAN, score DOUBLE PRECISION)",
    )
    .await
    .unwrap();
    let affected = conn
        .execute_with_params(
            "INSERT INTO t_v920_multi (id, val, flag, score) VALUES ($1, $2, $3, $4)",
            &[
                Value::I32(1),
                Value::I64(9999999999),
                Value::Bool(true),
                Value::F64(3.25),
            ],
        )
        .await
        .unwrap();
    assert_eq!(affected, 1);
    conn.execute("DROP TABLE IF EXISTS t_v920_multi")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_begin_transaction_and_in_transaction_flag() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    assert!(!conn.in_transaction());
    conn.begin_transaction().await.unwrap();
    assert!(conn.in_transaction());
    conn.rollback().await.unwrap();
    assert!(!conn.in_transaction());
}

#[tokio::test]
#[ignore]
async fn test_pg_commit_clears_transaction_flag() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.begin_transaction().await.unwrap();
    assert!(conn.in_transaction());
    conn.commit().await.unwrap();
    assert!(!conn.in_transaction());
}

#[tokio::test]
#[ignore]
async fn test_pg_rollback_clears_transaction_flag() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.begin_transaction().await.unwrap();
    conn.rollback().await.unwrap();
    assert!(!conn.in_transaction());
}

#[tokio::test]
#[ignore]
async fn test_pg_double_begin_returns_error() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.begin_transaction().await.unwrap();
    let result = conn.begin_transaction().await;
    assert!(result.is_err());
    conn.rollback().await.unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_is_connected_after_connect() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    assert!(conn.is_connected());
    conn.close().await.unwrap();
    assert!(!conn.is_connected());
}

#[tokio::test]
#[ignore]
async fn test_pg_ping_returns_true() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    assert!(conn.ping().await);
}

// ===================== T5: PG 特有类型转换(15 tests) =====================

#[tokio::test]
#[ignore]
async fn test_pg_query_bool_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_bool")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_bool (a BOOLEAN)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_bool VALUES (TRUE), (FALSE)")
        .await
        .unwrap();
    let rows = conn.query("SELECT a FROM t_v920_bool").await.unwrap();
    assert_eq!(rows.len(), 2);
    assert!(rows[0].get("a").unwrap().is_bool());
    conn.execute("DROP TABLE IF EXISTS t_v920_bool")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_query_int2_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn.query("SELECT 32767::INT2 AS val").await.unwrap();
    assert_eq!(rows.len(), 1);
    assert!(!rows[0].get("val").unwrap().is_null());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_int4_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn.query("SELECT 2147483647::INT4 AS val").await.unwrap();
    assert_eq!(rows.len(), 1);
    assert!(!rows[0].get("val").unwrap().is_null());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_int8_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT 9223372036854775807::INT8 AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("val").unwrap().is_i64());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_float4_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn.query("SELECT 3.14::FLOAT4 AS val").await.unwrap();
    assert_eq!(rows.len(), 1);
    assert!(!rows[0].get("val").unwrap().is_null());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_float8_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT 2.718281828459045::FLOAT8 AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("val").unwrap().is_f64());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_text_varchar_types() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT 'hello'::TEXT AS a, 'world'::VARCHAR(20) AS b")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("a").unwrap().is_string());
    assert!(rows[0].get("b").unwrap().is_string());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_numeric_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT 123456789.99::NUMERIC(20,2) AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // R2:NUMERIC 必须解码为任意精度 Decimal 字符串(不允许 f64/rust_decimal 截断)
    match rows[0].get("val").unwrap() {
        Value::Decimal(s) => assert_eq!(s, "123456789.99"),
        other => panic!("expected Value::Decimal, got {other:?}"),
    }
}

#[tokio::test]
#[ignore]
async fn test_pg_query_numeric_38_digit_is_exact() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT 12345678901234567890123456789012345678::NUMERIC(38,0) AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // R2:38 位有效数字必须逐位保真(rust_decimal 上限 28 位会静默截断)
    match rows[0].get("val").unwrap() {
        Value::Decimal(s) => assert_eq!(s, "12345678901234567890123456789012345678"),
        other => panic!("expected Value::Decimal, got {other:?}"),
    }
}

#[tokio::test]
#[ignore]
async fn test_pg_query_uuid_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT '550e8400-e29b-41d4-a716-446655440000'::UUID AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("val").unwrap().is_string());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_jsonb_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT '{\"key\": \"value\"}'::JSONB AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    let val = rows[0].get("val").unwrap();
    assert!(val.is_json() || val.is_null());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_bytea_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT E'\\\\xdeadbeef'::BYTEA AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    let val = rows[0].get("val").unwrap();
    assert!(val.is_bytes() || val.is_null());
}

#[tokio::test]
#[ignore]
async fn test_pg_query_date_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT '2026-09-27'::DATE AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // R1:DATE 必须解码为 Value::Date,禁止静默 NULL
    match rows[0].get("val").unwrap() {
        Value::Date(s) => assert_eq!(s, "2026-09-27"),
        other => panic!("expected Value::Date, got {other:?}"),
    }
}

#[tokio::test]
#[ignore]
async fn test_pg_query_timestamp_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT '2026-09-27 12:30:00'::TIMESTAMP AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // R1:TIMESTAMP 必须解码为 Value::DateTime
    match rows[0].get("val").unwrap() {
        Value::DateTime(s) => assert_eq!(s, "2026-09-27 12:30:00"),
        other => panic!("expected Value::DateTime, got {other:?}"),
    }
}

#[tokio::test]
#[ignore]
async fn test_pg_query_timestamptz_type() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT '2026-09-27 12:30:00+00'::TIMESTAMPTZ AS val")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // R1:TIMESTAMPTZ 统一归一化到 UTC 微秒精度
    match rows[0].get("val").unwrap() {
        Value::DateTime(s) => assert_eq!(s, "2026-09-27T12:30:00.000000Z"),
        other => panic!("expected Value::DateTime, got {other:?}"),
    }
}

#[tokio::test]
#[ignore]
async fn test_pg_query_null_values() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query("SELECT NULL::INT AS a, NULL::TEXT AS b")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("a").unwrap().is_null());
    assert!(rows[0].get("b").unwrap().is_null());
}

// ===================== T6: bulk_insert + PG 扩展(8 tests) =====================

#[tokio::test]
#[ignore]
async fn test_pg_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 TEXT)")
        .await
        .unwrap();
    let affected = conn
        .execute_with_params(
            "INSERT INTO t_v920_bulk (id, name) VALUES ($1, $2), ($3, $4)",
            &[
                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_pg_execute_with_params_empty_falls_back() {
    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();
}

#[tokio::test]
#[ignore]
async fn test_pg_execute_with_params_null_value() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_null_param")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_null_param (id INT, val TEXT)")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_null_param (id, val) VALUES ($1, $2)",
        &[Value::I32(1), Value::Null],
    )
    .await
    .unwrap();
    let rows = conn
        .query("SELECT val FROM t_v920_null_param WHERE id = 1")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("val").unwrap().is_null());
    conn.execute("DROP TABLE IF EXISTS t_v920_null_param")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_execute_with_params_bool_value() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_bool_param")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_bool_param (id INT, flag BOOLEAN)")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_bool_param (id, flag) VALUES ($1, $2)",
        &[Value::I32(1), Value::Bool(true)],
    )
    .await
    .unwrap();
    let rows = conn
        .query("SELECT flag FROM t_v920_bool_param WHERE id = 1")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("flag").unwrap().is_bool());
    conn.execute("DROP TABLE IF EXISTS t_v920_bool_param")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_execute_with_params_i64_value() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_i64_param")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_i64_param (id INT, big_val BIGINT)")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_i64_param (id, big_val) VALUES ($1, $2)",
        &[Value::I32(1), Value::I64(9223372036854775807)],
    )
    .await
    .unwrap();
    let rows = conn
        .query("SELECT big_val FROM t_v920_i64_param WHERE id = 1")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("big_val").unwrap().is_i64());
    conn.execute("DROP TABLE IF EXISTS t_v920_i64_param")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_execute_with_params_bytes_value() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_bytes_param")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_bytes_param (id INT, data BYTEA)")
        .await
        .unwrap();
    conn.execute_with_params(
        "INSERT INTO t_v920_bytes_param (id, data) VALUES ($1, $2)",
        &[Value::I32(1), Value::Bytes(vec![0xde, 0xad, 0xbe, 0xef])],
    )
    .await
    .unwrap();
    let rows = conn
        .query("SELECT data FROM t_v920_bytes_param WHERE id = 1")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].get("data").unwrap().is_bytes());
    conn.execute("DROP TABLE IF EXISTS t_v920_bytes_param")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_query_with_params_multiple_results() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_v920_multi_result")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_v920_multi_result (id INT, name TEXT)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_v920_multi_result VALUES (1, 'a'), (2, 'b'), (3, 'c')")
        .await
        .unwrap();
    let rows = conn
        .query_with_params(
            "SELECT id, name FROM t_v920_multi_result WHERE id >= $1 ORDER BY id",
            &[Value::I32(2)],
        )
        .await
        .unwrap();
    assert_eq!(rows.len(), 2);
    conn.execute("DROP TABLE IF EXISTS t_v920_multi_result")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_rollback_without_begin_is_noop() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.rollback().await.unwrap();
    assert!(!conn.in_transaction());
}

// ===================== D5: PG 占位符 `?` → `$n` 重写(sz-rust 反馈专项) =====================

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

#[tokio::test]
#[ignore]
async fn test_pg_query_with_params_question_mark_rewritten() {
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    conn.execute("DROP TABLE IF EXISTS t_d5_qsel")
        .await
        .unwrap();
    conn.execute("CREATE TABLE t_d5_qsel (id INT PRIMARY KEY, name TEXT)")
        .await
        .unwrap();
    conn.execute("INSERT INTO t_d5_qsel VALUES (1, 'alice'), (2, 'bob')")
        .await
        .unwrap();
    let rows = conn
        .query_with_params(
            "SELECT id, name FROM t_d5_qsel WHERE id >= ? AND name <> ? ORDER BY id",
            &[Value::I32(1), Value::String("bob".to_string())],
        )
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].get("id").unwrap(), &Value::I32(1));
    conn.execute("DROP TABLE IF EXISTS t_d5_qsel")
        .await
        .unwrap();
}

#[tokio::test]
#[ignore]
async fn test_pg_query_with_params_dollar_in_literal_and_question() {
    // 字符串字面量中的 `$1` 不是原生占位符,真实的 `?` 必须被重写为 `$1`
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query_with_params("SELECT '$1' AS lit, ? AS val", &[Value::I32(42)])
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(
        rows[0].get("lit").unwrap(),
        &Value::String("$1".to_string())
    );
    assert_eq!(rows[0].get("val").unwrap(), &Value::I32(42));
}

#[tokio::test]
#[ignore]
async fn test_pg_query_with_params_dollar_quoted_literal_and_question() {
    // dollar-quote 内的 `?` 是字面量,真实参数占位符 `?` 仍被重写
    let pool = AnyPool::connect(dsn()).await.unwrap();
    let mut conn = pool.create().await.unwrap();
    let rows = conn
        .query_with_params(
            "SELECT $$?$$ AS lit, ? AS val",
            &[Value::String("hello".to_string())],
        )
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].get("lit").unwrap(), &Value::String("?".to_string()));
    assert_eq!(
        rows[0].get("val").unwrap(),
        &Value::String("hello".to_string())
    );
}