-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlite.c
223 lines (188 loc) · 5.9 KB
/
sqlite.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include <sqlite3.h>
#include "dbrace.h"
#include "sqlite.h"
static sqlite3 *sqldb;
void sqlite_dump(void)
{
int rc;
sqlite3_stmt *sql_stmt;
rc = sqlite3_open(SQLITE_FILENAME, &sqldb);
if (rc != SQLITE_OK) {
printf("sqlite3_open: Couldn't open %s", SQLITE_FILENAME);
exit(1);
}
rc = sqlite3_prepare(sqldb, "select key,value from tbl;", -1, &sql_stmt, NULL);
if( rc!=SQLITE_OK ){
printf("sqlite3_prepare error: %s\n", sqlite3_errmsg(sqldb));
exit(1);
}
while ( SQLITE_ROW == (rc = sqlite3_step(sql_stmt)) )
if (print)
printf("Key: '%s' - Value: '%s'\n",
sqlite3_column_text(sql_stmt, 0),
sqlite3_column_text(sql_stmt, 1) );
sqlite3_finalize(sql_stmt);
rc = sqlite3_close(sqldb);
if (rc != SQLITE_OK)
printf("sqlite3_close: %s", sqlite3_errmsg(sqldb));
}
void sqlite_get(unsigned long n)
{
int rc;
sqlite3_stmt *sql_stmt;
unsigned long i;
rc = sqlite3_open(SQLITE_FILENAME, &sqldb);
if (rc != SQLITE_OK) {
printf("sqlite3_open: Couldn't open %s", SQLITE_FILENAME);
exit(1);
}
rc = sqlite3_prepare(sqldb, "select key,value from tbl where key=?;", -1, &sql_stmt, NULL);
if( rc!=SQLITE_OK ){
printf("sqlite3_prepare error: %s\n", sqlite3_errmsg(sqldb));
exit(1);
}
for (i=1; i < n; i+=2) {
rc = sqlite3_bind_int(sql_stmt, 1, i);
if( rc != SQLITE_OK ){
printf("sqlite3_bind_int error: %s\n", sqlite3_errmsg(sqldb));
exit(1);
}
rc = sqlite3_step(sql_stmt);
if ( rc == SQLITE_ROW ){
if (print)
printf("Key: '%s' - Value: '%s'\n",
sqlite3_column_text(sql_stmt, 0),
sqlite3_column_text(sql_stmt, 1) );
}
sqlite3_reset(sql_stmt);
}
for (i=2; i < n; i+=2) {
rc = sqlite3_bind_int(sql_stmt, 1, i);
if( rc != SQLITE_OK ){
printf("sqlite3_bind_int error: %s\n", sqlite3_errmsg(sqldb));
exit(1);
}
rc = sqlite3_step(sql_stmt);
if ( rc == SQLITE_ROW ){
if (print)
printf("Key: '%s' - Value: '%s'\n",
sqlite3_column_text(sql_stmt, 0),
sqlite3_column_text(sql_stmt, 1) );
}
sqlite3_reset(sql_stmt);
}
sqlite3_finalize(sql_stmt);
rc = sqlite3_close(sqldb);
if (rc != SQLITE_OK)
printf("sqlite3_close: %s", sqlite3_errmsg(sqldb));
}
static int sqlite_insert(sqlite3_stmt *sql_stmt, unsigned long key, int random)
{
int rc;
char data[256];
int dlen = 14, i;
if (random)
dlen = rand() % (255 - 1) + 1; /* 1 to 255 byte data */
for (i = 0; i < dlen - 1; i++)
data[i] = (key + i) % (128 - 32) + 32;
data[i] = 0;
rc = sqlite3_bind_int(sql_stmt, 1, key);
if( rc!=SQLITE_OK ){
printf("sqlite3_bind_int error: %s\n", sqlite3_errmsg(sqldb));
exit(1);
}
rc = sqlite3_bind_text(sql_stmt, 2, data, dlen, SQLITE_STATIC);
if( rc!=SQLITE_OK ){
printf("sqlite3_bind_text error: %s.\n", sqlite3_errmsg(sqldb));
exit(1);
}
rc = sqlite3_step(sql_stmt);
if( rc!=SQLITE_DONE ){
printf("sqlite3_step error.\n");
exit(1);
}
sqlite3_reset(sql_stmt);
return 0;
}
void sqlite_populate(unsigned int n, int random, unsigned long txnsize)
{
static int inited = 0;
int rc;
unsigned long i;
char sql_str[200];
char *zErrMsg = NULL;
sqlite3_stmt *sql_stmt;
unlink(SQLITE_FILENAME);
rc = sqlite3_open(SQLITE_FILENAME, &sqldb);
if (rc != SQLITE_OK) {
printf("sqlite3_open: Couldn't open %s", SQLITE_FILENAME);
exit(1);
}
if ( ! inited) {
rc = sqlite3_prepare(sqldb, "insert into tbl VALUES (?, ?);", -1, &sql_stmt, NULL);
if( rc!=SQLITE_OK ){
printf("sqlite3_prepare error.\n");
exit(1);
}
inited = 1;
}
rc = sqlite3_exec(sqldb, "create table tbl(key INTEGER PRIMARY KEY, value BLOB);", NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
sprintf(sql_str,"PRAGMA default_cache_size = %lu;", cache);
rc = sqlite3_exec(sqldb, sql_str, NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
if (txnsize > 1) {
rc = sqlite3_exec(sqldb, "BEGIN TRANSACTION;", NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
}
for (i = 1; i < n; i++) {
sqlite_insert(sql_stmt, i, random);
if (txnsize > 1 && i % txnsize == 0) {
rc = sqlite3_exec(sqldb, "END TRANSACTION;", NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
rc = sqlite3_exec(sqldb, "BEGIN TRANSACTION;", NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
}
}
if (txnsize > 1) {
rc = sqlite3_exec(sqldb, "END TRANSACTION;", NULL, NULL, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "sqlite3_exec error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(1);
}
}
sqlite3_finalize(sql_stmt);
rc = sqlite3_close(sqldb);
if (rc != SQLITE_OK)
printf("sqlite3_close: %s", sqlite3_errmsg(sqldb));
}