Book advanced data, optional series
This commit is contained in:
parent
e06ae559d6
commit
138a1b19b6
70
db.c
70
db.c
@ -38,22 +38,50 @@ 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 */
|
||||
"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
|
||||
db_book_free(Book *b)
|
||||
static void
|
||||
db_series_unfill(Series *p)
|
||||
{
|
||||
if (NULL == b)
|
||||
if (NULL == p)
|
||||
return;
|
||||
free(b->title);
|
||||
//TODO free series
|
||||
free(b->s.name);
|
||||
free(b);
|
||||
free(p->name);
|
||||
}
|
||||
|
||||
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
|
||||
db_books_load(struct kreq *r, Book **books, int limit)
|
||||
db_books_load(struct kreq *r, BookAdv **books, int limit)
|
||||
{
|
||||
if (limit < 0) {
|
||||
return 0;
|
||||
@ -65,9 +93,16 @@ db_books_load(struct kreq *r, Book **books, int limit)
|
||||
STMT_BOOKS);
|
||||
ksql_bind_int(stmt, 0, limit);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
books[i] = kcalloc(1, sizeof(Book));
|
||||
books[i]->id = ksql_stmt_int(stmt, 0);
|
||||
books[i]->title = kstrdup(ksql_stmt_str(stmt, 1));
|
||||
books[i] = kcalloc(1, sizeof(BookAdv));
|
||||
books[i]->b.id = ksql_stmt_int(stmt, 0);
|
||||
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++;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
@ -91,14 +126,17 @@ db_book_load(struct kreq *r, int64_t id)
|
||||
book = kcalloc(1, sizeof(Book));
|
||||
book->id = ksql_stmt_int(stmt, 0);
|
||||
book->title = kstrdup(ksql_stmt_str(stmt, 1));
|
||||
book->s_idx = ksql_stmt_int(stmt, 2);
|
||||
book->s.name = kstrdup(ksql_stmt_str(stmt, 3));
|
||||
book->s.id = ksql_stmt_int(stmt, 4);
|
||||
if ( ksql_stmt_isnull(stmt, 4) ) {
|
||||
book->s.id = -1;
|
||||
} else {
|
||||
book->s_idx = ksql_stmt_int(stmt, 2);
|
||||
book->s.name = kstrdup(ksql_stmt_str(stmt, 3));
|
||||
book->s.id = ksql_stmt_int(stmt, 4);
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
return book;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Open the database and stash the resulting handle in the d
|
||||
*/
|
||||
|
17
extern.h
17
extern.h
@ -33,7 +33,7 @@ typedef struct
|
||||
{
|
||||
int64_t id;
|
||||
char *title;
|
||||
int s_idx; /* series index */
|
||||
int s_idx; /* series index */ // FIXME pas un int
|
||||
Series s;
|
||||
} Book;
|
||||
|
||||
@ -49,19 +49,22 @@ typedef struct
|
||||
/*
|
||||
* A book. Advanced data: authors, tags.
|
||||
*/
|
||||
struct book_adv
|
||||
typedef struct
|
||||
{
|
||||
Book b;
|
||||
size_t autsize;
|
||||
Author *authors;
|
||||
Author *a;
|
||||
size_t asize;
|
||||
// TODO tags
|
||||
};
|
||||
} BookAdv;
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
Book *db_book_load(struct kreq *r, int64_t id);
|
||||
int db_books_load(struct kreq *r, Book **books, int limit);
|
||||
void db_book_free(Book *b); // TODO series
|
||||
int db_books_load(struct kreq *r, BookAdv **books, int limit);
|
||||
void db_book_free(Book *p);
|
||||
void db_book_adv_free(BookAdv *p);
|
||||
|
||||
void db_series_free(Series *p);
|
||||
|
||||
void db_close(struct kreq *);
|
||||
int db_open(struct kreq *, const char *);
|
||||
|
14
main.c
14
main.c
@ -100,6 +100,13 @@ putbook(struct kjsonreq *req, Book *b)
|
||||
kjson_obj_open(req);
|
||||
kjson_putintp(req, "id", b->id);
|
||||
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);
|
||||
}
|
||||
|
||||
@ -128,11 +135,12 @@ sendbooks(struct kreq *r)
|
||||
db_book_free(b);
|
||||
} else {
|
||||
kjson_array_open(&req);
|
||||
Book **books = kcalloc(10, sizeof(Book));
|
||||
BookAdv **books = kcalloc(10, sizeof(BookAdv));
|
||||
res = db_books_load(r, books, 10);
|
||||
while (i < res) {
|
||||
putbook(&req, books[i]);
|
||||
db_book_free(books[i]);
|
||||
Book b = books[i]->b;
|
||||
putbook(&req, &b);
|
||||
db_book_adv_free(books[i]);
|
||||
i++;
|
||||
}
|
||||
free(books);
|
||||
|
Loading…
Reference in New Issue
Block a user