diff --git a/src/index.rs b/src/index.rs index 8ea7e15..9bc81fa 100644 --- a/src/index.rs +++ b/src/index.rs @@ -20,7 +20,7 @@ use db; use db::ConnectionSource; use db::{directories, misc_settings, songs}; use vfs::{VFS, VFSSource}; -use errors::*; +use errors; use metadata; const INDEX_BUILDING_INSERT_BUFFER_SIZE: usize = 1000; // Insertions in each transaction @@ -34,7 +34,8 @@ pub enum Command { REINDEX, } -#[derive(Debug, Queryable, Serialize)] +#[derive(Debug, Queryable, QueryableByName, Serialize)] +#[table_name = "songs"] pub struct Song { #[serde(skip_serializing)] id: i32, @@ -85,7 +86,7 @@ struct NewSong { year: Option, album: Option, artwork: Option, - duration: Option + duration: Option, } #[derive(Debug, Insertable)] @@ -108,7 +109,9 @@ struct IndexBuilder<'conn> { } impl<'conn> IndexBuilder<'conn> { - fn new(connection: &Mutex, album_art_pattern: Regex) -> Result { + fn new(connection: &Mutex, + album_art_pattern: Regex) + -> Result { let mut new_songs = Vec::new(); let mut new_directories = Vec::new(); new_songs.reserve_exact(INDEX_BUILDING_INSERT_BUFFER_SIZE); @@ -121,35 +124,35 @@ impl<'conn> IndexBuilder<'conn> { }) } - fn flush_songs(&mut self) -> Result<()> { + fn flush_songs(&mut self) -> Result<(), errors::Error> { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); connection - .transaction::<_, Error, _>(|| { - diesel::insert_into(songs::table) - .values(&self.new_songs) - .execute(connection)?; - Ok(()) - })?; + .transaction::<_, errors::Error, _>(|| { + diesel::insert_into(songs::table) + .values(&self.new_songs) + .execute(connection)?; + Ok(()) + })?; self.new_songs.clear(); Ok(()) } - fn flush_directories(&mut self) -> Result<()> { + fn flush_directories(&mut self) -> Result<(), errors::Error> { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); connection - .transaction::<_, Error, _>(|| { - diesel::insert_into(directories::table) - .values(&self.new_directories) - .execute(connection)?; - Ok(()) - })?; + .transaction::<_, errors::Error, _>(|| { + diesel::insert_into(directories::table) + .values(&self.new_directories) + .execute(connection)?; + Ok(()) + })?; self.new_directories.clear(); Ok(()) } - fn push_song(&mut self, song: NewSong) -> Result<()> { + fn push_song(&mut self, song: NewSong) -> Result<(), errors::Error> { if self.new_songs.len() >= self.new_songs.capacity() { self.flush_songs()?; } @@ -157,7 +160,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(()) } - fn push_directory(&mut self, directory: NewDirectory) -> Result<()> { + fn push_directory(&mut self, directory: NewDirectory) -> Result<(), errors::Error> { if self.new_directories.len() >= self.new_directories.capacity() { self.flush_directories()?; } @@ -165,7 +168,7 @@ impl<'conn> IndexBuilder<'conn> { Ok(()) } - fn get_artwork(&self, dir: &Path) -> Result> { + fn get_artwork(&self, dir: &Path) -> Result, errors::Error> { for file in fs::read_dir(dir)? { let file = file?; if let Some(name_string) = file.file_name().to_str() { @@ -177,7 +180,10 @@ impl<'conn> IndexBuilder<'conn> { Ok(None) } - fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> { + fn populate_directory(&mut self, + parent: Option<&Path>, + path: &Path) + -> Result<(), errors::Error> { // Find artwork let artwork = self.get_artwork(path).unwrap_or(None); @@ -293,7 +299,7 @@ impl<'conn> IndexBuilder<'conn> { } } -fn clean(db: &T) -> Result<()> +fn clean(db: &T) -> Result<(), errors::Error> where T: ConnectionSource + VFSSource { let vfs = db.get_vfs()?; @@ -354,7 +360,7 @@ fn clean(db: &T) -> Result<()> Ok(()) } -fn populate(db: &T) -> Result<()> +fn populate(db: &T) -> Result<(), errors::Error> where T: ConnectionSource + VFSSource { let vfs = db.get_vfs()?; @@ -377,7 +383,7 @@ fn populate(db: &T) -> Result<()> Ok(()) } -pub fn update(db: &T) -> Result<()> +pub fn update(db: &T) -> Result<(), errors::Error> where T: ConnectionSource + VFSSource { let start = time::Instant::now(); @@ -433,7 +439,7 @@ pub fn self_trigger(db: &T, command_buffer: Arc>>) let sleep_duration; { let connection = db.get_connection(); - let settings: Result = misc_settings::table + let settings: Result = misc_settings::table .get_result(connection.deref()) .map_err(|e| e.into()); if let Err(ref e) = settings { @@ -475,7 +481,7 @@ fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option(db: &T, virtual_path: &Path) -> Result> +pub fn browse(db: &T, virtual_path: &Path) -> Result, errors::Error> where T: ConnectionSource + VFSSource { let mut output = Vec::new(); @@ -521,7 +527,7 @@ pub fn browse(db: &T, virtual_path: &Path) -> Result> Ok(output) } -pub fn flatten(db: &T, virtual_path: &Path) -> Result> +pub fn flatten(db: &T, virtual_path: &Path) -> Result, errors::Error> where T: ConnectionSource + VFSSource { use self::songs::dsl::*; @@ -545,7 +551,7 @@ pub fn flatten(db: &T, virtual_path: &Path) -> Result> Ok(virtual_songs.collect::>()) } -pub fn get_random_albums(db: &T, count: i64) -> Result> +pub fn get_random_albums(db: &T, count: i64) -> Result, errors::Error> where T: ConnectionSource + VFSSource { use self::directories::dsl::*; @@ -562,7 +568,7 @@ pub fn get_random_albums(db: &T, count: i64) -> Result> Ok(virtual_directories.collect::>()) } -pub fn get_recent_albums(db: &T, count: i64) -> Result> +pub fn get_recent_albums(db: &T, count: i64) -> Result, errors::Error> where T: ConnectionSource + VFSSource { use self::directories::dsl::*; @@ -579,7 +585,7 @@ pub fn get_recent_albums(db: &T, count: i64) -> Result> Ok(virtual_directories.collect::>()) } -pub fn search(db: &T, query: &str) -> Result> +pub fn search(db: &T, query: &str) -> Result, errors::Error> where T: ConnectionSource + VFSSource { let vfs = db.get_vfs()?; diff --git a/src/playlist.rs b/src/playlist.rs index 39fd8ff..162d8b2 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,7 +1,6 @@ use core::clone::Clone; use core::ops::Deref; use diesel; -use diesel::dsl::sql; use diesel::prelude::*; use diesel::BelongingToDsl; use diesel::types::*; @@ -9,7 +8,7 @@ use std::path::Path; #[cfg(test)] use db; use db::ConnectionSource; -use db::{playlists, playlist_songs, songs, users}; +use db::{playlists, playlist_songs, users}; use index::{self, Song}; use vfs::VFSSource; use errors::*; @@ -180,8 +179,8 @@ pub fn read_playlist(playlist_name: &str, owner: &str, db: &T) -> Result(r#" - SELECT s.id, s.path, s.parent, s.track_number, s.disc_number, s.title, s.artist, s.album_artist, s.year, s.album, s.artwork + let query = diesel::sql_query(r#" + SELECT s.id, s.path, s.parent, s.track_number, s.disc_number, s.title, s.artist, s.album_artist, s.year, s.album, s.artwork, s.duration FROM playlist_songs ps LEFT JOIN songs s ON ps.path = s.path WHERE ps.playlist = ?