Add a SQLite database backend #265

Merged
wojtek merged 20 commits from 248---replace-json-file-as-a-database-with-sqlite into main 2025-01-12 10:24:53 +01:00
4 changed files with 12 additions and 15 deletions
Showing only changes of commit 8c83c6fac6 - Show all commits

View File

@ -3,5 +3,5 @@ pub mod json;
#[cfg(feature = "database-sqlite")]
pub mod sql;
#[cfg(feature = "database-json")]
#[cfg(any(feature = "database-json", feature = "database-sqlite"))]
mod serde;

View File

@ -5,6 +5,8 @@ use crate::core::collection::{
musicbrainz::MbRefOption,
};
pub const V20250103: &str = "V20250103";
#[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "AlbumLibId")]
pub enum AlbumLibIdDef {

View File

@ -1,4 +1,4 @@
//! Module for storing MusicHoard data in a JSON file database.
//! Module for storing MusicHoard data in a SQLite database.
use std::path::PathBuf;
@ -10,7 +10,7 @@ use crate::{
collection::album::AlbumDate,
external::database::{
serde::{
common::SerdeAlbumDate,
common::{SerdeAlbumDate, V20250103},
deserialize::{DeserializeAlbum, DeserializeArtist, DeserializeDatabase},
serialize::{SerializeAlbum, SerializeArtist, SerializeDatabase},
},
@ -157,7 +157,7 @@ impl ISqlTransactionBackend for SqlTransactionSqliteBackend<'_> {
VALUES (?1, ?2)",
)?;
let version = match version {
SerializeDatabase::V20250103(_) => "V20250103",
SerializeDatabase::V20250103(_) => V20250103,
};
Self::execute(&mut stmt, ("version", version))
}
@ -168,16 +168,11 @@ impl ISqlTransactionBackend for SqlTransactionSqliteBackend<'_> {
let mut rows = Self::query(&mut stmt, ())?;
match Self::next_row(&mut rows)? {
Some(row) => {
let version: String = Self::get_value(row, 0)?;
match version.as_str() {
"V20250103" => Ok(DeserializeDatabase::V20250103(vec![])),
s => Err(Error::SerDeError(format!("unknown database version: {s}"))),
}
}
None => Err(Error::SerDeError(String::from(
"database version is missing",
))),
Some(row) => match Self::get_value::<String>(row, 0)?.as_str() {
V20250103 => Ok(DeserializeDatabase::V20250103(vec![])),
s => Err(Error::SerDeError(format!("unknown database version: {s}"))),
},
None => Err(Error::SerDeError(String::from("missing database version"))),
}
}

View File

@ -227,7 +227,7 @@ impl<'de> Deserialize<'de> for SerdeMbid {
}
#[derive(Debug, Clone)]
pub struct SerdeAlbumDate(pub AlbumDate);
pub struct SerdeAlbumDate(AlbumDate);
impl From<SerdeAlbumDate> for AlbumDate {
fn from(value: SerdeAlbumDate) -> Self {