Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 99dc27da rédigé par Kubat's avatar Kubat
Parcourir les fichiers

LIBLEKTOR-RS: DB now supports multiple languages per kara

parent d09cc2a7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -7,3 +7,4 @@ DROP TABLE tag; ...@@ -7,3 +7,4 @@ DROP TABLE tag;
DROP TABLE kara_tags; DROP TABLE kara_tags;
DROP TABLE history; DROP TABLE history;
DROP TABLE iso_639_1; DROP TABLE iso_639_1;
DROP TABLE kara_langs;
\ No newline at end of file
...@@ -23,7 +23,6 @@ CREATE TABLE kara ...@@ -23,7 +23,6 @@ CREATE TABLE kara
, song_type TEXT NOT NULL , song_type TEXT NOT NULL
, song_origin TEXT NOT NULL , song_origin TEXT NOT NULL
, source_name TEXT NOT NULL , source_name TEXT NOT NULL
, language TEXT NOT NULL REFERENCES iso_639_1(code)
, file_hash TEXT NOT NULL UNIQUE , file_hash TEXT NOT NULL UNIQUE
); );
...@@ -65,6 +64,12 @@ CREATE TABLE iso_639_1 ...@@ -65,6 +64,12 @@ CREATE TABLE iso_639_1
, is_macro BOOLEAN NOT NULL DEFAULT false , is_macro BOOLEAN NOT NULL DEFAULT false
); );
CREATE TABLE kara_langs
( id INTEGER NOT NULL REFERENCES kara ON DELETE CASCADE
, code TEXT NOT NULL REFERENCES iso_639_1 ON DELETE CASCADE
, PRIMARY KEY (id, code)
);
-- As defined in ISO 639-1: -- As defined in ISO 639-1:
-- https://archive.wikiwix.com/cache/index2.php?url=http%3A%2F%2Fwww.sil.org%2Fiso639-3%2Fcodes.asp%3Forder%3D639_1%26letter%3D%2525#federation=archive.wikiwix.com&tab=url -- https://archive.wikiwix.com/cache/index2.php?url=http%3A%2F%2Fwww.sil.org%2Fiso639-3%2Fcodes.asp%3Forder%3D639_1%26letter%3D%2525#federation=archive.wikiwix.com&tab=url
INSERT OR REPLACE INTO iso_639_1 (is_macro, is_iso, code, name_en) VALUES INSERT OR REPLACE INTO iso_639_1 (is_macro, is_iso, code, name_en) VALUES
......
...@@ -98,24 +98,33 @@ impl LktDatabaseConnection { ...@@ -98,24 +98,33 @@ impl LktDatabaseConnection {
/// Ensure that a given language is present in the database. If it's not /// Ensure that a given language is present in the database. If it's not
/// insert it. Existence test is done on the code of the language. /// insert it. Existence test is done on the code of the language.
pub fn ensure_language_exists(&mut self, lang: &Language) -> LktDatabaseResult<()> { pub fn ensure_languages_exist<'a>(&mut self, langs: &[Language<'a>]) -> LktDatabaseResult<()> {
self.sqlite.exclusive_transaction(|c| { self.sqlite.exclusive_transaction(|c| {
for lang in langs {
with_dsl!(iso_639_1 => match iso_639_1.filter(code.eq(lang.code)).count().get_result(c)? { with_dsl!(iso_639_1 => match iso_639_1.filter(code.eq(lang.code)).count().get_result(c)? {
1 => Ok(()), 1 => {},
0 => { diesel::insert_into(iso_639_1).values(lang).execute(c)?; Ok(()) } 0 => { diesel::insert_into(iso_639_1).values(langs).execute(c)?; }
count => Err(LktDatabaseError::String(format!("language `{lang:?}` has {count} occurences in the database..."))), count => return Err(LktDatabaseError::String(format!("language `{lang:?}` has {count} occurences in the database..."))),
}) })
}
Ok(())
}) })
} }
/// Add a kara with a request. /// Add a kara with a request.
pub fn add_kara_from_request(&mut self, kara: NewKaraRequest) -> LktDatabaseResult<()> { pub fn add_kara_from_request(&mut self, kara: NewKaraRequest) -> LktDatabaseResult<()> {
let (id, new_kara, lang, karamakers, tags) = kara; let (id, new_kara, karamakers, langs, tags) = kara;
self.ensure_language_exists(&lang)?; self.ensure_languages_exist(&langs)?;
self.sqlite.exclusive_transaction(|c| { self.sqlite.exclusive_transaction(|c| {
with_dsl!(kara => diesel::insert_into(kara).values(&new_kara).execute(c)?); with_dsl!(kara => diesel::insert_into(kara).values(&new_kara).execute(c)?);
with_dsl!(repo_kara => diesel::insert_into(repo_kara).values(id).execute(c)?); with_dsl!(repo_kara => diesel::insert_into(repo_kara).values(id).execute(c)?);
with_dsl!(kara_makers => diesel::insert_or_ignore_into(kara_makers).values(karamakers).execute(c)?); with_dsl!(kara_makers => diesel::insert_or_ignore_into(kara_makers).values(karamakers).execute(c)?);
with_dsl!(kara_langs => {
use models::{Language, KaraLanguage};
let langs = langs.into_iter().map(|Language { code: lang, .. }| KaraLanguage { id: new_kara.id, code: lang } );
diesel::delete(kara_langs.filter(id.eq(new_kara.id))).execute(c)?;
diesel::insert_into(kara_langs).values(langs.collect::<Vec<_>>()).execute(c)?;
});
with_dsl!(kara_tags => { with_dsl!(kara_tags => {
diesel::delete(kara_tags.filter(kara_id.eq(new_kara.id))).execute(c)?; diesel::delete(kara_tags.filter(kara_id.eq(new_kara.id))).execute(c)?;
diesel::insert_into(kara_tags).values(tags).execute(c)?; diesel::insert_into(kara_tags).values(tags).execute(c)?;
...@@ -153,10 +162,9 @@ impl LktDatabaseConnection { ...@@ -153,10 +162,9 @@ impl LktDatabaseConnection {
song_type: kara.song_type, song_type: kara.song_type,
song_origin: kara.category, song_origin: kara.category,
source_name: kara.source_name, source_name: kara.source_name,
language: lang.code,
file_hash: format!("{}", kara.unix_timestamp), file_hash: format!("{}", kara.unix_timestamp),
}; };
Ok((id, kara, lang, kara_makers, tags)) Ok((id, kara, kara_makers, vec![lang], tags))
} }
/// Peek the next kara to play /// Peek the next kara to play
......
...@@ -14,7 +14,7 @@ pub(self) use std::{collections::VecDeque, ops::Range, path::Path}; ...@@ -14,7 +14,7 @@ pub(self) use std::{collections::VecDeque, ops::Range, path::Path};
pub type NewKaraRequest<'a> = ( pub type NewKaraRequest<'a> = (
models::KaraId, models::KaraId,
models::NewKara<'a>, models::NewKara<'a>,
models::Language<'a>,
Vec<models::KaraMaker<'a>>, Vec<models::KaraMaker<'a>>,
Vec<models::Language<'a>>,
Vec<models::AddKaraTag>, Vec<models::AddKaraTag>,
); );
...@@ -28,7 +28,6 @@ pub struct NewKara<'a> { ...@@ -28,7 +28,6 @@ pub struct NewKara<'a> {
pub song_type: &'a str, pub song_type: &'a str,
pub song_origin: &'a str, pub song_origin: &'a str,
pub source_name: &'a str, pub source_name: &'a str,
pub language: &'a str,
pub file_hash: String, pub file_hash: String,
} }
...@@ -39,6 +38,13 @@ pub struct KaraMaker<'a> { ...@@ -39,6 +38,13 @@ pub struct KaraMaker<'a> {
pub name: &'a str, pub name: &'a str,
} }
#[derive(Debug, Insertable, Queryable, Selectable)]
#[diesel(table_name = kara_langs)]
pub struct KaraLanguage<'a> {
pub id: i32,
pub code: &'a str,
}
#[derive(Debug, Insertable, Queryable, Selectable)] #[derive(Debug, Insertable, Queryable, Selectable)]
#[diesel(table_name = iso_639_1)] #[diesel(table_name = iso_639_1)]
pub struct Language<'a> { pub struct Language<'a> {
......
...@@ -24,11 +24,17 @@ diesel::table! { ...@@ -24,11 +24,17 @@ diesel::table! {
song_type -> Text, song_type -> Text,
song_origin -> Text, song_origin -> Text,
source_name -> Text, source_name -> Text,
language -> Text,
file_hash -> Text, file_hash -> Text,
} }
} }
diesel::table! {
kara_langs (id, code) {
id -> Integer,
code -> Text,
}
}
diesel::table! { diesel::table! {
kara_makers (id, name) { kara_makers (id, name) {
id -> Integer, id -> Integer,
...@@ -67,7 +73,8 @@ diesel::table! { ...@@ -67,7 +73,8 @@ diesel::table! {
} }
diesel::joinable!(history -> kara (id)); diesel::joinable!(history -> kara (id));
diesel::joinable!(kara -> iso_639_1 (language)); diesel::joinable!(kara_langs -> iso_639_1 (code));
diesel::joinable!(kara_langs -> kara (id));
diesel::joinable!(kara_makers -> kara (id)); diesel::joinable!(kara_makers -> kara (id));
diesel::joinable!(kara_tags -> kara (kara_id)); diesel::joinable!(kara_tags -> kara (kara_id));
diesel::joinable!(kara_tags -> tag (tag_id)); diesel::joinable!(kara_tags -> tag (tag_id));
...@@ -78,6 +85,7 @@ diesel::allow_tables_to_appear_in_same_query!( ...@@ -78,6 +85,7 @@ diesel::allow_tables_to_appear_in_same_query!(
history, history,
iso_639_1, iso_639_1,
kara, kara,
kara_langs,
kara_makers, kara_makers,
kara_tags, kara_tags,
repo, repo,
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter