Free author mem

This commit is contained in:
Meutel 2016-12-30 15:54:32 +01:00
parent 609c2ed8ac
commit 8a6a0ba3ee

20
db.c
View File

@ -77,6 +77,13 @@ db_book_unfill(Book *p)
db_series_unfill(&p->s);
}
static void
db_author_unfill(Author *p) {
if (NULL == p)
return;
free(p->name);
}
void
db_book_free(Book *p)
{
@ -88,7 +95,9 @@ void
db_book_adv_free(BookAdv *p)
{
db_book_unfill(&p->b);
//TODO free authors
for (size_t i = 0; i < p->asize; i++) {
db_author_unfill(p->a[i]);
}
free(p);
}
@ -114,7 +123,9 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
}
struct ksqlstmt *stmt;
struct bookauth *p = NULL;
struct bookauth *item = NULL;
int i, count = 0;
int64_t bid;
ksql_stmt_alloc(r->arg, &stmt,
stmts[STMT_BOOKS],
@ -134,11 +145,11 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
ksql_bind_int(stmt, 0, limit);
while (KSQL_ROW == ksql_stmt_step(stmt)) {
// add author to list, link related book and increment asize
struct bookauth *item = kcalloc(1, sizeof(struct bookauth));
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));
int64_t bid = ksql_stmt_int(stmt, 2);
bid = ksql_stmt_int(stmt, 2);
for (i = 0; i < count && NULL == item->b; i++) {
if (books[i]->b.id == bid) {
item->b = books[i];
@ -157,9 +168,10 @@ db_books_load(struct kreq *r, BookAdv **books, int limit)
}
p->b->a[p->b->asize] = p->a;// add author
p->b->asize++;
item = p;
p = p->p;
free(item);
}
// TODO free linked list (not authors)
ksql_stmt_free(stmt);
return count;