From 8357087341912fd330f318d74603268c8d6b5bdd Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 10 Aug 2020 09:34:12 +0200
Subject: [PATCH] BUILD: Move the private sqlite defines to a private header

---
 inc/lektor/common.h     | 44 -------------------------------------
 src/database/config.c   |  2 ++
 src/database/find.c     |  2 ++
 src/database/macro.h    | 48 +++++++++++++++++++++++++++++++++++++++++
 src/database/open.c     |  2 ++
 src/database/playlist.c |  2 ++
 src/database/queue.c    |  2 ++
 src/database/stickers.c |  2 ++
 src/database/update.c   |  2 ++
 src/database/user.c     |  2 ++
 10 files changed, 64 insertions(+), 44 deletions(-)
 create mode 100644 src/database/macro.h

diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index 12bfb5fc..6699edef 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -118,53 +118,9 @@ typedef volatile enum {
 #define BUFFER_OUT_MAX      16
 #define MPD_VERSION         "0.21.16"
 
-#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",                      \
-                  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",                         \
-                  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",                 \
-                  (const char *) text, 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) {                    \
-        LOG_ERROR("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",                                   \
-                  sqlite3_errmsg((sqlite3 *) db));                              \
-        goto error;                                                             \
-    }
-
-#define SQLITE_STEP_ROW(db, stmt, error)    SQLITE_STEP(db, stmt, SQLITE_ROW, error)
-#define SQLITE_STEP_DONE(db, stmt, error)   SQLITE_STEP(db, stmt, SQLITE_DONE, error)
-
-#define SQLITE_DO_ROLLBACK(db) \
-    sqlite3_exec((sqlite3 *) db, "ROLLBACK TRANSACTION;\n", NULL, NULL, NULL);
-
 #define STR_MATCH(str1, str2)           (! strcasecmp(str1, str2))
 #define STR_NMATCH(str1, str2, n)       (! strncasecmp(str1, str2, n))
 
-#define sqlite3_column_chars (const char *) sqlite3_column_text
-
 #define GET_VA_COUNT(...)   PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
 #define PP_NARG_(...)       PP_ARG_N(__VA_ARGS__)
 #define PP_ARG_N(   _1, _2, _3, _4, _5, _6, _7, _8, _9,_10,_11,_12,_13,_14,     \
diff --git a/src/database/config.c b/src/database/config.c
index ea9a3221..ec8c5b13 100644
--- a/src/database/config.c
+++ b/src/database/config.c
@@ -8,6 +8,8 @@
 #include <string.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 bool
 database_config_set(volatile sqlite3 *db, const char *section, const char *key,
                     const char *value)
diff --git a/src/database/find.c b/src/database/find.c
index 5e3efba9..b8c35597 100644
--- a/src/database/find.c
+++ b/src/database/find.c
@@ -9,6 +9,8 @@
 #include <string.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 static inline int
 __check_sticker_type(const char *type)
 {
diff --git a/src/database/macro.h b/src/database/macro.h
new file mode 100644
index 00000000..76f44902
--- /dev/null
+++ b/src/database/macro.h
@@ -0,0 +1,48 @@
+#if ! defined(__LKT_DATABASE_MACRO_H__)
+#define __LKT_DATABASE_MACRO_H__
+
+#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",                      \
+                  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",                         \
+                  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",                 \
+                  (const char *) text, 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) {                    \
+        LOG_ERROR("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",                                   \
+                  sqlite3_errmsg((sqlite3 *) db));                              \
+        goto error;                                                             \
+    }
+
+#define SQLITE_STEP_ROW(db, stmt, error)    SQLITE_STEP(db, stmt, SQLITE_ROW, error)
+#define SQLITE_STEP_DONE(db, stmt, error)   SQLITE_STEP(db, stmt, SQLITE_DONE, error)
+
+#define SQLITE_DO_ROLLBACK(db) \
+    sqlite3_exec((sqlite3 *) db, "ROLLBACK TRANSACTION;\n", NULL, NULL, NULL);
+
+#define sqlite3_column_chars (const char *) sqlite3_column_text
+
+#endif /* __LKT_DATABASE_MACRO_H__ */
diff --git a/src/database/open.c b/src/database/open.c
index b12fd3c8..ed5ef19b 100644
--- a/src/database/open.c
+++ b/src/database/open.c
@@ -10,6 +10,8 @@
 #include <pthread.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 extern unsigned char ___src_database_memory_sql[];
 extern unsigned char ___src_database_disk_sql[];
 
diff --git a/src/database/playlist.c b/src/database/playlist.c
index 2df1327d..b0844840 100644
--- a/src/database/playlist.c
+++ b/src/database/playlist.c
@@ -6,6 +6,8 @@
 #include <strings.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 /* Find it in database/open.c */
 extern int
 is_sql_str_invalid(const char *str);
diff --git a/src/database/queue.c b/src/database/queue.c
index 21b7a425..1573e565 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -8,6 +8,8 @@
 #include <string.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 // *INDENT-OFF*
 #define sqlite_just_exec(func, query)                                   \
     bool func (volatile sqlite3 *db) {                                  \
diff --git a/src/database/stickers.c b/src/database/stickers.c
index ec06822a..a90685bf 100644
--- a/src/database/stickers.c
+++ b/src/database/stickers.c
@@ -8,6 +8,8 @@
 #include <stdio.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 static inline int
 __check_type(const char *type)
 {
diff --git a/src/database/update.c b/src/database/update.c
index 52bbda98..371b7040 100644
--- a/src/database/update.c
+++ b/src/database/update.c
@@ -13,6 +13,8 @@
 #include <time.h>
 #include <linux/limits.h>
 
+#include "macro.h"
+
 static inline bool
 __add_kara_to_update_job(volatile sqlite3 *db, size_t id)
 {
diff --git a/src/database/user.c b/src/database/user.c
index 50a7f6e4..34045b3f 100644
--- a/src/database/user.c
+++ b/src/database/user.c
@@ -5,6 +5,8 @@
 #include <stdio.h>
 #include <sqlite3.h>
 
+#include "macro.h"
+
 bool
 database_user_authentificate(volatile sqlite3 *db, const char *password)
 {
-- 
GitLab