Use database connection directly

This commit is contained in:
Elias Projahn 2023-02-11 13:35:26 +01:00
parent a3f585aadf
commit 75d4e82cf8
28 changed files with 893 additions and 889 deletions

View file

@ -1,11 +1,11 @@
use db::SqliteConnection;
use gio::traits::SettingsExt;
use log::warn;
use musicus_database::Database;
use std::{
cell::{Cell, RefCell},
path::PathBuf,
rc::Rc,
sync::Arc,
sync::{Arc, Mutex},
};
use tokio::sync::{broadcast, broadcast::Sender};
@ -59,7 +59,7 @@ pub struct Backend {
library_updated_sender: Sender<()>,
/// The database. This can be assumed to exist, when the state is set to BackendState::Ready.
database: RefCell<Option<Rc<Database>>>,
database: RefCell<Option<Arc<Mutex<SqliteConnection>>>>,
/// The player handling playlist and playback. This can be assumed to exist, when the state is
/// set to BackendState::Ready.

View file

@ -1,9 +1,10 @@
use crate::{Backend, BackendState, Player, Result};
use gio::prelude::*;
use log::warn;
use musicus_database::Database;
use musicus_database::SqliteConnection;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
impl Backend {
/// Initialize the music library if it is set in the settings.
@ -41,8 +42,11 @@ impl Backend {
let mut db_path = path.clone();
db_path.push("musicus.db");
let database = Rc::new(Database::new(db_path.to_str().unwrap())?);
self.database.replace(Some(Rc::clone(&database)));
let database = Arc::new(Mutex::new(musicus_database::connect(
db_path.to_str().unwrap(),
)?));
self.database.replace(Some(database));
let player = Player::new(path);
self.player.replace(Some(player));
@ -60,7 +64,7 @@ impl Backend {
}
/// Get an interface to the database and panic if there is none.
pub fn db(&self) -> Rc<Database> {
pub fn db(&self) -> Arc<Mutex<SqliteConnection>> {
self.database.borrow().clone().unwrap()
}

View file

@ -1,6 +1,7 @@
use crate::{Backend, Error, Result};
use db::Track;
use glib::clone;
use musicus_database::Track;
use musicus_database as db;
use std::cell::{Cell, RefCell};
use std::path::PathBuf;
use std::rc::Rc;
@ -458,7 +459,7 @@ impl TrackGenerator for RandomTrackGenerator {
}
fn next(&self) -> Vec<Track> {
vec![self.backend.db().random_track().unwrap()]
vec![db::random_track(&mut self.backend.db().lock().unwrap()).unwrap()]
}
}
@ -479,7 +480,7 @@ impl TrackGenerator for RandomRecordingGenerator {
}
fn next(&self) -> Vec<Track> {
let recording = self.backend.db().random_recording().unwrap();
self.backend.db().get_tracks(&recording.id).unwrap()
let recording = db::random_recording(&mut self.backend.db().lock().unwrap()).unwrap();
db::get_tracks(&mut self.backend.db().lock().unwrap(), &recording.id).unwrap()
}
}