aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c82
1 files changed, 47 insertions, 35 deletions
diff --git a/db.c b/db.c
index c6ddab2..5de57ec 100644
--- a/db.c
+++ b/db.c
@@ -1,76 +1,88 @@
#include <err.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include "db.h"
+static char * textdup(const unsigned char *);
+
sqlite3_stmt *
-byid(int id, char *sql)
+byid(char *table, int id)
{
+ char sql[100];
sqlite3_stmt *stmt;
+ snprintf(sql, 100, "SELECT oid, * FROM %s WHERE oid = ?", table);
+
if(sqlite3_prepare(db, sql, -1, &stmt, 0) != SQLITE_OK)
return NULL;
-
+
if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK)
return NULL;
- if(sqlite3_step(stmt) != SQLITE_ROW)
- return NULL;
-
return stmt;
}
-char *
-textdup(const unsigned char *s)
-{
- return s? strdup((char *)s): NULL;
-}
-
struct post *
-getpost(int id)
+getpost(sqlite3_stmt *stmt, int final)
{
- sqlite3_stmt *stmt;
struct post *post;
- if(!(stmt = byid(id, "SELECT parent, user, created, edited, subject, text FROM posts WHERE oid = ?"))){
- sqlite3_finalize(stmt);
- return NULL;
- }
+ if(!stmt)
+ goto err;
+
+ if(sqlite3_step(stmt) != SQLITE_ROW)
+ goto err;
if(!(post = malloc(sizeof(struct post))))
err(1, "malloc");
- post->parent = sqlite3_column_int(stmt, 0);
- post->user = sqlite3_column_int(stmt, 1);
- post->created = sqlite3_column_int(stmt, 2);
- post->edited = sqlite3_column_int(stmt, 3);
- post->subject = textdup(sqlite3_column_text(stmt, 4));
- post->text = textdup(sqlite3_column_text(stmt, 5));
+ post->id = sqlite3_column_int(stmt, 0);
+ post->parent = sqlite3_column_int(stmt, 1);
+ post->user = sqlite3_column_int(stmt, 2);
+ post->created = sqlite3_column_int(stmt, 3);
+ post->edited = sqlite3_column_int(stmt, 4);
+ post->subject = textdup(sqlite3_column_text(stmt, 5));
+ post->text = textdup(sqlite3_column_text(stmt, 6));
- sqlite3_finalize(stmt);
+ if(final) sqlite3_finalize(stmt);
return post;
+
+err:
+ if(final) sqlite3_finalize(stmt);
+ return NULL;
}
struct user *
-getuser(int id)
+getuser(sqlite3_stmt *stmt, int final)
{
- sqlite3_stmt *stmt;
struct user *user;
- if(!(stmt = byid(id, "SELECT name, full, hash FROM users WHERE oid = ?"))){
- sqlite3_finalize(stmt);
- return NULL;
- }
+ if(!stmt)
+ goto err;
+
+ if(sqlite3_step(stmt) != SQLITE_ROW)
+ goto err;
if(!(user = malloc(sizeof(struct user))))
err(1, "malloc");
- user->id = id;
- user->name = textdup(sqlite3_column_text(stmt, 0));
- user->full = textdup(sqlite3_column_text(stmt, 1));
- user->hash = textdup(sqlite3_column_text(stmt, 2));
+ user->id = sqlite3_column_int(stmt, 0);
+ user->name = textdup(sqlite3_column_text(stmt, 1));
+ user->full = textdup(sqlite3_column_text(stmt, 2));
+ user->hash = textdup(sqlite3_column_text(stmt, 3));
- sqlite3_finalize(stmt);
+ if(final) sqlite3_finalize(stmt);
return user;
+
+err:
+ if(final) sqlite3_finalize(stmt);
+ return NULL;
+}
+
+char *
+textdup(const unsigned char *s)
+{
+ return s? strdup((char *)s): NULL;
} \ No newline at end of file