Skip to content

Commit a0e6e42

Browse files
committed
feat(database): 支持多数据库后端连接
- 尝试添加对 MySQL 和 PostgreSQL 数据库的支持 - 使用条件编译特性启用不同数据库驱动 - 更新依赖配置以支持可选的数据库功能 - 修改连接管理逻辑适配多种数据库类型 - 调整插入操作实现以兼容各数据库语法差异 - 移除旧的单数据库限制并增强扩展性
1 parent f99b797 commit a0e6e42

File tree

10 files changed

+171
-34
lines changed

10 files changed

+171
-34
lines changed

Cargo.lock

Lines changed: 101 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,8 @@ panic = "abort"
99
strip = true
1010

1111
[workspace.dependencies]
12-
# NOTE: 可选数据库后端(todo)
13-
#pq-sys = { version = "0.7", features = ["bundled"] }
14-
#libsqlite3-sys = { version = "0.35", features = ["bundled"] }
15-
#mysqlclient-sys = { version = "0.4", features = ["bundled"] }
16-
17-
#openssl-sys = { version = "0.9", features = ["vendored"] }
18-
libsqlite3-sys = { version = "0.35", features = ["bundled"] }
1912
diesel = { version = "2.2", features = [
20-
"sqlite",
21-
# "mysql",
22-
# "postgres",
2313
"chrono",
24-
"returning_clauses_for_sqlite_3_35",
2514
"r2d2",
2615
] }
2716
diesel_migrations = "2.2"

file_classification_cli/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ path = "src/main.rs"
2020
assert_cmd = "2.0.14"
2121

2222
[target.'cfg(not(target_env = "musl"))'.dependencies]
23-
libsqlite3-sys = { workspace = true }
23+
24+
[features]
25+
default = ["sqlite"]
26+
sqlite = ["file_classification_core/sqlite"]
27+
mysql = ["file_classification_core/mysql"]
28+
postgres = ["file_classification_core/postgres"]

file_classification_core/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ edition = "2024"
66
[dependencies]
77
diesel = { workspace = true }
88
diesel_migrations = { workspace = true }
9-
libsqlite3-sys = { workspace = true }
9+
libsqlite3-sys = { version = "0.35", features = ["bundled"], optional = true }
10+
mysqlclient-sys = { version = "0.4", features = ["bundled"], optional = true }
11+
pq-sys = { version = "0.7", features = ["bundled"], optional = true }
12+
openssl-sys = { version = "0.9", features = ["vendored"], optional = true }
1013
chrono = { workspace = true }
1114
serde = { workspace = true }
1215
utoipa = { workspace = true, optional = true }
13-
#mysqlclient-sys = { workspace = true }
14-
#libsqlite3-sys = { workspace = true }
15-
#openssl-sys = { workspace = true }
16-
#pq-sys = { workspace = true }
1716

1817
[features]
19-
default = []
18+
default = ["sqlite"]
19+
sqlite = ["libsqlite3-sys", "diesel/sqlite", "diesel/returning_clauses_for_sqlite_3_35"]
20+
mysql = ["mysqlclient-sys", "diesel/mysql"]
21+
postgres = ["pq-sys", "openssl-sys", "diesel/postgres"]
2022
swagger = ["utoipa"]

file_classification_core/src/internal/files.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ pub fn insert_file(
2525
) -> Result<i32, diesel::result::Error> {
2626
// 使用 match 表达式根据连接类型选择实现方式
2727
match conn {
28+
#[cfg(feature = "sqlite")]
2829
// 对于 SQLite 连接,使用 returning 子句
2930
AnyConnection::Sqlite(_) => {
3031
diesel::insert_into(files::table).values(new_file).returning(files::id).get_result(conn)
3132
}
32-
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql,注意不要删除以下注释内容,将来会用到)
33-
/*
33+
#[cfg(feature = "mysql")]
34+
// 对于 MySQL 连接,使用事务方式
3435
AnyConnection::Mysql(_) => {
3536
conn.transaction(|conn| {
3637
// 执行插入操作
@@ -45,7 +46,6 @@ pub fn insert_file(
4546
Ok(last_id)
4647
})
4748
},
48-
*/
4949
// 默认情况(如其他数据库类型)使用 returning 子句
5050
_ => diesel::insert_into(files::table).values(new_file).returning(files::id).get_result(conn),
5151
}

