sz-orm-sqlx 10.13.4

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
//! SZ-ORM sqlx adapter
//!
//! Provides Connection and ConnectionFactory implementations for sz-orm-core,
//! supporting MySQL, PostgreSQL, and SQLite.
//!
//! Does not use sqlx::Any; instead implements each backend separately to avoid type limitations and lifetime issues.
//!
//! # Examples
//!
//! ```no_run
//! use sz_orm_core::{Pool, PoolConfigBuilder};
//! use sz_orm_sqlx::{SqlitePoolHandle, SqlxSqliteConnectionFactory};
//! use std::sync::Arc;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pool_handle = SqlitePoolHandle::connect("sqlite::memory:").await?;
//! let factory = Arc::new(SqlxSqliteConnectionFactory::new(Arc::new(pool_handle)));
//! let config = PoolConfigBuilder::new().max_size(10).build()?;
//! let pool = Pool::new(config, factory)?;
//!
//! let mut conn = pool.acquire().await?;
//! let rows = conn.query("SELECT 1 as one").await?;
//! assert_eq!(rows.len(), 1);
//! # Ok(())
//! # }
//! ```

mod any;
pub mod any_driver;
pub mod batch_executor;
pub mod enhanced;
mod error;
#[cfg(feature = "async-row-stream")]
pub mod row_stream_impl;
#[cfg(feature = "dialect-saphana-driver")]
pub mod saphana_adapter;
#[cfg(feature = "slow-query-capture")]
pub mod slow_query_guard;
pub mod sql_repository;
pub mod unified_pool;

/// 查询指标装饰器连接(2026-10-28 P1-3 可观测性接线;工厂 create() 统一包装)
pub mod metrics_conn;

pub use batch_executor::BatchExecutor;
#[cfg(feature = "slow-query-capture")]
pub use slow_query_guard::SlowQueryGuard;
pub use sql_repository::SqlRepository;

pub use any::{
    mysql_bulk_insert, pg_bulk_insert, sqlite_backup, MySqlPoolHandle, PgExtensions, PgPoolHandle,
    SqlitePoolHandle, SqlxMySqlConnection, SqlxMySqlConnectionFactory, SqlxPgConnection,
    SqlxPgConnectionFactory, SqlxSqliteConnection, SqlxSqliteConnectionFactory,
};
pub use any_driver::{
    create_connection, create_connection_by_type, AnyBackend, AnyConnection, AnyPool,
};
pub use enhanced::{
    CacheStats, EnhancedPoolConfig, EnhancedPoolConfigBuilder, PreparedStatementCache,
    TransactionIsolation,
};
pub use error::map_sqlx_error;
pub use unified_pool::UnifiedPool;

#[cfg(feature = "async-row-stream")]
pub use row_stream_impl::SqlxRowStream;

pub use sz_orm_core;
// v7.5.0 组6.3: 分布式 DB 兼容性报告
use serde::{Deserialize, Serialize};

/// 分布式数据库类型
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DistributedDbType {
    /// CockroachDB
    CockroachDb,
    /// YugabyteDB
    YugabyteDb,
    /// PostgreSQL 18 基准
    PostgreSql,
}

/// SQL 兼容性测试结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SqlCompatResult {
    /// 测试 SQL 语句
    pub sql: String,
    /// 是否兼容
    pub is_compatible: bool,
    /// 不兼容原因(兼容时为 None)
    pub reason: Option<String>,
    /// 替代 SQL 建议(不兼容时提供)
    pub alternative_sql: Option<String>,
}

/// 分布式事务行为结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxBehaviorResult {
    /// 隔离级别
    pub isolation_level: String,
    /// 超时行为
    pub timeout: String,
    /// 重试行为
    pub retry_behavior: String,
    /// 与 PostgreSQL 一致性
    pub consistency_with_pg: bool,
}

/// 基准对比
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkComparison {
    /// PostgreSQL 基准值
    pub pg_baseline: f64,
    /// 分布式 DB 结果
    pub distributed_result: f64,
    /// 吞吐量比率(distributed / pg)
    pub throughput_ratio: f64,
    /// 延迟比率(distributed / pg)
    pub latency_ratio: f64,
}

/// 分布式 DB 兼容性报告
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedDbCompatReport {
    /// 数据库类型
    pub db_type: DistributedDbType,
    /// SQL 兼容性测试结果列表
    pub sql_compat_results: Vec<SqlCompatResult>,
    /// 分布式事务行为结果列表
    pub distributed_tx_behavior: Vec<TxBehaviorResult>,
    /// 基准对比
    pub benchmark_vs_pg: Vec<BenchmarkComparison>,
}

