From 48910f40aee590877726904788bb052b002370d2 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 15 Jan 2017 15:44:44 +0100 Subject: [PATCH] DB search authors --- db_author.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ extern.h | 3 ++- main.c | 20 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/db_author.c b/db_author.c index 9e8ac9e..61b4fe4 100644 --- a/db_author.c +++ b/db_author.c @@ -31,6 +31,11 @@ #define STMT_PAGE " LIMIT ? OFFSET ?" #define STMT_AUTHORS0 "SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link "\ "WHERE authors.id = books_authors_link.author GROUP BY author " +#define STMT_AUTHORS_SEARCH "SELECT id, name FROM authors WHERE " +#define STMT_SEARCH_TERM " sort like ? " +#define STMT_BOOL_AND " AND " +#define STMT_BOOL_OR " OR " +#define STMT_SEARCH_ORDER " ORDER BY sort" enum stmt { STMT_AUTHORS_ID_ASC, @@ -198,3 +203,50 @@ db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, const cha return count; } +int +db_authors_search(struct kreq *r, int limit, Author** authors, const char **terms, int num, int all) +{ + if (limit < 0) { + return 0; + } + struct ksqlstmt *stmt; + int i, count = 0; + + size_t sqlsz = sizeof(STMT_AUTHORS_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_AUTHORS_SEARCH, sqlsz); + for (i=0; iarg, &stmt, sql, 0); + for (i=0; iid = ksql_stmt_int(stmt, 0); + authors[count]->name = kstrdup(ksql_stmt_str(stmt, 1)); + } + count++; + } + ksql_stmt_free(stmt); + + return count; +} + diff --git a/extern.h b/extern.h index a77a15d..e5a261f 100644 --- a/extern.h +++ b/extern.h @@ -164,6 +164,7 @@ void db_book_fill(Book *book, struct ksqlstmt *stmt); struct bookauth * db_load_books_authors(BookAdv **books, int count, struct ksqlstmt *stmt); void db_assign_book_authors(struct bookauth *list); +int db_authors_search(struct kreq *r, int limit, Author** authors, const char **terms, int num, int all); void db_author_unfill(Author *p); AuthorFull *db_author_load(struct kreq *r, int64_t id); int db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, const char *order, @@ -171,7 +172,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); +int db_series_search(struct kreq *r, int limit, Series** series, 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 6704d6e..413c948 100644 --- a/main.c +++ b/main.c @@ -340,6 +340,7 @@ sendauthors(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); http_open(r, KHTTP_200); @@ -361,6 +362,25 @@ sendauthors(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); + Author **authors = kcalloc(per, sizeof(Author)); + int c = db_authors_search(r, per, authors, terms, termz, 0); + kjson_obj_open(&req); + kjson_putintp(&req, "count", c); + kjson_arrayp_open(&req, "authors"); + while (i < per && i < c) { + kjson_obj_open(&req); + kjson_putintp(&req, "id", authors[i]->id); + kjson_putstringp(&req, "name", authors[i]->name); + kjson_obj_close(&req); + i++; + } + kjson_array_close(&req); + kjson_obj_close(&req); + free(authors); + free(terms); } else { kjson_array_open(&req); AuthorAdv **authors = kcalloc(per, sizeof(AuthorAdv));