Book advanced data, optional series

This commit is contained in:
Meutel 2016-12-30 11:19:23 +01:00
parent e06ae559d6
commit 138a1b19b6
3 changed files with 75 additions and 26 deletions

64
db.c
View File

@ -38,22 +38,50 @@ static const char *const stmts[STMT__MAX] = {
/* STMT_BOOK */ /* 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 = ?", "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 FROM books LIMIT ?", "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 LIMIT ?",
}; };
void static void
db_book_free(Book *b) db_series_unfill(Series *p)
{ {
if (NULL == b) if (NULL == p)
return; return;
free(b->title); free(p->name);
//TODO free series }
free(b->s.name);
free(b); void
db_series_free(Series *p)
{
db_series_unfill(p);
free(p);
}
static void
db_book_unfill(Book *p)
{
if (NULL == p)
return;
free(p->title);
db_series_unfill(&p->s);
}
void
db_book_free(Book *p)
{
db_book_unfill(p);
free(p);
}
void
db_book_adv_free(BookAdv *p)
{
db_book_unfill(&p->b);
//TODO free authors
free(p);
} }
int int
db_books_load(struct kreq *r, Book **books, int limit) db_books_load(struct kreq *r, BookAdv **books, int limit)
{ {
if (limit < 0) { if (limit < 0) {
return 0; return 0;
@ -65,9 +93,16 @@ db_books_load(struct kreq *r, Book **books, int limit)
STMT_BOOKS); STMT_BOOKS);
ksql_bind_int(stmt, 0, limit); ksql_bind_int(stmt, 0, limit);
while (KSQL_ROW == ksql_stmt_step(stmt)) { while (KSQL_ROW == ksql_stmt_step(stmt)) {
books[i] = kcalloc(1, sizeof(Book)); books[i] = kcalloc(1, sizeof(BookAdv));
books[i]->id = ksql_stmt_int(stmt, 0); books[i]->b.id = ksql_stmt_int(stmt, 0);
books[i]->title = kstrdup(ksql_stmt_str(stmt, 1)); books[i]->b.title = kstrdup(ksql_stmt_str(stmt, 1));
if ( ksql_stmt_isnull(stmt, 4) ) {
books[i]->b.s.id = -1;
} else {
books[i]->b.s_idx = ksql_stmt_int(stmt, 2);
books[i]->b.s.name = kstrdup(ksql_stmt_str(stmt, 3));
books[i]->b.s.id = ksql_stmt_int(stmt, 4);
}
i++; i++;
} }
ksql_stmt_free(stmt); ksql_stmt_free(stmt);
@ -91,14 +126,17 @@ db_book_load(struct kreq *r, int64_t id)
book = kcalloc(1, sizeof(Book)); book = kcalloc(1, sizeof(Book));
book->id = ksql_stmt_int(stmt, 0); book->id = ksql_stmt_int(stmt, 0);
book->title = kstrdup(ksql_stmt_str(stmt, 1)); book->title = kstrdup(ksql_stmt_str(stmt, 1));
if ( ksql_stmt_isnull(stmt, 4) ) {
book->s.id = -1;
} else {
book->s_idx = ksql_stmt_int(stmt, 2); book->s_idx = ksql_stmt_int(stmt, 2);
book->s.name = kstrdup(ksql_stmt_str(stmt, 3)); book->s.name = kstrdup(ksql_stmt_str(stmt, 3));
book->s.id = ksql_stmt_int(stmt, 4); book->s.id = ksql_stmt_int(stmt, 4);
}
ksql_stmt_free(stmt); ksql_stmt_free(stmt);
return book; return book;
} }
/* /*
* Open the database and stash the resulting handle in the d * Open the database and stash the resulting handle in the d
*/ */

View File

@ -33,7 +33,7 @@ typedef struct
{ {
int64_t id; int64_t id;
char *title; char *title;
int s_idx; /* series index */ int s_idx; /* series index */ // FIXME pas un int
Series s; Series s;
} Book; } Book;
@ -49,19 +49,22 @@ typedef struct
/* /*
* A book. Advanced data: authors, tags. * A book. Advanced data: authors, tags.
*/ */
struct book_adv typedef struct
{ {
Book b; Book b;
size_t autsize; Author *a;
Author *authors; size_t asize;
// TODO tags // TODO tags
}; } BookAdv;
__BEGIN_DECLS __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, Book **books, int limit); int db_books_load(struct kreq *r, BookAdv **books, int limit);
void db_book_free(Book *b); // TODO series void db_book_free(Book *p);
void db_book_adv_free(BookAdv *p);
void db_series_free(Series *p);
void db_close(struct kreq *); void db_close(struct kreq *);
int db_open(struct kreq *, const char *); int db_open(struct kreq *, const char *);

14
main.c
View File

@ -100,6 +100,13 @@ putbook(struct kjsonreq *req, Book *b)
kjson_obj_open(req); kjson_obj_open(req);
kjson_putintp(req, "id", b->id); kjson_putintp(req, "id", b->id);
kjson_putstringp(req, "title", b->title); kjson_putstringp(req, "title", b->title);
if (b->s.id >= 0) {
kjson_objp_open(req, "series");
kjson_putintp(req, "id", b->s.id);
kjson_putstringp(req, "name", b->s.name);
kjson_putintp(req, "idx", b->s_idx);
kjson_obj_close(req);
}
kjson_obj_close(req); kjson_obj_close(req);
} }
@ -128,11 +135,12 @@ sendbooks(struct kreq *r)
db_book_free(b); db_book_free(b);
} else { } else {
kjson_array_open(&req); kjson_array_open(&req);
Book **books = kcalloc(10, sizeof(Book)); BookAdv **books = kcalloc(10, sizeof(BookAdv));
res = db_books_load(r, books, 10); res = db_books_load(r, books, 10);
while (i < res) { while (i < res) {
putbook(&req, books[i]); Book b = books[i]->b;
db_book_free(books[i]); putbook(&req, &b);
db_book_adv_free(books[i]);
i++; i++;
} }
free(books); free(books);