Skip to content

Commit 567caeb

Browse files
committed
chore(core): insert of Business Table will return Business Object ID
1 parent f161522 commit 567caeb

8 files changed

Lines changed: 179 additions & 70 deletions

File tree

Cargo.lock

Lines changed: 34 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

file_classification_cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ edition = "2024"
66
[dependencies]
77
file_classification_core = { path = "../file_classification_core" }
88
clap = { version = "4.5", features = ["derive"] }
9-
dialoguer = "0.10.4"
10-
rustyline = "14.0.0"
9+
dialoguer = "0.12.0"
10+
rustyline = "17.0.2"
1111
shlex = "1.3.0"
1212

1313
[[bin]]

file_classification_core/src/internal/files.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,42 @@ use diesel::sql_types::Bool;
1818
/// - `new_file`: 包含待插入文件数据的 DTO 对象
1919
///
2020
/// 返回值:
21-
/// 成功时返回影响的行数(通常应为1),失败则返回数据库错误
22-
pub fn insert_file(conn: &mut AnyConnection, new_file: &CreateFileDTO) -> Result<usize, diesel::result::Error> {
23-
diesel::insert_into(files::table)
24-
.values(new_file).execute(conn)
21+
/// 成功时返回插入记录的ID,失败则返回数据库错误
22+
pub fn insert_file(conn: &mut AnyConnection, new_file: &CreateFileDTO) -> Result<i32, diesel::result::Error> {
23+
// 使用 match 表达式根据连接类型选择实现方式
24+
match conn {
25+
// 对于 SQLite 连接,使用 returning 子句
26+
AnyConnection::Sqlite(_) => {
27+
diesel::insert_into(files::table)
28+
.values(new_file)
29+
.returning(files::id)
30+
.get_result(conn)
31+
},
32+
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql)
33+
/*
34+
AnyConnection::Mysql(_) => {
35+
conn.transaction(|conn| {
36+
// 执行插入操作
37+
diesel::insert_into(files::table)
38+
.values(new_file)
39+
.execute(conn)?;
40+
41+
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
42+
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
43+
.get_result(conn)?;
44+
45+
Ok(last_id)
46+
})
47+
},
48+
*/
49+
// 默认情况(如其他数据库类型)使用 returning 子句
50+
_ => {
51+
diesel::insert_into(files::table)
52+
.values(new_file)
53+
.returning(files::id)
54+
.get_result(conn)
55+
}
56+
}
2557
}
2658

2759
/// 根据ID查找文件记录

file_classification_core/src/internal/groups.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,42 @@ use crate::utils::database::AnyConnection;
2020
/// - `new_group`: 包含待插入分组数据的 DTO 对象
2121
///
2222
/// 返回值:
23-
/// 成功时返回影响的行数(通常应为1),失败则返回数据库错误
24-
pub fn insert_group(conn: &mut AnyConnection, new_group: &CreateGroupDTO) -> Result<usize, diesel::result::Error> {
25-
diesel::insert_into(groups::table)
26-
.values(new_group).execute(conn)
23+
/// 成功时返回插入记录的ID,失败则返回数据库错误
24+
pub fn insert_group(conn: &mut AnyConnection, new_group: &CreateGroupDTO) -> Result<i32, diesel::result::Error> {
25+
// 使用 match 表达式根据连接类型选择实现方式
26+
match conn {
27+
// 对于 SQLite 连接,使用 returning 子句
28+
AnyConnection::Sqlite(_) => {
29+
diesel::insert_into(groups::table)
30+
.values(new_group)
31+
.returning(groups::id)
32+
.get_result(conn)
33+
},
34+
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql)
35+
/*
36+
AnyConnection::Mysql(_) => {
37+
conn.transaction(|conn| {
38+
// 执行插入操作
39+
diesel::insert_into(groups::table)
40+
.values(new_group)
41+
.execute(conn)?;
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(conn)?;
46+
47+
Ok(last_id)
48+
})
49+
},
50+
*/
51+
// 默认情况(如其他数据库类型)使用 returning 子句
52+
_ => {
53+
diesel::insert_into(groups::table)
54+
.values(new_group)
55+
.returning(groups::id)
56+
.get_result(conn)
57+
}
58+
}
2759
}
2860

2961
/// 根据名称查找分组记录

