rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
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
//! 连接池核心模块

use crossbeam_queue::SegQueue;
use rat_logger::{debug, error, info, warn};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, oneshot};

#[cfg(feature = "sqlite-support")]
use super::SqliteWorker;
use super::{
    DatabaseConnection, DatabaseOperation, ExtendedPoolConfig, MultiConnectionManager,
    PooledConnection,
};
use crate::adapter::DatabaseAdapter;
use crate::error::{QuickDbError, QuickDbResult};
use crate::model::FieldDefinition;
use crate::types::*;

/// 新的连接池 - 基于生产者/消费者模式
#[derive(Debug)]
pub struct ConnectionPool {
    /// 数据库配置
    pub db_config: DatabaseConfig,
    /// 扩展连接池配置
    pub config: ExtendedPoolConfig,
    /// 操作请求发送器
    pub operation_sender: mpsc::UnboundedSender<DatabaseOperation>,
    /// 数据库类型
    pub db_type: DatabaseType,
    /// 缓存管理器(可选)
    pub cache_manager: Option<Arc<crate::cache::CacheManager>>,
}

impl ConnectionPool {
    /// 使用配置创建连接池
    pub async fn with_config(
        db_config: DatabaseConfig,
        config: ExtendedPoolConfig,
    ) -> QuickDbResult<Self> {
        Self::with_config_and_cache(db_config, config, None).await
    }

    /// 使用配置和缓存管理器创建连接池
    pub async fn with_config_and_cache(
        db_config: DatabaseConfig,
        config: ExtendedPoolConfig,
        cache_manager: Option<Arc<crate::cache::CacheManager>>,
    ) -> QuickDbResult<Self> {
        let (operation_sender, operation_receiver) = mpsc::unbounded_channel();

        let pool = Self {
            db_type: db_config.db_type.clone(),
            db_config: db_config.clone(),
            config: config.clone(),
            operation_sender,
            cache_manager: cache_manager.clone(),
        };

        // 根据数据库类型启动对应的工作器
        match &db_config.db_type {
            #[cfg(feature = "sqlite-support")]
            DatabaseType::SQLite => {
                pool.start_sqlite_worker(operation_receiver, db_config, config)
                    .await?;
            }
            #[cfg(feature = "postgres-support")]
            DatabaseType::PostgreSQL => {
                pool.start_multi_connection_manager(operation_receiver, db_config, config)
                    .await?;
            }
            #[cfg(feature = "mysql-support")]
            DatabaseType::MySQL => {
                pool.start_multi_connection_manager(operation_receiver, db_config, config)
                    .await?;
            }
            #[cfg(feature = "mongodb-support")]
            DatabaseType::MongoDB => {
                pool.start_multi_connection_manager(operation_receiver, db_config, config)
                    .await?;
            }
            #[allow(unreachable_patterns)]
            _ => Err(QuickDbError::ConfigError {
                message: "不支持的数据库类型(可能需要启用相应的feature)".to_string(),
            })?,
        }

        Ok(pool)
    }

    /// 设置缓存管理器
    pub fn set_cache_manager(&mut self, cache_manager: Arc<crate::cache::CacheManager>) {
        self.cache_manager = Some(cache_manager);
    }

    /// 启动SQLite工作器
    #[cfg(feature = "sqlite-support")]
    async fn start_sqlite_worker(
        &self,
        operation_receiver: mpsc::UnboundedReceiver<DatabaseOperation>,
        db_config: DatabaseConfig,
        config: ExtendedPoolConfig,
    ) -> QuickDbResult<()> {
        let connection = self.create_sqlite_connection().await?;

        // 创建启动同步通道
        let (startup_tx, startup_rx) = oneshot::channel();

        // 创建适配器
        use crate::adapter::{create_adapter, create_adapter_with_cache};
        let (adapter, adapter_type) = if let Some(cache_manager) = &self.cache_manager {
            let adapter = create_adapter_with_cache(&db_config.db_type, cache_manager.clone())?;
            (adapter, "缓存适配器")
        } else {
            let adapter = create_adapter(&db_config.db_type)?;
            (adapter, "普通适配器")
        };

        info!("数据库 '{}' 使用 {}", db_config.alias, adapter_type);

        let worker = SqliteWorker {
            connection,
            operation_receiver,
            db_config: db_config.clone(),
            retry_count: 0,
            max_retries: config.max_retries,
            retry_interval_ms: config.retry_interval_ms,
            health_check_interval_sec: config.health_check_timeout_sec, // 复用健康检查超时作为间隔
            last_health_check: Instant::now(),
            is_healthy: true,
            cache_manager: self.cache_manager.clone(),
            adapter,
        };

        // 启动工作器
        tokio::spawn(async move {
            // 发送启动完成信号
            let _ = startup_tx.send(());
            worker.run().await;
        });

        // 等待工作器启动完成
        startup_rx
            .await
            .map_err(|_| QuickDbError::ConnectionError {
                message: crate::i18n::tf(
                    "error.sqlite_worker_startup",
                    &[("alias", &db_config.alias)],
                ),
            })?;

        info!("SQLite工作器启动完成: 别名={}", db_config.alias);
        Ok(())
    }

