Less verbose use of diesel

This commit is contained in:
Antoine Gersant 2017-06-29 00:06:43 -07:00
parent e41a5273a8
commit 29bc678c82
2 changed files with 27 additions and 32 deletions

View file

@ -268,9 +268,7 @@ impl Index {
let connection = db.get_connection(); let connection = db.get_connection();
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
all_songs = songs::table all_songs = songs::table.select(songs::path).load(connection)?;
.select(songs::columns::path)
.load(connection)?;
} }
let missing_songs = all_songs let missing_songs = all_songs
@ -286,7 +284,7 @@ impl Index {
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
for chunk in missing_songs[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) { for chunk in missing_songs[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
diesel::delete(songs::table.filter(songs::columns::path.eq_any(chunk))) diesel::delete(songs::table.filter(songs::path.eq_any(chunk)))
.execute(connection)?; .execute(connection)?;
} }
@ -300,7 +298,7 @@ impl Index {
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
all_directories = directories::table all_directories = directories::table
.select(directories::columns::path) .select(directories::path)
.load(connection)?; .load(connection)?;
} }
@ -317,9 +315,8 @@ impl Index {
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
for chunk in missing_directories[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) { for chunk in missing_directories[..].chunks(INDEX_BUILDING_CLEAN_BUFFER_SIZE) {
diesel::delete(directories::table.filter(directories::columns::path diesel::delete(directories::table.filter(directories::path.eq_any(chunk)))
.eq_any(chunk))) .execute(connection)?;
.execute(connection)?;
} }
} }
} }
@ -434,7 +431,7 @@ fn test_metadata() {
let connection = connection.lock().unwrap(); let connection = connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
let songs: Vec<Song> = songs::table let songs: Vec<Song> = songs::table
.filter(songs::columns::title.eq("シャーベット (Sherbet)")) .filter(songs::title.eq("シャーベット (Sherbet)"))
.load(connection) .load(connection)
.unwrap(); .unwrap();

View file

@ -104,14 +104,13 @@ impl DB {
if let Some(sleep_duration) = config.reindex_every_n_seconds { if let Some(sleep_duration) = config.reindex_every_n_seconds {
diesel::update(misc_settings::table) diesel::update(misc_settings::table)
.set(misc_settings::columns::index_sleep_duration_seconds.eq(sleep_duration as .set(misc_settings::index_sleep_duration_seconds.eq(sleep_duration as i32))
i32))
.execute(connection)?; .execute(connection)?;
} }
if let Some(ref album_art_pattern) = config.album_art_pattern { if let Some(ref album_art_pattern) = config.album_art_pattern {
diesel::update(misc_settings::table) diesel::update(misc_settings::table)
.set(misc_settings::columns::index_album_art_pattern.eq(album_art_pattern)) .set(misc_settings::index_album_art_pattern.eq(album_art_pattern))
.execute(connection)?; .execute(connection)?;
} }
@ -178,7 +177,7 @@ impl DB {
if virtual_path.components().count() == 0 { if virtual_path.components().count() == 0 {
// Browse top-level // Browse top-level
let real_directories: Vec<Directory> = directories::table let real_directories: Vec<Directory> = directories::table
.filter(directories::columns::parent.is_null()) .filter(directories::parent.is_null())
.load(connection)?; .load(connection)?;
let virtual_directories = real_directories let virtual_directories = real_directories
.into_iter() .into_iter()
@ -193,8 +192,8 @@ impl DB {
let real_path_string = real_path.as_path().to_string_lossy().into_owned(); let real_path_string = real_path.as_path().to_string_lossy().into_owned();
let real_directories: Vec<Directory> = directories::table let real_directories: Vec<Directory> = directories::table
.filter(directories::columns::parent.eq(&real_path_string)) .filter(directories::parent.eq(&real_path_string))
.order(directories::columns::path) .order(directories::path)
.load(connection)?; .load(connection)?;
let virtual_directories = real_directories let virtual_directories = real_directories
.into_iter() .into_iter()
@ -202,8 +201,8 @@ impl DB {
output.extend(virtual_directories.map(|d| CollectionFile::Directory(d))); output.extend(virtual_directories.map(|d| CollectionFile::Directory(d)));
let real_songs: Vec<Song> = songs::table let real_songs: Vec<Song> = songs::table
.filter(songs::columns::parent.eq(&real_path_string)) .filter(songs::parent.eq(&real_path_string))
.order(songs::columns::path) .order(songs::path)
.load(connection)?; .load(connection)?;
let virtual_songs = real_songs let virtual_songs = real_songs
.into_iter() .into_iter()
@ -215,14 +214,13 @@ impl DB {
} }
pub fn flatten(&self, virtual_path: &Path) -> Result<Vec<Song>> { pub fn flatten(&self, virtual_path: &Path) -> Result<Vec<Song>> {
use self::songs::dsl::*;
let vfs = self.get_vfs()?; let vfs = self.get_vfs()?;
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
let real_path = vfs.virtual_to_real(virtual_path)?; let real_path = vfs.virtual_to_real(virtual_path)?;
let like_path = real_path.as_path().to_string_lossy().into_owned() + "%"; let like_path = real_path.as_path().to_string_lossy().into_owned() + "%";
let real_songs: Vec<Song> = songs::table let real_songs: Vec<Song> = songs.filter(path.like(&like_path)).load(connection)?;
.filter(songs::columns::path.like(&like_path))
.load(connection)?;
let virtual_songs = real_songs let virtual_songs = real_songs
.into_iter() .into_iter()
.filter_map(|s| self.virtualize_song(&vfs, s)); .filter_map(|s| self.virtualize_song(&vfs, s));
@ -230,11 +228,12 @@ impl DB {
} }
pub fn get_random_albums(&self, count: i64) -> Result<Vec<Directory>> { pub fn get_random_albums(&self, count: i64) -> Result<Vec<Directory>> {
use self::directories::dsl::*;
let vfs = self.get_vfs()?; let vfs = self.get_vfs()?;
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
let real_directories = directories::table let real_directories = directories
.filter(directories::columns::album.is_not_null()) .filter(album.is_not_null())
.limit(count) .limit(count)
.order(sql::<types::Bool>("RANDOM()")) .order(sql::<types::Bool>("RANDOM()"))
.load(connection)?; .load(connection)?;
@ -245,12 +244,13 @@ impl DB {
} }
pub fn get_recent_albums(&self, count: i64) -> Result<Vec<Directory>> { pub fn get_recent_albums(&self, count: i64) -> Result<Vec<Directory>> {
use self::directories::dsl::*;
let vfs = self.get_vfs()?; let vfs = self.get_vfs()?;
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
let real_directories: Vec<Directory> = directories::table let real_directories: Vec<Directory> = directories
.filter(directories::columns::album.is_not_null()) .filter(album.is_not_null())
.order(directories::columns::date_added.desc()) .order(date_added.desc())
.limit(count) .limit(count)
.load(connection)?; .load(connection)?;
let virtual_directories = real_directories let virtual_directories = real_directories
@ -260,23 +260,21 @@ impl DB {
} }
pub fn auth(&self, username: &str, password: &str) -> Result<bool> { pub fn auth(&self, username: &str, password: &str) -> Result<bool> {
use self::users::dsl::*;
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
let user: User = users::table let user: User = users.filter(name.eq(username)).get_result(connection)?;
.filter(users::columns::name.eq(username))
.get_result(connection)?;
Ok(user.verify_password(password)) Ok(user.verify_password(password))
} }
} }
impl DDNSConfigSource for DB { impl DDNSConfigSource for DB {
fn get_ddns_config(&self) -> Result<DDNSConfig> { fn get_ddns_config(&self) -> Result<DDNSConfig> {
use self::ddns_config::dsl::*;
let connection = self.connection.lock().unwrap(); let connection = self.connection.lock().unwrap();
let connection = connection.deref(); let connection = connection.deref();
Ok(ddns_config::table Ok(ddns_config
.select((ddns_config::columns::host, .select((host, username, password))
ddns_config::columns::username,
ddns_config::columns::password))
.get_result(connection)?) .get_result(connection)?)
} }
} }