diff --git a/db.c b/db.c index 30da7d3..8408730 100644 --- a/db.c +++ b/db.c @@ -29,29 +29,31 @@ #include "extern.h" enum stmt { - STMT_BOOK, - STMT_BOOKS, - STMT__MAX + STMT_BOOK, + STMT_BOOKS, + STMT__MAX }; static const char *const stmts[STMT__MAX] = { /* 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 */ "SELECT books.id AS id,title FROM books LIMIT ?", }; void -db_book_free(struct book *b) +db_book_free(Book *b) { if (NULL == b) return; free(b->title); + //TODO free series + free(b->s.name); free(b); } 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) { return 0; @@ -63,7 +65,7 @@ db_books_load(struct kreq *r, struct book **books, int limit) STMT_BOOKS); ksql_bind_int(stmt, 0, limit); 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]->title = kstrdup(ksql_stmt_str(stmt, 1)); i++; @@ -72,11 +74,11 @@ db_books_load(struct kreq *r, struct book **books, int limit) return i; } -struct book * +Book * db_book_load(struct kreq *r, int64_t id) { struct ksqlstmt *stmt; - struct book *book; + Book *book; ksql_stmt_alloc(r->arg, &stmt, stmts[STMT_BOOK], STMT_BOOK); @@ -85,9 +87,12 @@ db_book_load(struct kreq *r, int64_t id) ksql_stmt_free(stmt); return(NULL); } - book = kcalloc(1, sizeof(struct book)); + 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); ksql_stmt_free(stmt); return book; } diff --git a/extern.h b/extern.h index 53ca5f1..5469c41 100644 --- a/extern.h +++ b/extern.h @@ -18,18 +18,50 @@ #define EXTERN_H /* - * A book. + * A book series. */ -struct book { - int64_t id; /* unique identifier */ +typedef struct +{ + int64_t id; + char *name; +} Series; + +/* + * A book. Generic data. + */ +typedef struct +{ + int64_t id; 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 -struct book * db_book_load(struct kreq *r, int64_t id); -int db_books_load(struct kreq *r, struct book **books, int limit); -void db_book_free(struct book *b); +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 void db_close(struct kreq *); int db_open(struct kreq *, const char *); diff --git a/main.c b/main.c index b691313..8c60648 100644 --- a/main.c +++ b/main.c @@ -95,7 +95,7 @@ puterror(struct kjsonreq *req, char *message) } void -putbook(struct kjsonreq *req, struct book *b) +putbook(struct kjsonreq *req, Book *b) { kjson_obj_open(req); kjson_putintp(req, "id", b->id); @@ -109,7 +109,7 @@ sendbooks(struct kreq *r) struct kjsonreq req; const char *errid; int64_t id = -1; - struct book *b = NULL; + Book *b = NULL; int res, i = 0; if (NULL != r->fieldmap[KEY_ID]) @@ -128,7 +128,7 @@ sendbooks(struct kreq *r) db_book_free(b); } else { 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); while (i < res) { putbook(&req, books[i]);