diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-18 01:52:04 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-18 01:52:04 +0200 |
commit | 24bf14f6653ebf1c1ba79b9f212d35417bb6acbf (patch) | |
tree | 913a0f1bebf835a1109652b1c61d363972ee9a28 /db.c | |
parent | e25f56cbe86041d4ab19e474e38032fa7e681ac7 (diff) | |
download | cforum-24bf14f6653ebf1c1ba79b9f212d35417bb6acbf.tar.gz |
Implement attachments
Diffstat (limited to 'db.c')
-rw-r--r-- | db.c | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -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) { |