diff --git a/inc/lektor/common.h b/inc/lektor/common.h index 12bfb5fca8020c900b46562f34ee0e8d7afd2b8f..6699edeffb2658f0948d96c562b063981efaec15 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 ea9a3221f4c822404a81b079d1abd15ff98f535a..ec8c5b139abcb0279ef7a3a4c3022f6b8e2d8af1 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 5e3efba9847611e233c1740a3dd126c8fbe0b37c..b8c35597bbfe1e2a0cd0169be8bd83c35027e79e 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 0000000000000000000000000000000000000000..76f449021bf6966c1d7e31f0f3e39e311f7cf9f2 --- /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 b12fd3c8b53c3e00246ecafb5e2d294644e1a724..ed5ef19b3812e7a80a1d608c584c5423d3eec560 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 2df1327d98bb610fee9cb6f13d4691152af3868e..b0844840423da17db07185d9cbc82522cd927148 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 21b7a4253caed3becbf7d1f360e3f7fb91b8241e..1573e56557e7023618ed3cfdbfc0129cfef5966b 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 ec06822ac64f066e8b266ec69ce81a759d75adb7..a90685bf54c3f54b8bbd84ea706c59af67921687 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 52bbda98d1d7771ba4f2a104d733108fa7bdbd87..371b7040f70e347a5609b12fbbe6b6361c01f69f 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 50a7f6e437f8f2ac5af4ae89df478e687f06ffec..34045b3f392486c357d0429876c8773a7da6e1c8 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) {