file_classification_core/src/internal/tags.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,45 @@ use diesel::sql_types::Bool;
2020
/// - `new_tag`: 包含待插入标签数据的 DTO 对象
2121
///
2222
/// 返回值:
23-
/// 成功时返回影响的行数(通常应为1),失败则返回数据库错误
23+
/// 成功时返回插入记录的ID,失败则返回数据库错误
2424
pub fn insert_tag(
2525
conn: &mut AnyConnection,
2626
new_tag: &CreateTagDTO,
27-
) -> Result<usize, diesel::result::Error> {
28-
diesel::insert_into(tags::table)
29-
.values(new_tag)
30-
.execute(conn)
27+
) -> Result<i32, diesel::result::Error> {
28+
// 使用 match 表达式根据连接类型选择实现方式
29+
match conn {
30+
// 对于 SQLite 连接,使用 returning 子句
31+
AnyConnection::Sqlite(_) => {
32+
diesel::insert_into(tags::table)
33+
.values(new_tag)
34+
.returning(tags::id)
35+
.get_result(conn)
36+
},
37+
// 对于 MySQL 连接,使用事务方式(暂时注释掉,因为目前没有启用mysql)
38+
/*
39+
AnyConnection::Mysql(_) => {
40+
conn.transaction(|conn| {
41+
// 执行插入操作
42+
diesel::insert_into(tags::table)
43+
.values(new_tag)
44+
.execute(conn)?;
45+
46+
// MySQL使用LAST_INSERT_ID()获取最后插入的ID
47+
let last_id: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("LAST_INSERT_ID()"))
48+
.get_result(conn)?;
49+
50+
Ok(last_id)
51+
})
52+
},
53+
*/
54+
// 默认情况(如其他数据库类型)使用 returning 子句
55+
_ => {
56+
diesel::insert_into(tags::table)
57+
.values(new_tag)
58+
.returning(tags::id)
59+
.get_result(conn)
60+
}
61+
}
3162
}
3263

3364
/// 根据名称查找标签记录

file_classification_core/src/service/files.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
//! 并处理文件与其关联分组、标签等资源的引用计数和级联删除。
66
77
use crate::internal::file_group::select_file_groups_by_conditions;
8-
use crate::internal::groups::{mark_group_as_primary, select_groups_by_conditions};
9-
use crate::model::models::{CreateFileDTO, File, FileCondition, FileFilter, FileGroupCondition, FileGroupDTO, FileQueryOptions, GroupCondition, GroupTagCondition, TagCondition, UpdateFileDTO, UpdateGroupDTO};
10-
use crate::service::groups::update_groups_by_conditions;
8+
use crate::internal::groups::{mark_group_as_primary};
9+
use crate::model::models::{CreateFileDTO, File, FileCondition, FileFilter, FileGroupCondition, FileGroupDTO, FileQueryOptions, GroupTagCondition, UpdateFileDTO};
1110
use crate::service::AppError;
1211
use crate::utils::errors::AppError::{FuturePrimaryGroupShouldBeEmpty};
1312
use crate::{internal, service};
@@ -28,9 +27,9 @@ use crate::utils::database::AnyConnection;
2827
/// - `create_file_dto`: 包含文件信息的DTO对象
2928
///
3029
/// 返回值:
31-
/// 成功时返回创建的记录数,失败时返回相应的错误
32-
pub fn create_file(conn: &mut AnyConnection, create_file_dto: CreateFileDTO) -> Result<usize, AppError> {
33-
conn.transaction::<usize, AppError, _>(|conn| {
30+
/// 成功时返回插入记录的ID,失败则返回相应的错误
31+
pub fn create_file(conn: &mut AnyConnection, create_file_dto: CreateFileDTO) -> Result<i32, AppError> {
32+
conn.transaction::<i32, AppError, _>(|conn| {
3433
// 验证目标分组是否存在
3534
let target_group = internal::groups::get_group_by_id(conn, create_file_dto.group_id)?;
3635

@@ -43,32 +42,38 @@ pub fn create_file(conn: &mut AnyConnection, create_file_dto: CreateFileDTO) ->
4342
if internal::file_group::check_group_empty(conn, target_group.id)? == false {
4443
return Err(FuturePrimaryGroupShouldBeEmpty);
4544
}
46-
47-
let mut count = 0;
45+
46+
// 记录影响条数
47+
// let mut count = 0;
48+
4849
// 创建文件记录
49-
count += internal::files::insert_file(conn, &create_file_dto)?;
50-
51-
// 获取刚创建的文件 主分组id可以区别文件
52-
let file_list = internal::files::select_files_by_conditions(conn, vec![
53-
FileCondition::GroupId(create_file_dto.group_id)
54-
], None)?;
55-
let file = file_list.get(0).ok_or(AppError::FileNotFound)?;
50+
let mut file_id = internal::files::insert_file(conn, &create_file_dto)?;
51+
52+
// count += 1;
53+
54+
// // 获取刚创建的文件 主分组id可以区别文件
55+
// let file_list = internal::files::select_files_by_conditions(conn, vec![
56+
// FileCondition::GroupId(create_file_dto.group_id)
57+
// ], None)?;
58+
// let file = file_list.get(0).ok_or(AppError::FileNotFound)?;
59+
// file_id = file.id;
5660

5761
// 建立文件与主分组的关联关系
58-
match service::file_group::create_file_group(conn, FileGroupDTO { file_id: file.id, group_id: target_group.id }) {
62+
match service::file_group::create_file_group(conn, FileGroupDTO { file_id, group_id: target_group.id }) {
5963
Ok(_) => {
60-
count += 1;
64+
// count += 1;
6165
}
6266
Err(e) => {
6367
return Err(e)
6468
}
6569
}
6670

6771
// 将目标分组标记为主分组
68-
count += mark_group_as_primary(conn, target_group.id)?;
72+
mark_group_as_primary(conn, target_group.id)?;
73+
// count += mark_group_as_primary(conn, target_group.id)?;
6974

70-
// 返回修改条数
71-
Ok(count)
75+
// 返回文件id
76+
Ok(file_id)
7277
})
7378
}
7479

0 commit comments

Comments
 (0)