sz-orm-core 5.0.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
//! # 连接级多租户隔离
//!
//! 在同一连接池中连接绑定到特定租户(通过 `SET app.tenant_id = ?`),
//! 避免每租户独立池的资源开销。支持三种连接亲和策略 + RAII 守卫。

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use crate::db_type::DbType;
use crate::pool::Pool;

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// 连接级隔离机制
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionLevelIsolation {
    /// 通过 `SET app.tenant_id = ?` 设置租户上下文
    SetTenantId,
    /// Schema 隔离,路由到 `tenant_{id}_{table}`
    SchemaIsolation,
    /// 连接绑定,连接专属租户
    ConnectionBinding,
}

/// 连接亲和策略
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionAffinityPolicy {
    /// 严格亲和:仅使用绑定到该租户的连接
    Strict,
    /// 优先亲和:优先使用绑定连接,无可用时获取任意连接
    Preferred,
    /// 无亲和:任意连接,每次设置租户上下文
    None,
}

/// 连接级多租户配置
#[derive(Debug, Clone)]
pub struct ConnectionLevelTenantConfig {
    /// 隔离机制
    pub isolation: ConnectionLevelIsolation,
    /// 亲和策略
    pub affinity_policy: ConnectionAffinityPolicy,
    /// 亲和超时(毫秒)
    pub affinity_timeout_ms: u64,
    /// 数据库类型
    pub db_type: DbType,
}

impl ConnectionLevelTenantConfig {
    /// 创建默认配置(SetTenantId, Preferred, 5000ms)
    pub fn new(db_type: DbType) -> Self {
        Self {
            isolation: ConnectionLevelIsolation::SetTenantId,
            affinity_policy: ConnectionAffinityPolicy::Preferred,
            affinity_timeout_ms: 5_000,
            db_type,
        }
    }

    /// 设置隔离机制
    pub fn with_isolation(mut self, isolation: ConnectionLevelIsolation) -> Self {
        self.isolation = isolation;
        self
    }

    /// 设置亲和策略
    pub fn with_affinity_policy(mut self, policy: ConnectionAffinityPolicy) -> Self {
        self.affinity_policy = policy;
        self
    }

    /// 设置亲和超时(毫秒)
    pub fn with_affinity_timeout_ms(mut self, ms: u64) -> Self {
        self.affinity_timeout_ms = ms;
        self
    }
}

/// 租户错误类型
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TenantError {
    /// 无可用绑定连接
    NoBoundConnection,
    /// 篡改被拒绝
    TamperingRejected,
    /// 清理失败
    CleanupFailed,
    /// 不支持的方言
    UnsupportedDialect,
    /// 租户 ID 为空
    EmptyTenantId,
}

impl std::fmt::Display for TenantError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TenantError::NoBoundConnection => write!(f, "no connection bound to tenant"),
            TenantError::TamperingRejected => write!(f, "tenant context tampering rejected"),
            TenantError::CleanupFailed => write!(f, "tenant context cleanup failed"),
            TenantError::UnsupportedDialect => {
                write!(f, "unsupported dialect for SET app.tenant_id")
            }
            TenantError::EmptyTenantId => write!(f, "tenant_id is empty"),
        }
    }
}

impl std::error::Error for TenantError {}

/// 连接 ID 类型
pub type ConnectionId = u64;

/// 连接租户绑定记录
#[derive(Debug, Clone)]
pub struct TenantBinding {
    /// 连接 ID
    pub connection_id: ConnectionId,
    /// 绑定的租户 ID
    pub tenant_id: String,
    /// 绑定时间戳
    pub bound_at: u64,
}

/// 连接租户绑定器
pub struct ConnectionTenantBinder {
    pool: Arc<Pool>,
    config: ConnectionLevelTenantConfig,
    tenant_bindings: Mutex<HashMap<String, Vec<ConnectionId>>>,
    next_connection_id: Mutex<u64>,
}

impl ConnectionTenantBinder {
    /// 创建连接租户绑定器
    pub fn new(pool: Arc<Pool>, config: ConnectionLevelTenantConfig) -> Self {
        Self {
            pool,
            config,
            tenant_bindings: Mutex::new(HashMap::new()),
            next_connection_id: Mutex::new(1),
        }
    }

    /// 获取配置
    pub fn config(&self) -> &ConnectionLevelTenantConfig {
        &self.config
    }