file_classification_core/src/internal/groups.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ pub fn insert_group(
2626
) -> Result<i32, diesel::result::Error> {
2727
// 使用 match 表达式根据连接类型选择实现方式
2828
match conn {
29+
#[cfg(feature = "sqlite")]
2930
// 对于 SQLite 连接,使用 returning 子句
3031
AnyConnection::Sqlite(_) => {
3132
diesel::insert_into(groups::table).values(new_group).returning(groups::id).get_result(conn)
3233
}
33-
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql,注意不要删除以下注释内容,将来会用到)
34-
/*
34+
#[cfg(feature = "mysql")]
35+
// 对于 MySQL 连接,使用事务方式
3536
AnyConnection::Mysql(_) => {
3637
conn.transaction(|conn| {
3738
// 执行插入操作
@@ -46,7 +47,6 @@ pub fn insert_group(
4647
Ok(last_id)
4748
})
4849
},
49-
*/
5050
// 默认情况(如其他数据库类型)使用 returning 子句
5151
_ => {
5252
diesel::insert_into(groups::table).values(new_group).returning(groups::id).get_result(conn)

file_classification_core/src/internal/tags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ pub fn insert_tag(
2525
) -> Result<i32, diesel::result::Error> {
2626
// 使用 match 表达式根据连接类型选择实现方式
2727
match conn {
28+
#[cfg(feature = "sqlite")]
2829
// 对于 SQLite 连接,使用 returning 子句
2930
AnyConnection::Sqlite(_) => {
3031
diesel::insert_into(tags::table).values(new_tag).returning(tags::id).get_result(conn)
3132
}
33+
#[cfg(feature = "mysql")]
3234
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql,注意不要删除以下注释内容,将来会用到)
33-
/*
3435
AnyConnection::Mysql(_) => {
3536
conn.transaction(|conn| {
3637
// 执行插入操作
@@ -45,7 +46,6 @@ pub fn insert_tag(
4546
Ok(last_id)
4647
})
4748
},
48-
*/
4949
// 默认情况(如其他数据库类型)使用 returning 子句
5050
_ => diesel::insert_into(tags::table).values(new_tag).returning(tags::id).get_result(conn),
5151
}

file_classification_core/src/utils/database.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
// database.rs
22
//! 数据库连接管理模块
33
//!
4-
//! 提供数据库连接的建立和管理功能,支持多种数据库类型(当前仅支持 SQLite)
4+
//! 提供数据库连接的建立和管理功能,支持多种数据库类型,
55
//! 并封装了通用的数据库连接类型。
66
77
pub use diesel::{Connection, QueryResult};
88
use diesel::RunQueryDsl;
9+
910
/// 通用数据库连接枚举
1011
///
1112
/// 使用 `diesel::MultiConnection` 宏定义的枚举类型,支持多种数据库连接类型。
12-
/// 当前仅支持 SQLite 连接,但预留了扩展其他数据库类型的接口。
1313
#[derive(diesel::MultiConnection)]
1414
pub enum AnyConnection {
15-
// Postgresql(diesel::PgConnection),
16-
// Mysql(diesel::MysqlConnection),
15+
#[cfg(feature = "postgres")]
16+
/// PostgreSQL 数据库连接
17+
Postgresql(diesel::PgConnection),
18+
19+
#[cfg(feature = "mysql")]
20+
/// MySQL 数据库连接
21+
Mysql(diesel::MysqlConnection),
22+
23+
#[cfg(feature = "sqlite")]
1724
/// SQLite 数据库连接
1825
Sqlite(diesel::SqliteConnection),
1926
}
@@ -24,13 +31,14 @@ pub enum AnyConnection {
2431
///
2532
/// 参数:
2633
/// - `database_url`: 数据库连接字符串
27-
/// - `database_type`: 数据库类型(当前仅支持 "sqlite")
34+
/// - `database_type`: 数据库类型
2835
///
2936
/// 返回值:
3037
/// 成功时返回封装好的数据库连接对象,失败时会 panic 并输出错误信息
3138
pub fn establish_connection(database_url: &str, database_type: &str) -> AnyConnection {
3239
// 根据数据库类型建立相应的连接
3340
match database_type {
41+
#[cfg(feature = "sqlite")]
3442
"sqlite" => {
3543
let mut conn = AnyConnection::Sqlite(
3644
diesel::SqliteConnection::establish(database_url)
@@ -44,8 +52,25 @@ pub fn establish_connection(database_url: &str, database_type: &str) -> AnyConne
4452

4553
conn
4654
},
55+
56+
#[cfg(feature = "mysql")]
57+
"mysql" => {
58+
AnyConnection::Mysql(
59+
diesel::MysqlConnection::establish(database_url)
60+
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
61+
)
62+
},
63+
64+
#[cfg(feature = "postgres")]
65+
"postgres" => {
66+
AnyConnection::Postgresql(
67+
diesel::PgConnection::establish(database_url)
68+
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
69+
)
70+
},
71+
4772
// 不支持的数据库类型直接 panic
48-
_ => panic!("Unsupported database type: {}", database_type),
73+
_ => panic!("Unsupported database type: {} or feature not enabled", database_type),
4974
}
5075
}
5176

file_classification_webapi/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ mime_guess = "2.0.5"
2626
fern = "0.6"
2727
dotenvy = "0.15"
2828

29+
[features]
30+
default = ["sqlite"]
31+
sqlite = ["file_classification_core/sqlite", "diesel/sqlite", "diesel/returning_clauses_for_sqlite_3_35"]
32+
mysql = ["file_classification_core/mysql", "diesel/mysql"]
33+
postgres = ["file_classification_core/postgres", "diesel/postgres"]
34+
2935
[[bin]]
3036
name = "file_classification_webapi"
3137
doc = false

0 commit comments

Comments
 (0)