diff --git a/inc/lektor/database.h b/inc/lektor/database.h index b12c6d85a30d0e53073effa7417da0ac22a624bd..4b7b3d51a3ea2832d30dadd08f4b532034103064 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -8,6 +8,7 @@ extern "C" { #include <lektor/common.h> #include <lektor/mkv.h> #include <lektor/uri.h> +#include <lektor/lib/strv.h> #include <sqlite3.h> #include <mpv/client.h> @@ -181,6 +182,7 @@ bool database_queue_skip_current(lkt_db *db, char filepath[PATH_MAX]); /* Set a value in the config table */ bool database_validate_conf (lkt_db *db); bool database_config_set (lkt_db *db, const char *section, const char *key, const char *value); +bool database_config_set_strv_1_2 (lkt_db *db, const char *section, const struct strv, const struct strv); bool database_config_get_text (lkt_db *db, const char *section, const char *key, char *value, size_t len); bool database_config_get_text_nospace (lkt_db *db, const char *section, const char *key, char *value, size_t len); bool database_config_get_int (lkt_db *db, const char *section, const char *key, int *value); diff --git a/inc/lektor/internal/dbmacro.h b/inc/lektor/internal/dbmacro.h index 81d2f606eb389d3e72a443c860542e00397ef355..d24cdda09bd3c2c46397ec8fbb883b1509db98d4 100644 --- a/inc/lektor/internal/dbmacro.h +++ b/inc/lektor/internal/dbmacro.h @@ -35,6 +35,15 @@ bool database_unprotected_attach(lkt_db *, const char *, const char *); } \ } +#define SQLITE_BIND_STRV(db, stmt, pos, strv, error) \ + { \ + if (sqlite3_bind_text(stmt, pos, (strv).data, (strv).count, 0) != SQLITE_OK) { \ + ___LOCAL_DEBUG("Failed to bind text " STRV_FMT " at pos %d: %s", STRV_ARG(strv), pos, \ + sqlite3_errmsg((sqlite3 *)db)); \ + goto error; \ + } \ + } + #define SQLITE_BIND_INT(db, stmt, pos, integer, error) \ { \ if (sqlite3_bind_int(stmt, pos, integer) != SQLITE_OK) { \ diff --git a/src/database/config.c b/src/database/config.c index 46f666824de3b862e7903638baa7eb6140462bb8..a7864179d5d1b16928fe85ef72af7705b80f83a0 100644 --- a/src/database/config.c +++ b/src/database/config.c @@ -1,7 +1,39 @@ #include <lektor/common.h> #include <lektor/database.h> +#include <lektor/lib/strv.h> #include <lektor/internal/dbmacro.h> +bool +database_config_set_strv_1_2(lkt_db *db, const char *section, const struct strv key, + const struct strv value) +{ + static const char *SQL_STMT = "INSERT OR REPLACE INTO" + " config (section, key, value)" + " VALUES (?, ?, ?);\n"; + sqlite3_stmt *stmt = 0; + bool ret = false; + int code; + + SQLITE_PREPARE(db, stmt, SQL_STMT, error); + SQLITE_BIND_TEXT(db, stmt, 1, section, error); + SQLITE_BIND_STRV(db, stmt, 2, key, error); + SQLITE_BIND_STRV(db, stmt, 3, value, error); + + code = sqlite3_step(stmt); + + if (code != SQLITE_OK && code != SQLITE_DONE) { + LOG_ERROR("DB", "Failed to insert or replace: %s", sqlite3_errmsg((sqlite3 *)db)); + goto error; + } + + LOG_DEBUG("CONFIG", "Set [%s/"STRV_FMT"] to "STRV_FMT, section, STRV_ARG(key), + STRV_ARG(value)); + ret = true; +error: + sqlite3_finalize(stmt); + return ret; +} + bool database_config_set(lkt_db *db, const char *section, const char *key, const char *value) {