Skip to content

Commit 7bf9e21

Browse files
committed
feat(db): 支持 PostgreSQL 的 returning 子句插入操作
- 在文件、分组和标签模块中添加对 PostgreSQL 数据库的支持 - 使用 returning 子句获取插入记录的 ID,替代手动查询 - 为 MySQL 数据库保留事务方式插入并获取 LAST_INSERT_ID() - 条件编译支持不同数据库特性(mysql 和 postgres)
1 parent e6875a1 commit 7bf9e21

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

file_classification_core/src/internal/files.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,24 @@ pub fn insert_file(
3333
#[cfg(feature = "mysql")]
3434
// 对于 MySQL 连接,使用事务方式
3535
AnyConnection::Mysql(mysql) => {
36-
conn.transaction(|mysql| {
37-
// 执行插入操作
38-
diesel::insert_into(files::table)
39-
.values(new_file)
40-
.execute(mysql)?;
41-
42-
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
43-
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
44-
.get_result(mysql)?;
45-
46-
Ok(last_id)
47-
})
36+
conn.transaction(|mysql| {
37+
// 执行插入操作
38+
diesel::insert_into(files::table)
39+
.values(new_file)
40+
.execute(mysql)?;
41+
42+
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
43+
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
44+
.get_result(mysql)?;
45+
46+
Ok(last_id)
47+
})
4848
},
49-
// 默认情况(如其他数据库类型)使用 returning 子句
50-
other_database_type => diesel::insert_into(files::table).values(new_file).returning(files::id).get_result(other_database_type),
49+
#[cfg(feature = "postgres")]
50+
// 对于 PostgreSQL 连接,使用 returning 子句
51+
AnyConnection::Postgresql(postgres) => {
52+
diesel::insert_into(files::table).values(new_file).returning(files::id).get_result(postgres)
53+
}
5154
}
5255
}
5356

file_classification_core/src/internal/groups.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,23 @@ pub fn insert_group(
3434
#[cfg(feature = "mysql")]
3535
// 对于 MySQL 连接,使用事务方式
3636
AnyConnection::Mysql(mysql) => {
37-
mysql.transaction(|mysql| {
38-
// 执行插入操作
39-
diesel::insert_into(groups::table)
40-
.values(new_group)
41-
.execute(mysql)?;
42-
43-
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
44-
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
45-
.get_result(mysql)?;
46-
47-
Ok(last_id)
48-
})
37+
mysql.transaction(|mysql| {
38+
// 执行插入操作
39+
diesel::insert_into(groups::table)
40+
.values(new_group)
41+
.execute(mysql)?;
42+
43+
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
44+
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
45+
.get_result(mysql)?;
46+
47+
Ok(last_id)
48+
})
4949
},
50-
// 默认情况(如其他数据库类型)使用 returning 子句
51-
other_database_type => {
52-
diesel::insert_into(groups::table).values(new_group).returning(groups::id).get_result(other_database_type)
50+
#[cfg(feature = "postgres")]
51+
// 对于 PostgreSQL 连接,使用 returning 子句
52+
AnyConnection::Postgresql(postgres) => {
53+
diesel::insert_into(groups::table).values(new_group).returning(groups::id).get_result(postgres)
5354
}
5455
}
5556
}

file_classification_core/src/internal/tags.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,26 @@ pub fn insert_tag(
3131
diesel::insert_into(tags::table).values(new_tag).returning(tags::id).get_result(sqlite)
3232
}
3333
#[cfg(feature = "mysql")]
34-
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql,注意不要删除以下注释内容,将来会用到)
34+
// 对于 MySQL 连接,使用事务方式
3535
AnyConnection::Mysql(mysql) => {
36-
mysql.transaction(|mysql| {
37-
// 执行插入操作
38-
diesel::insert_into(tags::table)
39-
.values(new_tag)
40-
.execute(mysql)?;
41-
42-
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
43-
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
44-
.get_result(mysql)?;
45-
46-
Ok(last_id)
47-
})
36+
mysql.transaction(|mysql| {
37+
// 执行插入操作
38+
diesel::insert_into(tags::table)
39+
.values(new_tag)
40+
.execute(mysql)?;
41+
42+
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
43+
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
44+
.get_result(mysql)?;
45+
46+
Ok(last_id)
47+
})
4848
},
49-
// 默认情况(如其他数据库类型)使用 returning 子句
50-
other_database_type => diesel::insert_into(tags::table).values(new_tag).returning(tags::id).get_result(other_database_type),
49+
#[cfg(feature = "postgres")]
50+
// 对于 PostgreSQL 连接,使用 returning 子句
51+
AnyConnection::Postgresql(postgres) => {
52+
diesel::insert_into(tags::table).values(new_tag).returning(tags::id).get_result(postgres)
53+
}
5154
}
5255
}
5356

0 commit comments

Comments
 (0)