Book search
This commit is contained in:
parent
dca5b43600
commit
f2b45370b5
52
db_book.c
52
db_book.c
@ -37,6 +37,11 @@
|
|||||||
#define STMT_BOOKS_AUTHORS0 "SELECT authors.id, authors.name, books_authors_link.book as book "\
|
#define STMT_BOOKS_AUTHORS0 "SELECT authors.id, authors.name, books_authors_link.book as book "\
|
||||||
"FROM authors, books_authors_link WHERE books_authors_link.author = authors.id "\
|
"FROM authors, books_authors_link WHERE books_authors_link.author = authors.id "\
|
||||||
"AND books_authors_link.book IN ( SELECT id FROM books "
|
"AND books_authors_link.book IN ( SELECT id FROM books "
|
||||||
|
#define STMT_WHERE " WHERE "
|
||||||
|
#define STMT_SEARCH_TERM " books.sort like ? "
|
||||||
|
#define STMT_BOOL_AND " AND "
|
||||||
|
#define STMT_BOOL_OR " OR "
|
||||||
|
#define STMT_SEARCH_ORDER " ORDER BY books.sort"
|
||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
STMT_BOOKS_COUNT,
|
STMT_BOOKS_COUNT,
|
||||||
@ -521,3 +526,50 @@ db_book_load(struct kreq *r, int64_t id)
|
|||||||
return book;
|
return book;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
db_books_search(struct kreq *r, int limit, Book** books, char **terms, int num, int all)
|
||||||
|
{
|
||||||
|
if (limit < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
struct ksqlstmt *stmt;
|
||||||
|
int i, count = 0;
|
||||||
|
|
||||||
|
size_t sqlsz = sizeof(STMT_BOOKS0)
|
||||||
|
+ sizeof(STMT_WHERE)
|
||||||
|
+ num * sizeof(STMT_SEARCH_TERM)
|
||||||
|
+ (num-1) * sizeof(STMT_BOOL_AND)
|
||||||
|
+ sizeof(STMT_SEARCH_ORDER);
|
||||||
|
char *sql = kcalloc(sqlsz, sizeof(char));
|
||||||
|
strlcat(sql, STMT_BOOKS0, sqlsz);
|
||||||
|
strlcat(sql, STMT_WHERE, 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);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
books[count] = kcalloc(1, sizeof(Book));
|
||||||
|
db_book_fill(books[count],stmt);
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
1
extern.h
1
extern.h
@ -145,6 +145,7 @@ struct bookdata {
|
|||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
|
int db_books_search(struct kreq *r, int limit, Book** books, char **terms, int num, int all);
|
||||||
BookFull *db_book_load(struct kreq *r, int64_t id);
|
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_load(struct kreq *r, BookAdv **books, const char *sort, const char *order, int limit, int offset);
|
||||||
int db_books_count(struct kreq *r);
|
int db_books_count(struct kreq *r);
|
||||||
|
Loading…
Reference in New Issue
Block a user