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

DB: Begin to implement search by a struct with builder like patterns.

parent c0709481
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
use crate::{models::*, types::*, uri::LktUri, *}; use crate::{models::*, types::*, uri::LktUri, *};
use hashbrown::HashMap; use hashbrown::HashMap;
use kurisu_api::v1 as api_v1; use kurisu_api::v1 as api_v1;
use smallstring::SmallString;
/// Create a connexion to a database and run automatically the migrations. /// Create a connexion to a database and run automatically the migrations.
fn establish_connection(path: impl AsRef<str>) -> Result<SqliteConnection, String> { fn establish_connection(path: impl AsRef<str>) -> Result<SqliteConnection, String> {
...@@ -17,6 +18,24 @@ pub struct LktDatabaseConnection { ...@@ -17,6 +18,24 @@ pub struct LktDatabaseConnection {
sqlite: SqliteConnection, sqlite: SqliteConnection,
} }
/// Builder to realize a search operation in the database.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct LktDatabaseSearch {
search_type: LktDatabaseSearchType,
uri: LktUri,
range: Option<Range<usize>>,
}
/// A searcg type, to determine which table we are search.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum LktDatabaseSearchType {
Playlist(SmallString),
History,
#[default]
Database,
}
/// Load the diesel DSL for a given table. With the loaded dsl execute the /// Load the diesel DSL for a given table. With the loaded dsl execute the
/// expression... /// expression...
macro_rules! with_dsl { macro_rules! with_dsl {
...@@ -38,6 +57,30 @@ macro_rules! fuzzy { ...@@ -38,6 +57,30 @@ macro_rules! fuzzy {
(@inner $x:ident $y:ident $($xs:ident)+) => { fuzzy!(@inner $x $y).concat(fuzzy!(@inner $($xs)+)) }; (@inner $x:ident $y:ident $($xs:ident)+) => { fuzzy!(@inner $x $y).concat(fuzzy!(@inner $($xs)+)) };
} }
impl LktDatabaseSearch {
pub fn new(search_type: LktDatabaseSearchType) -> Self {
Self {
search_type,
..Default::default()
}
}
pub fn search_type(mut self, ty: LktDatabaseSearchType) -> Self {
self.search_type = ty;
self
}
pub fn range(mut self, range: Range<usize>) -> Self {
self.range = Some(range);
self
}
pub fn uri(mut self, uri: LktUri) -> Self {
self.uri = uri;
self
}
}
impl LktDatabaseConnection { impl LktDatabaseConnection {
/// Create a new database connexion. /// Create a new database connexion.
pub fn try_new(path: impl AsRef<Path>) -> LktDatabaseResult<Self> { pub fn try_new(path: impl AsRef<Path>) -> LktDatabaseResult<Self> {
...@@ -201,17 +244,43 @@ impl LktDatabaseConnection { ...@@ -201,17 +244,43 @@ impl LktDatabaseConnection {
}} }}
} }
/// Search the history by URIs. We return the local ids. /// Search the database with the built search. We return the local ids.
pub fn search_history(&mut self, _uri: LktUri) -> LktDatabaseResult<Vec<i64>> { pub fn search(&mut self, search: LktDatabaseSearch) -> LktDatabaseResult<Vec<i64>> {
todo!() // We begin by filtering the database, for KaraMaker, Language, Search
} // and Playlist we get all non virtual karas, because we are joining
// other tables we can't use [into_boxed]...
let karas = match search.uri {
LktUri::Id(uri_id) => with_dsl! { kara => kara
.filter(id.is(uri_id))
.filter(is_virtual.is(false))
.into_boxed()
},
LktUri::Origin(ref uri_origin) => with_dsl! { kara => kara
.filter(song_origin.is(uri_origin.as_str()))
.filter(is_virtual.is(false))
.into_boxed()
},
LktUri::Type(ref uri_type) => with_dsl!(kara => kara
.filter(song_type.is(uri_type.as_str()))
.filter(is_virtual.is(false))
.into_boxed()
),
LktUri::FuzzySearch(_) => with_dsl! { kara => kara.into_boxed() },
LktUri::KaraMaker(_)
| LktUri::Language(_)
| LktUri::Search(_)
| LktUri::Playlist(_)
| LktUri::Any => with_dsl! { kara => kara
.filter(is_virtual.is(false))
.into_boxed()
},
};
/// Search the given playlist by URIs. We return the local ids.
pub fn search_playlist<S: AsRef<str>>(
&mut self,
_playlist: S,
_uri: LktUri,
) -> LktDatabaseResult<Vec<i64>> {
todo!() todo!()
} }
......
use lektor_c_compat::rs::{LktCUri, LktUriField}; use lektor_c_compat::rs::{LktCUri, LktUriField};
use smallstring::SmallString; use smallstring::SmallString;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum LktUri { pub enum LktUri {
/// The id of the kara must match the URI. /// The id of the kara must match the URI.
Id(i64), Id(i64),
......
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