Search series
This commit is contained in:
parent
114e075d6d
commit
0587faf352
53
db_series.c
53
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; 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) {
|
||||
series[count] = kcalloc(1, sizeof(Series));
|
||||
series[count]->id = ksql_stmt_int(stmt, 0);
|
||||
series[count]->name = kstrdup(ksql_stmt_str(stmt, 1));
|
||||
}
|
||||
count++;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
1
extern.h
1
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);
|
||||
|
20
main.c
20
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));
|
||||
|
Loading…
Reference in New Issue
Block a user