Book list
This commit is contained in:
parent
9d74bab6ae
commit
9fb4a9f5cd
31
db.c
31
db.c
@ -30,21 +30,44 @@
|
|||||||
|
|
||||||
enum stmt {
|
enum stmt {
|
||||||
STMT_BOOK,
|
STMT_BOOK,
|
||||||
|
STMT_BOOKS,
|
||||||
STMT__MAX
|
STMT__MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *const stmts[STMT__MAX] = {
|
static const char *const stmts[STMT__MAX] = {
|
||||||
/* STMT_BOOK */
|
/* STMT_BOOK */
|
||||||
"SELECT books.id AS id,title FROM books WHERE books.id = ?",
|
"SELECT books.id AS id,title FROM books WHERE books.id = ?",
|
||||||
|
/* STMT_BOOKS */
|
||||||
|
"SELECT books.id AS id,title FROM books LIMIT ?",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
db_books_load(struct kreq *r, struct book **books, int limit)
|
||||||
|
{
|
||||||
|
if (limit < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
struct ksqlstmt *stmt;
|
||||||
|
int i = 0;
|
||||||
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
|
stmts[STMT_BOOKS],
|
||||||
|
STMT_BOOKS);
|
||||||
|
ksql_bind_int(stmt, 0, limit);
|
||||||
|
while (KSQL_ROW == ksql_stmt_step(stmt)) {
|
||||||
|
books[i] = kcalloc(1, sizeof(struct book));
|
||||||
|
books[i]->id = ksql_stmt_int(stmt, 0);
|
||||||
|
books[i]->title = kstrdup(ksql_stmt_str(stmt, 1));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ksql_stmt_free(stmt);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
struct book *
|
struct book *
|
||||||
db_book_load(struct kreq *r, int64_t id)
|
db_book_load(struct kreq *r, int64_t id)
|
||||||
{
|
{
|
||||||
struct ksqlstmt *stmt;
|
struct ksqlstmt *stmt;
|
||||||
struct book *book;
|
struct book *book;
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
ksql_stmt_alloc(r->arg, &stmt,
|
ksql_stmt_alloc(r->arg, &stmt,
|
||||||
stmts[STMT_BOOK],
|
stmts[STMT_BOOK],
|
||||||
STMT_BOOK);
|
STMT_BOOK);
|
||||||
@ -54,8 +77,8 @@ db_book_load(struct kreq *r, int64_t id)
|
|||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
book = kcalloc(1, sizeof(struct book));
|
book = kcalloc(1, sizeof(struct book));
|
||||||
book->id = ksql_stmt_int(stmt, i++);
|
book->id = ksql_stmt_int(stmt, 0);
|
||||||
book->title = kstrdup(ksql_stmt_str(stmt, i++));
|
book->title = kstrdup(ksql_stmt_str(stmt, 1));
|
||||||
ksql_stmt_free(stmt);
|
ksql_stmt_free(stmt);
|
||||||
return book;
|
return book;
|
||||||
}
|
}
|
||||||
|
1
extern.h
1
extern.h
@ -28,6 +28,7 @@ struct book {
|
|||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
struct book * db_book_load(struct kreq *r, int64_t id);
|
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_close(struct kreq *);
|
void db_close(struct kreq *);
|
||||||
int db_open(struct kreq *, const char *);
|
int db_open(struct kreq *, const char *);
|
||||||
|
45
main.c
45
main.c
@ -86,6 +86,23 @@ http_open(struct kreq *r, enum khttp code)
|
|||||||
khttp_body(r);
|
khttp_body(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
puterror(struct kjsonreq *req, char *message)
|
||||||
|
{
|
||||||
|
kjson_obj_open(req);
|
||||||
|
kjson_putstringp(req, "msg", message);
|
||||||
|
kjson_obj_close(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
putbook(struct kjsonreq *req, struct book *b)
|
||||||
|
{
|
||||||
|
kjson_obj_open(req);
|
||||||
|
kjson_putintp(req, "id", b->id);
|
||||||
|
kjson_putstringp(req, "title", b->title);
|
||||||
|
kjson_obj_close(req);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sendbooks(struct kreq *r)
|
sendbooks(struct kreq *r)
|
||||||
{
|
{
|
||||||
@ -93,6 +110,7 @@ sendbooks(struct kreq *r)
|
|||||||
const char *errid;
|
const char *errid;
|
||||||
int64_t id = -1;
|
int64_t id = -1;
|
||||||
struct book *b = NULL;
|
struct book *b = NULL;
|
||||||
|
int res, i = 0;
|
||||||
|
|
||||||
if (NULL != r->fieldmap[KEY_ID])
|
if (NULL != r->fieldmap[KEY_ID])
|
||||||
id = r->fieldmap[KEY_ID]->parsed.i;
|
id = r->fieldmap[KEY_ID]->parsed.i;
|
||||||
@ -103,24 +121,19 @@ sendbooks(struct kreq *r)
|
|||||||
}
|
}
|
||||||
http_open(r, (id > 0 && NULL == b) ? KHTTP_404 : KHTTP_200);
|
http_open(r, (id > 0 && NULL == b) ? KHTTP_404 : KHTTP_200);
|
||||||
kjson_open(&req, r);
|
kjson_open(&req, r);
|
||||||
kjson_obj_open(&req);
|
if (id >0 && NULL == b) {
|
||||||
if (NULL == b) {
|
puterror(&req, "Unknown book");
|
||||||
kjson_objp_open(&req, "error");
|
} else if (NULL != b) {
|
||||||
kjson_putstringp(&req, "msg", "Unknown book");
|
putbook(&req, b);
|
||||||
kjson_obj_close(&req);
|
|
||||||
} else {
|
} else {
|
||||||
kjson_objp_open(&req, "book");
|
kjson_array_open(&req);
|
||||||
kjson_putstringp(&req, "data", "books");
|
struct book **books = kcalloc(10, sizeof(struct book));
|
||||||
kjson_putintp(&req, "id", b->id);
|
res = db_books_load(r, books, 10);
|
||||||
kjson_putstringp(&req, "title", b->title);
|
while (i < res) {
|
||||||
kjson_obj_close(&req);
|
putbook(&req, books[i++]);
|
||||||
|
}
|
||||||
|
kjson_array_close(&req);
|
||||||
}
|
}
|
||||||
#ifdef __DEBUG__
|
|
||||||
kjson_putstringp(&req, "_path", r->path);
|
|
||||||
kjson_putstringp(&req, "_pname", r->pname);
|
|
||||||
kjson_putstringp(&req, "_pagename", r->pagename);
|
|
||||||
#endif
|
|
||||||
kjson_obj_close(&req);
|
|
||||||
kjson_close(&req);
|
kjson_close(&req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user