Load book tags
This commit is contained in:
parent
23b36febff
commit
24b6ca74ee
151
db.c
151
db.c
@ -30,10 +30,12 @@
|
||||
|
||||
enum stmt {
|
||||
STMT_BOOK,
|
||||
STMT_BOOK_TAGS,
|
||||
STMT_BOOK_AUTHORS,
|
||||
STMT_BOOK_DATA,
|
||||
STMT_BOOKS,
|
||||
STMT_BOOKS_COUNT,
|
||||
STMT_BOOKS_TAGS,
|
||||
STMT_BOOKS_AUTHORS,
|
||||
STMT__MAX
|
||||
};
|
||||
@ -51,6 +53,8 @@ static const char *const stmts[STMT__MAX] = {
|
||||
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_TAGS */
|
||||
"SELECT name FROM tags, books_tags_link WHERE tags.id = books_tags_link.tag AND books_tags_link.book = ?",
|
||||
/* 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 \
|
||||
@ -64,6 +68,10 @@ static const char *const stmts[STMT__MAX] = {
|
||||
LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY id LIMIT ? OFFSET ?",
|
||||
/* STMT_BOOKS_COUNT */
|
||||
"SELECT count(id) FROM books",
|
||||
/* STMT_BOOKS_TAGS */
|
||||
"SELECT name, books_tags_link.book as book FROM tags, books_tags_link \
|
||||
WHERE tags.id = books_tags_link.tag AND books_tags_link.book \
|
||||
IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)",
|
||||
/* 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 \
|
||||
@ -79,6 +87,15 @@ struct bookauth {
|
||||
struct bookauth *p;
|
||||
};
|
||||
|
||||
/*
|
||||
* Linked list tags of book.
|
||||
*/
|
||||
struct booktag {
|
||||
char *tag;
|
||||
BookAdv *b;
|
||||
struct booktag *p;
|
||||
};
|
||||
|
||||
/*
|
||||
* Linked list book data.
|
||||
*/
|
||||
@ -147,10 +164,15 @@ db_book_adv_free(BookAdv *p)
|
||||
{
|
||||
if (NULL == p)
|
||||
return;
|
||||
size_t i;
|
||||
|
||||
db_book_unfill(&p->b);
|
||||
for (size_t i = 0; i < p->asize; i++) {
|
||||
for (i = 0; i < p->asize; i++) {
|
||||
db_author_unfill(p->a[i]);
|
||||
}
|
||||
for (i = 0; i < p->tsize; i++) {
|
||||
free(p->tags[i]);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
|
||||
@ -215,6 +237,26 @@ db_books_count(struct kreq *r)
|
||||
return(count);
|
||||
}
|
||||
|
||||
static void
|
||||
db_assign_book_tags(struct booktag *list)
|
||||
{
|
||||
struct booktag *item = NULL;
|
||||
struct booktag *p = NULL;
|
||||
|
||||
p = list;
|
||||
while (NULL != p) {
|
||||
if (NULL != p->b && NULL == p->b->tags && p->b->tsize > 0) {
|
||||
p->b->tags = kcalloc(p->b->tsize, sizeof(char));
|
||||
p->b->tsize = 0; // use as counter for insert
|
||||
}
|
||||
p->b->tags[p->b->tsize] = p->tag;
|
||||
p->b->tsize++;
|
||||
item = p;
|
||||
p = p->p;
|
||||
free(item);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
db_assign_book_authors(struct bookauth *list)
|
||||
{
|
||||
@ -235,31 +277,45 @@ db_assign_book_authors(struct bookauth *list)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
|
||||
static struct booktag *
|
||||
db_load_books_tags(struct kreq *r, BookAdv **books, int count, int limit, int offset)
|
||||
{
|
||||
if (limit < 0 || offset < 0) {
|
||||
return 0;
|
||||
}
|
||||
struct ksqlstmt *stmt;
|
||||
struct bookauth *p = NULL;
|
||||
struct bookauth *item = NULL;
|
||||
int i, count = 0;
|
||||
struct booktag *p = NULL;
|
||||
struct booktag *item = NULL;
|
||||
int i;
|
||||
int64_t bid;
|
||||
|
||||
ksql_stmt_alloc(r->arg, &stmt,
|
||||
stmts[STMT_BOOKS],
|
||||
STMT_BOOKS);
|
||||
stmts[STMT_BOOKS_TAGS],
|
||||
STMT_BOOKS_TAGS);
|
||||
ksql_bind_int(stmt, 0, limit);
|
||||
ksql_bind_int(stmt, 1, offset);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
books[count] = kcalloc(1, sizeof(BookAdv));
|
||||
db_book_fill(&books[count]->b,stmt);
|
||||
books[count]->asize = 0;
|
||||
count++;
|
||||
item = kcalloc(1, sizeof(struct booktag));
|
||||
item->tag = kstrdup(ksql_stmt_str(stmt, 0));
|
||||
bid = ksql_stmt_int(stmt, 1);
|
||||
for (i = 0; i < count && NULL == item->b; i++) {
|
||||
if (books[i]->b.id == bid) {
|
||||
item->b = books[i];
|
||||
item->b->tsize++;
|
||||
}
|
||||
}
|
||||
item->p = p;
|
||||
p = item;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
return p;
|
||||
}
|
||||
|
||||
static struct bookauth *
|
||||
db_load_books_authors(struct kreq *r, BookAdv **books, int count, int limit, int offset)
|
||||
{
|
||||
struct ksqlstmt *stmt;
|
||||
struct bookauth *p = NULL;
|
||||
struct bookauth *item = NULL;
|
||||
int i;
|
||||
int64_t bid;
|
||||
|
||||
ksql_stmt_alloc(r->arg, &stmt,
|
||||
stmts[STMT_BOOKS_AUTHORS],
|
||||
@ -282,15 +338,69 @@ db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
|
||||
item->p = p;
|
||||
p = item;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
return p;
|
||||
}
|
||||
|
||||
// assign authors to books
|
||||
db_assign_book_authors(p);
|
||||
int
|
||||
db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
|
||||
{
|
||||
if (limit < 0 || offset < 0) {
|
||||
return 0;
|
||||
}
|
||||
struct ksqlstmt *stmt;
|
||||
struct bookauth *p_a;
|
||||
struct booktag *p_t;
|
||||
int count = 0;
|
||||
|
||||
ksql_stmt_alloc(r->arg, &stmt,
|
||||
stmts[STMT_BOOKS],
|
||||
STMT_BOOKS);
|
||||
ksql_bind_int(stmt, 0, limit);
|
||||
ksql_bind_int(stmt, 1, offset);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
books[count] = kcalloc(1, sizeof(BookAdv));
|
||||
db_book_fill(&books[count]->b,stmt);
|
||||
books[count]->asize = 0;
|
||||
count++;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
p_a = db_load_books_authors(r, books, count, limit, offset);
|
||||
db_assign_book_authors(p_a);
|
||||
|
||||
p_t = db_load_books_tags(r, books, count, limit, offset);
|
||||
db_assign_book_tags(p_t);
|
||||
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static void
|
||||
db_book_full_tags(BookAdv *b, struct kreq *r, int64_t id)
|
||||
{
|
||||
struct ksqlstmt *stmt;
|
||||
struct booktag *p = NULL;
|
||||
struct booktag *item = NULL;
|
||||
|
||||
ksql_stmt_alloc(r->arg, &stmt,
|
||||
stmts[STMT_BOOK_TAGS],
|
||||
STMT_BOOK_TAGS);
|
||||
ksql_bind_int(stmt, 0, id);
|
||||
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||
item = kcalloc(1, sizeof(struct booktag));
|
||||
item->tag = kstrdup(ksql_stmt_str(stmt, 0));
|
||||
item->b = b;
|
||||
item->b->tsize++;
|
||||
item->p = p;
|
||||
p = item;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
db_assign_book_tags(p);
|
||||
}
|
||||
|
||||
static void
|
||||
db_book_full_authors(BookAdv *b, struct kreq *r, int64_t id) {
|
||||
struct ksqlstmt *stmt;
|
||||
@ -311,10 +421,10 @@ db_book_full_authors(BookAdv *b, struct kreq *r, int64_t id) {
|
||||
item->p = p;
|
||||
p = item;
|
||||
}
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
// assign authors to book
|
||||
db_assign_book_authors(p);
|
||||
|
||||
ksql_stmt_free(stmt);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -383,6 +493,7 @@ db_book_load(struct kreq *r, int64_t id)
|
||||
|
||||
ksql_stmt_free(stmt);
|
||||
|
||||
db_book_full_tags(&book->ba, r, id);
|
||||
db_book_full_authors(&book->ba, r, id);
|
||||
db_book_full_data(book, r, id);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user