    /// 启动多连接管理器
    async fn start_multi_connection_manager(
        &self,
        operation_receiver: mpsc::UnboundedReceiver<DatabaseOperation>,
        db_config: DatabaseConfig,
        config: ExtendedPoolConfig,
    ) -> QuickDbResult<()> {
        let manager = MultiConnectionManager {
            workers: Vec::new(),
            available_workers: SegQueue::new(),
            operation_receiver,
            db_config,
            config,
            keepalive_handle: None,
            cache_manager: self.cache_manager.clone(),
        };

        // 启动管理器
        tokio::spawn(async move {
            manager.run().await;
        });

        Ok(())
    }

    /// 创建SQLite连接
    #[cfg(feature = "sqlite-support")]
    async fn create_sqlite_connection(&self) -> QuickDbResult<DatabaseConnection> {
        let (path, create_if_missing) = match &self.db_config.connection {
            crate::types::ConnectionConfig::SQLite {
                path,
                create_if_missing,
            } => (path.clone(), *create_if_missing),
            _ => {
                return Err(QuickDbError::ConfigError {
                    message: crate::i18n::t("error.sqlite_config_mismatch"),
                });
            }
        };

        // 特殊处理内存数据库:直接连接,不创建文件
        if path == ":memory:" {
            info!("连接SQLite内存数据库: 别名={}", self.db_config.alias);
            let pool = sqlx::SqlitePool::connect(&path).await.map_err(|e| {
                QuickDbError::ConnectionError {
                    message: crate::i18n::tf("error.sqlite_memory", &[("message", &e.to_string())]),
                }
            })?;
            return Ok(DatabaseConnection::SQLite(pool));
        }

        // 检查数据库文件是否存在
        let file_exists = std::path::Path::new(&path).exists();

        // 如果文件不存在且不允许创建,则返回错误
        if !file_exists && !create_if_missing {
            return Err(QuickDbError::ConnectionError {
                message: crate::i18n::tf("error.sqlite_file_not_found", &[("path", &path)]),
            });
        }

        // 如果需要创建文件且文件不存在,则创建父目录
        if create_if_missing && !file_exists {
            if let Some(parent) = std::path::Path::new(&path).parent() {
                tokio::fs::create_dir_all(parent).await.map_err(|e| {
                    QuickDbError::ConnectionError {
                        message: crate::i18n::tf(
                            "error.sqlite_dir_create",
                            &[("message", &e.to_string())],
                        ),
                    }
                })?;
            }

            // 创建空的数据库文件
            tokio::fs::File::create(&path)
                .await
                .map_err(|e| QuickDbError::ConnectionError {
                    message: crate::i18n::tf(
                        "error.sqlite_file_create",
                        &[("message", &e.to_string())],
                    ),
                })?;
        }

        let pool =
            sqlx::SqlitePool::connect(&path)
                .await
                .map_err(|e| QuickDbError::ConnectionError {
                    message: crate::i18n::tf(
                        "error.sqlite_connection",
                        &[("message", &e.to_string())],
                    ),
                })?;
        Ok(DatabaseConnection::SQLite(pool))
    }

    /// 发送操作请求并等待响应
    async fn send_operation<T>(&self, operation: DatabaseOperation) -> QuickDbResult<T>
    where
        T: Send + 'static,
    {
        // 这是一个泛型占位符,实际实现需要根据具体操作类型来处理
        Err(QuickDbError::QueryError {
            message: "操作发送未实现".to_string(),
        })
    }

