From f72ae0837052001a7bab164a0a9c694094bf2c28 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 14 Jan 2017 14:36:01 +0100 Subject: [PATCH] Book search query --- db_book.c | 3 +-- extern.h | 4 +++- main.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/db_book.c b/db_book.c index a1a9eff..fab5053 100644 --- a/db_book.c +++ b/db_book.c @@ -527,7 +527,7 @@ db_book_load(struct kreq *r, int64_t id) } int -db_books_search(struct kreq *r, int limit, Book** books, char **terms, int num, int all) +db_books_search(struct kreq *r, int limit, Book** books, const char **terms, int num, int all) { if (limit < 0) { return 0; @@ -570,6 +570,5 @@ db_books_search(struct kreq *r, int limit, Book** books, char **terms, int num, } ksql_stmt_free(stmt); - return count; } diff --git a/extern.h b/extern.h index df1a42d..c80b818 100644 --- a/extern.h +++ b/extern.h @@ -145,7 +145,9 @@ struct bookdata { __BEGIN_DECLS -int db_books_search(struct kreq *r, int limit, Book** books, char **terms, int num, int all); +int kvalid_term(struct kpair *p); + +int db_books_search(struct kreq *r, int limit, Book** books, const char **terms, int num, int all); BookFull *db_book_load(struct kreq *r, int64_t id); int db_books_load(struct kreq *r, BookAdv **books, const char *sort, const char *order, int limit, int offset); int db_books_count(struct kreq *r); diff --git a/main.c b/main.c index 45c5c40..4cbdceb 100644 --- a/main.c +++ b/main.c @@ -46,6 +46,7 @@ enum key { KEY_PERPAGE, KEY_SORT, KEY_ORDER, + KEY_TERM, KEY__MAX }; @@ -61,6 +62,7 @@ static const struct kvalid keys[KEY__MAX] = { { kvalid_int, "perpage" }, /* KEY_PERPAGE */ { kvalid_string, "sort" }, /* KEY_SORT */ { kvalid_string, "order" }, /* KEY_ORDER */ + { kvalid_stringne, "term" }, /* KEY_TERM */ }; /* @@ -223,6 +225,29 @@ initpage(struct kreq *r) return 1; } +static size_t +inittermz(struct kreq *r) +{ + size_t termz = 0; + for (size_t j=0; jfieldsz; j++) { + struct kpair field = r->fields[j]; + if (KPAIR_VALID == field.state && 0 == strcmp(keys[KEY_TERM].name, field.key)) + termz++; + } + return termz; +} + +static void +initterms(struct kreq *r, const char **terms) +{ + int i = 0; + for (size_t j=0; jfieldsz; j++) { + struct kpair field = r->fields[j]; + if (KPAIR_VALID == field.state && 0 == strcmp(keys[KEY_TERM].name, field.key)) + terms[i++] = field.parsed.s; + } +} + static int64_t idfrompath(struct kreq *r) { @@ -251,8 +276,10 @@ sendbooks(struct kreq *r) int per = initperpage(r); const char* sort = initsort(r); const char* order = initorder(r); + size_t termz = inittermz(r); int64_t id = idfrompath(r); + if (id >= 0) { b = db_book_load(r, id); } @@ -267,6 +294,19 @@ sendbooks(struct kreq *r) putbookdata(&req, b); kjson_obj_close(&req); db_book_full_free(b); + } if (termz > 0) { + kjson_array_open(&req); + const char **terms = kcalloc(termz, sizeof(char *)); + initterms(r, terms); + Book **books = kcalloc(10, sizeof(Book)); + int c = db_books_search(r, 10, books, terms, termz, 0); + while (i < 10 && i < c) { + putbook(&req, books[i]); + kjson_obj_close(&req); + i++; + } + // TODO free + kjson_array_close(&req); } else { kjson_array_open(&req); BookAdv **books = kcalloc(per, sizeof(BookAdv)); @@ -276,6 +316,7 @@ sendbooks(struct kreq *r) i++; } free(books); + kjson_array_close(&req); } kjson_close(&req);