audiot_core 0.2.0

Library that helps build the search database through the CLI, and eventually can be used to read data from that same database.
Documentation
use log::debug;
use rusqlite::{Connection, params};

use crate::brainz::ReleaseGroup;

pub fn insert_batch(conn: &mut Connection, batch: &[ReleaseGroup]) {
    let tx = conn.transaction().unwrap();

    debug!("Starting writing batch to SQLite");

    for release_group in batch.iter() {
        let serialized = serde_json::to_string(&release_group).unwrap();

        let rg_insert = "insert into release_groups (id, json_data) values (?1, ?2)";

        tx.execute(rg_insert, params![&release_group.id, serialized])
            .unwrap();

        let artist_rg_insert = "insert or ignore into artist_release_groups (artist_id, release_group_id) values (?1, ?2)";

        for artist_id in release_group.artist_ids() {
            tx.execute(artist_rg_insert, params![artist_id, &release_group.id])
                .unwrap();
        }

        let search_insert = "insert into search_release_groups (id, artists, title, genres, priority) values (?1, ?2, ?3, ?4, ?5)";

        tx.execute(
            search_insert,
            params![
                &release_group.id,
                release_group.artist_content(),
                &release_group.title,
                release_group.genres_content(),
                release_group.priority()
            ],
        )
        .unwrap();

        let artist_insert = "insert or ignore into artist_names (id, name) values (?1, ?2)";

        for artist_reference in release_group.artist_references().iter() {
            tx.execute(
                artist_insert,
                params![&artist_reference.id, &artist_reference.name],
            )
            .unwrap();
        }
    }

    debug!("Finished writing batch to SQLite");

    tx.commit().unwrap();
}