Book count

This commit is contained in:
Meutel 2016-12-31 10:52:48 +01:00
parent a11f314224
commit 3779d6ff99
4 changed files with 25 additions and 4 deletions

21
db.c
View File

@ -31,6 +31,7 @@
enum stmt { enum stmt {
STMT_BOOK, STMT_BOOK,
STMT_BOOKS, STMT_BOOKS,
STMT_BOOKS_COUNT,
STMT_BOOKS_AUTHORS, STMT_BOOKS_AUTHORS,
STMT__MAX STMT__MAX
}; };
@ -40,6 +41,8 @@ static const char *const stmts[STMT__MAX] = {
"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 = ?", "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 */ /* 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 ?", "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_COUNT */
"SELECT count(id) FROM books",
/* STMT_BOOKS_AUTHORS */ /* 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 ? OFFSET ?)", "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 ?)",
}; };
@ -115,6 +118,24 @@ db_book_fill(Book *book, struct ksqlstmt *stmt)
} }
} }
int
db_books_count(struct kreq *r)
{
int count;
struct ksqlstmt *stmt;
ksql_stmt_alloc(r->arg, &stmt,
stmts[STMT_BOOKS_COUNT],
STMT_BOOKS_COUNT);
if (KSQL_ROW != ksql_stmt_step(stmt)) {
ksql_stmt_free(stmt);
return(0);
}
count = ksql_stmt_int(stmt, 0);
ksql_stmt_free(stmt);
return(count);
}
int int
db_books_load(struct kreq *r, BookAdv **books, int limit, int offset) db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
{ {

View File

@ -61,6 +61,7 @@ __BEGIN_DECLS
Book *db_book_load(struct kreq *r, int64_t id); Book *db_book_load(struct kreq *r, int64_t id);
int db_books_load(struct kreq *r, BookAdv **books, int limit, int offset); int db_books_load(struct kreq *r, BookAdv **books, int limit, int offset);
int db_books_count(struct kreq *r);
void db_book_free(Book *p); void db_book_free(Book *p);
void db_book_adv_free(BookAdv *p); void db_book_adv_free(BookAdv *p);

View File

@ -37,12 +37,11 @@
} }
function indexSuccess(resp) { function indexSuccess(resp) {
app.booksCount = 9999; //TODO app.booksCount = resp.count;
} }
function loadIndex() { function loadIndex() {
// TODO load books count sendQuery('cgi-bin/bouquins/index', stdError, indexSuccess);
sendQuery('cgi-bin/bouquins/books', stdError, indexSuccess);
} }
function loadBooks() { function loadBooks() {

2
main.c
View File

@ -214,7 +214,7 @@ sendindex(struct kreq *r)
http_open(r, KHTTP_200); http_open(r, KHTTP_200);
kjson_open(&req, r); kjson_open(&req, r);
kjson_obj_open(&req); kjson_obj_open(&req);
kjson_putstringp(&req, "data", "index"); kjson_putintp(&req, "count", db_books_count(r));
kjson_obj_close(&req); kjson_obj_close(&req);
kjson_close(&req); kjson_close(&req);
} }