aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/db.c b/db.c
index 5de57ec..653065e 100644
--- a/db.c
+++ b/db.c
@@ -24,6 +24,40 @@ byid(char *table, int id)
return stmt;
}
+struct attachment *
+getattachment(sqlite3_stmt *stmt, int final)
+{
+ struct attachment *attachment;
+
+ if(!stmt)
+ goto err;
+
+ if(sqlite3_step(stmt) != SQLITE_ROW)
+ goto err;
+
+ if(!(attachment = malloc(sizeof(struct attachment))))
+ 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));
+
+ attachment->bytes = sqlite3_column_bytes(stmt, 5);
+ if(!(attachment->data = malloc(attachment->bytes)))
+ err(1, "malloc");
+ memcpy(attachment->data, sqlite3_column_blob(stmt, 5),
+ attachment->bytes);
+
+ if(final) sqlite3_finalize(stmt);
+ return attachment;
+
+err:
+ if(final) sqlite3_finalize(stmt);
+ return NULL;
+}
+
struct post *
getpost(sqlite3_stmt *stmt, int final)
{