From efbe2be19c788c438fd07bb35619979f2c4dc96e Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 30 Dec 2016 11:37:08 +0100 Subject: [PATCH] Factorize book query --- db.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/db.c b/db.c index 8b8e977..89290c3 100644 --- a/db.c +++ b/db.c @@ -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; }