int
lms_db_set_file_dtime(sqlite3_stmt *stmt, const struct lms_file_info *finfo)
{
    int r, ret;

    ret = lms_db_bind_int(stmt, 1, finfo->dtime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 2, finfo->itime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int64(stmt, 3, finfo->id);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        fprintf(stderr, "ERROR: could not set file dtime: %s\n",
                sqlite3_errmsg(sqlite3_db_handle(stmt)));
        ret = -3;
        goto done;
    }

    ret = 0;

  done:
    lms_db_reset_stmt(stmt);

    return ret;
}
static int
_db_insert(lms_db_playlist_t *ldp, const struct lms_playlist_info *info)
{
    sqlite3_stmt *stmt;
    int r, ret;

    stmt = ldp->insert;

    ret = lms_db_bind_int64(stmt, 1, info->id);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 3, info->n_entries);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        fprintf(stderr, "ERROR: could not insert playlist info: %s\n",
                sqlite3_errmsg(ldp->db));
        ret = -4;
        goto done;
    }

    ret = 0;

  done:
    lms_db_reset_stmt(stmt);

    return ret;
}
int
lms_db_table_version_set(sqlite3 *db, const char *table, unsigned int version)
{
    int r, ret;
    sqlite3_stmt *stmt;

    stmt = lms_db_compile_stmt(db,
        "INSERT OR REPLACE INTO lms_internal (tab, version) VALUES (?, ?)");
    if (!stmt)
        return -1;

    ret = lms_db_bind_text(stmt, 1, table, -1);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 2, version);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        ret = -1;
        fprintf(stderr, "ERROR: could not set table '%s' version: %s\n",
                table, sqlite3_errmsg(db));
    }

  done:
    lms_db_reset_stmt(stmt);
    lms_db_finalize_stmt(stmt, "table_version_set");

    return ret;
}
int
lms_db_insert_file_info(sqlite3_stmt *stmt, struct lms_file_info *finfo,
                        unsigned int update_id)
{
    int r, ret;

    ret = lms_db_bind_blob(stmt, 1, finfo->path, finfo->path_len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 2, finfo->mtime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 3, finfo->dtime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 4, finfo->itime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 5, finfo->size);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 6, update_id);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        fprintf(stderr, "ERROR: could not insert file info: %s\n",
                sqlite3_errmsg(sqlite3_db_handle(stmt)));
        ret = -5;
        goto done;
    }

    finfo->id = sqlite3_last_insert_rowid(sqlite3_db_handle(stmt));
    ret = 0;

  done:
    lms_db_reset_stmt(stmt);

    return ret;
}
int
lms_db_update_file_info(sqlite3_stmt *stmt, const struct lms_file_info *finfo, unsigned int update_id)
{
    int r, ret;

    ret = lms_db_bind_int(stmt, 1, finfo->mtime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 2, finfo->dtime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 3, finfo->itime);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 4, finfo->size);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 5, update_id);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 6, finfo->id);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        fprintf(stderr, "ERROR: could not update file info: %s\n",
                sqlite3_errmsg(sqlite3_db_handle(stmt)));
        ret = -5;
        goto done;
    }

    ret = 0;

  done:
    lms_db_reset_stmt(stmt);

    return ret;
}
static int
_db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
{
    sqlite3_stmt *stmt;
    int r, ret;

    stmt = ldi->insert;

    ret = lms_db_bind_int64(stmt, 1, info->id);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 2, info->title.str, info->title.len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 3, info->artist.str, info->artist.len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 4, info->date);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 5, info->width);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 6, info->height);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_int(stmt, 7, info->orientation);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_double(stmt, 8, info->gps.latitude);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_double(stmt, 9, info->gps.longitude);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_double(stmt, 10, info->gps.altitude);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 11, info->dlna_profile.str,
                           info->dlna_profile.len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 12, info->dlna_mime.str, info->dlna_mime.len);
    if (ret != 0)
        goto done;

    ret = lms_db_bind_text(stmt, 13, info->container.str, info->container.len);
    if (ret != 0)
        goto done;

    r = sqlite3_step(stmt);
    if (r != SQLITE_DONE) {
        fprintf(stderr, "ERROR: could not insert image info: %s\n",
                sqlite3_errmsg(ldi->db));
        ret = -11;
        goto done;
    }

    ret = 0;

done:
    lms_db_reset_stmt(stmt);

    return ret;
}