Fix tag count sorting, hopefully
This commit is contained in:
68
src/tags.rs
68
src/tags.rs
@@ -507,31 +507,51 @@ impl TagDao for SqliteTagDao {
|
|||||||
trace_db_call(context, "query", "get_files_with_all_tags", |_| {
|
trace_db_call(context, "query", "get_files_with_all_tags", |_| {
|
||||||
use diesel::dsl::*;
|
use diesel::dsl::*;
|
||||||
|
|
||||||
let exclude_subquery = tagged_photo::table
|
// Create the placeholders for the IN clauses
|
||||||
.filter(tagged_photo::tag_id.eq_any(exclude_tag_ids.clone()))
|
let tag_placeholders = std::iter::repeat_n("?", tag_ids.len())
|
||||||
.select(tagged_photo::photo_name)
|
.collect::<Vec<_>>()
|
||||||
.into_boxed();
|
.join(",");
|
||||||
|
let exclude_placeholders = std::iter::repeat_n("?", exclude_tag_ids.len())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(",");
|
||||||
|
|
||||||
tagged_photo::table
|
let query = sql_query(format!(
|
||||||
.filter(tagged_photo::tag_id.eq_any(tag_ids.clone()))
|
r#"
|
||||||
.filter(tagged_photo::photo_name.ne_all(exclude_subquery))
|
WITH filtered_photos AS (
|
||||||
.group_by(tagged_photo::photo_name)
|
SELECT photo_name
|
||||||
.select((
|
FROM tagged_photo tp
|
||||||
tagged_photo::photo_name,
|
WHERE tp.tag_id IN ({})
|
||||||
count_distinct(tagged_photo::tag_id),
|
AND tp.photo_name NOT IN (
|
||||||
))
|
SELECT photo_name
|
||||||
.having(count_distinct(tagged_photo::tag_id).ge(tag_ids.len() as i64))
|
FROM tagged_photo
|
||||||
.get_results::<(String, i64)>(&mut self.connection)
|
WHERE tag_id IN ({})
|
||||||
.map(|results| {
|
)
|
||||||
results
|
GROUP BY photo_name
|
||||||
.into_iter()
|
HAVING COUNT(DISTINCT tag_id) >= {}
|
||||||
.map(|(file_name, tag_count)| FileWithTagCount {
|
)
|
||||||
file_name,
|
SELECT
|
||||||
tag_count,
|
fp.photo_name as file_name,
|
||||||
})
|
COUNT(DISTINCT tp2.tag_id) as tag_count
|
||||||
.collect()
|
FROM filtered_photos fp
|
||||||
})
|
JOIN tagged_photo tp2 ON fp.photo_name = tp2.photo_name
|
||||||
.with_context(|| format!("Unable to get Tagged photos with ids: {:?}", tag_ids))
|
GROUP BY fp.photo_name"#,
|
||||||
|
tag_placeholders,
|
||||||
|
exclude_placeholders,
|
||||||
|
tag_ids.len()
|
||||||
|
))
|
||||||
|
.into_boxed();
|
||||||
|
|
||||||
|
// Bind all parameters
|
||||||
|
let query = tag_ids
|
||||||
|
.into_iter()
|
||||||
|
.fold(query, |q, id| q.bind::<Integer, _>(id));
|
||||||
|
let query = exclude_tag_ids
|
||||||
|
.into_iter()
|
||||||
|
.fold(query, |q, id| q.bind::<Integer, _>(id));
|
||||||
|
|
||||||
|
query
|
||||||
|
.load::<FileWithTagCount>(&mut self.connection)
|
||||||
|
.with_context(|| "Unable to get tagged photos with all specified tags")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user