Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 2cbbf82733148c16a834f273dc9025c366b1b088
  • master par défaut protégée
  • rust-playlist-sync
  • rust
  • fix-qt-deprecated-qvariant-type
  • fix-mpris-qtwindow-race-condition
  • rust-appimage-wayland
  • windows-build-rebased
  • v2.5 protégée
  • v2.4 protégée
  • v2.3-1 protégée
  • v2.3 protégée
  • v2.2 protégée
  • v2.1 protégée
  • v2.0 protégée
  • v1.8-3 protégée
  • v1.8-2 protégée
  • v1.8-1 protégée
  • v1.8 protégée
  • v1.7 protégée
  • v1.6 protégée
  • v1.5 protégée
  • v1.4 protégée
  • v1.3 protégée
  • v1.2 protégée
  • v1.1 protégée
  • v1.0 protégée
27 résultats

mod.rs

Blame
  • mod.rs 3,07 Kio
    //! Database implementation in rust for lektor.
    
    pub(self) use diesel::prelude::*;
    use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
    pub(self) use log::*;
    
    pub mod models;
    pub mod schema;
    pub mod unsafe_interface;
    
    use self::models::*;
    
    use crate::{database::schema::kara_tags, kurisu_api::v1 as api_v1};
    
    /// The migrations!
    const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
    
    /// Run migrations in a connexion!
    fn run_migration(conn: &mut SqliteConnection) -> Result<(), String> {
        conn.run_pending_migrations(MIGRATIONS)
            .map_err(|err| err.to_string())
            .map(|_| ())
    }
    
    /// Create a connexion to a database and run automatically the migrations.
    pub fn establish_connection(path: impl AsRef<str>) -> Result<SqliteConnection, String> {
        let mut conn = SqliteConnection::establish(path.as_ref())
            .map_err(|err| format!("error connecting to {}: {}", path.as_ref(), err))?;
        self::run_migration(&mut conn)?;
        Ok(conn)
    }
    
    /// All the information needed to add a kara recieved from a repo!
    pub type NewKaraRequest<'a> = (
        models::NewKaraId,
        models::NewKara<'a>,
        models::NewLanguage<'a>,
        Vec<models::AddKaraMaker<'a>>,
        Vec<models::AddKaraTag>,
    );
    
    pub struct LktDatabaseConnection {
        sqlite: SqliteConnection,
    }
    
    impl LktDatabaseConnection {
        /// Get a tag id by its name.
        pub fn get_tag_id_by_name(&mut self, tag_name: impl AsRef<str>) -> i32 {
            use self::schema::tag::dsl::*;
            tag.filter(name.is(tag_name.as_ref()))
                .first::<Tag>(&mut self.sqlite)
                .unwrap()
                .id
        }
    
        /// Get a free local id for all karas.
        pub fn get_kara_new_local_id(&mut self) -> i32 {
            use self::schema::kara::dsl::*;
            use diesel::dsl::*;
            kara.select(max(id))
                .first::<Option<i32>>(&mut self.sqlite)
                .unwrap()
                .unwrap_or(0)
        }
    
        /// Create a series of models from a kara signature from Kurisu's V1 API.
        pub fn new_kara<'a>(&mut self, repo_id: u64, kara: api_v1::Kara<'a>) -> NewKaraRequest<'a> {
            error!("todo: query the database for a new local id");
            let local_id = self.get_kara_new_local_id();
            let id = NewKaraId {
                repo_id: repo_id as i32,
                local_kara_id: local_id as i32,
                repo_kara_id: kara.id as i32,
            };
            let lang = NewLanguage::from(kara.get_language());
            let kara_makers = vec![AddKaraMaker {
                id: local_id as i32,
                name: kara.author_name,
            }];
            let tags = vec![AddKaraTag {
                kara_id: local_id as i32,
                tag_id: self.get_tag_id_by_name("number"),
                value: Some(format!("{}", kara.song_number)),
            }];
            let kara = NewKara {
                id: local_id as i32,
                song_title: kara.song_name,
                song_type: kara.song_type,
                song_origin: kara.category,
                source_name: kara.source_name,
                language: lang.code,
                file_hash: format!("{}", kara.unix_timestamp),
            };
            (id, kara, lang, kara_makers, tags)
        }
    }