Book search query

This commit is contained in:
Meutel 2017-01-14 14:36:01 +01:00
parent f2b45370b5
commit f72ae08370
3 changed files with 45 additions and 3 deletions

View File

@ -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;
}

View File

@ -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);

41
main.c
View File

@ -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; j<r->fieldsz; 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; j<r->fieldsz; 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);