2016-12-18 15:50:56 +00:00
|
|
|
|
/* $Id$ */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <kcgi.h>
|
|
|
|
|
#include <kcgijson.h>
|
2016-12-29 19:41:31 +00:00
|
|
|
|
#include "ksql.h"
|
2016-12-18 15:50:56 +00:00
|
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
2016-12-29 20:02:58 +00:00
|
|
|
|
enum stmt {
|
2016-12-30 09:10:55 +00:00
|
|
|
|
STMT_BOOK,
|
2017-01-03 18:14:19 +00:00
|
|
|
|
STMT_BOOK_TAGS,
|
2016-12-31 14:33:09 +00:00
|
|
|
|
STMT_BOOK_AUTHORS,
|
2017-01-01 14:44:51 +00:00
|
|
|
|
STMT_BOOK_DATA,
|
2016-12-30 09:10:55 +00:00
|
|
|
|
STMT_BOOKS,
|
2016-12-31 09:52:48 +00:00
|
|
|
|
STMT_BOOKS_COUNT,
|
2017-01-03 18:14:19 +00:00
|
|
|
|
STMT_BOOKS_TAGS,
|
2016-12-30 14:40:21 +00:00
|
|
|
|
STMT_BOOKS_AUTHORS,
|
2016-12-30 09:10:55 +00:00
|
|
|
|
STMT__MAX
|
2016-12-29 20:02:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char *const stmts[STMT__MAX] = {
|
|
|
|
|
/* STMT_BOOK */
|
2016-12-31 14:33:09 +00:00
|
|
|
|
"SELECT books.id AS id,title, series_index, series.name AS series_name, series.id AS series_id, \
|
2016-12-31 15:53:44 +00:00
|
|
|
|
strftime('%s', timestamp), strftime('%Y', pubdate), isbn,lccn,path,uuid,has_cover, \
|
2017-01-01 14:44:51 +00:00
|
|
|
|
languages.lang_code, publishers.name AS pubname FROM books \
|
2016-12-31 14:33:09 +00:00
|
|
|
|
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 = ?",
|
2017-01-03 18:14:19 +00:00
|
|
|
|
/* STMT_BOOK_TAGS */
|
|
|
|
|
"SELECT name FROM tags, books_tags_link WHERE tags.id = books_tags_link.tag AND books_tags_link.book = ?",
|
2016-12-31 14:33:09 +00:00
|
|
|
|
/* 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 = ?",
|
2017-01-01 14:44:51 +00:00
|
|
|
|
/* STMT_BOOK_DATA */
|
|
|
|
|
"SELECT data.name, data.format, data.uncompressed_size \
|
|
|
|
|
FROM data WHERE data.book = ?",
|
2016-12-30 15:16:00 +00:00
|
|
|
|
/* STMT_BOOKS */
|
2016-12-31 14:33:09 +00:00
|
|
|
|
"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 ?",
|
2016-12-31 09:52:48 +00:00
|
|
|
|
/* STMT_BOOKS_COUNT */
|
|
|
|
|
"SELECT count(id) FROM books",
|
2017-01-03 18:14:19 +00:00
|
|
|
|
/* 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 ?)",
|
2016-12-30 14:40:21 +00:00
|
|
|
|
/* STMT_BOOKS_AUTHORS */
|
2016-12-31 14:33:09 +00:00
|
|
|
|
"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 ?)",
|
2016-12-30 14:40:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Linked list authors of book.
|
|
|
|
|
*/
|
|
|
|
|
struct bookauth {
|
|
|
|
|
Author *a;
|
|
|
|
|
BookAdv *b;
|
|
|
|
|
struct bookauth *p;
|
2016-12-29 20:02:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
/*
|
|
|
|
|
* Linked list tags of book.
|
|
|
|
|
*/
|
|
|
|
|
struct booktag {
|
|
|
|
|
char *tag;
|
|
|
|
|
BookAdv *b;
|
|
|
|
|
struct booktag *p;
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-01 14:44:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* Linked list book data.
|
|
|
|
|
*/
|
|
|
|
|
struct bookdata {
|
|
|
|
|
BookData *d;
|
|
|
|
|
BookFull *b;
|
|
|
|
|
struct bookdata *p;
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 15:53:44 +00:00
|
|
|
|
static char *
|
|
|
|
|
strornull(struct ksqlstmt *stmt, int col)
|
|
|
|
|
{
|
|
|
|
|
if (ksql_stmt_isnull(stmt, col))
|
|
|
|
|
return(NULL);
|
|
|
|
|
return(kstrdup(ksql_stmt_str(stmt, col)));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 10:19:23 +00:00
|
|
|
|
static void
|
|
|
|
|
db_series_unfill(Series *p)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == p)
|
|
|
|
|
return;
|
|
|
|
|
free(p->name);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 23:45:20 +00:00
|
|
|
|
void
|
2016-12-30 10:19:23 +00:00
|
|
|
|
db_series_free(Series *p)
|
2016-12-29 23:45:20 +00:00
|
|
|
|
{
|
2016-12-30 10:19:23 +00:00
|
|
|
|
db_series_unfill(p);
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
db_book_unfill(Book *p)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == p)
|
2016-12-29 23:45:20 +00:00
|
|
|
|
return;
|
2016-12-30 10:19:23 +00:00
|
|
|
|
free(p->title);
|
|
|
|
|
db_series_unfill(&p->s);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 14:54:32 +00:00
|
|
|
|
static void
|
|
|
|
|
db_author_unfill(Author *p) {
|
|
|
|
|
if (NULL == p)
|
|
|
|
|
return;
|
|
|
|
|
free(p->name);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-01 14:44:51 +00:00
|
|
|
|
static void
|
|
|
|
|
db_book_data_unfill(BookData *p) {
|
|
|
|
|
if (NULL == p)
|
|
|
|
|
return;
|
|
|
|
|
free(p->name);
|
|
|
|
|
free(p->format);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 10:19:23 +00:00
|
|
|
|
void
|
|
|
|
|
db_book_free(Book *p)
|
|
|
|
|
{
|
|
|
|
|
db_book_unfill(p);
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
db_book_adv_free(BookAdv *p)
|
|
|
|
|
{
|
2016-12-31 14:33:09 +00:00
|
|
|
|
if (NULL == p)
|
|
|
|
|
return;
|
2017-01-03 18:14:19 +00:00
|
|
|
|
size_t i;
|
|
|
|
|
|
2016-12-30 10:19:23 +00:00
|
|
|
|
db_book_unfill(&p->b);
|
2017-01-03 18:14:19 +00:00
|
|
|
|
for (i = 0; i < p->asize; i++) {
|
2016-12-30 14:54:32 +00:00
|
|
|
|
db_author_unfill(p->a[i]);
|
|
|
|
|
}
|
2017-01-03 18:14:19 +00:00
|
|
|
|
for (i = 0; i < p->tsize; i++) {
|
|
|
|
|
free(p->tags[i]);
|
|
|
|
|
}
|
2016-12-30 10:19:23 +00:00
|
|
|
|
free(p);
|
2016-12-29 23:45:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 14:33:09 +00:00
|
|
|
|
void
|
|
|
|
|
db_book_full_unfill(BookFull *p)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == p)
|
|
|
|
|
return;
|
2017-01-01 14:44:51 +00:00
|
|
|
|
for (size_t i = 0; i < p->dsize; i++) {
|
|
|
|
|
db_book_data_unfill(p->data[i]);
|
|
|
|
|
}
|
2016-12-31 14:33:09 +00:00
|
|
|
|
free(p->isbn);
|
|
|
|
|
free(p->lccn);
|
|
|
|
|
free(p->path);
|
|
|
|
|
free(p->uuid);
|
|
|
|
|
free(p->lang);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 10:37:08 +00:00
|
|
|
|
static void
|
|
|
|
|
db_book_fill(Book *book, struct ksqlstmt *stmt)
|
|
|
|
|
{
|
|
|
|
|
book->id = ksql_stmt_int(stmt, 0);
|
|
|
|
|
book->title = kstrdup(ksql_stmt_str(stmt, 1));
|
|
|
|
|
if ( ksql_stmt_isnull(stmt, 4) ) {
|
|
|
|
|
book->s.id = -1;
|
|
|
|
|
} else {
|
|
|
|
|
book->s_idx = ksql_stmt_double(stmt, 2);
|
|
|
|
|
book->s.name = kstrdup(ksql_stmt_str(stmt, 3));
|
|
|
|
|
book->s.id = ksql_stmt_int(stmt, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 09:52:48 +00:00
|
|
|
|
int
|
|
|
|
|
db_books_count(struct kreq *r)
|
|
|
|
|
{
|
|
|
|
|
int count;
|
|
|
|
|
struct ksqlstmt *stmt;
|
|
|
|
|
|
|
|
|
|
ksql_stmt_alloc(r->arg, &stmt,
|
|
|
|
|
stmts[STMT_BOOKS_COUNT],
|
|
|
|
|
STMT_BOOKS_COUNT);
|
|
|
|
|
if (KSQL_ROW != ksql_stmt_step(stmt)) {
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
count = ksql_stmt_int(stmt, 0);
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
return(count);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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) {
|
2017-01-03 18:48:22 +00:00
|
|
|
|
p->b->tags = kcalloc(p->b->tsize, sizeof(char*));
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 14:33:09 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
static struct booktag *
|
|
|
|
|
db_load_books_tags(struct kreq *r, BookAdv **books, int count, int limit, int offset)
|
2016-12-29 23:38:31 +00:00
|
|
|
|
{
|
|
|
|
|
struct ksqlstmt *stmt;
|
2017-01-03 18:14:19 +00:00
|
|
|
|
struct booktag *p = NULL;
|
|
|
|
|
struct booktag *item = NULL;
|
|
|
|
|
int i;
|
2016-12-30 14:54:32 +00:00
|
|
|
|
int64_t bid;
|
2016-12-30 14:40:21 +00:00
|
|
|
|
|
2016-12-29 23:38:31 +00:00
|
|
|
|
ksql_stmt_alloc(r->arg, &stmt,
|
2017-01-03 18:14:19 +00:00
|
|
|
|
stmts[STMT_BOOKS_TAGS],
|
|
|
|
|
STMT_BOOKS_TAGS);
|
2016-12-29 23:38:31 +00:00
|
|
|
|
ksql_bind_int(stmt, 0, limit);
|
2016-12-30 15:16:00 +00:00
|
|
|
|
ksql_bind_int(stmt, 1, offset);
|
2016-12-29 23:38:31 +00:00
|
|
|
|
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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;
|
2016-12-30 14:40:21 +00:00
|
|
|
|
}
|
|
|
|
|
ksql_stmt_free(stmt);
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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;
|
2016-12-30 14:40:21 +00:00
|
|
|
|
|
|
|
|
|
ksql_stmt_alloc(r->arg, &stmt,
|
|
|
|
|
stmts[STMT_BOOKS_AUTHORS],
|
|
|
|
|
STMT_BOOKS_AUTHORS);
|
|
|
|
|
ksql_bind_int(stmt, 0, limit);
|
2016-12-30 15:16:00 +00:00
|
|
|
|
ksql_bind_int(stmt, 1, offset);
|
2016-12-30 14:40:21 +00:00
|
|
|
|
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
|
|
|
|
// add author to list, link related book and increment asize
|
2016-12-30 14:54:32 +00:00
|
|
|
|
item = kcalloc(1, sizeof(struct bookauth));
|
2016-12-30 14:40:21 +00:00
|
|
|
|
item->a = kcalloc(1, sizeof(Author));
|
|
|
|
|
item->a->id = ksql_stmt_int(stmt, 0);
|
|
|
|
|
item->a->name = kstrdup(ksql_stmt_str(stmt, 1));
|
2016-12-30 14:54:32 +00:00
|
|
|
|
bid = ksql_stmt_int(stmt, 2);
|
2016-12-30 14:40:21 +00:00
|
|
|
|
for (i = 0; i < count && NULL == item->b; i++) {
|
|
|
|
|
if (books[i]->b.id == bid) {
|
|
|
|
|
item->b = books[i];
|
|
|
|
|
item->b->asize++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
item->p = p;
|
|
|
|
|
p = item;
|
2016-12-29 23:38:31 +00:00
|
|
|
|
}
|
2017-01-03 18:14:19 +00:00
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2016-12-30 14:40:21 +00:00
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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;
|
2017-01-03 18:48:22 +00:00
|
|
|
|
books[count]->tsize = 0;
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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);
|
2016-12-31 14:33:09 +00:00
|
|
|
|
|
2016-12-30 14:40:21 +00:00
|
|
|
|
return count;
|
2016-12-29 23:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
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++;
|
2017-01-03 18:48:22 +00:00
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
item->p = p;
|
|
|
|
|
p = item;
|
|
|
|
|
}
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
|
|
|
|
|
db_assign_book_tags(p);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-01 14:44:51 +00:00
|
|
|
|
static void
|
|
|
|
|
db_book_full_authors(BookAdv *b, struct kreq *r, int64_t id) {
|
|
|
|
|
struct ksqlstmt *stmt;
|
|
|
|
|
struct bookauth *p = NULL;
|
|
|
|
|
struct bookauth *item = NULL;
|
|
|
|
|
|
|
|
|
|
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 = b;
|
|
|
|
|
item->b->asize++;
|
|
|
|
|
item->p = p;
|
|
|
|
|
p = item;
|
|
|
|
|
}
|
2017-01-03 18:14:19 +00:00
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
|
2017-01-01 14:44:51 +00:00
|
|
|
|
// assign authors to book
|
|
|
|
|
db_assign_book_authors(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
db_book_full_data(BookFull *b, struct kreq *r, int64_t id) {
|
|
|
|
|
struct ksqlstmt *stmt;
|
|
|
|
|
struct bookdata *p = NULL;
|
|
|
|
|
struct bookdata *item = NULL;
|
|
|
|
|
|
|
|
|
|
ksql_stmt_alloc(r->arg, &stmt,
|
|
|
|
|
stmts[STMT_BOOK_DATA],
|
|
|
|
|
STMT_BOOK_DATA);
|
|
|
|
|
ksql_bind_int(stmt, 0, id);
|
|
|
|
|
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
|
|
|
|
item = kcalloc(1, sizeof(struct bookdata));
|
|
|
|
|
item->d = kcalloc(1, sizeof(BookData));
|
|
|
|
|
item->d->name = kstrdup(ksql_stmt_str(stmt, 0));
|
|
|
|
|
item->d->format = kstrdup(ksql_stmt_str(stmt, 1));
|
|
|
|
|
item->d->size = ksql_stmt_int(stmt, 2);
|
|
|
|
|
item->b = b;
|
|
|
|
|
item->b->dsize++;
|
|
|
|
|
item->p = p;
|
|
|
|
|
p = item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
|
|
|
|
|
while (NULL != p) {
|
|
|
|
|
if (NULL != p->b && NULL == p->b->data && p->b->dsize > 0) {
|
|
|
|
|
p->b->data = kcalloc(p->b->dsize, sizeof(BookData));
|
|
|
|
|
p->b->dsize = 0; // use as counter for insert
|
|
|
|
|
}
|
|
|
|
|
p->b->data[p->b->dsize] = p->d;
|
|
|
|
|
p->b->dsize++;
|
|
|
|
|
item = p;
|
|
|
|
|
p = p->p;
|
|
|
|
|
free(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 14:33:09 +00:00
|
|
|
|
BookFull *
|
2016-12-29 20:02:58 +00:00
|
|
|
|
db_book_load(struct kreq *r, int64_t id)
|
|
|
|
|
{
|
|
|
|
|
struct ksqlstmt *stmt;
|
2016-12-31 14:33:09 +00:00
|
|
|
|
BookFull *book;
|
2016-12-30 09:15:14 +00:00
|
|
|
|
|
2016-12-29 20:02:58 +00:00
|
|
|
|
ksql_stmt_alloc(r->arg, &stmt,
|
|
|
|
|
stmts[STMT_BOOK],
|
|
|
|
|
STMT_BOOK);
|
|
|
|
|
ksql_bind_int(stmt, 0, id);
|
|
|
|
|
if (KSQL_ROW != ksql_stmt_step(stmt)) {
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
return(NULL);
|
|
|
|
|
}
|
2016-12-31 14:33:09 +00:00
|
|
|
|
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);
|
2016-12-31 15:53:44 +00:00
|
|
|
|
book->isbn = strornull(stmt, 7);
|
|
|
|
|
book->lccn = strornull(stmt, 8);
|
|
|
|
|
book->path = strornull(stmt, 9);
|
|
|
|
|
book->uuid = strornull(stmt, 10);
|
2016-12-31 14:33:09 +00:00
|
|
|
|
book->has_cover = ksql_stmt_int(stmt, 11);
|
2016-12-31 15:53:44 +00:00
|
|
|
|
book->lang = strornull(stmt, 12);
|
2017-01-01 14:44:51 +00:00
|
|
|
|
book->publisher = strornull(stmt, 13);
|
2016-12-31 14:33:09 +00:00
|
|
|
|
|
|
|
|
|
ksql_stmt_free(stmt);
|
|
|
|
|
|
2017-01-03 18:14:19 +00:00
|
|
|
|
db_book_full_tags(&book->ba, r, id);
|
2017-01-01 14:44:51 +00:00
|
|
|
|
db_book_full_authors(&book->ba, r, id);
|
|
|
|
|
db_book_full_data(book, r, id);
|
2016-12-31 14:33:09 +00:00
|
|
|
|
|
2016-12-29 20:02:58 +00:00
|
|
|
|
return book;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:50:56 +00:00
|
|
|
|
/*
|
2016-12-29 19:41:31 +00:00
|
|
|
|
* Open the database and stash the resulting handle in the d
|
2016-12-18 15:50:56 +00:00
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
db_open(struct kreq *r, const char *file)
|
|
|
|
|
{
|
2016-12-29 19:41:31 +00:00
|
|
|
|
struct ksqlcfg cfg;
|
|
|
|
|
struct ksql *sql;
|
2016-12-18 15:50:56 +00:00
|
|
|
|
|
2016-12-29 19:41:31 +00:00
|
|
|
|
/* Configure normal database except with foreign keys. */
|
|
|
|
|
|
|
|
|
|
memset(&cfg, 0, sizeof(struct ksqlcfg));
|
|
|
|
|
cfg.flags = KSQL_EXIT_ON_ERR |
|
|
|
|
|
KSQL_FOREIGN_KEYS |
|
|
|
|
|
KSQL_SAFE_EXIT;
|
|
|
|
|
cfg.err = ksqlitemsg;
|
|
|
|
|
cfg.dberr = ksqlitedbmsg;
|
|
|
|
|
|
|
|
|
|
/* Allocate database. */
|
|
|
|
|
|
|
|
|
|
if (NULL == (sql = ksql_alloc(&cfg)))
|
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
|
|
ksql_open(sql, file);
|
|
|
|
|
r->arg = sql;
|
|
|
|
|
return(1);
|
2016-12-18 15:50:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Close the database stashed in the kreq's argument.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
db_close(struct kreq *r)
|
|
|
|
|
{
|
2016-12-29 19:41:31 +00:00
|
|
|
|
|
|
|
|
|
ksql_free(r->arg);
|
|
|
|
|
r->arg = NULL;
|
2016-12-18 15:50:56 +00:00
|
|
|
|
}
|