Skip to content

Commit 04de1cc

Browse files
committed
snapshot
1 parent c258185 commit 04de1cc

17 files changed

Lines changed: 471 additions & 25 deletions

Cargo.lock

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

file_classification_cli/src/bin/list_file_groups_by_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
// break;
1818
}
1919

20-
match select_file_groups_by_conditions(connection, conditions, 20) {
20+
match select_file_groups_by_conditions(connection, conditions, Some(20)) {
2121
Ok(file_groups) => {
2222
println!("\n查询结果 (共 {} 条记录):", file_groups.len());
2323
println!("-------------------------");

file_classification_cli/src/bin/list_files_by_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
// break;
2020
}
2121

22-
match select_files_by_conditions(connection, conditions, 20) {
22+
match select_files_by_conditions(connection, conditions, Some(20)) {
2323
Ok(files) => {
2424
println!("\n查询结果 (共 {} 条记录):", files.len());
2525
println!("-------------------------");

file_classification_cli/src/bin/list_group_tags_by_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
// break;
1818
}
1919

20-
match select_group_tags_by_conditions(connection, conditions, 20) {
20+
match select_group_tags_by_conditions(connection, conditions, Some(20)) {
2121
Ok(group_tags) => {
2222
println!("\n查询结果 (共 {} 条记录):", group_tags.len());
2323
println!("-------------------------");

file_classification_cli/src/bin/list_groups_by_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
// break;
2020
}
2121

22-
match select_groups_by_conditions(connection, conditions, 20) {
22+
match select_groups_by_conditions(connection, conditions, Some(20)) {
2323
Ok(groups) => {
2424
println!("\n查询结果 (共 {} 条记录):", groups.len());
2525
println!("-------------------------");

file_classification_cli/src/bin/list_tags_by_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
// break;
2020
}
2121

22-
match select_tags_by_conditions(connection, conditions, 20) {
22+
match select_tags_by_conditions(connection, conditions, Some(20)) {
2323
Ok(tags) => {
2424
println!("\n查询结果 (共 {} 条记录):", tags.len());
2525
println!("-------------------------");

file_classification_core/src/internal/file_group.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl Debug for FileGroupDTO {
3131
}
3232

3333
use diesel::sqlite::Sqlite;
34+
use crate::model::models::{FileGroupOrderBy, FileGroupQueryOptions, OrderDirection};
3435

3536
// 将 FileGroupCondition 转换为 diesel 查询条件的辅助函数
3637
fn build_file_group_condition(condition: FileGroupCondition) -> Box<dyn BoxableExpression<file_groups::table, Sqlite, SqlType = diesel::sql_types::Bool>> {
@@ -76,7 +77,7 @@ fn build_file_group_condition(condition: FileGroupCondition) -> Box<dyn BoxableE
7677
pub fn select_file_groups_by_conditions(
7778
conn: &mut SqliteConnection,
7879
conditions: Vec<FileGroupCondition>,
79-
limit: i64,
80+
limit: Option<i64>,
8081
) -> Result<Vec<FileGroupDTO>, diesel::result::Error> {
8182
let mut query = file_groups::table.into_boxed::<Sqlite>();
8283

@@ -86,8 +87,56 @@ pub fn select_file_groups_by_conditions(
8687
query = query.filter(boxed_condition);
8788
}
8889

90+
if let Some(limit) = limit{
91+
query = query.limit(limit)
92+
}
93+
94+
query
95+
.select((file_groups::file_id, file_groups::group_id))
96+
.load(conn)
97+
}
98+
99+
pub fn select_file_groups_by_conditions_with_options(
100+
conn: &mut SqliteConnection,
101+
conditions: Vec<FileGroupCondition>,
102+
options: FileGroupQueryOptions,
103+
) -> Result<Vec<FileGroupDTO>, diesel::result::Error> {
104+
let mut query = file_groups::table.into_boxed::<Sqlite>();
105+
106+
// 对每个条件应用 AND 逻辑
107+
for condition in conditions {
108+
let boxed_condition = build_file_group_condition(condition);
109+
query = query.filter(boxed_condition);
110+
}
111+
112+
// 应用查询选项(排序、限制等)
113+
if let Some(limit) = options.limit {
114+
query = query.limit(limit);
115+
}
116+
117+
if let Some(offset) = options.offset {
118+
query = query.offset(offset);
119+
}
120+
121+
// 应用排序
122+
for order_by in options.order_by {
123+
query = match order_by {
124+
FileGroupOrderBy::FileId(direction) => {
125+
match direction {
126+
OrderDirection::Asc => query.order(file_groups::file_id.asc()),
127+
OrderDirection::Desc => query.order(file_groups::file_id.desc()),
128+
}
129+
},
130+
FileGroupOrderBy::GroupId(direction) => {
131+
match direction {
132+
OrderDirection::Asc => query.order(file_groups::group_id.asc()),
133+
OrderDirection::Desc => query.order(file_groups::group_id.desc()),
134+
}
135+
},
136+
};
137+
}
138+
89139
query
90-
.limit(limit)
91140
.select((file_groups::file_id, file_groups::group_id))
92141
.load(conn)
93142
}

file_classification_core/src/internal/files.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use crate::model::schema::files::dsl::*;
7070
use std::fmt::{Debug, Formatter, Result as fmtResult};
7171
use diesel::sql_types::Bool;
7272
use diesel::sqlite::Sqlite;
73+
use crate::model::models::{FileOrderBy, FileQueryOptions, OrderDirection};
7374

7475
impl Debug for File {
7576
fn fmt(&self, f: &mut Formatter<'_>) -> fmtResult {
@@ -136,7 +137,29 @@ fn build_file_condition(condition: FileCondition) -> Box<dyn BoxableExpression<f
136137
pub fn select_files_by_conditions(
137138
conn: &mut SqliteConnection,
138139
conditions: Vec<FileCondition>,
139-
limit: i64,
140+
limit: Option<i64>,
141+
) -> Result<Vec<File>, diesel::result::Error> {
142+
let mut query = files::table.into_boxed::<Sqlite>();
143+
144+
// 对每个条件应用 AND 逻辑
145+
for condition in conditions {
146+
let boxed_condition = build_file_condition(condition);
147+
query = query.filter(boxed_condition);
148+
}
149+
150+
if let Some(limit) = limit {
151+
query = query.limit(limit)
152+
}
153+
154+
query
155+
.select(File::as_select())
156+
.load(conn)
157+
}
158+
159+
pub fn select_files_by_conditions_with_options(
160+
conn: &mut SqliteConnection,
161+
conditions: Vec<FileCondition>,
162+
options: FileQueryOptions,
140163
) -> Result<Vec<File>, diesel::result::Error> {
141164
let mut query = files::table.into_boxed::<Sqlite>();
142165

@@ -146,8 +169,52 @@ pub fn select_files_by_conditions(
146169
query = query.filter(boxed_condition);
147170
}
148171

172+
// 应用查询选项(排序、限制等)
173+
if let Some(limit) = options.limit {
174+
query = query.limit(limit);
175+
}
176+
177+
if let Some(offset) = options.offset {
178+
query = query.offset(offset);
179+
}
180+
181+
// 应用排序
182+
for order_by in options.order_by {
183+
query = match order_by {
184+
FileOrderBy::Id(direction) => {
185+
match direction {
186+
OrderDirection::Asc => query.order(files::id.asc()),
187+
OrderDirection::Desc => query.order(files::id.desc()),
188+
}
189+
},
190+
FileOrderBy::Type(direction) => {
191+
match direction {
192+
OrderDirection::Asc => query.order(files::type_.asc()),
193+
OrderDirection::Desc => query.order(files::type_.desc()),
194+
}
195+
},
196+
FileOrderBy::Path(direction) => {
197+
match direction {
198+
OrderDirection::Asc => query.order(files::path.asc()),
199+
OrderDirection::Desc => query.order(files::path.desc()),
200+
}
201+
},
202+
FileOrderBy::ReferenceCount(direction) => {
203+
match direction {
204+
OrderDirection::Asc => query.order(files::reference_count.asc()),
205+
OrderDirection::Desc => query.order(files::reference_count.desc()),
206+
}
207+
},
208+
FileOrderBy::GroupId(direction) => {
209+
match direction {
210+
OrderDirection::Asc => query.order(files::group_id.asc()),
211+
OrderDirection::Desc => query.order(files::group_id.desc()),
212+
}
213+
},
214+
};
215+
}
216+
149217
query
150-
.limit(limit)
151218
.select(File::as_select())
152219
.load(conn)
153220
}

file_classification_core/src/internal/group_tag.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use super::models::GroupTagCondition;
3737
use diesel::dsl::not;
3838
use diesel::sql_types::Bool;
3939
use diesel::sqlite::Sqlite;
40+
use crate::model::models::{GroupTagOrderBy, GroupTagQueryOptions, OrderDirection};
4041

4142
// 将 GroupTagCondition 转换为 diesel 查询条件的辅助函数
4243
fn build_group_tag_condition(condition: GroupTagCondition) -> Box<dyn BoxableExpression<group_tags::table, Sqlite, SqlType = diesel::sql_types::Bool>> {
@@ -82,7 +83,7 @@ fn build_group_tag_condition(condition: GroupTagCondition) -> Box<dyn BoxableExp
8283
pub fn select_group_tags_by_conditions(
8384
conn: &mut SqliteConnection,
8485
conditions: Vec<GroupTagCondition>,
85-
limit: i64,
86+
limit: Option<i64>,
8687
) -> Result<Vec<GroupTagDTO>, diesel::result::Error> {
8788
let mut query = group_tags::table.into_boxed::<Sqlite>();
8889

@@ -92,8 +93,56 @@ pub fn select_group_tags_by_conditions(
9293
query = query.filter(boxed_condition);
9394
}
9495

96+
if let Some(limit) = limit {
97+
query = query.limit(limit);
98+
}
99+
100+
query
101+
.select((group_tags::group_id, group_tags::tag_id))
102+
.load(conn)
103+
}
104+
105+
pub fn select_group_tags_by_conditions_with_options(
106+
conn: &mut SqliteConnection,
107+
conditions: Vec<GroupTagCondition>,
108+
options: GroupTagQueryOptions,
109+
) -> Result<Vec<GroupTagDTO>, diesel::result::Error> {
110+
let mut query = group_tags::table.into_boxed::<Sqlite>();
111+
112+
// 对每个条件应用 AND 逻辑
113+
for condition in conditions {
114+
let boxed_condition = build_group_tag_condition(condition);
115+
query = query.filter(boxed_condition);
116+
}
117+
118+
// 应用查询选项(排序、限制等)
119+
if let Some(limit) = options.limit {
120+
query = query.limit(limit);
121+
}
122+
123+
if let Some(offset) = options.offset {
124+
query = query.offset(offset);
125+
}
126+
127+
// 应用排序
128+
for order_by in options.order_by {
129+
query = match order_by {
130+
GroupTagOrderBy::GroupId(direction) => {
131+
match direction {
132+
OrderDirection::Asc => query.order(group_tags::group_id.asc()),
133+
OrderDirection::Desc => query.order(group_tags::group_id.desc()),
134+
}
135+
},
136+
GroupTagOrderBy::TagId(direction) => {
137+
match direction {
138+
OrderDirection::Asc => query.order(group_tags::tag_id.asc()),
139+
OrderDirection::Desc => query.order(group_tags::tag_id.desc()),
140+
}
141+
},
142+
};
143+
}
144+
95145
query
96-
.limit(limit)
97146
.select((group_tags::group_id, group_tags::tag_id))
98147
.load(conn)
99148
}

0 commit comments

Comments
 (0)