-
Notifications
You must be signed in to change notification settings - Fork 1
/
zzsql.c
342 lines (316 loc) · 8.57 KB
/
zzsql.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "zzsql.h"
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <pwd.h>
#include "sqlinit.h" // creates sqlinit global
bool zzquery(struct zzdb *zdb, const char *statement, int (*callback)(void*,int,char**,char**), void *cbdata)
{
int rc;
char *zErrMsg = NULL;
rc = sqlite3_exec(zdb->sqlite, statement, callback, cbdata, &zErrMsg);
if (rc != SQLITE_OK)
{
if (zErrMsg)
{
fprintf(stderr, "SQL error from %s: %s\n", statement, zErrMsg);
sqlite3_free(zErrMsg);
}
exit(-1); // for now...
return false;
}
return true;
}
const char *zzdatetime(time_t value)
{
static char lastmod[MAX_LEN_DATETIME];
struct tm lasttm;
strftime(lastmod, sizeof(lastmod), DATETIME_FORMAT, localtime_r(&value, &lasttm));
return lastmod;
}
time_t zzundatetime(const char *datetime)
{
struct tm tmval;
strptime(datetime, DATETIME_FORMAT, &tmval);
return mktime(&tmval);
}
// return whether we did update local db
bool zzdbupdate(struct zzdb *zdb, struct zzfile *zz)
{
char *modified = NULL;
uint16_t group, element;
char studyInstanceUid[MAX_LEN_UI];
char seriesInstanceUid[MAX_LEN_UI];
char patientsName[MAX_LEN_PN];
char modality[MAX_LEN_CS];
long len;
bool done = false;
memset(studyInstanceUid, 0, sizeof(studyInstanceUid));
memset(seriesInstanceUid, 0, sizeof(seriesInstanceUid));
memset(patientsName, 0, sizeof(patientsName));
memset(modality, 0, sizeof(modality));
zziterinit(zz);
while (zziternext(zz, &group, &element, &len) && !done)
{
switch (ZZ_KEY(group, element))
{
case DCM_StudyInstanceUID:
zzgetstring(zz, studyInstanceUid, sizeof(studyInstanceUid));
break;
case DCM_SeriesInstanceUID:
zzgetstring(zz, seriesInstanceUid, sizeof(seriesInstanceUid));
break;
case DCM_PatientsName:
zzgetstring(zz, patientsName, sizeof(patientsName));
break;
case DCM_Modality:
zzgetstring(zz, modality, sizeof(modality));
break;
case DCM_PixelData:
done = true;
break;
default:
break;
}
}
// Check if date on file is newer, if so, skip the update and return false
struct zzdbiter zq = zzdbquery(zdb, "SELECT lastmodified FROM instances WHERE filename=@s", zz->fullPath);
if (zzdbnext(zdb, &zq, "@s", &modified))
{
//printf("MODIFIED: %s (%d) vs %d\n", modified, (int)zz->modifiedTime, (int)zzundatetime(modified));
if (modified[0] != '\0' && zz->modifiedTime <= zzundatetime(modified))
{
printf("%s is unchanged (%d <= %d)\n", zz->fullPath, (int)zz->modifiedTime, (int)zzundatetime(modified));
zzdbdone(zq);
return false;
}
}
zzdbdone(zq);
zzdbdone(zzdbquery(zdb, "BEGIN TRANSACTION"));
zzdbdone(zzdbquery(zdb, "INSERT OR REPLACE INTO instances(filename, sopclassuid, instanceuid, size, "
"lastmodified, seriesuid) values (@s1, @s2, @s3, @d, @s4, @s5)", zz->fullPath,
zz->sopClassUid, zz->sopInstanceUid, zz->fileSize, zzdatetime(zz->modifiedTime), seriesInstanceUid));
zzdbdone(zzdbquery(zdb, "INSERT OR REPLACE INTO series(seriesuid, modality, studyuid) values (@s6, @s7, @s8)",
seriesInstanceUid, modality, studyInstanceUid));
zzdbdone(zzdbquery(zdb, "INSERT OR REPLACE INTO studies(studyuid, patientsname) values (@s9, @s10)",
studyInstanceUid, patientsName));
zzdbdone(zzdbquery(zdb, "COMMIT"));
return true;
}
struct zzdb *zzdbclose(struct zzdb *zdb)
{
sqlite3_close(zdb->sqlite);
return NULL;
}
static const char *user_home()
{
const char *home = getenv("HOME");
if (home)
{
return home;
}
struct passwd *pw = getpwuid(getuid());
return pw->pw_dir;
}
struct zzdb *zzdbopen(struct zzdb *zdb)
{
sqlite3 *db;
char dbname[PATH_MAX];
int rc;
bool exists;
char *zErrMsg = NULL;
struct stat buf;
memset(dbname, 0, sizeof(dbname));
strncpy(dbname, user_home(), sizeof(dbname) - 1);
strncat(dbname, "/.zzdb", sizeof(dbname) - 1);
// Check if exists
exists = (stat(dbname, &buf) == 0);
rc = sqlite3_open(dbname, &db);
if (!db || rc != SQLITE_OK)
{
fprintf(stderr, "Failed to open db %s: %s\n", dbname, sqlite3_errmsg(db));
exit(-2);
}
if (!exists) // create local dicom database
{
rc = sqlite3_exec(db, sqlinit, NULL, NULL, &zErrMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(-3);
}
}
zdb->sqlite = db;
return zdb;
}
static void mydbrow(struct zzdbiter *zq, char const *fmt, va_list arg)
{
int index = 0;
int64_t *int_temp = 0;
double *float_temp;
const unsigned char **strref_temp;
char ch;
const void **ptr_temp;
while ((ch = *fmt++))
{
if ('@' == ch)
{
switch (ch = *fmt++)
{
case 'f':
float_temp = va_arg(arg, double *);
*float_temp = sqlite3_column_double(zq->stmt, index++);
//printf("GET PARAM(%d)%%f=%f\n", index - 1, *float_temp);
break;
case 's':
strref_temp = va_arg(arg, const unsigned char **);
*strref_temp = sqlite3_column_text(zq->stmt, index++);
//printf("GET PARAM(%d)%%s=%s\n", index - 1, *strref_temp);
break;
case 'd':
int_temp = va_arg(arg, int64_t *);
*int_temp = sqlite3_column_int(zq->stmt, index++);
//printf("GET PARAM(%d)%%d=%ld\n", index - 1, (long)*int_temp);
break;
case 'l':
assert(index > 0);
if (index <= 0) break; // oops
int_temp = va_arg(arg, int64_t *);
*int_temp = sqlite3_column_bytes(zq->stmt, index - 1);
break;
case 'p':
ptr_temp = va_arg(arg, const void **);
*ptr_temp = sqlite3_column_blob(zq->stmt, index++);
//printf("GET PARAM(%d)%%p=%p\n", index - 1, *ptr_temp);
break;
}
}
}
}
static void mydbquery(struct zzdb *zdb, struct zzdbiter *zq, char const *fmt, va_list arg)
{
int index = 1;
int64_t int_temp = 0;
double float_temp;
char *string_temp;
char ch;
int result;
void *ptr_temp;
while ((ch = *fmt++))
{
if ('@' == ch)
{
result = SQLITE_OK;
switch (ch = *fmt++)
{
case 'f':
float_temp = va_arg(arg, double);
//printf("PARAM(%d)%%f=%f\n", index, float_temp);
result = sqlite3_bind_double(zq->stmt, index, float_temp);
index++;
break;
case 's':
string_temp = va_arg(arg, char *);
//printf("PARAM(%d)%%s=%s\n", index, string_temp);
result = sqlite3_bind_text(zq->stmt, index, string_temp, strlen(string_temp), SQLITE_TRANSIENT);
int_temp = 0;
index++;
break;
case 'd':
int_temp = va_arg(arg, int64_t);
//printf("PARAM(%d)%%d=%ld\n", index, (long)int_temp);
result = sqlite3_bind_int(zq->stmt, index, int_temp);
index++;
break;
case 'l':
int_temp = va_arg(arg, int64_t);
//printf("PARAM(%d)%%l=%ld\n", index + 1, (long)int_temp);
break;
case 'p':
ptr_temp = va_arg(arg, void *);
//printf("PARAM(%d)%%p=%p\n", index, ptr_temp);
result = sqlite3_bind_blob(zq->stmt, index, ptr_temp, int_temp, SQLITE_STATIC);
index++;
int_temp = 0;
break;
case 'm':
ptr_temp = va_arg(arg, void *);
//printf("PARAM(%d)%%m=%p\n", index, ptr_temp);
result = sqlite3_bind_blob(zq->stmt, index, ptr_temp, int_temp, free);
index++;
int_temp = 0;
break;
}
if (result != SQLITE_OK)
{
fprintf(stderr, "Bind failed: %s\n", sqlite3_errmsg(zdb->sqlite));
}
}
}
}
// @d - 64bit int
// @f - 64bit double
// @s - utf8 string
// @p - binary blob, blob must not be freed before db connection is closed
// @m - malloced binary blob, ownership of memory transferred to database
// @l - length of next blob ???
// if multiple instances of each type is used in a format string, add an incremented number
// behind the letter, eg first integer is @d, second is @d2, third is @d3, and so on.
struct zzdbiter zzdbquery(struct zzdb *zdb, char const *fmt, ...)
{
struct zzdbiter zq;
va_list arg;
memset(&zq, 0, sizeof(zq));
int result = sqlite3_prepare_v2(zdb->sqlite, fmt, strlen(fmt), &zq.stmt, NULL);
if (result != SQLITE_OK)
{
fprintf(stderr, "Prepare failed: %s\n", sqlite3_errmsg(zdb->sqlite));
return zq;
}
va_start(arg, fmt);
mydbquery(zdb, &zq, fmt, arg);
va_end(arg);
zq.retval = sqlite3_step(zq.stmt);
return zq;
}
void zzdbdone(struct zzdbiter zq)
{
if (zq.stmt)
{
sqlite3_finalize(zq.stmt);
}
}
// @l must be AFTER pointer, in this case, unlike above
// no @m
bool zzdbnext(struct zzdb *zdb, struct zzdbiter *zq, const char *fmt, ...)
{
va_list arg;
if (zq->index > 0)
{
zq->retval = sqlite3_step(zq->stmt);
}
if (zq->retval == SQLITE_ROW && fmt)
{
va_start(arg, fmt);
mydbrow(zq, fmt, arg);
va_end(arg);
zq->index++;
return true;
}
else
{
if (zq->retval != SQLITE_DONE)
{
fprintf(stderr, "Step failed: %s\n", sqlite3_errmsg(zdb->sqlite));
}
sqlite3_finalize(zq->stmt);
zq->stmt = NULL;
return false;
}
}