    /// 判断是否支持 `SET app.tenant_id`
    pub fn supports_set_tenant_id(&self) -> bool {
        matches!(self.config.db_type, DbType::PostgreSQL | DbType::MySQL)
    }

    /// 生成 `SET app.tenant_id` SQL
    pub fn build_set_tenant_sql(&self, tenant_id: &str) -> String {
        format!("SET app.tenant_id = '{}'", tenant_id)
    }

    /// 生成清理租户上下文 SQL
    pub fn build_clear_tenant_sql(&self) -> String {
        "SET app.tenant_id = NULL".to_string()
    }

    /// 绑定连接到租户
    pub fn bind_connection(&self, tenant_id: &str) -> ConnectionId {
        let conn_id = {
            let mut next = self
                .next_connection_id
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            let id = *next;
            *next += 1;
            id
        };
        let mut bindings = self
            .tenant_bindings
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        bindings
            .entry(tenant_id.to_string())
            .or_default()
            .push(conn_id);
        conn_id
    }

    /// 查找绑定到指定租户的连接
    pub fn find_bound_connections(&self, tenant_id: &str) -> Vec<ConnectionId> {
        let bindings = self
            .tenant_bindings
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        bindings.get(tenant_id).cloned().unwrap_or_default()
    }

    /// 解绑连接
    pub fn unbind_connection(&self, tenant_id: &str, conn_id: ConnectionId) {
        let mut bindings = self
            .tenant_bindings
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        if let Some(conns) = bindings.get_mut(tenant_id) {
            conns.retain(|&id| id != conn_id);
        }
    }

    /// 获取绑定数量
    pub fn binding_count(&self, tenant_id: &str) -> usize {
        let bindings = self
            .tenant_bindings
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        bindings.get(tenant_id).map(|v| v.len()).unwrap_or(0)
    }

    /// 获取所有租户绑定
    pub fn all_bindings(&self) -> Vec<TenantBinding> {
        let bindings = self
            .tenant_bindings
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let mut result = Vec::new();
        for (tenant_id, conn_ids) in bindings.iter() {
            for &conn_id in conn_ids {
                result.push(TenantBinding {
                    connection_id: conn_id,
                    tenant_id: tenant_id.clone(),
                    bound_at: now_ms(),
                });
            }
        }
        result
    }

    /// 获取连接池引用
    pub fn pool(&self) -> &Arc<Pool> {
        &self.pool
    }

    /// 验证租户 ID
    pub fn validate_tenant_id(&self, tenant_id: &str) -> Result<(), TenantError> {
        if tenant_id.is_empty() {
            return Err(TenantError::EmptyTenantId);
        }
        Ok(())
    }

    /// 确定实际隔离机制(处理方言降级)
    pub fn resolve_isolation(&self) -> ConnectionLevelIsolation {
        if self.config.isolation == ConnectionLevelIsolation::SetTenantId
            && !self.supports_set_tenant_id()
        {
            ConnectionLevelIsolation::SchemaIsolation
        } else {
            self.config.isolation.clone()
        }
    }
}

/// 租户连接守卫(RAII)
pub struct TenantConnectionGuard {
    binder: Arc<ConnectionTenantBinder>,
    tenant_id: String,
    connection_id: ConnectionId,
    active: bool,
}

impl TenantConnectionGuard {
    /// 创建守卫
    pub fn new(
        binder: Arc<ConnectionTenantBinder>,
        tenant_id: String,
        connection_id: ConnectionId,
    ) -> Self {
        Self {
            binder,
            tenant_id,
            connection_id,
            active: true,
        }
    }

    /// 获取租户 ID
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }

    /// 获取连接 ID
    pub fn connection_id(&self) -> ConnectionId {
        self.connection_id
    }

    /// 是否活跃
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// 获取清理 SQL
    pub fn clear_tenant_sql(&self) -> String {
        self.binder.build_clear_tenant_sql()
    }

    /// 手动释放(提前清理)
    pub fn release(&mut self) -> Result<(), TenantError> {
        if !self.active {
            return Ok(());
        }
        self.binder
            .unbind_connection(&self.tenant_id, self.connection_id);
        self.active = false;
        Ok(())
    }
}