    /// 创建记录
    pub async fn create(
        &self,
        table: &str,
        data: &HashMap<String, DataValue>,
        id_strategy: &IdStrategy,
    ) -> QuickDbResult<DataValue> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::Create {
            table: table.to_string(),
            data: data.clone(),
            id_strategy: id_strategy.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 根据ID查找记录
    pub async fn find_by_id(
        &self,
        table: &str,
        id: &DataValue,
    ) -> QuickDbResult<Option<DataValue>> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::FindById {
            table: table.to_string(),
            id: id.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 查找记录
    pub async fn find(
        &self,
        table: &str,
        conditions: &[QueryConditionWithConfig],
        options: &QueryOptions,
    ) -> QuickDbResult<Vec<DataValue>> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::Find {
            table: table.to_string(),
            conditions: conditions.to_vec(),
            options: options.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 更新记录
    pub async fn update(
        &self,
        table: &str,
        conditions: &[QueryConditionWithConfig],
        data: &HashMap<String, DataValue>,
    ) -> QuickDbResult<u64> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::Update {
            table: table.to_string(),
            conditions: conditions.to_vec(),
            data: data.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 根据ID更新记录
    pub async fn update_by_id(
        &self,
        table: &str,
        id: &DataValue,
        data: &HashMap<String, DataValue>,
    ) -> QuickDbResult<bool> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::UpdateById {
            table: table.to_string(),
            id: id.clone(),
            data: data.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 删除记录
    pub async fn delete(
        &self,
        table: &str,
        conditions: &[QueryConditionWithConfig],
        alias: &str,
    ) -> QuickDbResult<u64> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::Delete {
            table: table.to_string(),
            conditions: conditions.to_vec(),
            alias: alias.to_string(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 根据ID删除记录
    pub async fn delete_by_id(
        &self,
        table: &str,
        id: &DataValue,
        alias: &str,
    ) -> QuickDbResult<bool> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::DeleteById {
            table: table.to_string(),
            id: id.clone(),
            alias: alias.to_string(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 统计记录
    pub async fn count(
        &self,
        table: &str,
        conditions: &[QueryConditionWithConfig],
        alias: &str,
    ) -> QuickDbResult<u64> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::Count {
            table: table.to_string(),
            conditions: conditions.to_vec(),
            alias: alias.to_string(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 创建表
    pub async fn create_table(
        &self,
        table: &str,
        fields: &HashMap<String, FieldDefinition>,
        id_strategy: &IdStrategy,
    ) -> QuickDbResult<()> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::CreateTable {
            table: table.to_string(),
            fields: fields.clone(),
            id_strategy: id_strategy.clone(),
            alias: self.db_config.alias.clone(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 创建索引
    pub async fn create_index(
        &self,
        table: &str,
        index_name: &str,
        fields: &[String],
        unique: bool,
    ) -> QuickDbResult<()> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::CreateIndex {
            table: table.to_string(),
            index_name: index_name.to_string(),
            fields: fields.to_vec(),
            unique,
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 检查表是否存在
    pub async fn table_exists(&self, table: &str) -> QuickDbResult<bool> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::TableExists {
            table: table.to_string(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 删除表
    pub async fn drop_table(&self, table: &str) -> QuickDbResult<()> {
        let (response_sender, response_receiver) = oneshot::channel();

        let operation = DatabaseOperation::DropTable {
            table: table.to_string(),
            response: response_sender,
        };

        self.operation_sender
            .send(operation)
            .map_err(|_| QuickDbError::QueryError {
                message: "发送操作失败".to_string(),
            })?;

        response_receiver
            .await
            .map_err(|_| QuickDbError::QueryError {
                message: "接收响应失败".to_string(),
            })?
    }

    /// 获取数据库类型
    pub fn get_database_type(&self) -> &DatabaseType {
        &self.db_config.db_type
    }

    /// 获取连接(兼容旧接口)
    pub async fn get_connection(&self) -> QuickDbResult<PooledConnection> {
        // 在新架构中,我们不再直接返回连接
        // 这个方法主要用于兼容性,返回一个虚拟连接
        Ok(PooledConnection {
            id: format!("{}-virtual", self.db_config.alias),
            db_type: self.db_config.db_type.clone(),
            alias: self.db_config.alias.clone(),
        })
    }

    /// 释放连接(兼容旧接口)
    pub async fn release_connection(&self, _connection_id: &str) -> QuickDbResult<()> {
        // 在新架构中,连接由工作器自动管理,这个方法为空实现
        Ok(())
    }

    /// 清理过期连接(兼容旧接口)
    pub async fn cleanup_expired_connections(&self) {
        // 在新架构中,连接由工作器自动管理,这个方法为空实现
        debug!("清理过期连接(新架构中自动管理)");
    }
}