11. cratewav (scheme + monolith dictionary version)

This is a version of cratewav, which creates an ftable in the monolith dictionary and loads the file from a crate previously opened in monolith. The UUID can be partially given in the typical hex format, as well as ergo-id encoding.

<<sqlar_scheme_entries>>=
{"sqlar:cratewav", pp_sqlar_cratewav, 3, 3, {STR,STR,STR}},

(sqlar:cratewav "crate" "ft1" "asdlfkjasd")

<<static_sqlar_function_declarations>>=
int sqlar_loadwav_db(sqlite3 *db,
                     const char *filename,
                     sp_ftbl **ft);
<<sqlar_scheme_functions>>=
static cell pp_sqlar_cratewav(cell x)
{
    const char *dbkey;
    const char *ftname;
    const char *idstr;
    monolith_d *m;
    int rc;
    sqlite3 *db;
    const char *filename;
    sqlite3_stmt *stmt;
    monolith_dict_entry *dbent;
    monolith_dict_entry *ftent;
    sp_ftbl *ft;
    monolith_dict *dict;
    int count;
    char *ergo;

    m = monolith_data_get();
    dict = monolith_dict_get(m);

    dbkey = string(car(x));
    x = cdr(x);
    ftname = string(car(x));
    x = cdr(x);
    idstr = string(car(x));

    /* get DB from monolith dictionary */

    rc = monolith_dict_find(dict, &dbent, dbkey, strlen(dbkey));

    if (rc != MONOLITH_OK) {
        error("Could not find SQLite database", UNSPECIFIC);
    }

    db = monolith_dict_entry_data(dbent);

    sqlite3_prepare_v2(db,
                       "SELECT value, COUNT(DISTINCT uuid) "
                       "from wikizet "
                       "WHERE uuid LIKE ?1 || \"%\" "
                       "AND value LIKE \"/%\";",
                       -1, &stmt, NULL);

    ergo = NULL;
    if (idstr[0] == 'g') {
        size_t sz;
        /* 'g' is truncated, so N - 1 */
        sz = strlen(idstr) - 1;
        ergo = malloc(sz + 1);
        ergo[sz] = '\0';
        ergo_to_hex(&idstr[1], sz, ergo);

        sqlite3_bind_text(stmt, 1, ergo, -1, NULL);
    } else {
        sqlite3_bind_text(stmt, 1, idstr, -1, NULL);
    }

    rc = sqlite3_step(stmt);

    if (rc != SQLITE_ROW) {
        fprintf(stderr, "cratewav: %s\n", sqlite3_errmsg(db));
        error("error", UNSPECIFIC);
    }

    count = sqlite3_column_int(stmt, 1);

    if (count < 1) {
        fprintf(stderr, "Could not resolve id %s\n", idstr);
        sqlite3_finalize(stmt);
        error("error", UNSPECIFIC);
        return UNSPECIFIC;
    } else if (count > 1) {
        fprintf(stderr, "id pattern %s not unique.\n", idstr);
        sqlite3_finalize(stmt);
        error("error", UNSPECIFIC);
        return UNSPECIFIC;
    }

    /* create new ftable entry */

    rc = monolith_dict_newentry(dict, &ftent,
                                ftname, strlen(ftname));

    if (rc != MONOLITH_OK) {
        error("Unable to create entry", UNSPECIFIC);
    }

    /* load file into ftable */
    filename = (const char *)sqlite3_column_text(stmt, 0);
    rc = sqlar_loadwav_db(db, &filename[1], &ft);

    /* bind ftable */

    monolith_dict_entry_ftbl(ftent, ft);

    if (rc != SP_OK) {
        error("There were SQLar problems", UNSPECIFIC);
        sqlite3_finalize(stmt);
    }

    if (ergo != NULL) free(ergo);

    return UNSPECIFIC;
}



prev | home | next