DB search authors
This commit is contained in:
parent
d235672bfe
commit
48910f40ae
52
db_author.c
52
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; i<num; i++) {
|
||||
strlcat(sql, STMT_SEARCH_TERM, sqlsz);
|
||||
if (i<num-1 && all)
|
||||
strlcat(sql, STMT_BOOL_AND, sqlsz);
|
||||
if (i<num-1 && !all)
|
||||
strlcat(sql, STMT_BOOL_OR, sqlsz);
|
||||
}
|
||||
strlcat(sql, STMT_SEARCH_ORDER, sqlsz);
|
||||
|
||||
debug(r, "SQL: %s", sql);
|
||||
ksql_stmt_alloc(r->arg, &stmt, sql, 0);
|
||||
for (i=0; i<num; i++) {
|
||||
size_t sz = 3 + strlen(terms[i]);
|
||||
char *term = kcalloc(sz, sizeof(char));
|
||||
strlcat(term, "\%", sz);
|
||||
strlcat(term, terms[i], sz);
|
||||
strlcat(term, "\%", sz);
|
||||
ksql_bind_str(stmt, i, term);
|
||||
}
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
if (count < limit) {
|
||||
authors[count] = kcalloc(1, sizeof(Author));
|
||||
authors[count]->id = ksql_stmt_int(stmt, 0);
|
||||
authors[count]->name = kstrdup(ksql_stmt_str(stmt, 1));
|
||||
}
|
||||
count++;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
3
extern.h
3
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);
|
||||
|
20
main.c
20
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));
|
||||
|
Loading…
Reference in New Issue
Block a user