aboutsummaryrefslogtreecommitdiff
path: root/db_post.c
diff options
context:
space:
mode:
Diffstat (limited to 'db_post.c')
-rw-r--r--db_post.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/db_post.c b/db_post.c
new file mode 100644
index 0000000..2f25ea2
--- /dev/null
+++ b/db_post.c
@@ -0,0 +1,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;
+}