From 9a92a32faabcad0f6300e7e322308d17db3a7241 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 16 Sep 2020 09:13:13 +0200
Subject: [PATCH] MISC & DB: Some fixes around the sqlite queries and DB

- move queue_tmp to :memory:
- fix for correct deletion of content of queue_tmp
- avoid segfault when finalizing an already finalized stmt
- also make things compile with -Wextra
- make things compile again
---
 inc/lektor/common.h     |  2 +-
 src/base/commands.c     |  3 ++-
 src/base/common.c       |  4 ++--
 src/base/config.c       | 12 ++++++------
 src/database/disk.sql   |  7 -------
 src/database/macro.h    | 10 +++++-----
 src/database/memory.sql |  8 ++++++++
 src/database/queue.c    | 12 ++++--------
 src/main/lkt.c          |  2 +-
 9 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index c65f732a..d12ca94e 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -51,13 +51,13 @@ extern void (*__lkt_assert)(const char *file, int line, const char *func, const
 #endif
 
 /* Custom log functions. */
-extern int log_level;
 enum log_level {
     ERROR   = 1,
     WARN    = 2,
     INFO    = 3,
     DEBUG   = 4,
 };
+extern enum log_level __log_level;
 void __lkt_log(enum log_level, const char *section, const char *func, const char *format, ...);
 #define LOG_INFO( section, ...) __lkt_log(INFO,  section, __func__, __VA_ARGS__)
 #define LOG_WARN( section, ...) __lkt_log(WARN,  section, __func__, __VA_ARGS__)
diff --git a/src/base/commands.c b/src/base/commands.c
index cb0808dd..65db132a 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -62,7 +62,8 @@ command_update(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]
 }
 
 bool
-command_rescan(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX], int forced)
+command_rescan(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX],
+               int __attribute__((unused)) forced)
 {
     UNUSED(argv);
     RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false);
diff --git a/src/base/common.c b/src/base/common.c
index 093e5ff3..d1de4234 100644
--- a/src/base/common.c
+++ b/src/base/common.c
@@ -36,7 +36,7 @@ __set_assert(void)
 
 /* Log functions */
 
-int log_level = 0; /* None by default */
+enum log_level __log_level = 0; /* None by default */
 
 void
 __lkt_log(enum log_level level, const char *section, const char *func, const char *format, ...)
@@ -44,7 +44,7 @@ __lkt_log(enum log_level level, const char *section, const char *func, const cha
     char line[LKT_MESSAGE_MAX];
     va_list ap;
 
-    if (level <= log_level) {
+    if (level <= __log_level) {
         safe_snprintf(line, LKT_MESSAGE_MAX, " %c [%s] %s: %s\n",
                       level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ',
                       section, func, format);
diff --git a/src/base/config.c b/src/base/config.c
index 6ec549ae..e5efa353 100644
--- a/src/base/config.c
+++ b/src/base/config.c
@@ -62,17 +62,17 @@ __set_log_level(const char *name, const char *level)
     }
 
     if (STR_MATCH(level, "error"))
-        log_level = ERROR;
+        __log_level = ERROR;
     else if (STR_MATCH(level, "warn") || STR_MATCH(level, "warning"))
-        log_level = WARN;
+        __log_level = WARN;
     else if (STR_MATCH(level, "info"))
-        log_level = INFO;
+        __log_level = INFO;
     else if (STR_MATCH(level, "debug"))
-        log_level = DEBUG;
+        __log_level = DEBUG;
     else
-        log_level = strtol(level, NULL, 0);
+        __log_level = strtol(level, NULL, 0);
 
-    LOG_INFO("CONFIG", "Log level set to %d", log_level);
+    LOG_INFO("CONFIG", "Log level set to %d", __log_level);
 }
 
 static inline int
diff --git a/src/database/disk.sql b/src/database/disk.sql
index 691a028a..d1e356f6 100644
--- a/src/database/disk.sql
+++ b/src/database/disk.sql
@@ -77,13 +77,6 @@ CREATE TABLE IF NOT EXISTS queue
   , priority INTEGER NOT NULL DEFAULT 1 CHECK(priority > 0 AND priority < 6)
   );
 
--- Temporary queue table used when reordering the queue (for inserts)
-CREATE TABLE IF NOT EXISTS queue_tmp
-   ( position INTEGER PRIMARY KEY AUTOINCREMENT CHECK(position > 0)
-   , kara_id INTEGER
-   , priority INTEGER NOT NULL DEFAULT 1 CHECK(priority > 0 AND priority < 6)
-   );
-
 
 -- The user table
 -- Used for the [password {passwd}] MPD command. The documentation can be found