impl Drop for TenantConnectionGuard {
    fn drop(&mut self) {
        if self.active {
            self.binder
                .unbind_connection(&self.tenant_id, self.connection_id);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pool::{ConnectionFactory, PoolConfigBuilder};

    struct MockFactory;

    #[async_trait::async_trait]
    impl ConnectionFactory for MockFactory {
        async fn create(&self) -> Result<Box<dyn crate::pool::Connection>, crate::DbError> {
            Err(crate::DbError::PoolError(
                crate::error::PoolError::InvalidConfig("mock".to_string()),
            ))
        }
    }

    fn make_pool() -> Arc<Pool> {
        let config = PoolConfigBuilder::new().max_size(1).build().unwrap();
        let factory: Arc<dyn ConnectionFactory> = Arc::new(MockFactory);
        Arc::new(Pool::new(config, factory).unwrap())
    }

    #[test]
    fn test_config_default() {
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        assert_eq!(config.isolation, ConnectionLevelIsolation::SetTenantId);
        assert_eq!(config.affinity_policy, ConnectionAffinityPolicy::Preferred);
        assert_eq!(config.affinity_timeout_ms, 5_000);
        assert_eq!(config.db_type, DbType::PostgreSQL);
    }

    #[test]
    fn test_config_builder() {
        let config = ConnectionLevelTenantConfig::new(DbType::MySQL)
            .with_isolation(ConnectionLevelIsolation::ConnectionBinding)
            .with_affinity_policy(ConnectionAffinityPolicy::Strict)
            .with_affinity_timeout_ms(10_000);
        assert_eq!(
            config.isolation,
            ConnectionLevelIsolation::ConnectionBinding
        );
        assert_eq!(config.affinity_policy, ConnectionAffinityPolicy::Strict);
        assert_eq!(config.affinity_timeout_ms, 10_000);
    }

    #[test]
    fn test_isolation_serde() {
        let isolations = vec![
            ConnectionLevelIsolation::SetTenantId,
            ConnectionLevelIsolation::SchemaIsolation,
            ConnectionLevelIsolation::ConnectionBinding,
        ];
        for i in &isolations {
            let json = serde_json::to_string(i).unwrap();
            let decoded: ConnectionLevelIsolation = serde_json::from_str(&json).unwrap();
            assert_eq!(*i, decoded);
        }
    }

    #[test]
    fn test_affinity_policy_serde() {
        let policies = vec![
            ConnectionAffinityPolicy::Strict,
            ConnectionAffinityPolicy::Preferred,
            ConnectionAffinityPolicy::None,
        ];
        for p in &policies {
            let json = serde_json::to_string(p).unwrap();
            let decoded: ConnectionAffinityPolicy = serde_json::from_str(&json).unwrap();
            assert_eq!(*p, decoded);
        }
    }

    #[test]
    fn test_supports_set_tenant_id() {
        let pool = make_pool();
        let pg_config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let pg_binder = ConnectionTenantBinder::new(pool.clone(), pg_config);
        assert!(pg_binder.supports_set_tenant_id());

        let mysql_config = ConnectionLevelTenantConfig::new(DbType::MySQL);
        let mysql_binder = ConnectionTenantBinder::new(pool.clone(), mysql_config);
        assert!(mysql_binder.supports_set_tenant_id());

        let sqlite_config = ConnectionLevelTenantConfig::new(DbType::Sqlite);
        let sqlite_binder = ConnectionTenantBinder::new(pool.clone(), sqlite_config);
        assert!(!sqlite_binder.supports_set_tenant_id());

        let oracle_config = ConnectionLevelTenantConfig::new(DbType::Oracle);
        let oracle_binder = ConnectionTenantBinder::new(pool, oracle_config);
        assert!(!oracle_binder.supports_set_tenant_id());
    }

    #[test]
    fn test_build_set_tenant_sql() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);
        let sql = binder.build_set_tenant_sql("tenant_123");
        assert!(sql.contains("SET app.tenant_id"));
        assert!(sql.contains("tenant_123"));
    }

