aboutsummaryrefslogtreecommitdiff
path: root/db_post.c
blob: 2f25ea2bb0e9ea0331b54f091a7e0d3471601fdb (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
60
61
62
63
#define _POSIX_C_SOURCE 200809L

#include "db.h"
#include <sqlite3.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

void db_post_free(struct db_post *const p)
{
    if (!p)
        return;

    free(p->text);
}

int db_post(sqlite3 *const db, sqlite3_stmt *const stmt, struct db_post *const p)
{
    char *text = NULL;
    unsigned long long id, uid, topid;
    long long creat;

    if (db_biguint(db, stmt, "id", &id))
    {
        fprintf(stderr, "%s: failed to get id\n", __func__);
        goto failure;
    }
    else if (db_biguint(db, stmt, "topid", &topid))
    {
        fprintf(stderr, "%s: failed to get topic id\n", __func__);
        goto failure;
    }
    else if (db_biguint(db, stmt, "uid", &uid))
    {
        fprintf(stderr, "%s: failed to user id\n", __func__);
        goto failure;
    }
    else if (db_bigint(db, stmt, "creat", &creat))
    {
        fprintf(stderr, "%s: failed to get creation time\n", __func__);
        goto failure;
    }
    else if (!(text = db_str(db, stmt, "text")))
    {
        fprintf(stderr, "%s: failed to get text\n", __func__);
        goto failure;
    }

    *p = (const struct db_post)
    {
        .topid = topid,
        .creat = creat,
        .text = text,
        .uid = uid,
        .id = id
    };

    return 0;

failure:
    free(text);
    return -1;
}