From 0292e86cf884a26108a59d81d69cee83b54f4c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 18 Sep 2021 10:38:09 +0200 Subject: Clean up database code --- Makefile | 16 +++++++++--- README | 40 ++++++++++++++--------------- cforum.c | 12 ++++----- ctl.c | 22 ++++++++-------- ctl.h | 2 +- db.c | 86 ++++++++++++++++++++++++++++++++++++--------------------------- db.h | 11 +++++--- err.c | 2 -- t/front.t | 4 +-- t/post.t | 10 ++++---- 10 files changed, 113 insertions(+), 92 deletions(-) diff --git a/Makefile b/Makefile index fae0385..38d7adf 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,21 @@ TPL = $(shell ls t/*.t | sed 's/$$/c/') .SUFFIXES: .t .tc -cforum: $(C) $(H) $(TPL) - $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o cforum $(C) +all: cforum README clean: rm cforum rm $(TPL) +cforum: $(C) $(H) $(TPL) + $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o cforum $(C) + +README: .README + cp .README README + +.README: $(C) $(H) $(TPL) + .README awk '/^ / {if(!i++){print;system($$0"|sed s/^/\\ /")}exit} {print}' + .t.tc: mktpl/mktpl <$< mktpl/mktpl >$@ @@ -24,5 +32,5 @@ db: sqlite3 db "CREATE TABLE posts(parent INT, user INT NOT NULL, created INT NOT NULL, edited INT, subject NOT NULL, text NOT NULL, FOREIGN KEY (user) REFERENCES users(oid));" sqlite3 db "INSERT INTO posts values(NULL, 1, 1462137896, NULL, 'Hello World!', 'This is the first post.');" sqlite3 db "INSERT INTO posts values(1, 1, 1462138896, NULL, 'Re: Hello World!', 'This is the second post!');" - sqlite3 db "CREATE TABLE attachments(post INT NOT NULL, name NOT NULL, description, mime NOT NULL, data BLOB, FOREIGN KEY (post) REFERENCES posts(oid));" - sqlite3 db "$$(printf "INSERT INTO attachments values(1, 'example', 'Some example shell code.', 'text/plain', '#!/bin/sh\necho Hello World!');")" \ No newline at end of file + sqlite3 db "CREATE TABLE atts(post INT NOT NULL, name NOT NULL, description, mime NOT NULL, data BLOB, FOREIGN KEY (post) REFERENCES posts(oid));" + sqlite3 db "$$(printf "INSERT INTO atts values(1, 'example', 'Some example shell code.', 'text/plain', '#!/bin/sh\necho Hello World!');")" \ No newline at end of file diff --git a/README b/README index 4266c7f..2086996 100644 --- a/README +++ b/README @@ -5,24 +5,24 @@ SQLite as its database. It is designed to be a simple, fast and reliable alternative to software like phpBB. Written in portable C89, it can be run on practically any UNIX system. -It is also rather small. +It is also rather small: -wc -l *.c *.h */*.t */*.lex - 96 cforum.c - 96 ctl.c - 121 db.c - 12 err.c - 106 query.c - 3 ctl.h - 34 db.h - 0 err.h - 14 query.h - 2 site.h - 3 t/err.t - 1 t/foot.t - 13 t/front.t - 6 t/head.t - 27 t/post.t - 2 t/user.t - 87 mktpl/mktpl.lex - 623 total + wc -l *.c *.h */*.t */*.lex + 96 cforum.c + 96 ctl.c + 133 db.c + 10 err.c + 106 query.c + 3 ctl.h + 37 db.h + 0 err.h + 14 query.h + 2 site.h + 3 t/err.t + 1 t/foot.t + 13 t/front.t + 6 t/head.t + 27 t/post.t + 2 t/user.t + 87 mktpl/mktpl.lex + 636 total diff --git a/cforum.c b/cforum.c index c5cc411..36782f0 100644 --- a/cforum.c +++ b/cforum.c @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) { char err[MAXERR], *p, *v; - int attachmentid, postid, userid; + int attid, postid, userid; sqlite3_stmt *stmt; /* @@ -70,11 +70,11 @@ main(int argc, char *argv[]) } /* Parse query string. */ - attachmentid = postid = userid = 0; + attid = postid = userid = 0; while(p = nextparam(GET, 128)){ v = split(p); - if(!attachmentid && strcmp(p, "attachment") == 0) - attachmentid = atoi(v); + if(!attid && strcmp(p, "att") == 0) + attid = atoi(v); else if(!postid && strcmp(p, "post") == 0) postid = atoi(v); else if(!userid && strcmp(p, "user") == 0) @@ -82,8 +82,8 @@ main(int argc, char *argv[]) } /* Handle request. */ - if(attachmentid) - showattachment(attachmentid); + if(attid) + showatt(attid); else if(postid) showpost(postid); else if(userid) diff --git a/ctl.c b/ctl.c index 6ae9832..2d7f41a 100644 --- a/ctl.c +++ b/ctl.c @@ -20,17 +20,17 @@ printdate(int timestamp) } void -showattachment(id) +showatt(id) { - struct attachment *attachment; + struct att *att; - if(!(attachment = getattachment(byid("attachments", id), 1))){ - srverr("Could not retrieve attachment"); + if(!(att = getatt(byid("atts", id)))){ + srverr("Could not retrieve att"); return; } - printf("Content-Type: %s\n\n", attachment->mime); - printf("%.*s", attachment->bytes, attachment->data); + printf("Content-Type: %s\n\n", att->mime); + printf("%.*s", att->bytes, att->data); } void @@ -57,22 +57,22 @@ void showpost(int id) { char *title; - struct attachment *attachment; + struct att *att; struct post *post; struct user *user; sqlite3_stmt *stmt; - if(!(post = getpost(byid("posts", id), 1))){ + if(!(post = getpost(byid("posts", id)))){ srverr("Could not retrieve post"); return; } - if(!(user = getuser(byid("users", post->user), 1))){ + if(!(user = getuser(byid("users", post->user)))){ srverr("Could not retrieve post author"); return; } - stmt = byid("attachments", id); + stmt = byid("atts", id); title = site.name; printf("Content-Type: text/html\n\n"); @@ -85,7 +85,7 @@ showuser(int id) char *title; struct user *user; - if(!(user = getuser(byid("users", id), 1))){ + if(!(user = getuser(byid("users", id)))){ srverr("Could not retrieve user"); return; } diff --git a/ctl.h b/ctl.h index c3797fc..1dda7af 100644 --- a/ctl.h +++ b/ctl.h @@ -1,4 +1,4 @@ -void showattachment(int); +void showatt(int); void showfront(void); void showpost(int); void showuser(int); \ No newline at end of file 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 * diff --git a/db.h b/db.h index 9841d9c..4d956f9 100644 --- a/db.h +++ b/db.h @@ -2,7 +2,7 @@ sqlite3 *db; -struct attachment{ +struct att{ int id; int post; int bytes; @@ -30,6 +30,9 @@ struct user{ }; sqlite3_stmt *byid(char *, int); -struct attachment *getattachment(sqlite3_stmt *, int); -struct post *getpost(sqlite3_stmt *, int); -struct user *getuser(sqlite3_stmt *, int); \ No newline at end of file +struct att *getatt(sqlite3_stmt *); +struct post *getpost(sqlite3_stmt *); +struct user *getuser(sqlite3_stmt *); +struct att *nextatt(sqlite3_stmt *); +struct post *nextpost(sqlite3_stmt *); +struct user *nextuser(sqlite3_stmt *); \ No newline at end of file diff --git a/err.c b/err.c index d6f66ad..dd15ebd 100644 --- a/err.c +++ b/err.c @@ -7,7 +7,5 @@ srverr(char *err) printf("Status: %s\n", title); printf("Content-Type: text/html\n\n"); - #include "t/head.tc" #include "t/err.tc" - #include "t/foot.tc" } \ No newline at end of file diff --git a/t/front.t b/t/front.t index fcdecdb..fd9f46c 100644 --- a/t/front.t +++ b/t/front.t @@ -2,8 +2,8 @@

<%= site.name %>

Latest posts

- <% while(post = getpost(stmt, 0)){ - user = getuser(byid("users", post->user), 1); %> + <% while(post = nextpost(stmt)){ + user = getuser(byid("users", post->user)); %> diff --git a/t/post.t b/t/post.t index 549b4ec..b1cbe99 100644 --- a/t/post.t +++ b/t/post.t @@ -13,16 +13,16 @@
 <%= post->text %>
 
-<% if(attachment = getattachment(stmt, 0)){ %> +<% if(att = nextatt(stmt)){ %>

Attachments

id); %>"><%= post->subject %> user); %>"><%= user->name %>
<% do{ %> - - - + + + - <% }while(attachment = getattachment(stmt, 0)); %> + <% }while(att = nextatt(stmt)); %>
id); %>"><%= attachment->name %><%= attachment->mime %><%= attachment->description %>id); %>"><%= att->name %><%= att->mime %><%= att->description %>
<% } %> <% #include "foot.tc" %> \ No newline at end of file -- cgit v1.2.3