impl DistributedDbCompatReport {
    /// 创建新的兼容性报告
    pub fn new(db_type: DistributedDbType) -> Self {
        Self {
            db_type,
            sql_compat_results: Vec::new(),
            distributed_tx_behavior: Vec::new(),
            benchmark_vs_pg: Vec::new(),
        }
    }

    /// 添加 SQL 兼容性结果
    pub fn add_sql_compat(&mut self, result: SqlCompatResult) {
        self.sql_compat_results.push(result);
    }

    /// 添加事务行为结果
    pub fn add_tx_behavior(&mut self, result: TxBehaviorResult) {
        self.distributed_tx_behavior.push(result);
    }

    /// 添加基准对比
    pub fn add_benchmark(&mut self, comparison: BenchmarkComparison) {
        self.benchmark_vs_pg.push(comparison);
    }

    /// 计算总体 SQL 兼容率
    pub fn overall_sql_compat_rate(&self) -> f64 {
        if self.sql_compat_results.is_empty() {
            return 0.0;
        }
        let compatible = self
            .sql_compat_results
            .iter()
            .filter(|r| r.is_compatible)
            .count();
        compatible as f64 / self.sql_compat_results.len() as f64
    }

    /// 序列化为 JSON
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }
}
// ============================================================================
// v7.6.0 数据库后端兼容性验证(Oracle / MSSQL / PostGIS vs PostgreSQL 18 基准)
// ============================================================================

#[cfg(feature = "db-backend-compat")]
mod db_backend_compat {
    use serde::{Deserialize, Serialize};

    /// 数据库后端类型
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub enum BackendDbType {
        Oracle,
        Mssql,
        Postgis,
        PostgreSql18,
    }

    impl BackendDbType {
        pub fn name(&self) -> &str {
            match self {
                BackendDbType::Oracle => "Oracle 23ai",
                BackendDbType::Mssql => "MSSQL",
                BackendDbType::Postgis => "PostGIS",
                BackendDbType::PostgreSql18 => "PostgreSQL 18",
            }
        }
    }

    /// SQL 兼容性结果
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BackendSqlCompatResult {
        pub sql: String,
        pub compatible: bool,
        pub incompatible_reason: Option<String>,
        pub alternative_sql: Option<String>,
    }

    /// 事务行为结果
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BackendTxBehavior {
        pub isolation_level: String,
        pub supports_snapshot: bool,
        pub timeout_ms: u64,
        pub retry_supported: bool,
    }

    /// 基准对比
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BackendBenchmark {
        pub query: String,
        pub pg_latency_ms: f64,
        pub backend_latency_ms: f64,
        pub ratio: f64,
    }

    /// 数据库后端兼容性报告
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct DbBackendCompatReport {
        pub db_type: BackendDbType,
        pub sql_compat_results: Vec<BackendSqlCompatResult>,
        pub tx_behavior: Vec<BackendTxBehavior>,
        pub benchmark: Vec<BackendBenchmark>,
    }

    impl DbBackendCompatReport {
        pub fn new(db_type: BackendDbType) -> Self {
            Self {
                db_type,
                sql_compat_results: Vec::new(),
                tx_behavior: Vec::new(),
                benchmark: Vec::new(),
            }
        }

        pub fn add_sql_result(&mut self, result: BackendSqlCompatResult) {
            self.sql_compat_results.push(result);
        }

        pub fn add_tx_behavior(&mut self, behavior: BackendTxBehavior) {
            self.tx_behavior.push(behavior);
        }

        pub fn add_benchmark(&mut self, bench: BackendBenchmark) {
            self.benchmark.push(bench);
        }

        pub fn compat_rate(&self) -> f64 {
            if self.sql_compat_results.is_empty() {
                return 0.0;
            }
            let compatible = self
                .sql_compat_results
                .iter()
                .filter(|r| r.compatible)
                .count();
            compatible as f64 / self.sql_compat_results.len() as f64
        }

        pub fn to_json(&self) -> Result<String, serde_json::Error> {
            serde_json::to_string_pretty(self)
        }
    }