diff --git a/src/database/macro.h b/src/database/macro.h
index 76f44902..da23e82c 100644
--- a/src/database/macro.h
+++ b/src/database/macro.h
@@ -3,21 +3,21 @@
 
 #define SQLITE_PREPARE(db, stmt, SQL, goto_label)                               \
     if (sqlite3_prepare_v2((sqlite3 *) db, SQL, -1, &(stmt), 0) != SQLITE_OK) { \
-        LOG_ERROR("DB", "Failed to prepare statement: %s",                      \
+        LOG_DEBUG("DB", "Failed to prepare statement: %s",                      \
                   sqlite3_errmsg((sqlite3 *) db));                              \
         goto goto_label;                                                        \
     }
 
 #define SQLITE_EXEC(db, SQL, goto_label)                                        \
     if (sqlite3_exec((sqlite3 *) db, SQL, NULL, NULL, NULL) != SQLITE_OK) {     \
-        LOG_ERROR("DB", "Failed to exec statement: %s",                         \
+        LOG_DEBUG("DB", "Failed to exec statement: %s",                         \
                   sqlite3_errmsg((sqlite3 *) db));                              \
         goto goto_label;                                                        \
     }
 
 #define SQLITE_BIND_TEXT(db, stmt, pos, text, error) {                          \
     if (sqlite3_bind_text(stmt, pos, text, -1, 0) != SQLITE_OK) {               \
-        LOG_ERROR("DB", "Failed to bind text %s at pos %d: %s",                 \
+        LOG_DEBUG("DB", "Failed to bind text %s at pos %d: %s",                 \
                   (const char *) text, pos,                                     \
                   sqlite3_errmsg((sqlite3 *) db));                              \
         goto error;                                                             \
@@ -25,14 +25,14 @@
 
 #define SQLITE_BIND_INT(db, stmt, pos, integer, error) {                        \
     if (sqlite3_bind_int(stmt, pos, integer) != SQLITE_OK) {                    \
-        LOG_ERROR("DB", "Failed to bind int %d at pos %d: %s",                  \
+        LOG_DEBUG("DB", "Failed to bind int %d at pos %d: %s",                  \
                   integer, pos, sqlite3_errmsg((sqlite3 *) db));                \
         goto error;                                                             \
     }}
 
 #define SQLITE_STEP(db, stmt, code, error)                                      \
     if (sqlite3_step(stmt) != code) {                                           \
-        LOG_ERROR("DB", "Failed to step: %s",                                   \
+        LOG_DEBUG("DB", "Failed to step: %s",                                   \
                   sqlite3_errmsg((sqlite3 *) db));                              \
         goto error;                                                             \
     }
diff --git a/src/database/memory.sql b/src/database/memory.sql
index 5a36f3a9..83d8bfe7 100644
--- a/src/database/memory.sql
+++ b/src/database/memory.sql
@@ -26,6 +26,14 @@ CREATE TABLE IF NOT EXISTS queue_state
 INSERT INTO queue_state (id) VALUES (42);
 
 
+-- Temporary queue table used when reordering the queue (for inserts)
+CREATE TABLE IF NOT EXISTS queue_tmp
+   ( position INTEGER PRIMARY KEY AUTOINCREMENT CHECK(position > 0)
+   , kara_id INTEGER
+   , priority INTEGER NOT NULL DEFAULT 1 CHECK(priority > 0 AND priority < 6)
+   );
+
+
 -- Used to store the content of the ini configuration file.
 
 CREATE TABLE IF NOT EXISTS config
diff --git a/src/database/queue.c b/src/database/queue.c
index ac5d1181..959d416e 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -253,12 +253,13 @@ database_queue_add_id(volatile sqlite3 *db, int id, int priority)
     sqlite3_finalize(stmt);
 
     /* Do the move shit only if the priority > 1 */
-    reorder(db, priority, error);
+    reorder(db, priority, error_no_stmt);
 
     SQLITE_EXEC(db, "COMMIT;", error);
     return true;
 error:
     sqlite3_finalize(stmt);
+error_no_stmt:
     SQLITE_DO_ROLLBACK(db);
     return false;
 }
@@ -293,8 +294,8 @@ database_queue_del_id(volatile sqlite3 *db, int id)
 {
     static const char *SQL_TEMPLATE =
         "BEGIN TRANSACTION;"
-        "CREATE TEMPORARY TABLE IF NOT EXISTS queue_tmp (position INTEGER, kara_id INTEGER, priority INTEGER);"
         "DELETE FROM queue_tmp;"
+        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         /* Move the current 'pointer' */
         "UPDATE queue_state SET current = (SELECT NULLIF(COUNT(position), 0) FROM queue JOIN queue_state ON position <= current AND kara_id != %d);"
         "DELETE FROM queue WHERE kara_id = %d;"                                                 /* Delete any kara with the specified id */
@@ -570,13 +571,8 @@ database_queue_shuffle(volatile sqlite3 *db)
     const char *SQL =
         "BEGIN TRANSACTION;"
         /* Create temporary queue */
-        "CREATE TEMPORARY TABLE IF NOT EXISTS queue_tmp"
-        "  ( position INTEGER PRIMARY KEY AUTOINCREMENT CHECK(position > 0)"
-        "  , kara_id INTEGER"
-        "  , priority INTEGER NOT NULL DEFAULT 1 CHECK(priority > 0 AND priority < 6)"
-        "  );"
-        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         "DELETE FROM queue_tmp;"
+        "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
         /* When current is NULL, that thing is also NULL, so no insertion is done */
         "INSERT INTO queue_tmp (kara_id, priority)"
         "  SELECT kara_id, 5"
diff --git a/src/main/lkt.c b/src/main/lkt.c
index 042d3467..079a34e1 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -1101,7 +1101,7 @@ parse_args(args_t *args, int argc, const char **argv)
 int
 main(int argc, const char **argv)
 {
-    log_level = ERROR;
+    __log_level = ERROR;
     executable_name = "lkt";
     assert(NULL != setlocale(LC_ALL, "en_US.UTF-8"));   /* BECAUSE! */
     if (signal(SIGPIPE, sigpipe__)) {
-- 
GitLab