Full book data
This commit is contained in:
parent
2c5026c469
commit
c399fece1e
135
db.c
135
db.c
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
STMT_BOOK,
|
STMT_BOOK,
|
||||||
|
STMT_BOOK_AUTHORS,
|
||||||
STMT_BOOKS,
|
STMT_BOOKS,
|
||||||
STMT_BOOKS_COUNT,
|
STMT_BOOKS_COUNT,
|
||||||
STMT_BOOKS_AUTHORS,
|
STMT_BOOKS_AUTHORS,
|
||||||
@ -38,13 +39,31 @@ 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,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, series.name AS series_name, series.id AS series_id, \
|
||||||
|
timestamp,pubdate, isbn,lccn,path,uuid,has_cover, \
|
||||||
|
languages.lang_code,format,uncompressed_size,data.name AS data_name, publishers.name AS pubname FROM books \
|
||||||
|
LEFT OUTER JOIN books_languages_link ON books_languages_link.book = books.id \
|
||||||
|
LEFT OUTER JOIN languages ON languages.id = books_languages_link.lang_code \
|
||||||
|
LEFT OUTER JOIN data ON data.book = books.id \
|
||||||
|
LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book \
|
||||||
|
LEFT OUTER JOIN series ON series.id = books_series_link.series \
|
||||||
|
LEFT OUTER JOIN books_publishers_link ON books.id = books_publishers_link.book \
|
||||||
|
LEFT OUTER JOIN publishers ON publishers.id = books_publishers_link.publisher \
|
||||||
|
WHERE books.id = ?",
|
||||||
|
/* STMT_BOOK_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 = ?",
|
||||||
/* 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 */
|
/* STMT_BOOKS_COUNT */
|
||||||
"SELECT count(id) FROM books",
|
"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 ?)",
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -97,6 +116,8 @@ db_book_free(Book *p)
|
|||||||
void
|
void
|
||||||
db_book_adv_free(BookAdv *p)
|
db_book_adv_free(BookAdv *p)
|
||||||
{
|
{
|
||||||
|
if (NULL == p)
|
||||||
|
return;
|
||||||
db_book_unfill(&p->b);
|
db_book_unfill(&p->b);
|
||||||
for (size_t i = 0; i < p->asize; i++) {
|
for (size_t i = 0; i < p->asize; i++) {
|
||||||
db_author_unfill(p->a[i]);
|
db_author_unfill(p->a[i]);
|
||||||
@ -104,6 +125,34 @@ db_book_adv_free(BookAdv *p)
|
|||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
db_book_full_unfill(BookFull *p)
|
||||||
|
{
|
||||||
|
if (NULL == p)
|
||||||
|
return;
|
||||||
|
free(p->isbn);
|
||||||
|
free(p->lccn);
|
||||||
|
free(p->path);
|
||||||
|
free(p->uuid);
|
||||||
|
free(p->lang);
|
||||||
|
free(p->format);
|
||||||
|
free(p->dataname);
|
||||||
|
free(p->publisher);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
db_book_full_free(BookFull *p)
|
||||||
|
{
|
||||||
|
if (NULL == p)
|
||||||
|
return;
|
||||||
|
db_book_unfill(&p->ba.b);
|
||||||
|
for (size_t i = 0; i < p->ba.asize; i++) {
|
||||||
|
db_author_unfill(p->ba.a[i]);
|
||||||
|
}
|
||||||
|
db_book_full_unfill(p);
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
db_book_fill(Book *book, struct ksqlstmt *stmt)
|
db_book_fill(Book *book, struct ksqlstmt *stmt)
|
||||||
{
|
{
|
||||||
@ -136,6 +185,27 @@ db_books_count(struct kreq *r)
|
|||||||
return(count);
|
return(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
db_assign_book_authors(struct bookauth *list)
|
||||||
|
{
|
||||||
|
struct bookauth *item = NULL;
|
||||||
|
struct bookauth *p = NULL;
|
||||||
|
|
||||||
|
p = list;
|
||||||
|
while (NULL != p) {
|
||||||
|
if (NULL != p->b && NULL == p->b->a && p->b->asize > 0) {
|
||||||
|
p->b->a = kcalloc(p->b->asize, sizeof(Author));
|
||||||
|
p->b->asize = 0; // use as counter for insert
|
||||||
|
}
|
||||||
|
p->b->a[p->b->asize] = p->a;// add author
|
||||||
|
p->b->asize++;
|
||||||
|
item = p;
|
||||||
|
p = p->p;
|
||||||
|
free(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -184,28 +254,22 @@ db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// assign authors to books
|
// assign authors to books
|
||||||
while (NULL != p) {
|
db_assign_book_authors(p);
|
||||||
if (NULL != p->b && NULL == p->b->a && p->b->asize > 0) {
|
|
||||||
p->b->a = kcalloc(p->b->asize, sizeof(Author));
|
|
||||||
p->b->asize = 0; // use as counter for insert
|
|
||||||
}
|
|
||||||
p->b->a[p->b->asize] = p->a;// add author
|
|
||||||
p->b->asize++;
|
|
||||||
item = p;
|
|
||||||
p = p->p;
|
|
||||||
free(item);
|
|
||||||
}
|
|
||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
Book *
|
BookFull *
|
||||||
db_book_load(struct kreq *r, int64_t id)
|
db_book_load(struct kreq *r, int64_t id)
|
||||||
{
|
{
|
||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
Book *book;
|
struct bookauth *p = NULL;
|
||||||
|
struct bookauth *item = NULL;
|
||||||
|
BookFull *book;
|
||||||
|
|
||||||
|
fputs(stmts[STMT_BOOK], stderr);
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[STMT_BOOK],
|
stmts[STMT_BOOK],
|
||||||
STMT_BOOK);
|
STMT_BOOK);
|
||||||
@ -214,9 +278,44 @@ 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(Book));
|
fputs("stmt ok", stderr);
|
||||||
db_book_fill(book, stmt);
|
book = kcalloc(1, sizeof(BookFull));
|
||||||
|
db_book_fill(&book->ba.b, stmt);
|
||||||
|
|
||||||
|
book->timestamp = ksql_stmt_int(stmt, 5);
|
||||||
|
book->pubdate = ksql_stmt_int(stmt, 6);
|
||||||
|
book->isbn = kstrdup(ksql_stmt_str(stmt, 7));
|
||||||
|
book->lccn = kstrdup(ksql_stmt_str(stmt, 8));
|
||||||
|
book->path = kstrdup(ksql_stmt_str(stmt, 9));
|
||||||
|
book->uuid = kstrdup(ksql_stmt_str(stmt, 10));
|
||||||
|
book->has_cover = ksql_stmt_int(stmt, 11);
|
||||||
|
book->lang = kstrdup(ksql_stmt_str(stmt, 12));
|
||||||
|
book->format = kstrdup(ksql_stmt_str(stmt, 13));
|
||||||
|
book->dataname = kstrdup(ksql_stmt_str(stmt, 14));
|
||||||
|
book->unz_size = ksql_stmt_int(stmt, 15);
|
||||||
|
book->publisher = kstrdup(ksql_stmt_str(stmt, 16));
|
||||||
|
|
||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
|
stmts[STMT_BOOK_AUTHORS],
|
||||||
|
STMT_BOOK_AUTHORS);
|
||||||
|
ksql_bind_int(stmt, 0, id);
|
||||||
|
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||||
|
item = kcalloc(1, sizeof(struct bookauth));
|
||||||
|
item->a = kcalloc(1, sizeof(Author));
|
||||||
|
item->a->id = ksql_stmt_int(stmt, 0);
|
||||||
|
item->a->name = kstrdup(ksql_stmt_str(stmt, 1));
|
||||||
|
item->b = &book->ba;
|
||||||
|
item->b->asize++;
|
||||||
|
item->p = p;
|
||||||
|
p = item;
|
||||||
|
}
|
||||||
|
// assign authors to book
|
||||||
|
db_assign_book_authors(p);
|
||||||
|
|
||||||
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
return book;
|
return book;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
extern.h
20
extern.h
@ -57,13 +57,31 @@ typedef struct
|
|||||||
// TODO tags
|
// TODO tags
|
||||||
} BookAdv;
|
} BookAdv;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
BookAdv ba;
|
||||||
|
int64_t timestamp;
|
||||||
|
int64_t pubdate;
|
||||||
|
char *isbn;
|
||||||
|
char *lccn;
|
||||||
|
char *path;
|
||||||
|
char *uuid;
|
||||||
|
int64_t has_cover;
|
||||||
|
char *lang;
|
||||||
|
char *format;
|
||||||
|
char *dataname;
|
||||||
|
int64_t unz_size;
|
||||||
|
char *publisher;
|
||||||
|
} BookFull;
|
||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
Book *db_book_load(struct kreq *r, int64_t id);
|
BookFull *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);
|
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);
|
||||||
|
void db_book_full_free(BookFull *p);
|
||||||
|
|
||||||
void db_series_free(Series *p);
|
void db_series_free(Series *p);
|
||||||
|
|
||||||
|
52
main.c
52
main.c
@ -127,13 +127,49 @@ putbook(struct kjsonreq *req, Book *b)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
putauthors(struct kjsonreq *req, BookAdv *b)
|
||||||
|
{
|
||||||
|
if (b->asize > 0) {
|
||||||
|
kjson_arrayp_open(req, "authors");
|
||||||
|
for (size_t j=0; j<b->asize; j++) {
|
||||||
|
putauthor(req, b->a[j]);
|
||||||
|
kjson_obj_close(req);
|
||||||
|
}
|
||||||
|
kjson_array_close(req);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
putbookfull(struct kjsonreq *req, BookFull *b)
|
||||||
|
{
|
||||||
|
char tsstr[30];
|
||||||
|
char pubdatestr[30];
|
||||||
|
|
||||||
|
putbook(req, &b->ba.b);
|
||||||
|
kutil_epoch2str(b->timestamp, tsstr, sizeof(tsstr));
|
||||||
|
kutil_epoch2str(b->pubdate, pubdatestr, sizeof(pubdatestr));
|
||||||
|
kjson_putstringp(req, "timestamp", tsstr);
|
||||||
|
kjson_putstringp(req, "pubdate", pubdatestr);
|
||||||
|
kjson_putstringp(req, "isbn", b->isbn);
|
||||||
|
kjson_putstringp(req, "lccn", b->lccn);
|
||||||
|
kjson_putstringp(req, "path", b->path);
|
||||||
|
kjson_putstringp(req, "uuid", b->uuid);
|
||||||
|
kjson_putboolp(req, "has_cover", b->has_cover);
|
||||||
|
kjson_putstringp(req, "lang", b->lang);
|
||||||
|
kjson_putstringp(req, "format", b->format);
|
||||||
|
kjson_putstringp(req, "data", b->dataname);
|
||||||
|
kjson_putintp(req, "uncompressed_size", b->unz_size);
|
||||||
|
kjson_putstringp(req, "publisher", b->publisher);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sendbooks(struct kreq *r)
|
sendbooks(struct kreq *r)
|
||||||
{
|
{
|
||||||
struct kjsonreq req;
|
struct kjsonreq req;
|
||||||
const char *errid;
|
const char *errid;
|
||||||
int64_t id = -1;
|
int64_t id = -1;
|
||||||
Book *b = NULL;
|
BookFull *b = NULL;
|
||||||
int res, i = 0, page = 1, per = 20;
|
int res, i = 0, page = 1, per = 20;
|
||||||
|
|
||||||
if (NULL != r->fieldmap[KEY_PAGE])
|
if (NULL != r->fieldmap[KEY_PAGE])
|
||||||
@ -152,9 +188,10 @@ sendbooks(struct kreq *r)
|
|||||||
if (id >= 0 && NULL == b) {
|
if (id >= 0 && NULL == b) {
|
||||||
puterror(&req, "Unknown book");
|
puterror(&req, "Unknown book");
|
||||||
} else if (NULL != b) {
|
} else if (NULL != b) {
|
||||||
putbook(&req, b);
|
putbookfull(&req, b);
|
||||||
|
putauthors(&req, &b->ba);
|
||||||
kjson_obj_close(&req);
|
kjson_obj_close(&req);
|
||||||
db_book_free(b);
|
db_book_full_free(b);
|
||||||
} else {
|
} else {
|
||||||
kjson_array_open(&req);
|
kjson_array_open(&req);
|
||||||
BookAdv **books = kcalloc(per, sizeof(BookAdv));
|
BookAdv **books = kcalloc(per, sizeof(BookAdv));
|
||||||
@ -162,14 +199,7 @@ sendbooks(struct kreq *r)
|
|||||||
while (i < res) {
|
while (i < res) {
|
||||||
Book b = books[i]->b;
|
Book b = books[i]->b;
|
||||||
putbook(&req, &b);
|
putbook(&req, &b);
|
||||||
if (books[i]->asize > 0) {
|
putauthors(&req, books[i]);
|
||||||
kjson_arrayp_open(&req, "authors");
|
|
||||||
for (size_t j=0; j<books[i]->asize; j++) {
|
|
||||||
putauthor(&req, books[i]->a[j]);
|
|
||||||
kjson_obj_close(&req);
|
|
||||||
}
|
|
||||||
kjson_array_close(&req);
|
|
||||||
}
|
|
||||||
kjson_obj_close(&req);
|
kjson_obj_close(&req);
|
||||||
db_book_adv_free(books[i]);
|
db_book_adv_free(books[i]);
|
||||||
i++;
|
i++;
|
||||||
|
Loading…
Reference in New Issue
Block a user