highwind / clog

Simple blogging engine for my personal use, written in C. Yes, C.

clog / db.c
100644 159 lines (128 sloc) 2.945 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#include "db.h"
 
// clog_allocate_query() allocates memory for a
// string that is enough to hold whatever is contained
// in the *transaction object. -qlen- upon a successful
// return will contain the exact size of the string.
// to avoid, malloc'ing all the time, buffer is reused.
static char *
clog_allocate_query(
struct clog_transaction *transaction,
int *qlen);
 
 
 
// compute nearest power of two that is
// greater than or equal to n
inline int pow2(int n)
{
int i;
for (i=1;i<n;i*=2);
return i;
}
 
sqlite3 *
clog_db_open(const char *name)
{
sqlite3 *db;
sqlite3_open(name, &db);
return db;
}
 
void
clog_db_close(sqlite3 *db)
{
if (!db)
return;
 
sqlite3_close(db);
}
 
char *
clog_allocate_query(
struct clog_transaction *transaction,
int *qlen)
{
static int querylen = 0;
static char *query = NULL;
  
int total_length;
 
if (!transaction ||
!transaction->title ||
!transaction->content) {
return NULL;
}
 
total_length += strlen(transaction->title) + SYMBOL_PAD;
total_length += strlen(transaction->content) + SYMBOL_PAD;
total_length += RFCDATE_PAD + QUERY_PAD;
total_length = pow2(total_length);
 
if (!query) {
query = (char *) malloc(total_length);
querylen = total_length;
} else if (total_length > querylen) {
query = (char *) realloc(query, total_length);
querylen = total_length;
}
 
(*qlen) = querylen;
return query;
}
 
int
clog_modify_db(
sqlite3 *db,
struct clog_transaction *transaction)
{
mod_type mtype;
char *querystr, *zErrMsg;
int len, qlen, rc;
 
if (!transaction ||
!transaction->title ||
!transaction->content ||
transaction->time <= 0) {
return -1;
}
 
mtype = transaction->type;
if (mtype != CLOG_DB_INSERT &&
transaction->id < 0) {
return -1;
}
 
querystr = clog_allocate_query(transaction, &qlen);
 
// TODO: take care of single-quote(') and backslash (\)
 
switch (mtype) {
 
case CLOG_DB_INSERT:
len = snprintf(querystr, qlen,
"INSERT INTO entries "
"(title, content, c_time) "
"VALUES ('%s','%s',%d)",
transaction->title,
transaction->content,
(int)transaction->time);
break;
 
case CLOG_DB_UPDATE:
len = snprintf(querystr, qlen,
"UPDATE entries SET "
"title = '%s',"
"content = '%s',"
"u_time = %d "
"WHERE id = %d "
"LIMIT 1",
transaction->title,
transaction->content,
(int)transaction->time,
transaction->id);
break;
 
case CLOG_DB_DELETE:
len = snprintf(querystr, qlen,
"DELETE FROM entries "
"WHERE id = %d "
"LIMIT 1",
transaction->id);
break;
 
default:
return -1;
 
}
 
if (len >= qlen) {
// a return value of qlen or more means that the output was truncated.
// don't want truncated a query, bail out.
return -1;
}
 
    rc = sqlite3_exec(db, querystr, NULL, 0, &zErrMsg);
    if (rc != SQLITE_OK) {
        sqlite3_free(zErrMsg);
        return -1;
    }
 
return 0;
}