Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 13458e53 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

WIP: First step with macros

parent a2b7166d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!53Resolve "Use of sqlite databases to export and import playlists"
#pragma once
#ifndef __FUNCTION__
#define __FUNCTION__ __func__
#endif /* __FUNCTION__ */
#define SQLITE_PREPARE(db, stmt, SQL, goto_label) \
if (sqlite3_prepare_v2(db, SQL, -1, &stmt, 0) != SQLITE_OK) { \
fprintf(stderr, " ! %s: Failed to prepare statement: %s\n", \
__FUNCTION__, sqlite3_errmsg(db)); \
goto goto_label; \
}
#ifndef MAX
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#endif /* MAX */
#ifndef MIN
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#endif /* MIN */
#define _POSIX_C_SOURCE 200809L
#include <lektor/database.h>
#include <lektor/macro.h>
#include <linux/limits.h>
#include <stdio.h>
#include <string.h>
#define max(a, b) ((a) < (b) ? (b) : (a))
/* Find in in database/open.c */
extern int is_sql_str_invalid(const char *);
......@@ -20,11 +21,7 @@ database_queue_state(sqlite3 *db, struct lkt_queue_state *res)
sqlite3_stmt *stmt = 0;
bool ret = false;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_state: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_step(stmt) != SQLITE_ROW) {
fprintf(stderr, " ! database_queue_state: queue_state has no row.\n");
......@@ -66,12 +63,7 @@ database_queue_current_kara(sqlite3 *db, struct kara_metadata *res)
sqlite3_stmt *stmt = 0;
int code = -1;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_current_kara: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
memset(res, 0, sizeof(struct kara_metadata));
code = sqlite3_step(stmt);
......@@ -116,12 +108,7 @@ queue_add_with_col_like_str(sqlite3 *db, const char *col, const char *val, int p
sqlite3_stmt *stmt = NULL;
snprintf(SQL, 1024, SQL_STMT, col);
if (sqlite3_prepare_v2(db, SQL, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! queue_add_with_col_like_str: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL, error);
if (sqlite3_bind_int(stmt, 1, priority) != SQLITE_OK ||
sqlite3_bind_text(stmt, 2, val, -1, 0) != SQLITE_OK) {
......@@ -178,12 +165,7 @@ queue_insert_with_col_like_str(sqlite3 *db, const char *col, const char *val, in
sqlite3_stmt *stmt = NULL;
snprintf(SQL, 1024, SQL_STMT, col);
if (sqlite3_prepare_v2(db, SQL, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! queue_insert_with_col_like_str: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL, error);
if (sqlite3_bind_int(stmt, 1, pos) != SQLITE_OK ||
sqlite3_bind_text(stmt, 2, val, -1, 0) != SQLITE_OK) {
......@@ -230,11 +212,7 @@ database_queue_add_plt(sqlite3 *db, const char *plt_name, int priority)
bool status = false;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_add_plt: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_bind_int(stmt, 1, priority) != SQLITE_OK ||
sqlite3_bind_text(stmt, 2, plt_name, -1, 0) != SQLITE_OK) {
......@@ -328,8 +306,7 @@ database_queue_add_id(sqlite3 *db, int id, int priority)
bool status = false;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK)
goto error;
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_bind_int(stmt, 1, id) != SQLITE_OK ||
sqlite3_bind_int(stmt, 2, priority) != SQLITE_OK)
......@@ -399,16 +376,12 @@ database_queue_next(sqlite3 *db, char filepath[PATH_MAX])
int code = SQLITE_OK, id;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_next: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
code = sqlite3_step(stmt);
if (code == SQLITE_ROW) {
id = max(1, sqlite3_column_int(stmt, 1));
id = MAX(1, sqlite3_column_int(stmt, 1));
snprintf(SQL_UPDATE, PATH_MAX, "UPDATE queue_state SET current = %d;", id);
if (filepath != NULL)
......@@ -473,16 +446,11 @@ database_queue_prev(sqlite3 *db, char filepath[PATH_MAX])
int code = SQLITE_OK, id;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_prev: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
code = sqlite3_step(stmt);
if (code == SQLITE_ROW) {
id = max(1, sqlite3_column_int(stmt, 1));
id = MAX(1, sqlite3_column_int(stmt, 1));
snprintf(SQL_UPDATE, PATH_MAX, "UPDATE queue_state SET current = %d;", id);
if (filepath != NULL)
......@@ -566,11 +534,7 @@ database_queue_move(sqlite3 *db, int from, int to)
bool ret = false;
int code;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_move: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_bind_int(stmt, 1, to) != SQLITE_OK ||
sqlite3_bind_int(stmt, 2, to) != SQLITE_OK ||
......@@ -603,11 +567,7 @@ database_queue_play(sqlite3 *db, int pos)
bool ret = false;
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_play: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_bind_int(stmt, 1, pos) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_play: Failed to bind start position: %s\n",
......@@ -683,12 +643,7 @@ database_queue_get_current_file(sqlite3 *db, char filepath[PATH_MAX])
if (filepath == NULL)
goto error;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_get_current_file: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
code = sqlite3_step(stmt);
if (code == SQLITE_ROW)
......@@ -804,11 +759,7 @@ database_queue_list_abs(sqlite3 *db, unsigned int from, unsigned int to, void *a
bool ret = false;
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_list: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
if (sqlite3_bind_int(stmt, 1, from) != SQLITE_OK
|| sqlite3_bind_int(stmt, 2, to) != SQLITE_OK) {
......@@ -866,11 +817,7 @@ database_queue_list_from(sqlite3 *db, unsigned int count, void *args,
snprintf(SQL_STMT, stmt_len, SQL_TEMPLATE, count);
if (sqlite3_prepare_v2(db, SQL_STMT, -1, &stmt, 0) != SQLITE_OK) {
fprintf(stderr, " ! database_queue_list: Failed to prepare statement: %s\n",
sqlite3_errmsg(db));
goto error;
}
SQLITE_PREPARE(db, stmt, SQL_STMT, error);
for (;;) {
code = sqlite3_step(stmt);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter