typedef book, series, authors

This commit is contained in:
Meutel 2016-12-30 10:10:55 +01:00
parent dcff9e437c
commit a28be1c39f
3 changed files with 56 additions and 19 deletions

19
db.c
View File

@ -36,22 +36,24 @@ enum stmt {
static const char *const stmts[STMT__MAX] = { static const char *const stmts[STMT__MAX] = {
/* STMT_BOOK */ /* STMT_BOOK */
"SELECT books.id AS id,title FROM books 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 FROM books LIMIT ?",
}; };
void void
db_book_free(struct book *b) db_book_free(Book *b)
{ {
if (NULL == b) if (NULL == b)
return; return;
free(b->title); free(b->title);
//TODO free series
free(b->s.name);
free(b); free(b);
} }
int int
db_books_load(struct kreq *r, struct book **books, int limit) db_books_load(struct kreq *r, Book **books, int limit)
{ {
if (limit < 0) { if (limit < 0) {
return 0; return 0;
@ -63,7 +65,7 @@ db_books_load(struct kreq *r, struct 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(struct book)); books[i] = kcalloc(1, sizeof(Book));
books[i]->id = ksql_stmt_int(stmt, 0); books[i]->id = ksql_stmt_int(stmt, 0);
books[i]->title = kstrdup(ksql_stmt_str(stmt, 1)); books[i]->title = kstrdup(ksql_stmt_str(stmt, 1));
i++; i++;
@ -72,11 +74,11 @@ db_books_load(struct kreq *r, struct book **books, int limit)
return i; return i;
} }
struct book * Book *
db_book_load(struct kreq *r, int64_t id) db_book_load(struct kreq *r, int64_t id)
{ {
struct ksqlstmt *stmt; struct ksqlstmt *stmt;
struct book *book; Book *book;
ksql_stmt_alloc(r->arg, &stmt, ksql_stmt_alloc(r->arg, &stmt,
stmts[STMT_BOOK], stmts[STMT_BOOK],
STMT_BOOK); STMT_BOOK);
@ -85,9 +87,12 @@ db_book_load(struct kreq *r, int64_t id)
ksql_stmt_free(stmt); ksql_stmt_free(stmt);
return(NULL); return(NULL);
} }
book = kcalloc(1, sizeof(struct 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));
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); ksql_stmt_free(stmt);
return book; return book;
} }

View File

@ -18,18 +18,50 @@
#define EXTERN_H #define EXTERN_H
/* /*
* A book. * A book series.
*/ */
struct book { typedef struct
int64_t id; /* unique identifier */ {
int64_t id;
char *name;
} Series;
/*
* A book. Generic data.
*/
typedef struct
{
int64_t id;
char *title; char *title;
int s_idx; /* series index */
Series s;
} Book;
/*
* An author.
*/
typedef struct
{
int64_t id;
char *name;
} Author;
/*
* A book. Advanced data: authors, tags.
*/
struct book_adv
{
Book b;
size_t autsize;
Author *authors;
// TODO tags
}; };
__BEGIN_DECLS __BEGIN_DECLS
struct 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, struct book **books, int limit); int db_books_load(struct kreq *r, Book **books, int limit);
void db_book_free(struct book *b); void db_book_free(Book *b); // TODO series
void db_close(struct kreq *); void db_close(struct kreq *);
int db_open(struct kreq *, const char *); int db_open(struct kreq *, const char *);

6
main.c
View File

@ -95,7 +95,7 @@ puterror(struct kjsonreq *req, char *message)
} }
void void
putbook(struct kjsonreq *req, struct book *b) 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);
@ -109,7 +109,7 @@ sendbooks(struct kreq *r)
struct kjsonreq req; struct kjsonreq req;
const char *errid; const char *errid;
int64_t id = -1; int64_t id = -1;
struct book *b = NULL; Book *b = NULL;
int res, i = 0; int res, i = 0;
if (NULL != r->fieldmap[KEY_ID]) if (NULL != r->fieldmap[KEY_ID])
@ -128,7 +128,7 @@ sendbooks(struct kreq *r)
db_book_free(b); db_book_free(b);
} else { } else {
kjson_array_open(&req); kjson_array_open(&req);
struct book **books = kcalloc(10, sizeof(struct book)); Book **books = kcalloc(10, sizeof(Book));
res = db_books_load(r, books, 10); res = db_books_load(r, books, 10);
while (i < res) { while (i < res) {
putbook(&req, books[i]); putbook(&req, books[i]);