diff --git a/src/tags.rs b/src/tags.rs index 4c6dac6..9c2ea59 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -503,43 +503,49 @@ impl TagDao for SqliteTagDao { ) -> anyhow::Result> { trace_db_call(&context, "query", "get_files_with_any_tags", |_| { use diesel::dsl::*; - - let tag_ids_str = tag_ids - .iter() - .map(|id| id.to_string()) + // Create the placeholders for the IN clauses + let tag_placeholders = std::iter::repeat("?") + .take(tag_ids.len()) .collect::>() .join(","); - - let exclude_tag_ids_str = exclude_tag_ids - .iter() - .map(|id| id.to_string()) + let exclude_placeholders = std::iter::repeat("?") + .take(exclude_tag_ids.len()) .collect::>() .join(","); let query = sql_query(format!( r#" -WITH filtered_photos AS ( - SELECT DISTINCT photo_name - FROM tagged_photo tp - WHERE tp.tag_id IN ({}) - AND tp.photo_name NOT IN ( - SELECT photo_name - FROM tagged_photo - WHERE tag_id IN ({}) - ) - ) - SELECT - fp.photo_name as file_name, - COUNT(DISTINCT tp2.tag_id) as tag_count - FROM filtered_photos fp - JOIN tagged_photo tp2 ON fp.photo_name = tp2.photo_name - GROUP BY fp.photo_name"#, - tag_ids_str, exclude_tag_ids_str - )); + WITH filtered_photos AS ( + SELECT DISTINCT photo_name + FROM tagged_photo tp + WHERE tp.tag_id IN ({}) + AND tp.photo_name NOT IN ( + SELECT photo_name + FROM tagged_photo + WHERE tag_id IN ({}) + ) + ) + SELECT + fp.photo_name as file_name, + COUNT(DISTINCT tp2.tag_id) as tag_count + FROM filtered_photos fp + JOIN tagged_photo tp2 ON fp.photo_name = tp2.photo_name + GROUP BY fp.photo_name"#, + tag_placeholders, exclude_placeholders + )) + .into_boxed(); - // Execute the query: - let results = query.load::(&mut self.connection)?; - Ok(results) + // Bind all parameters + let query = tag_ids + .into_iter() + .fold(query, |q, id| q.bind::(id)); + let query = exclude_tag_ids + .into_iter() + .fold(query, |q, id| q.bind::(id)); + + query + .load::(&mut self.connection) + .with_context(|| "Unable to get tagged photos") }) } } diff --git a/src/video.rs b/src/video.rs index 79483bc..1f3d230 100644 --- a/src/video.rs +++ b/src/video.rs @@ -234,7 +234,7 @@ impl Handler for PlaylistGenerator { playlist_path, msg.video_path.file_name().unwrap().to_str().unwrap() ); - + let tracer = global_tracer(); let mut span = tracer .span_builder("playlistgenerator.generate_playlist") @@ -243,7 +243,7 @@ impl Handler for PlaylistGenerator { KeyValue::new("playlist_file", playlist_file.clone()), ]) .start(&tracer); - + Box::pin(async move { let wait_start = std::time::Instant::now(); let permit = semaphore @@ -255,9 +255,13 @@ impl Handler for PlaylistGenerator { "Waited for {:?} before starting ffmpeg", wait_start.elapsed() ); - span.add_event("Waited for FFMPEG semaphore", vec![ - KeyValue::new("wait_time", wait_start.elapsed().as_secs_f64()) - ]); + span.add_event( + "Waited for FFMPEG semaphore", + vec![KeyValue::new( + "wait_time", + wait_start.elapsed().as_secs_f64(), + )], + ); if Path::new(&playlist_file).exists() { debug!("Playlist already exists: {}", playlist_file); @@ -298,7 +302,7 @@ impl Handler for PlaylistGenerator { if let Ok(ref res) = ffmpeg_result { debug!("ffmpeg output: {:?}", res); } - + span.set_status(Status::Ok); ffmpeg_result