Cleanup db module

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2022-03-22 17:41:02 -04:00
parent b61aee0a6a
commit 37d9f627ad
2 changed files with 60 additions and 78 deletions

View File

@ -275,9 +275,11 @@ pub fn analyse_new_cue_tracks(
cue_tracks: Vec<cue::CueTrack>,
) -> Result<()> {
let total = cue_tracks.len();
let progress = ProgressBar::new(total.try_into().unwrap()).with_style(ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}")
.progress_chars("=> "));
let progress = ProgressBar::new(total.try_into().unwrap()).with_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}")
.progress_chars("=> "),
);
let results = analyze_cue_streaming(cue_tracks)?;
let mut analysed = 0;

130
src/db.rs
View File

@ -1,5 +1,3 @@
use crate::cue;
use crate::tags;
/**
* Analyse music with Bliss
*
@ -7,6 +5,8 @@ use crate::tags;
* GPLv3 license.
*
**/
use crate::cue;
use crate::tags;
use bliss_audio::{Analysis, AnalysisIndex};
use indicatif::{ProgressBar, ProgressStyle};
use rusqlite::{params, Connection};
@ -56,7 +56,7 @@ impl Db {
}
pub fn init(&self) {
match self.conn.execute(
let cmd = self.conn.execute(
"CREATE TABLE IF NOT EXISTS Tracks (
File text primary key,
Title text,
@ -88,34 +88,30 @@ impl Db {
Chroma10 real
);",
[],
) {
Ok(_) => {}
Err(_) => {
log::error!("Failed to create DB table");
process::exit(-1);
}
);
if let Err(e) = cmd {
log::error!("Failed to create DB table");
process::exit(-1);
}
match self.conn.execute(
let cmd = self.conn.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS Tracks_idx ON Tracks(File)",
[],
) {
Ok(_) => {}
Err(_) => {
log::error!("Failed to create DB index");
process::exit(-1);
}
);
if let Err(e) = cmd {
log::error!("Failed to create DB index");
process::exit(-1);
}
}
pub fn close(self) {
match self.conn.close() {
Ok(_) => {}
Err(_) => {}
}
let _ = self.conn.close();
}
pub fn get_rowid(&self, path: &String) -> Result<usize, rusqlite::Error> {
let mut db_path = path.clone();
pub fn get_rowid(&self, path: &str) -> Result<usize, rusqlite::Error> {
let mut db_path = path.to_string();
if cfg!(windows) {
db_path = db_path.replace("\\", "/");
}
@ -212,14 +208,12 @@ impl Db {
let count_before = self.get_track_count();
for t in to_remove {
//log::debug!("Remove '{}'", t);
match self
let cmd = self
.conn
.execute("DELETE FROM Tracks WHERE File = ?;", params![t])
{
Ok(_) => {}
Err(e) => {
log::error!("Failed to remove '{}' - {}", t, e)
}
.execute("DELETE FROM Tracks WHERE File = ?;", params![t]);
if let Err(e) = cmd {
log::error!("Failed to remove '{}' - {}", t, e)
}
}
let count_now = self.get_track_count();
@ -244,13 +238,14 @@ impl Db {
pub fn update_tags(&self, mpaths: &Vec<PathBuf>) {
let total = self.get_track_count();
if total > 0 {
let pb = ProgressBar::new(total.try_into().unwrap());
let style = ProgressStyle::default_bar()
.template(
"[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}",
)
.progress_chars("=> ");
pb.set_style(style);
let progress = ProgressBar::new(total.try_into().unwrap()).with_style(
ProgressStyle::default_bar()
.template(
"[{elapsed_precise}] [{bar:25}] {percent:>3}% {pos:>6}/{len:6} {wide_msg}",
)
.progress_chars("=> "),
);
let mut stmt = self.conn.prepare("SELECT rowid, File, Title, Artist, AlbumArtist, Album, Genre, Duration FROM Tracks ORDER BY File ASC;").unwrap();
let track_iter = stmt
.query_map([], |row| {
@ -272,34 +267,23 @@ impl Db {
let dbtags = tr.unwrap();
if !dbtags.file.contains(cue::MARKER) {
let dtags = Metadata {
title: dbtags.title.unwrap_or(String::new()),
artist: dbtags.artist.unwrap_or(String::new()),
album_artist: dbtags.album_artist.unwrap_or(String::new()),
album: dbtags.album.unwrap_or(String::new()),
genre: dbtags.genre.unwrap_or(String::new()),
title: dbtags.title.unwrap_or_default(),
artist: dbtags.artist.unwrap_or_default(),
album_artist: dbtags.album_artist.unwrap_or_default(),
album: dbtags.album.unwrap_or_default(),
genre: dbtags.genre.unwrap_or_default(),
duration: dbtags.duration,
};
pb.set_message(format!("{}", dbtags.file));
progress.set_message(format!("{}", dbtags.file));
for mpath in mpaths {
let track_path = mpath.join(&dbtags.file);
if track_path.exists() {
let path = String::from(track_path.to_string_lossy());
let ftags = tags::read(&path);
if ftags.title.is_empty()
&& ftags.artist.is_empty()
&& ftags.album_artist.is_empty()
&& ftags.album.is_empty()
&& ftags.genre.is_empty()
{
if ftags.is_empty() {
log::error!("Failed to read tags of '{}'", dbtags.file);
} else if ftags.duration != dtags.duration
|| ftags.title != dtags.title
|| ftags.artist != dtags.artist
|| ftags.album_artist != dtags.album_artist
|| ftags.album != dtags.album
|| ftags.genre != dtags.genre
{
} else if ftags != dtags {
match self.conn.execute("UPDATE Tracks SET Title=?, Artist=?, AlbumArtist=?, Album=?, Genre=?, Duration=? WHERE rowid=?;",
params![ftags.title, ftags.artist, ftags.album_artist, ftags.album, ftags.genre, ftags.duration, dbtags.rowid]) {
Ok(_) => { updated += 1; },
@ -310,18 +294,17 @@ impl Db {
}
}
}
pb.inc(1);
progress.inc(1);
}
pb.finish_with_message(format!("{} Updated.", updated))
progress.finish_with_message(format!("{} Updated.", updated))
}
}
pub fn clear_ignore(&self) {
match self.conn.execute("UPDATE Tracks SET Ignore=0;", []) {
Ok(_) => {}
Err(e) => {
log::error!("Failed clear Ignore column. {}", e);
}
let cmd = self.conn.execute("UPDATE Tracks SET Ignore=0;", []);
if let Err(e) = cmd {
log::error!("Failed clear Ignore column. {}", e);
}
}
@ -329,24 +312,21 @@ impl Db {
log::info!("Ignore: {}", line);
if line.starts_with("SQL:") {
let sql = &line[4..];
match self
let cmd = self
.conn
.execute(&format!("UPDATE Tracks Set Ignore=1 WHERE {}", sql), [])
{
Ok(_) => {}
Err(e) => {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
.execute(&format!("UPDATE Tracks Set Ignore=1 WHERE {}", sql), []);
if let Err(e) = cmd {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
} else {
match self.conn.execute(
let cmd = self.conn.execute(
&format!("UPDATE Tracks SET Ignore=1 WHERE File LIKE \"{}%\"", line),
[],
) {
Ok(_) => {}
Err(e) => {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
);
if let Err(e) = cmd {
log::error!("Failed set Ignore column for '{}'. {}", line, e);
}
}
}