    /// 生成 Oracle 兼容性报告
    pub fn generate_oracle_compat_report() -> DbBackendCompatReport {
        let mut report = DbBackendCompatReport::new(BackendDbType::Oracle);
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT * FROM users WHERE id = ?".to_string(),
            compatible: true,
            incompatible_reason: None,
            alternative_sql: None,
        });
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT * FROM users LIMIT 10".to_string(),
            compatible: false,
            incompatible_reason: Some(
                "Oracle 不支持 LIMIT,需用 ROWNUM 或 FETCH FIRST".to_string(),
            ),
            alternative_sql: Some("SELECT * FROM users WHERE ROWNUM <= 10".to_string()),
        });
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT * FROM users OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY".to_string(),
            compatible: true,
            incompatible_reason: None,
            alternative_sql: None,
        });
        report.add_tx_behavior(BackendTxBehavior {
            isolation_level: "SERIALIZABLE".to_string(),
            supports_snapshot: true,
            timeout_ms: 30000,
            retry_supported: true,
        });
        report
    }

    /// 生成 MSSQL 兼容性报告
    pub fn generate_mssql_compat_report() -> DbBackendCompatReport {
        let mut report = DbBackendCompatReport::new(BackendDbType::Mssql);
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT TOP 10 * FROM users".to_string(),
            compatible: true,
            incompatible_reason: None,
            alternative_sql: None,
        });
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT * FROM users LIMIT 10".to_string(),
            compatible: false,
            incompatible_reason: Some("MSSQL 不支持 LIMIT,需用 TOP 或 OFFSET FETCH".to_string()),
            alternative_sql: Some("SELECT TOP 10 * FROM users".to_string()),
        });
        report.add_tx_behavior(BackendTxBehavior {
            isolation_level: "SNAPSHOT".to_string(),
            supports_snapshot: true,
            timeout_ms: 30000,
            retry_supported: true,
        });
        report
    }

    /// 生成 PostGIS 兼容性报告
    pub fn generate_postgis_compat_report() -> DbBackendCompatReport {
        let mut report = DbBackendCompatReport::new(BackendDbType::Postgis);
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT ST_AsText(geom) FROM locations".to_string(),
            compatible: true,
            incompatible_reason: None,
            alternative_sql: None,
        });
        report.add_sql_result(BackendSqlCompatResult {
            sql: "SELECT ST_Distance(a.geom, b.geom) FROM locations a, locations b".to_string(),
            compatible: true,
            incompatible_reason: None,
            alternative_sql: None,
        });
        report.add_tx_behavior(BackendTxBehavior {
            isolation_level: "SERIALIZABLE".to_string(),
            supports_snapshot: true,
            timeout_ms: 30000,
            retry_supported: true,
        });
        report
    }

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

        #[test]
        fn oracle_compat_report() {
            let report = generate_oracle_compat_report();
            assert_eq!(report.db_type, BackendDbType::Oracle);
            assert!(report.sql_compat_results.len() >= 2);
            assert!(report.compat_rate() > 0.0);
        }

        #[test]
        fn mssql_compat_report() {
            let report = generate_mssql_compat_report();
            assert_eq!(report.db_type, BackendDbType::Mssql);
            assert!(report.sql_compat_results.len() >= 2);
        }

        #[test]
        fn postgis_compat_report() {
            let report = generate_postgis_compat_report();
            assert_eq!(report.db_type, BackendDbType::Postgis);
            assert!(report.compat_rate() > 0.0);
        }

        #[test]
        fn oracle_limit_incompatible() {
            let report = generate_oracle_compat_report();
            let limit_result = report
                .sql_compat_results
                .iter()
                .find(|r| r.sql.contains("LIMIT"))
                .unwrap();
            assert!(!limit_result.compatible);
            assert!(limit_result.alternative_sql.is_some());
        }

        #[test]
        fn mssql_limit_incompatible() {
            let report = generate_mssql_compat_report();
            let limit_result = report
                .sql_compat_results
                .iter()
                .find(|r| r.sql.contains("LIMIT"))
                .unwrap();
            assert!(!limit_result.compatible);
            assert!(limit_result.alternative_sql.is_some());
        }

        #[test]
        fn postgis_spatial_compatible() {
            let report = generate_postgis_compat_report();
            assert!(report.sql_compat_results.iter().all(|r| r.compatible));
        }

        #[test]
        fn compat_report_to_json() {
            let report = generate_oracle_compat_report();
            let json = report.to_json().unwrap();
            assert!(json.contains("Oracle"));
            assert!(json.contains("compatible"));
        }

        #[test]
        fn backend_db_type_name() {
            assert_eq!(BackendDbType::Oracle.name(), "Oracle 23ai");
            assert_eq!(BackendDbType::Mssql.name(), "MSSQL");
            assert_eq!(BackendDbType::Postgis.name(), "PostGIS");
            assert_eq!(BackendDbType::PostgreSql18.name(), "PostgreSQL 18");
        }
    }
}

#[cfg(feature = "db-backend-compat")]
pub use db_backend_compat::{
    generate_mssql_compat_report, generate_oracle_compat_report, generate_postgis_compat_report,
    BackendBenchmark, BackendDbType, BackendSqlCompatResult, BackendTxBehavior,
    DbBackendCompatReport,
};