diff options
Diffstat (limited to 'db_section.c')
| -rw-r--r-- | db_section.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/db_section.c b/db_section.c new file mode 100644 index 0000000..72aa227 --- /dev/null +++ b/db_section.c @@ -0,0 +1,59 @@ +#define _POSIX_C_SOURCE 200809L + +#include "db.h" +#include <sqlite3.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> + +void db_section_free(struct db_section *const s) +{ + if (!s) + return; + + free(s->name); + free(s->desc); +} + +int db_section(sqlite3 *const db, sqlite3_stmt *const stmt, + struct db_section *const s) +{ + unsigned long long id, catid; + char *name = NULL, *desc = NULL; + + if (db_biguint(db, stmt, "id", &id)) + { + fprintf(stderr, "%s: failed to get id\n", __func__); + goto failure; + } + else if (db_biguint(db, stmt, "catid", &catid)) + { + fprintf(stderr, "%s: failed to get category id\n", __func__); + goto failure; + } + else if (!(name = db_str(db, stmt, "name"))) + { + fprintf(stderr, "%s: failed to get name\n", __func__); + goto failure; + } + else if (!(desc = db_str(db, stmt, "description"))) + { + fprintf(stderr, "%s: failed to get name\n", __func__); + goto failure; + } + + *s = (const struct db_section) + { + .catid = catid, + .name = name, + .desc = desc, + .id = id + }; + + return 0; + +failure: + free(desc); + free(name); + return -1; +} |
