Factorize book query

This commit is contained in:
Meutel 2016-12-30 11:37:08 +01:00
parent 8a88942650
commit efbe2be19c

34
db.c
View File

@ -80,6 +80,20 @@ db_book_adv_free(BookAdv *p)
free(p);
}
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);
}
}
int
db_books_load(struct kreq *r, BookAdv **books, int limit)
{
@ -94,15 +108,7 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
ksql_bind_int(stmt, 0, limit);
while (KSQL_ROW == ksql_stmt_step(stmt)) {
books[i] = kcalloc(1, sizeof(BookAdv));
books[i]->b.id = ksql_stmt_int(stmt, 0);
books[i]->b.title = kstrdup(ksql_stmt_str(stmt, 1));
if ( ksql_stmt_isnull(stmt, 4) ) {
books[i]->b.s.id = -1;
} else {
books[i]->b.s_idx = ksql_stmt_double(stmt, 2);
books[i]->b.s.name = kstrdup(ksql_stmt_str(stmt, 3));
books[i]->b.s.id = ksql_stmt_int(stmt, 4);
}
db_book_fill(&books[i]->b,stmt);
i++;
}
ksql_stmt_free(stmt);
@ -124,15 +130,7 @@ db_book_load(struct kreq *r, int64_t id)
return(NULL);
}
book = kcalloc(1, sizeof(Book));
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);
}
db_book_fill(book, stmt);
ksql_stmt_free(stmt);
return book;
}