Paginate books
This commit is contained in:
parent
8a6a0ba3ee
commit
927997d667
12
db.c
12
db.c
@ -38,10 +38,10 @@ enum stmt {
|
||||
static const char *const stmts[STMT__MAX] = {
|
||||
/* STMT_BOOK */
|
||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book LEFT OUTER JOIN series ON series.id = books_series_link.series WHERE books.id = ?",
|
||||
/* STMT_BOOKS */ //TODO offset
|
||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY id LIMIT ?",
|
||||
/* STMT_BOOKS */
|
||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY id LIMIT ? OFFSET ?",
|
||||
/* STMT_BOOKS_AUTHORS */
|
||||
"SELECT authors.id, authors.name, books_authors_link.book as book FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ( SELECT id FROM books ORDER BY id LIMIT ? )",
|
||||
"SELECT authors.id, authors.name, books_authors_link.book as book FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)",
|
||||
};
|
||||
|
||||
/*
|
||||
@ -116,9 +116,9 @@ db_book_fill(Book *book, struct ksqlstmt *stmt)
|
||||
}
|
||||
|
||||
int
|
||||
db_books_load(struct kreq *r, BookAdv **books, int limit)
|
||||
db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
|
||||
{
|
||||
if (limit < 0) {
|
||||
if (limit < 0 || offset < 0) {
|
||||
return 0;
|
||||
}
|
||||
struct ksqlstmt *stmt;
|
||||
@ -131,6 +131,7 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
|
||||
stmts[STMT_BOOKS],
|
||||
STMT_BOOKS);
|
||||
ksql_bind_int(stmt, 0, limit);
|
||||
ksql_bind_int(stmt, 1, offset);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
books[count] = kcalloc(1, sizeof(BookAdv));
|
||||
db_book_fill(&books[count]->b,stmt);
|
||||
@ -143,6 +144,7 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
|
||||
stmts[STMT_BOOKS_AUTHORS],
|
||||
STMT_BOOKS_AUTHORS);
|
||||
ksql_bind_int(stmt, 0, limit);
|
||||
ksql_bind_int(stmt, 1, offset);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
// add author to list, link related book and increment asize
|
||||
item = kcalloc(1, sizeof(struct bookauth));
|
||||
|
2
extern.h
2
extern.h
@ -60,7 +60,7 @@ typedef struct
|
||||
__BEGIN_DECLS
|
||||
|
||||
Book *db_book_load(struct kreq *r, int64_t id);
|
||||
int db_books_load(struct kreq *r, BookAdv **books, int limit);
|
||||
int db_books_load(struct kreq *r, BookAdv **books, int limit, int offset);
|
||||
void db_book_free(Book *p);
|
||||
void db_book_adv_free(BookAdv *p);
|
||||
|
||||
|
14
main.c
14
main.c
@ -43,6 +43,8 @@ enum page {
|
||||
|
||||
enum key {
|
||||
KEY_ID,
|
||||
KEY_PAGE,
|
||||
KEY_PERPAGE,
|
||||
KEY__MAX
|
||||
};
|
||||
|
||||
@ -55,6 +57,8 @@ static const char *const pages[PAGE__MAX] = {
|
||||
|
||||
static const struct kvalid keys[KEY__MAX] = {
|
||||
{ kvalid_int, "id" }, /* KEY_ID */
|
||||
{ kvalid_int, "page" }, /* KEY_PAGE */
|
||||
{ kvalid_int, "perpage" }, /* KEY_PERPAGE */
|
||||
};
|
||||
|
||||
/*
|
||||
@ -130,8 +134,12 @@ sendbooks(struct kreq *r)
|
||||
const char *errid;
|
||||
int64_t id = -1;
|
||||
Book *b = NULL;
|
||||
int res, i = 0;
|
||||
int res, i = 0, page = 1, per = 20;
|
||||
|
||||
if (NULL != r->fieldmap[KEY_PAGE])
|
||||
page = r->fieldmap[KEY_PAGE]->parsed.i;
|
||||
if (NULL != r->fieldmap[KEY_PERPAGE])
|
||||
per = r->fieldmap[KEY_PERPAGE]->parsed.i;
|
||||
if (NULL != r->fieldmap[KEY_ID])
|
||||
id = r->fieldmap[KEY_ID]->parsed.i;
|
||||
if (r->path[0] != '\0')
|
||||
@ -149,8 +157,8 @@ sendbooks(struct kreq *r)
|
||||
db_book_free(b);
|
||||
} else {
|
||||
kjson_array_open(&req);
|
||||
BookAdv **books = kcalloc(200, sizeof(BookAdv));
|
||||
res = db_books_load(r, books, 200);
|
||||
BookAdv **books = kcalloc(per, sizeof(BookAdv));
|
||||
res = db_books_load(r, books, per, per * (page-1));
|
||||
while (i < res) {
|
||||
Book b = books[i]->b;
|
||||
putbook(&req, &b);
|
||||
|
Loading…
Reference in New Issue
Block a user