From daa46f15418ec3d7e5b6a3ae60529d3d39987144 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 4 May 2020 13:58:14 +0200
Subject: [PATCH] Use the init function for search add and search insert
 commands

---
 inc/lektor/database.h | 19 +++++++++++--------
 src/commands.c        |  9 +++++++--
 src/database/find.c   | 12 ++++++++----
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index 58e91d60..c749eaf4 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -88,23 +88,26 @@ struct lkt_search {
         lkt_add_queue,
     } type;
 
-    bool (*init)(volatile sqlite3 *);   /* Called at the end of the init phase          */
-    void (*call)(void);                 /* Called during the iter phase, casted         */
+    void (*init)(void);     /* 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;                  /* The continuation state of the client         */
-    int msg_count;                      /* How much messages we can send                */
-    const char *name;                   /* Stickers and playlists                       */
+    long continuation;      /* The continuation state of the client         */
+    int msg_count;          /* How much messages we can send                */
+    const char *name;       /* Stickers and playlists                       */
     union {
-        int qu_priority;                /* Can be used... where the priority is needed! */
-        int st_value;                   /* The value of a sticker                       */
+        int qu_priority;    /* Can be used... where the priority is needed! */
+        int st_value;       /* The value of a sticker                       */
     };
-    int st_uri;                         /* URI of a sticker                             */
+    int st_uri;             /* URI of a sticker                             */
+    struct lkt_uri *qu_uri; /* Kara uri for a queue init function           */
 };
 
+typedef bool (*lkt_search_init_add_func)(volatile sqlite3 *, struct lkt_uri *, int);
 typedef bool (*lkt_search_database_func)(struct lkt_state *srv, size_t c, int id, int id_len, const char *row);
+typedef bool (*lkt_search_queue_func)   (struct lkt_state *srv, size_t c, int id, int id_len, const char *row);
 typedef bool (*lkt_search_sticker_func) (struct lkt_state *srv, size_t c, const char *sticker, const char *type, int uri, int value);
 
 bool database_search_database_init(volatile sqlite3 *db, char *col_name, char *rgx,  struct lkt_search *ret);
diff --git a/src/commands.c b/src/commands.c
index 8532e2d9..17503ee4 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -420,10 +420,16 @@ __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], lo
         break;
     case LKT_FND_ACT_ENQUEUE:
         search.call = (void(*)(void)) lkt_callback_send_row_v2;
+        search.init = (void(*)(void)) database_queue_add_uri;
+        search.type = lkt_add_queue;
+        search.qu_priority = 5;
         srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
         break;
     case LKT_FND_ACT_ADD:
         search.call = (void(*)(void)) lkt_callback_send_row_v2;
+        search.init = (void(*)(void)) database_queue_add_uri;
+        search.qu_priority = 1;
+        search.type = lkt_add_queue;
         srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
         break;
     default:
@@ -433,8 +439,7 @@ __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], lo
     /* Select the right column */
 
     mpd_tag = cmd_args[0];
-
-    if (!strcasecmp("any", mpd_tag) || !strcasecmp("all", mpd_tag) || !strcasecmp("a", mpd_tag))
+    if (!strcasecmp("any", mpd_tag) || !strcasecmp("all", mpd_tag) || !strcasecmp("query", mpd_tag))
         col_name = LKT_DATABASE_KARA_COLUMNT_ANY;
     else if (!strcasecmp("author", mpd_tag))
         col_name = LKT_DATABASE_NAME_KAUTHOR;
diff --git a/src/database/find.c b/src/database/find.c
index 5807a541..cc20524e 100644
--- a/src/database/find.c
+++ b/src/database/find.c
@@ -11,16 +11,16 @@
 static inline bool
 __init_search(volatile sqlite3 *db, struct lkt_search *ret)
 {
-    UNUSED(db);
+    bool sta = false;
     switch (ret->type) {
     case lkt_add_queue:
-        /* Use the add uri */
-        return false;
+        sta = ((lkt_search_init_add_func) ret->init)(db, ret->qu_uri, ret->qu_priority);
+        ret->type = lkt_search_queue;
     default:
         LOG_WARN_SCT("DB", "Init for type %d is not implemented", ret->type);
         return false;
     }
-    return false;
+    return sta;
 }
 
 bool
@@ -145,6 +145,10 @@ database_search_iter(struct lkt_search *item)
         id_len = sqlite3_column_int(item->stmt, 2);
         return ((lkt_search_database_func) item->call)(item->srv, item->c, id, id_len, sql_row);
     case lkt_search_queue:
+        id = sqlite3_column_int(item->stmt, 0);
+        sql_row = (const char *) sqlite3_column_text(item->stmt, 1);
+        id_len = sqlite3_column_int(item->stmt, 2);
+        return ((lkt_search_queue_func) item->call)(item->srv, item->c, id, id_len, sql_row);
     case lkt_search_playlist:
     default:
         LOG_WARN_SCT("DB", "Search operation %d is not implemented", item->type);
-- 
GitLab