diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-18 10:38:09 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-18 10:38:09 +0200 |
commit | 0292e86cf884a26108a59d81d69cee83b54f4c88 (patch) | |
tree | 3f5b30fe172013802955213503a5827616be6fec | |
parent | ae9b583b9575679f129680a0f56163fdb6590052 (diff) | |
download | cforum-0292e86cf884a26108a59d81d69cee83b54f4c88.tar.gz |
Clean up database code
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | README | 40 | ||||
-rw-r--r-- | cforum.c | 12 | ||||
-rw-r--r-- | ctl.c | 22 | ||||
-rw-r--r-- | ctl.h | 2 | ||||
-rw-r--r-- | db.c | 86 | ||||
-rw-r--r-- | db.h | 11 | ||||
-rw-r--r-- | err.c | 2 | ||||
-rw-r--r-- | t/front.t | 4 | ||||
-rw-r--r-- | t/post.t | 10 |
10 files changed, 113 insertions, 92 deletions
@@ -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 >.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 @@ -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 @@ -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) @@ -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; } @@ -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 @@ -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 * @@ -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 @@ -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 @@ -2,8 +2,8 @@ <h1><%= site.name %></h1> <h3>Latest posts</h3> <table border="1"> - <% while(post = getpost(stmt, 0)){ - user = getuser(byid("users", post->user), 1); %> + <% while(post = nextpost(stmt)){ + user = getuser(byid("users", post->user)); %> <tr> <td><a href="?post=<% printf("%d", post->id); %>"><%= post->subject %></a></td> <td><a href="?user=<% printf("%d", post->user); %>"><%= user->name %></a></td> @@ -13,16 +13,16 @@ <pre> <%= post->text %> </pre> -<% if(attachment = getattachment(stmt, 0)){ %> +<% if(att = nextatt(stmt)){ %> <h3>Attachments</h3> <table border="1"> <% do{ %> <tr> - <td><a href="?attachment=<% printf("%d", attachment->id); %>"><%= attachment->name %></a></td> - <td><%= attachment->mime %></td> - <td><%= attachment->description %></td> + <td><a href="?att=<% printf("%d", att->id); %>"><%= att->name %></a></td> + <td><%= att->mime %></td> + <td><%= att->description %></td> </tr> - <% }while(attachment = getattachment(stmt, 0)); %> + <% }while(att = nextatt(stmt)); %> </table> <% } %> <% #include "foot.tc" %>
\ No newline at end of file |