diff --git a/inc/common/macro.h b/inc/common/macro.h index c3175cd5a2476939b30d84436a5728675346c6c4..016c51a38b824be76a5c3b80bc7e2a134a47cbd6 100644 --- a/inc/common/macro.h +++ b/inc/common/macro.h @@ -146,7 +146,8 @@ typedef volatile enum { #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", \ - text, pos, sqlite3_errmsg((sqlite3 *) db)); \ + (const char *) text, pos, \ + sqlite3_errmsg((sqlite3 *) db)); \ goto error; \ } diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 1f6f802a4431227d6560ed9aa771f36656b170f7..ea396b229b66930e474fd911e2159528e9892c11 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -55,7 +55,7 @@ void database_update_touch (volatile sqlite3 *db, int id); /* Control the content of the queue. */ bool database_queue_add_uri(volatile sqlite3 *db, struct lkt_uri *uri, int priority); -bool database_queue_add_id(volatile sqlite3 *db, int id, int priority); +bool database_queue_add_id (volatile sqlite3 *db, int id, int priority); bool database_queue_del_id (volatile sqlite3 *db, int id); bool database_queue_del_pos(volatile sqlite3 *db, int pos); bool database_queue_clear (volatile sqlite3 *db); @@ -99,12 +99,14 @@ struct lkt_search { long continuation; /* The continuation state of the client */ int msg_count; /* How much messages we can send */ - const char *name; /* Stickers and playlists */ + + const char *st_name; /* Stickers and playlists */ int st_value; /* The value of a sticker */ int st_uri; /* URI of a sticker */ char st_op; /* Comparaison operator for stickers */ char *st_type; /* Type of sticker */ - char *ka_rgx; /* Regex for the content of the selected column */ + + struct lkt_uri ka_uri; /* Search by uri, depract the use of ka_rgx */ }; typedef bool (*lkt_search_init_add_func)(volatile sqlite3 *, struct lkt_uri *, int); diff --git a/src/commands.c b/src/commands.c index 5a40fd84f8d6fbd6cc7baf00c463f2523e8ffa41..7250628fbcc7eb9736744ee1da43d8f6808d4eb2 100644 --- a/src/commands.c +++ b/src/commands.c @@ -435,10 +435,9 @@ lkt_callback_send_row_v2(struct lkt_state *srv, size_t c, int id, int id_len, co } bool -command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], long continuation, - bool(*init)(volatile sqlite3 *, struct lkt_search *)) +command_find(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX], + long continuation, bool(*init)(volatile sqlite3 *, struct lkt_search *)) { - char rgx[PATH_MAX], *mpd_tag; int count; struct lkt_search search = { .srv = srv, @@ -446,44 +445,13 @@ command_find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MA .continuation = continuation, .msg_count = lkt_remaining_msg(srv, c) - 3, /* Reserve slots for OK/ACK and continue: */ .call = (void(*)(void)) lkt_callback_send_row_v2, - .ka_rgx = rgx, + .ka_uri = {0}, }; /* Check args */ - RETURN_UNLESS(cmd_args && cmd_args[0], "Invalid argument", false); - - /* Select the right column */ - mpd_tag = cmd_args[0]; - if (STR_MATCH("query", mpd_tag)) - search.name = LKT_DATABASE_KARA_COLUMNT_ANY; - - else if (STR_MATCH("author", mpd_tag)) - search.name = LKT_DATABASE_NAME_KAUTHOR; - - else if (STR_MATCH("category", mpd_tag)) - search.name = LKT_DATABASE_NAME_KCAT; - - else if (STR_MATCH("type", mpd_tag)) - search.name = LKT_DATABASE_NAME_KTYPE; - - else if (STR_MATCH("lang", mpd_tag)) - search.name = LKT_DATABASE_NAME_KLANG; - - else if (STR_MATCH("id", mpd_tag)) - search.name = LKT_DATABASE_NAME_KID; - - else - return false; - - /* Get the regex */ - RETURN_UNLESS(cmd_args[1], "No regex", false); - memset(rgx, 0, PATH_MAX * sizeof(char)); - - for (int i = 1; cmd_args[i]; ++i) { - strncat(rgx, cmd_args[i], PATH_MAX - 1); - if (cmd_args[i + 1]) - strncat(rgx, " ", PATH_MAX - 1); - } + RETURN_UNLESS(args[0], "Invalid argument", false); + RETURN_UNLESS(lkt_uri_from_list(&search.ka_uri, args), + "Failed to create the uri from the cmd", false); /* Make the search langand do the right action */ RETURN_UNLESS(init(srv->db, &search), "Failed to init search", false); @@ -817,8 +785,8 @@ command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS /* list {type} {uri} {name} command */ else if (argv[0] != NULL && argv[1] != NULL && argv[2] != NULL && argv[3] == NULL) { - callback.name = argv[2]; - callback.st_uri = atoi(argv[1]); + callback.st_name = argv[2]; + callback.st_uri = atoi(argv[1]); if (!database_search_sticker_init(srv->db, &callback)) return false; } @@ -827,9 +795,9 @@ command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS else if (argv[0] != NULL && argv[1] != NULL && argv[2] != NULL && argv[3] != NULL && argv[4] != NULL && argv[5] == NULL) { - callback.name = argv[2]; - callback.st_uri = atoi(argv[1]); - callback.st_op = argv[3][0]; + callback.st_name = argv[2]; + callback.st_uri = atoi(argv[1]); + callback.st_op = argv[3][0]; callback.st_value = atoi(argv[4]); if (!database_search_sticker_init(srv->db, &callback)) return false; diff --git a/src/database/find.c b/src/database/find.c index ef8f475ae41df4990e29f008833d2bedeb8db70a..6d1024feff341213ab59f3884b39d8b70faad964 100644 --- a/src/database/find.c +++ b/src/database/find.c @@ -23,9 +23,9 @@ database_search_database_init(volatile sqlite3 *db, struct lkt_search *ret) /* Search part */ safe_snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT, SQL_STMT_TEMPLATE, - ret->name, ret->msg_count, ret->continuation); + ret->ka_uri.column_name, ret->msg_count, ret->continuation); SQLITE_PREPARE(db, ret->stmt, SQL_STMT, error); - SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->ka_rgx, error); + SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->ka_uri.value, error); ret->db = db; return true; error: @@ -63,11 +63,11 @@ database_search_sticker_init(volatile sqlite3 *db, struct lkt_search *ret) sprintf(SQL + strlen(SQL), " AND sts.value %s %d", ret->st_op == '>' ? ">=" : ret->st_op == '<' ? "<=" : "=", ret->st_value); - strcat(SQL, !ret->name ? ";" : " AND name = ?;"); + strcat(SQL, !ret->st_name ? ";" : " AND name = ?;"); SQLITE_PREPARE(db, ret->stmt, SQL, error); - if (ret->name) - SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->name, error); + if (ret->st_name) + SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->st_name, error); return true; error: sqlite3_finalize(ret->stmt); @@ -94,9 +94,9 @@ database_search_queue_init(volatile sqlite3 *db, struct lkt_search *ret) /* Search part */ safe_snprintf(SQL_STMT, LKT_MAX_SQLITE_STATEMENT, SQL_STMT_TEMPLATE, - ret->name, ret->msg_count, ret->continuation); + ret->ka_uri.column_name, ret->msg_count, ret->continuation); SQLITE_PREPARE(db, ret->stmt, SQL_STMT, error); - SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->ka_rgx, error); + SQLITE_BIND_TEXT(db, ret->stmt, 1, ret->ka_uri.value, error); ret->db = db; return true; error: