From 0587faf3525afac402dab5a868a30396012d14c5 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 15 Jan 2017 14:46:06 +0100 Subject: [PATCH] Search series --- db_series.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ extern.h | 1 + main.c | 20 ++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/db_series.c b/db_series.c index c2214a3..fc654cf 100644 --- a/db_series.c +++ b/db_series.c @@ -36,6 +36,11 @@ "FROM authors, books_authors_link, books_series_link "\ "WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id "\ "AND books_series_link.series IN ( SELECT id FROM series " +#define STMT_SERIES_SEARCH "SELECT series.id, series.name FROM series WHERE " +#define STMT_SEARCH_TERM " series.sort like ? " +#define STMT_BOOL_AND " AND " +#define STMT_BOOL_OR " OR " +#define STMT_SEARCH_ORDER " ORDER BY series.sort" enum stmt { STMT_SERIES_ID_ASC, @@ -334,3 +339,51 @@ db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, const char return count; } + +int +db_series_search(struct kreq *r, int limit, Series** series, const char **terms, int num, int all) +{ + if (limit < 0) { + return 0; + } + struct ksqlstmt *stmt; + int i, count = 0; + + size_t sqlsz = sizeof(STMT_SERIES_SEARCH) + + num * sizeof(STMT_SEARCH_TERM) + + (num-1) * sizeof(STMT_BOOL_AND) + + sizeof(STMT_SEARCH_ORDER); + char *sql = kcalloc(sqlsz, sizeof(char)); + strlcat(sql, STMT_SERIES_SEARCH, sqlsz); + for (i=0; iarg, &stmt, sql, 0); + for (i=0; iid = ksql_stmt_int(stmt, 0); + series[count]->name = kstrdup(ksql_stmt_str(stmt, 1)); + } + count++; + } + ksql_stmt_free(stmt); + + return count; +} + diff --git a/extern.h b/extern.h index fc7a2b0..a77a15d 100644 --- a/extern.h +++ b/extern.h @@ -171,6 +171,7 @@ int db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, co void db_author_free(Author *p); void db_author_adv_free(AuthorAdv *p); +int db_series_search(struct kreq *r, int limit, Series** books, const char **terms, int num, int all); void db_series_unfill(Series *p); int db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, const char *order, int limit, int offset); diff --git a/main.c b/main.c index ba99131..6704d6e 100644 --- a/main.c +++ b/main.c @@ -391,6 +391,7 @@ sendseries(struct kreq *r) debug(r, "sendseries"); int page = initpage(r); int per = initperpage(r); + size_t termz = inittermz(r); const char* sort = initsort(r); const char* order = initorder(r); int64_t id = idfrompath(r); @@ -423,6 +424,25 @@ sendseries(struct kreq *r) kjson_array_close(&req); } kjson_obj_close(&req); + } else if (termz > 0) { + const char **terms = kcalloc(termz, sizeof(char *)); + initterms(r, terms); + Series **series = kcalloc(per, sizeof(Series)); + int c = db_series_search(r, per, series, terms, termz, 0); + kjson_obj_open(&req); + kjson_putintp(&req, "count", c); + kjson_arrayp_open(&req, "series"); + while (i < per && i < c) { + kjson_obj_open(&req); + kjson_putintp(&req, "id", series[i]->id); + kjson_putstringp(&req, "name", series[i]->name); + kjson_obj_close(&req); + i++; + } + kjson_array_close(&req); + kjson_obj_close(&req); + free(series); + free(terms); } else { kjson_array_open(&req); SeriesAdv **series = kcalloc(per, sizeof(SeriesAdv));