    #[test]
    fn test_build_clear_tenant_sql() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);
        let sql = binder.build_clear_tenant_sql();
        assert!(sql.contains("NULL"));
    }

    #[test]
    fn test_bind_and_find_connection() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);

        let conn_id1 = binder.bind_connection("tenant_1");
        let conn_id2 = binder.bind_connection("tenant_1");
        let conn_id3 = binder.bind_connection("tenant_2");

        assert_ne!(conn_id1, conn_id2);
        assert_ne!(conn_id1, conn_id3);

        let tenant1_conns = binder.find_bound_connections("tenant_1");
        assert_eq!(tenant1_conns.len(), 2);
        assert!(tenant1_conns.contains(&conn_id1));
        assert!(tenant1_conns.contains(&conn_id2));

        let tenant2_conns = binder.find_bound_connections("tenant_2");
        assert_eq!(tenant2_conns.len(), 1);
        assert!(tenant2_conns.contains(&conn_id3));

        assert_eq!(binder.binding_count("tenant_1"), 2);
        assert_eq!(binder.binding_count("tenant_2"), 1);
        assert_eq!(binder.binding_count("tenant_3"), 0);
    }

    #[test]
    fn test_unbind_connection() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);

        let conn_id1 = binder.bind_connection("tenant_1");
        let conn_id2 = binder.bind_connection("tenant_1");
        assert_eq!(binder.binding_count("tenant_1"), 2);

        binder.unbind_connection("tenant_1", conn_id1);
        assert_eq!(binder.binding_count("tenant_1"), 1);

        let conns = binder.find_bound_connections("tenant_1");
        assert!(conns.contains(&conn_id2));
        assert!(!conns.contains(&conn_id1));

        binder.unbind_connection("tenant_1", conn_id2);
        assert_eq!(binder.binding_count("tenant_1"), 0);
    }

    #[test]
    fn test_validate_tenant_id() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);

        assert!(binder.validate_tenant_id("tenant_1").is_ok());
        assert_eq!(
            binder.validate_tenant_id("").unwrap_err(),
            TenantError::EmptyTenantId
        );
    }

    #[test]
    fn test_resolve_isolation_pg() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);
        assert_eq!(
            binder.resolve_isolation(),
            ConnectionLevelIsolation::SetTenantId
        );
    }

    #[test]
    fn test_resolve_isolation_sqlite_fallback() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::Sqlite);
        let binder = ConnectionTenantBinder::new(pool, config);
        assert_eq!(
            binder.resolve_isolation(),
            ConnectionLevelIsolation::SchemaIsolation
        );
    }

    #[test]
    fn test_resolve_isolation_schema_no_fallback() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::Sqlite)
            .with_isolation(ConnectionLevelIsolation::SchemaIsolation);
        let binder = ConnectionTenantBinder::new(pool, config);
        assert_eq!(
            binder.resolve_isolation(),
            ConnectionLevelIsolation::SchemaIsolation
        );
    }

    #[test]
    fn test_tenant_error_display() {
        let err = TenantError::NoBoundConnection;
        assert!(err.to_string().contains("no connection"));
        let err = TenantError::EmptyTenantId;
        assert!(err.to_string().contains("empty"));
        let err = TenantError::UnsupportedDialect;
        assert!(err.to_string().contains("unsupported"));
    }

    #[test]
    fn test_guard_drop_unbinds() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = Arc::new(ConnectionTenantBinder::new(pool, config));

        let conn_id = binder.bind_connection("tenant_1");
        assert_eq!(binder.binding_count("tenant_1"), 1);

        {
            let _guard =
                TenantConnectionGuard::new(binder.clone(), "tenant_1".to_string(), conn_id);
            assert_eq!(binder.binding_count("tenant_1"), 1);
        }

        assert_eq!(binder.binding_count("tenant_1"), 0);
    }

    #[test]
    fn test_guard_release() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = Arc::new(ConnectionTenantBinder::new(pool, config));

        let conn_id = binder.bind_connection("tenant_1");
        let mut guard = TenantConnectionGuard::new(binder, "tenant_1".to_string(), conn_id);
        assert!(guard.is_active());
        guard.release().unwrap();
        assert!(!guard.is_active());
    }

    #[test]
    fn test_guard_properties() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = Arc::new(ConnectionTenantBinder::new(pool, config));

        let guard = TenantConnectionGuard::new(binder, "tenant_42".to_string(), 999);
        assert_eq!(guard.tenant_id(), "tenant_42");
        assert_eq!(guard.connection_id(), 999);
        assert!(guard.is_active());
        assert!(guard.clear_tenant_sql().contains("NULL"));
    }

    #[test]
    fn test_all_bindings() {
        let pool = make_pool();
        let config = ConnectionLevelTenantConfig::new(DbType::PostgreSQL);
        let binder = ConnectionTenantBinder::new(pool, config);

        binder.bind_connection("tenant_1");
        binder.bind_connection("tenant_1");
        binder.bind_connection("tenant_2");

        let all = binder.all_bindings();
        assert_eq!(all.len(), 3);
    }
}