From 80e64c2542f72e6bdfc7409f195127de9b146479 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 4 May 2020 11:34:05 +0200
Subject: [PATCH] Add an init callback to search functions

---
 inc/lektor/database.h |  7 ++++---
 src/database/find.c   | 16 ++++++++++++++--
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index 7494f9a2..5e442b27 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 9956610c..e4d405d4 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);
-- 
GitLab