aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c86
1 files changed, 49 insertions, 37 deletions
diff --git a/db.c b/db.c
index 653065e..ced02bf 100644
--- a/db.c
+++ b/db.c
@@ -24,50 +24,72 @@ byid(char *table, int id)
return stmt;
}
-struct attachment *
-getattachment(sqlite3_stmt *stmt, int final)
+struct att *
+getatt(sqlite3_stmt *stmt)
{
- struct attachment *attachment;
+ struct att *att;
+ att = nextatt(stmt);
+ sqlite3_finalize(stmt);
+ return att;
+}
+
+struct post *
+getpost(sqlite3_stmt *stmt)
+{
+ struct post *post;
+ post = nextpost(stmt);
+ sqlite3_finalize(stmt);
+ return post;
+}
+
+struct user *
+getuser(sqlite3_stmt *stmt)
+{
+ struct user *user;
+ user = nextuser(stmt);
+ sqlite3_finalize(stmt);
+ return user;
+}
+
+struct att *
+nextatt(sqlite3_stmt *stmt)
+{
+ struct att *att;
if(!stmt)
- goto err;
+ return NULL;
if(sqlite3_step(stmt) != SQLITE_ROW)
- goto err;
+ return NULL;
- if(!(attachment = malloc(sizeof(struct attachment))))
+ if(!(att = malloc(sizeof(struct att))))
err(1, "malloc");
- attachment->id = sqlite3_column_int(stmt, 0);
- attachment->post = sqlite3_column_int(stmt, 1);
- attachment->name = textdup(sqlite3_column_text(stmt, 2));
- attachment->description = textdup(sqlite3_column_text(stmt, 3));
- attachment->mime = textdup(sqlite3_column_text(stmt, 4));
+ att->id = sqlite3_column_int(stmt, 0);
+ att->post = sqlite3_column_int(stmt, 1);
+ att->name = textdup(sqlite3_column_text(stmt, 2));
+ att->description = textdup(sqlite3_column_text(stmt, 3));
+ att->mime = textdup(sqlite3_column_text(stmt, 4));
- attachment->bytes = sqlite3_column_bytes(stmt, 5);
- if(!(attachment->data = malloc(attachment->bytes)))
+ att->bytes = sqlite3_column_bytes(stmt, 5);
+ if(!(att->data = malloc(att->bytes)))
err(1, "malloc");
- memcpy(attachment->data, sqlite3_column_blob(stmt, 5),
- attachment->bytes);
-
- if(final) sqlite3_finalize(stmt);
- return attachment;
+ memcpy(att->data, sqlite3_column_blob(stmt, 5),
+ att->bytes);
-err:
- if(final) sqlite3_finalize(stmt);
- return NULL;
+ return att;
}
struct post *
-getpost(sqlite3_stmt *stmt, int final)
+nextpost(sqlite3_stmt *stmt)
{
struct post *post;
if(!stmt)
- goto err;
+ return NULL;
if(sqlite3_step(stmt) != SQLITE_ROW)
- goto err;
+ return NULL;
if(!(post = malloc(sizeof(struct post))))
err(1, "malloc");
@@ -80,24 +102,19 @@ getpost(sqlite3_stmt *stmt, int final)
post->subject = textdup(sqlite3_column_text(stmt, 5));
post->text = textdup(sqlite3_column_text(stmt, 6));
- if(final) sqlite3_finalize(stmt);
return post;
-
-err:
- if(final) sqlite3_finalize(stmt);
- return NULL;
}
struct user *
-getuser(sqlite3_stmt *stmt, int final)
+nextuser(sqlite3_stmt *stmt)
{
struct user *user;
if(!stmt)
- goto err;
+ return NULL;
if(sqlite3_step(stmt) != SQLITE_ROW)
- goto err;
+ return NULL;
if(!(user = malloc(sizeof(struct user))))
err(1, "malloc");
@@ -107,12 +124,7 @@ getuser(sqlite3_stmt *stmt, int final)
user->full = textdup(sqlite3_column_text(stmt, 2));
user->hash = textdup(sqlite3_column_text(stmt, 3));
- if(final) sqlite3_finalize(stmt);
return user;
-
-err:
- if(final) sqlite3_finalize(stmt);
- return NULL;
}
char *