Sort and order books, authors, series
This commit is contained in:
parent
f1cd51b551
commit
d23aa7f0ec
2
db.c
2
db.c
@ -28,6 +28,8 @@
|
|||||||
|
|
||||||
#include "extern.h"
|
#include "extern.h"
|
||||||
|
|
||||||
|
#define STMT_PAGE " LIMIT ? OFFSET ?"
|
||||||
|
|
||||||
char *
|
char *
|
||||||
strornull(struct ksqlstmt *stmt, int col)
|
strornull(struct ksqlstmt *stmt, int col)
|
||||||
{
|
{
|
||||||
|
49
db_author.c
49
db_author.c
@ -28,9 +28,15 @@
|
|||||||
|
|
||||||
#include "extern.h"
|
#include "extern.h"
|
||||||
|
|
||||||
|
#define STMT_PAGE " LIMIT ? OFFSET ?"
|
||||||
|
#define STMT_AUTHORS0 "SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link "\
|
||||||
|
"WHERE authors.id = books_authors_link.author GROUP BY author "
|
||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
STMT_AUTHORS_ID,
|
STMT_AUTHORS_ID_ASC,
|
||||||
STMT_AUTHORS_NAME,
|
STMT_AUTHORS_ID_DESC,
|
||||||
|
STMT_AUTHORS_NAME_ASC,
|
||||||
|
STMT_AUTHORS_NAME_DESC,
|
||||||
STMT_AUTHOR_BOOKS,
|
STMT_AUTHOR_BOOKS,
|
||||||
STMT_AUTHORS_AUTHORS,
|
STMT_AUTHORS_AUTHORS,
|
||||||
STMT_AUTHOR,
|
STMT_AUTHOR,
|
||||||
@ -38,14 +44,14 @@ enum stmt {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const char *const stmts[STMT__MAX] = {
|
static const char *const stmts[STMT__MAX] = {
|
||||||
/* STMT_AUTHORS_ID */
|
/* STMT_AUTHORS_ID_ASC */
|
||||||
"SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link \
|
STMT_AUTHORS0 "ORDER BY authors.id " STMT_PAGE,
|
||||||
WHERE authors.id = books_authors_link.author GROUP BY author \
|
/* STMT_AUTHORS_ID_DESC */
|
||||||
ORDER BY authors.id LIMIT ? OFFSET ?",
|
STMT_AUTHORS0 "ORDER BY authors.id DESC " STMT_PAGE,
|
||||||
/* STMT_AUTHORS_NAME */
|
/* STMT_AUTHORS_NAME_ASC */
|
||||||
"SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link \
|
STMT_AUTHORS0 "ORDER BY authors.sort " STMT_PAGE,
|
||||||
WHERE authors.id = books_authors_link.author GROUP BY author \
|
/* STMT_AUTHORS_NAME_DESC */
|
||||||
ORDER BY authors.sort LIMIT ? OFFSET ?",
|
STMT_AUTHORS0 "ORDER BY authors.sort DESC " STMT_PAGE,
|
||||||
/* STMT_AUTHOR_BOOKS */
|
/* STMT_AUTHOR_BOOKS */
|
||||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_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 \
|
FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book \
|
||||||
@ -81,6 +87,18 @@ db_author_free(Author *p) {
|
|||||||
db_author_unfill(p);
|
db_author_unfill(p);
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_authors(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_AUTHORS_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "name"))
|
||||||
|
query = STMT_AUTHORS_NAME_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
AuthorFull *
|
AuthorFull *
|
||||||
db_author_load(struct kreq *r, int64_t id)
|
db_author_load(struct kreq *r, int64_t id)
|
||||||
{
|
{
|
||||||
@ -150,20 +168,15 @@ db_author_load(struct kreq *r, int64_t id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, int limit, int offset)
|
db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, const char *order, int limit, int offset)
|
||||||
{
|
{
|
||||||
if (limit < 0 || offset < 0) {
|
if (limit < 0 || offset < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
int query, count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if (sort == NULL)
|
int query = query_authors(sort, order);
|
||||||
query = STMT_AUTHORS_ID;
|
|
||||||
else if (0 == strcmp(sort, "name"))
|
|
||||||
query = STMT_AUTHORS_NAME;
|
|
||||||
else
|
|
||||||
query = STMT_AUTHORS_ID;
|
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[query],
|
stmts[query],
|
||||||
query);
|
query);
|
||||||
|
144
db_book.c
144
db_book.c
@ -28,22 +28,40 @@
|
|||||||
|
|
||||||
#include "extern.h"
|
#include "extern.h"
|
||||||
|
|
||||||
|
#define STMT_PAGE " LIMIT ? OFFSET ?"
|
||||||
|
#define STMT_BOOKS0 "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 "
|
||||||
|
#define STMT_BOOKS_TAGS0 "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 "
|
||||||
|
#define STMT_BOOKS_AUTHORS0 "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 "
|
||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
|
STMT_BOOKS_COUNT,
|
||||||
STMT_BOOK,
|
STMT_BOOK,
|
||||||
STMT_BOOK_TAGS,
|
STMT_BOOK_TAGS,
|
||||||
STMT_BOOK_AUTHORS,
|
STMT_BOOK_AUTHORS,
|
||||||
STMT_BOOK_DATA,
|
STMT_BOOK_DATA,
|
||||||
STMT_BOOKS_ID,
|
STMT_BOOKS_ID_ASC,
|
||||||
STMT_BOOKS_TITLE,
|
STMT_BOOKS_ID_DESC,
|
||||||
STMT_BOOKS_COUNT,
|
STMT_BOOKS_TITLE_ASC,
|
||||||
STMT_BOOKS_TAGS_ID,
|
STMT_BOOKS_TITLE_DESC,
|
||||||
STMT_BOOKS_TAGS_TITLE,
|
STMT_BOOKS_TAGS_ID_ASC,
|
||||||
STMT_BOOKS_AUTHORS_ID,
|
STMT_BOOKS_TAGS_ID_DESC,
|
||||||
STMT_BOOKS_AUTHORS_TITLE,
|
STMT_BOOKS_TAGS_TITLE_ASC,
|
||||||
|
STMT_BOOKS_TAGS_TITLE_DESC,
|
||||||
|
STMT_BOOKS_AUTHORS_ID_ASC,
|
||||||
|
STMT_BOOKS_AUTHORS_ID_DESC,
|
||||||
|
STMT_BOOKS_AUTHORS_TITLE_ASC,
|
||||||
|
STMT_BOOKS_AUTHORS_TITLE_DESC,
|
||||||
STMT__MAX
|
STMT__MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *const stmts[STMT__MAX] = {
|
static const char *const stmts[STMT__MAX] = {
|
||||||
|
/* STMT_BOOKS_COUNT */
|
||||||
|
"SELECT count(id) FROM books",
|
||||||
/* STMT_BOOK */
|
/* STMT_BOOK */
|
||||||
"SELECT books.id AS id,title, series_index, series.name AS series_name, series.id AS series_id, \
|
"SELECT books.id AS id,title, series_index, series.name AS series_name, series.id AS series_id, \
|
||||||
strftime('%s', timestamp), strftime('%Y', pubdate), isbn,lccn,path,uuid,has_cover, \
|
strftime('%s', timestamp), strftime('%Y', pubdate), isbn,lccn,path,uuid,has_cover, \
|
||||||
@ -65,32 +83,30 @@ static const char *const stmts[STMT__MAX] = {
|
|||||||
/* STMT_BOOK_DATA */
|
/* STMT_BOOK_DATA */
|
||||||
"SELECT data.name, data.format, data.uncompressed_size \
|
"SELECT data.name, data.format, data.uncompressed_size \
|
||||||
FROM data WHERE data.book = ?",
|
FROM data WHERE data.book = ?",
|
||||||
/* STMT_BOOKS_ID */
|
/* STMT_BOOKS_ID_ASC */
|
||||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id \
|
STMT_BOOKS0 " ORDER BY id" STMT_PAGE,
|
||||||
FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book \
|
/* STMT_BOOKS_ID_DESC */
|
||||||
LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY id LIMIT ? OFFSET ?",
|
STMT_BOOKS0 "ORDER BY id DESC" STMT_PAGE,
|
||||||
/* STMT_BOOKS_TITLE */
|
/* STMT_BOOKS_TITLE_ASC */
|
||||||
"SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id \
|
STMT_BOOKS0 "ORDER BY books.sort" STMT_PAGE,
|
||||||
FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book \
|
/* STMT_BOOKS_TITLE_DESC */
|
||||||
LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY books.sort LIMIT ? OFFSET ?",
|
STMT_BOOKS0 "ORDER BY books.sort DESC" STMT_PAGE,
|
||||||
/* STMT_BOOKS_COUNT */
|
/* STMT_BOOKS_TAGS_ID_ASC */
|
||||||
"SELECT count(id) FROM books",
|
STMT_BOOKS_TAGS0 "ORDER BY id" STMT_PAGE ")",
|
||||||
/* STMT_BOOKS_TAGS_ID */
|
/* STMT_BOOKS_TAGS_ID_DESC */
|
||||||
"SELECT name, books_tags_link.book as book FROM tags, books_tags_link \
|
STMT_BOOKS_TAGS0 "ORDER BY id DESC" STMT_PAGE ")",
|
||||||
WHERE tags.id = books_tags_link.tag AND books_tags_link.book \
|
/* STMT_BOOKS_TAGS_TITLE_ASC */
|
||||||
IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)",
|
STMT_BOOKS_TAGS0 "ORDER BY books.sort" STMT_PAGE ")",
|
||||||
/* STMT_BOOKS_TAGS_TITLE */
|
/* STMT_BOOKS_TAGS_TITLE_DESC */
|
||||||
"SELECT name, books_tags_link.book as book FROM tags, books_tags_link \
|
STMT_BOOKS_TAGS0 "ORDER BY books.sort DESC" STMT_PAGE ")",
|
||||||
WHERE tags.id = books_tags_link.tag AND books_tags_link.book \
|
/* STMT_BOOKS_AUTHORS_ID_ASC */
|
||||||
IN ( SELECT id FROM books ORDER BY books.sort LIMIT ? OFFSET ?)",
|
STMT_BOOKS_AUTHORS0 "ORDER BY id" STMT_PAGE ")",
|
||||||
/* STMT_BOOKS_AUTHORS_ID */
|
/* STMT_BOOKS_AUTHORS_ID_DESC */
|
||||||
"SELECT authors.id, authors.name, books_authors_link.book as book \
|
STMT_BOOKS_AUTHORS0 "ORDER BY id DESC" STMT_PAGE ")",
|
||||||
FROM authors, books_authors_link WHERE books_authors_link.author = authors.id \
|
/* STMT_BOOKS_AUTHORS_TITLE_ASC */
|
||||||
AND books_authors_link.book IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)",
|
STMT_BOOKS_AUTHORS0 "ORDER BY books.sort" STMT_PAGE ")",
|
||||||
/* STMT_BOOKS_AUTHORS_TITLE */
|
/* STMT_BOOKS_AUTHORS_TITLE_DESC */
|
||||||
"SELECT authors.id, authors.name, books_authors_link.book as book \
|
STMT_BOOKS_AUTHORS0 "ORDER BY books.sort DESC" STMT_PAGE ")",
|
||||||
FROM authors, books_authors_link WHERE books_authors_link.author = authors.id \
|
|
||||||
AND books_authors_link.book IN ( SELECT id FROM books ORDER BY books.sort LIMIT ? OFFSET ?)",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -235,21 +251,49 @@ db_assign_book_authors(struct bookauth *list)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_books(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_BOOKS_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "title"))
|
||||||
|
query = STMT_BOOKS_TITLE_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_books_authors(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_BOOKS_AUTHORS_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "title"))
|
||||||
|
query = STMT_BOOKS_AUTHORS_TITLE_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_books_tags(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_BOOKS_TAGS_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "title"))
|
||||||
|
query = STMT_BOOKS_TAGS_TITLE_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
static struct booktag *
|
static struct booktag *
|
||||||
db_load_books_tags(struct kreq *r, BookAdv **books, int count, const char *sort, int limit, int offset)
|
db_load_books_tags(struct kreq *r, BookAdv **books, int count, const char *sort, const char *order, int limit, int offset)
|
||||||
{
|
{
|
||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
struct booktag *p = NULL;
|
struct booktag *p = NULL;
|
||||||
struct booktag *item = NULL;
|
struct booktag *item = NULL;
|
||||||
int i, query;
|
int i;
|
||||||
int64_t bid;
|
int64_t bid;
|
||||||
|
|
||||||
if (sort == NULL)
|
int query = query_books_tags(sort, order);
|
||||||
query = STMT_BOOKS_TAGS_ID;
|
|
||||||
else if (0 == strcmp(sort, "title"))
|
|
||||||
query = STMT_BOOKS_TAGS_TITLE;
|
|
||||||
else
|
|
||||||
query = STMT_BOOKS_TAGS_ID;
|
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[query],
|
stmts[query],
|
||||||
query);
|
query);
|
||||||
@ -300,7 +344,7 @@ db_load_books_authors(BookAdv **books, int count, struct ksqlstmt *stmt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
db_books_load(struct kreq *r, BookAdv **books, const char *sort, int limit, int offset)
|
db_books_load(struct kreq *r, BookAdv **books, const char *sort, const char *order, int limit, int offset)
|
||||||
{
|
{
|
||||||
if (limit < 0 || offset < 0) {
|
if (limit < 0 || offset < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -308,18 +352,9 @@ db_books_load(struct kreq *r, BookAdv **books, const char *sort, int limit, int
|
|||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
struct bookauth *p_a;
|
struct bookauth *p_a;
|
||||||
struct booktag *p_t;
|
struct booktag *p_t;
|
||||||
int query, querya, count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if (sort == NULL) {
|
int query = query_books(sort, order);
|
||||||
query = STMT_BOOKS_ID;
|
|
||||||
querya = STMT_BOOKS_AUTHORS_ID;
|
|
||||||
} else if (0 == strcmp(sort, "title")) {
|
|
||||||
query = STMT_BOOKS_TITLE;
|
|
||||||
querya = STMT_BOOKS_AUTHORS_TITLE;
|
|
||||||
} else {
|
|
||||||
query = STMT_BOOKS_ID;
|
|
||||||
querya = STMT_BOOKS_AUTHORS_ID;
|
|
||||||
}
|
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[query],
|
stmts[query],
|
||||||
query);
|
query);
|
||||||
@ -334,6 +369,7 @@ db_books_load(struct kreq *r, BookAdv **books, const char *sort, int limit, int
|
|||||||
}
|
}
|
||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
|
int querya = query_books_authors(sort, order);
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[querya],
|
stmts[querya],
|
||||||
querya);
|
querya);
|
||||||
@ -343,7 +379,7 @@ db_books_load(struct kreq *r, BookAdv **books, const char *sort, int limit, int
|
|||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
db_assign_book_authors(p_a);
|
db_assign_book_authors(p_a);
|
||||||
|
|
||||||
p_t = db_load_books_tags(r, books, count, sort, limit, offset);
|
p_t = db_load_books_tags(r, books, count, sort, order, limit, offset);
|
||||||
db_assign_book_tags(p_t);
|
db_assign_book_tags(p_t);
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
93
db_series.c
93
db_series.c
@ -28,11 +28,24 @@
|
|||||||
|
|
||||||
#include "extern.h"
|
#include "extern.h"
|
||||||
|
|
||||||
|
#define STMT_PAGE " LIMIT ? OFFSET ?"
|
||||||
|
#define STMT_SERIES0 "SELECT series.id, series.name, count(book) FROM series "\
|
||||||
|
"LEFT OUTER JOIN books_series_link ON books_series_link.series = series.id "\
|
||||||
|
"GROUP BY series.id "
|
||||||
|
#define STMT_SERIES_AUTHORS0 "SELECT DISTINCT authors.id, authors.name, books_series_link.series "\
|
||||||
|
"FROM authors, books_authors_link, books_series_link "\
|
||||||
|
"WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id "\
|
||||||
|
"AND books_series_link.series IN ( SELECT id FROM series "
|
||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
STMT_SERIES_ID,
|
STMT_SERIES_ID_ASC,
|
||||||
STMT_SERIES_NAME,
|
STMT_SERIES_ID_DESC,
|
||||||
STMT_SERIES_AUTHORS_ID,
|
STMT_SERIES_NAME_ASC,
|
||||||
STMT_SERIES_AUTHORS_NAME,
|
STMT_SERIES_NAME_DESC,
|
||||||
|
STMT_SERIES_AUTHORS_ID_ASC,
|
||||||
|
STMT_SERIES_AUTHORS_ID_DESC,
|
||||||
|
STMT_SERIES_AUTHORS_NAME_ASC,
|
||||||
|
STMT_SERIES_AUTHORS_NAME_DESC,
|
||||||
STMT_SERIE,
|
STMT_SERIE,
|
||||||
STMT_SERIE_BOOKS,
|
STMT_SERIE_BOOKS,
|
||||||
STMT_SERIE_AUTHORS,
|
STMT_SERIE_AUTHORS,
|
||||||
@ -40,24 +53,22 @@ enum stmt {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const char *const stmts[STMT__MAX] = {
|
static const char *const stmts[STMT__MAX] = {
|
||||||
/* STMT_SERIES_ID */
|
/* STMT_SERIES_ID_ASC */
|
||||||
"SELECT series.id, series.name, count(book) FROM series \
|
STMT_SERIES0 " ORDER BY series.id" STMT_PAGE,
|
||||||
LEFT OUTER JOIN books_series_link ON books_series_link.series = series.id \
|
/* STMT_SERIES_ID_DESC */
|
||||||
GROUP BY series.id ORDER BY series.id LIMIT ? OFFSET ?",
|
STMT_SERIES0 " ORDER BY series.id DESC" STMT_PAGE,
|
||||||
/* STMT_SERIES_NAME */
|
/* STMT_SERIES_NAME_ASC */
|
||||||
"SELECT series.id, series.name, count(book) FROM series \
|
STMT_SERIES0 " ORDER BY series.sort" STMT_PAGE,
|
||||||
LEFT OUTER JOIN books_series_link ON books_series_link.series = series.id \
|
/* STMT_SERIES_NAME_DESC */
|
||||||
GROUP BY series.id ORDER BY sort LIMIT ? OFFSET ?",
|
STMT_SERIES0 " ORDER BY series.sort DESC" STMT_PAGE,
|
||||||
/* STMT_SERIES_AUTHORS_ID */
|
/* STMT_SERIES_AUTHORS_ID_ASC */
|
||||||
"SELECT DISTINCT authors.id, authors.name, books_series_link.series \
|
STMT_SERIES_AUTHORS0 " ORDER BY series.id" STMT_PAGE ")",
|
||||||
FROM authors, books_authors_link, books_series_link \
|
/* STMT_SERIES_AUTHORS_ID_DESC */
|
||||||
WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id \
|
STMT_SERIES_AUTHORS0 " ORDER BY series.id DESC" STMT_PAGE ")",
|
||||||
AND books_series_link.series IN ( SELECT id FROM series ORDER BY series.id LIMIT ? OFFSET ?)",
|
/* STMT_SERIES_AUTHORS_NAME_ASC */
|
||||||
/* STMT_SERIES_AUTHORS */
|
STMT_SERIES_AUTHORS0 " ORDER BY series.sort" STMT_PAGE ")",
|
||||||
"SELECT DISTINCT authors.id, authors.name, books_series_link.series \
|
/* STMT_SERIES_AUTHORS_NAME_DESC */
|
||||||
FROM authors, books_authors_link, books_series_link \
|
STMT_SERIES_AUTHORS0 " ORDER BY series.sort DESC" STMT_PAGE ")",
|
||||||
WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id \
|
|
||||||
AND books_series_link.series IN ( SELECT id FROM series ORDER BY sort LIMIT ? OFFSET ?)",
|
|
||||||
/* STMT_SERIE */
|
/* STMT_SERIE */
|
||||||
"SELECT series.id, series.name FROM series WHERE series.id = ?",
|
"SELECT series.id, series.name FROM series WHERE series.id = ?",
|
||||||
/* STMT_SERIE_BOOKS */
|
/* STMT_SERIE_BOOKS */
|
||||||
@ -109,6 +120,28 @@ db_series_adv_free(SeriesAdv *p)
|
|||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_series(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_SERIES_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "name"))
|
||||||
|
query = STMT_SERIES_NAME_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
query_seriesauth(const char* sort, const char *order)
|
||||||
|
{
|
||||||
|
int query = STMT_SERIES_AUTHORS_ID_ASC;
|
||||||
|
if (NULL != sort && 0 == strcmp(sort, "name"))
|
||||||
|
query = STMT_SERIES_AUTHORS_NAME_ASC;
|
||||||
|
if (NULL != order && 0 == strcmp(order, "desc"))
|
||||||
|
query++;
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
db_assign_series_authors(struct seriesauth *list)
|
db_assign_series_authors(struct seriesauth *list)
|
||||||
{
|
{
|
||||||
@ -255,24 +288,15 @@ db_serie_load(struct kreq *r, int64_t id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, int limit, int offset)
|
db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, const char *order, int limit, int offset)
|
||||||
{
|
{
|
||||||
if (limit < 0 || offset < 0)
|
if (limit < 0 || offset < 0)
|
||||||
return 0;
|
return 0;
|
||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
int query, queryauth, count = 0;
|
int count = 0;
|
||||||
struct seriesauth *p = NULL;
|
struct seriesauth *p = NULL;
|
||||||
|
|
||||||
if (sort == NULL) {
|
int query = query_series(sort, order);
|
||||||
query = STMT_SERIES_ID;
|
|
||||||
queryauth = STMT_SERIES_AUTHORS_ID;
|
|
||||||
} else if (0 == strcmp(sort, "name")) {
|
|
||||||
query = STMT_SERIES_NAME;
|
|
||||||
queryauth = STMT_SERIES_AUTHORS_NAME;
|
|
||||||
} else {
|
|
||||||
query = STMT_SERIES_ID;
|
|
||||||
queryauth = STMT_SERIES_AUTHORS_ID;
|
|
||||||
}
|
|
||||||
|
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[query],
|
stmts[query],
|
||||||
@ -289,6 +313,7 @@ db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, int limit,
|
|||||||
}
|
}
|
||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
|
|
||||||
|
int queryauth = query_seriesauth(sort, order);
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[queryauth],
|
stmts[queryauth],
|
||||||
queryauth);
|
queryauth);
|
||||||
|
8
extern.h
8
extern.h
@ -146,7 +146,7 @@ struct bookdata {
|
|||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
BookFull *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, const char *sort, int limit, int offset);
|
int db_books_load(struct kreq *r, BookAdv **books, const char *sort, const char *order, 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);
|
||||||
@ -157,12 +157,14 @@ void db_assign_book_authors(struct bookauth *list);
|
|||||||
|
|
||||||
void db_author_unfill(Author *p);
|
void db_author_unfill(Author *p);
|
||||||
AuthorFull *db_author_load(struct kreq *r, int64_t id);
|
AuthorFull *db_author_load(struct kreq *r, int64_t id);
|
||||||
int db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, int limit, int offset);
|
int db_authors_load(struct kreq *r, AuthorAdv **authors, const char *sort, const char *order,
|
||||||
|
int limit, int offset);
|
||||||
void db_author_free(Author *p);
|
void db_author_free(Author *p);
|
||||||
void db_author_adv_free(AuthorAdv *p);
|
void db_author_adv_free(AuthorAdv *p);
|
||||||
|
|
||||||
void db_series_unfill(Series *p);
|
void db_series_unfill(Series *p);
|
||||||
int db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, int limit, int offset);
|
int db_series_load(struct kreq *r, SeriesAdv **series, const char *sort, const char *order,
|
||||||
|
int limit, int offset);
|
||||||
SeriesFull *db_serie_load(struct kreq *r, int64_t id);
|
SeriesFull *db_serie_load(struct kreq *r, int64_t id);
|
||||||
void db_series_free(Series *p);
|
void db_series_free(Series *p);
|
||||||
void db_series_adv_free(SeriesAdv *p);
|
void db_series_adv_free(SeriesAdv *p);
|
||||||
|
19
main.c
19
main.c
@ -45,6 +45,7 @@ enum key {
|
|||||||
KEY_PAGE,
|
KEY_PAGE,
|
||||||
KEY_PERPAGE,
|
KEY_PERPAGE,
|
||||||
KEY_SORT,
|
KEY_SORT,
|
||||||
|
KEY_ORDER,
|
||||||
KEY__MAX
|
KEY__MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -59,6 +60,7 @@ static const struct kvalid keys[KEY__MAX] = {
|
|||||||
{ kvalid_int, "page" }, /* KEY_PAGE */
|
{ kvalid_int, "page" }, /* KEY_PAGE */
|
||||||
{ kvalid_int, "perpage" }, /* KEY_PERPAGE */
|
{ kvalid_int, "perpage" }, /* KEY_PERPAGE */
|
||||||
{ kvalid_string, "sort" }, /* KEY_SORT */
|
{ kvalid_string, "sort" }, /* KEY_SORT */
|
||||||
|
{ kvalid_string, "order" }, /* KEY_ORDER */
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -205,6 +207,14 @@ initsort(struct kreq *r)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char*
|
||||||
|
initorder(struct kreq *r)
|
||||||
|
{
|
||||||
|
if (NULL != r->fieldmap[KEY_ORDER])
|
||||||
|
return r->fieldmap[KEY_ORDER]->parsed.s;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
initpage(struct kreq *r)
|
initpage(struct kreq *r)
|
||||||
{
|
{
|
||||||
@ -240,6 +250,7 @@ sendbooks(struct kreq *r)
|
|||||||
int page = initpage(r);
|
int page = initpage(r);
|
||||||
int per = initperpage(r);
|
int per = initperpage(r);
|
||||||
const char* sort = initsort(r);
|
const char* sort = initsort(r);
|
||||||
|
const char* order = initorder(r);
|
||||||
int64_t id = idfrompath(r);
|
int64_t id = idfrompath(r);
|
||||||
|
|
||||||
if (id >= 0) {
|
if (id >= 0) {
|
||||||
@ -259,7 +270,7 @@ sendbooks(struct kreq *r)
|
|||||||
} else {
|
} else {
|
||||||
kjson_array_open(&req);
|
kjson_array_open(&req);
|
||||||
BookAdv **books = kcalloc(per, sizeof(BookAdv));
|
BookAdv **books = kcalloc(per, sizeof(BookAdv));
|
||||||
res = db_books_load(r, books, sort, per, per * (page-1));
|
res = db_books_load(r, books, sort, order, per, per * (page-1));
|
||||||
while (i < res) {
|
while (i < res) {
|
||||||
putbookadv(&req, books[i]);
|
putbookadv(&req, books[i]);
|
||||||
i++;
|
i++;
|
||||||
@ -280,6 +291,7 @@ sendauthors(struct kreq *r)
|
|||||||
int page = initpage(r);
|
int page = initpage(r);
|
||||||
int per = initperpage(r);
|
int per = initperpage(r);
|
||||||
const char* sort = initsort(r);
|
const char* sort = initsort(r);
|
||||||
|
const char* order = initorder(r);
|
||||||
int64_t id = idfrompath(r);
|
int64_t id = idfrompath(r);
|
||||||
|
|
||||||
http_open(r, KHTTP_200);
|
http_open(r, KHTTP_200);
|
||||||
@ -304,7 +316,7 @@ sendauthors(struct kreq *r)
|
|||||||
} else {
|
} else {
|
||||||
kjson_array_open(&req);
|
kjson_array_open(&req);
|
||||||
AuthorAdv **authors = kcalloc(per, sizeof(AuthorAdv));
|
AuthorAdv **authors = kcalloc(per, sizeof(AuthorAdv));
|
||||||
res = db_authors_load(r, authors, sort, per, per * (page-1));
|
res = db_authors_load(r, authors, sort, order, per, per * (page-1));
|
||||||
while (i < res) {
|
while (i < res) {
|
||||||
kjson_obj_open(&req);
|
kjson_obj_open(&req);
|
||||||
kjson_putintp(&req, "id", authors[i]->a.id);
|
kjson_putintp(&req, "id", authors[i]->a.id);
|
||||||
@ -331,6 +343,7 @@ sendseries(struct kreq *r)
|
|||||||
int page = initpage(r);
|
int page = initpage(r);
|
||||||
int per = initperpage(r);
|
int per = initperpage(r);
|
||||||
const char* sort = initsort(r);
|
const char* sort = initsort(r);
|
||||||
|
const char* order = initorder(r);
|
||||||
int64_t id = idfrompath(r);
|
int64_t id = idfrompath(r);
|
||||||
|
|
||||||
http_open(r, KHTTP_200);
|
http_open(r, KHTTP_200);
|
||||||
@ -364,7 +377,7 @@ sendseries(struct kreq *r)
|
|||||||
} else {
|
} else {
|
||||||
kjson_array_open(&req);
|
kjson_array_open(&req);
|
||||||
SeriesAdv **series = kcalloc(per, sizeof(SeriesAdv));
|
SeriesAdv **series = kcalloc(per, sizeof(SeriesAdv));
|
||||||
res = db_series_load(r, series, sort, per, per * (page-1));
|
res = db_series_load(r, series, sort, order, per, per * (page-1));
|
||||||
while (i < res) {
|
while (i < res) {
|
||||||
kjson_obj_open(&req);
|
kjson_obj_open(&req);
|
||||||
kjson_putintp(&req, "id", series[i]->s.id);
|
kjson_putintp(&req, "id", series[i]->s.id);
|
||||||
|
Loading…
Reference in New Issue
Block a user