diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 7494f9a21e70a520a8182a5e5fbafb017088cf79..5e442b27658cc5c1399bc60e003dd8b0189ba6b4 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -91,11 +91,12 @@ struct lkt_search { lkt_search_playlist, lkt_search_queue, } type; - void (*call)(void); /* Will be casted. */ + bool (*init)(volatile sqlite3 *); /* Called at the end of the init phase */ + void (*call)(void); /* Called during the iter phase, casted */ struct lkt_state *srv; size_t c; - long continuation; /* Is this a continuation from a previous command? */ - int msg_count; /* How much messages we can send. */ + long continuation; /* The continuation state of the client */ + int msg_count; /* How much messages we can send */ }; typedef bool (*lkt_search_database_func)(struct lkt_state *srv, size_t c, int id, int id_len, diff --git a/src/database/find.c b/src/database/find.c index 9956610cdab7b533234b3cfa46a2280f3910ecf0..e4d405d43308ccf1a765046d91adcc71040c53dd 100644 --- a/src/database/find.c +++ b/src/database/find.c @@ -19,13 +19,19 @@ database_search_database_init(volatile sqlite3 *db, char *col_name, char *rgx, s "FROM content LIMIT %d OFFSET %d;"; char SQL_STMT[LKT_MAX_SQLITE_STATEMENT]; + /* Init */ + if (ret->init && ! ret->init(db)) { + LOG_ERROR_SCT("DB", "%s", "Failed to init search query, called to init func failed"); + return false; + } + + /* Search part */ snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT - 1, SQL_STMT_TEMPLATE, col_name, ret->msg_count, ret->continuation); SQL_STMT[LKT_MAX_SQLITE_STATEMENT - 1] = 0; SQLITE_PREPARE(db, ret->stmt, SQL_STMT, error); SQLITE_BIND_TEXT(db, ret->stmt, 1, rgx, error); ret->db = db; - /* Assign the callback. */ return true; error: sqlite3_finalize(ret->stmt); @@ -47,13 +53,19 @@ database_search_queue_init(volatile sqlite3 *db, char *col_name, char *rgx, stru "FROM content LIMIT %d OFFSET %d;"; char SQL_STMT[LKT_MAX_SQLITE_STATEMENT]; + /* Init */ + if (ret->init && ! ret->init(db)) { + LOG_ERROR_SCT("DB", "%s", "Failed to init search query, called to init func failed"); + return false; + } + + /* Search part */ snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT - 1, SQL_STMT_TEMPLATE, col_name, ret->msg_count, ret->continuation); SQL_STMT[LKT_MAX_SQLITE_STATEMENT - 1] = 0; SQLITE_PREPARE(db, ret->stmt, SQL_STMT, error); SQLITE_BIND_TEXT(db, ret->stmt, 1, rgx, error); ret->db = db; - /* Assign the callback. */ return true; error: sqlite3_finalize(ret->stmt);