aboutsummaryrefslogtreecommitdiff
path: root/db_section.c
blob: 72aa2274bfde760bac9f228b29afcf3c1f48be2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
}