-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsbk-sqlite.c
230 lines (188 loc) · 4.64 KB
/
sbk-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
224
225
226
227
228
229
230
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "sbk-internal.h"
int
sbk_sqlite_open(sqlite3 **db, const char *path)
{
if (sqlite3_open(path, db) != SQLITE_OK) {
sbk_sqlite_warnd(*db, "Cannot open database");
return -1;
}
return 0;
}
int
sbk_sqlite_exec(struct sbk_ctx *ctx, const char *sql)
{
char *errmsg;
if (sqlite3_exec(ctx->db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
warnx("Cannot execute SQL statement: %s", errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
int
sbk_sqlite_prepare(struct sbk_ctx *ctx, sqlite3_stmt **stm, const char *query)
{
if (sqlite3_prepare_v2(ctx->db, query, -1, stm, NULL) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot prepare SQL statement");
return -1;
}
return 0;
}
int
sbk_sqlite_step(struct sbk_ctx *ctx, sqlite3_stmt *stm)
{
int ret;
ret = sqlite3_step(stm);
if (ret != SQLITE_ROW && ret != SQLITE_DONE)
sbk_sqlite_warn(ctx, "Cannot execute SQL statement");
return ret;
}
int
sbk_sqlite_bind_null(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx)
{
if (sqlite3_bind_null(stm, idx) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_bind_int(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx, int val)
{
if (sqlite3_bind_int(stm, idx, val) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_bind_int64(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx,
sqlite3_int64 val)
{
if (sqlite3_bind_int64(stm, idx, val) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_bind_double(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx,
double val)
{
if (sqlite3_bind_double(stm, idx, val) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_bind_blob(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx,
const void *val, size_t len)
{
if (sqlite3_bind_blob(stm, idx, val, len, SQLITE_STATIC) !=
SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_bind_text(struct sbk_ctx *ctx, sqlite3_stmt *stm, int idx,
const char *val)
{
if (sqlite3_bind_text(stm, idx, val, -1, SQLITE_STATIC) != SQLITE_OK) {
sbk_sqlite_warn(ctx, "Cannot bind SQL parameter");
return -1;
}
return 0;
}
int
sbk_sqlite_column_text_copy(struct sbk_ctx *ctx, char **buf, sqlite3_stmt *stm,
int idx)
{
#ifdef notyet
const unsigned char *txt;
int len;
*buf = NULL;
if (sqlite3_column_type(stm, idx) == SQLITE_NULL)
return 0;
if ((txt = sqlite3_column_text(stm, idx)) == NULL) {
sbk_sqlite_warn(ctx, "Cannot get column text");
return -1;
}
if ((len = sqlite3_column_bytes(stm, idx)) < 0) {
sbk_sqlite_warn(ctx, "Cannot get column size");
return -1;
}
if ((*buf = malloc((size_t)len + 1)) == NULL) {
warn(NULL);
return -1;
}
memcpy(*buf, txt, (size_t)len + 1);
return len;
#else
const unsigned char *txt;
*buf = NULL;
if (sqlite3_column_type(stm, idx) == SQLITE_NULL)
return 0;
if ((txt = sqlite3_column_text(stm, idx)) == NULL) {
sbk_sqlite_warn(ctx, "Cannot get column text");
return -1;
}
if ((*buf = strdup((const char *)txt)) == NULL) {
warn(NULL);
return -1;
}
return 0;
#endif
}
static void
sbk_sqlite_vwarnd(sqlite3 *db, const char *fmt, va_list ap)
{
char *msg;
if (fmt == NULL)
warnx("%s", sqlite3_errmsg(db));
else {
if (vasprintf(&msg, fmt, ap) == -1) {
warnx("vasprintf() failed");
return;
}
warnx("%s: %s", msg, sqlite3_errmsg(db));
free(msg);
}
}
void
sbk_sqlite_warnd(sqlite3 *db, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sbk_sqlite_vwarnd(db, fmt, ap);
va_end(ap);
}
void
sbk_sqlite_warn(struct sbk_ctx *ctx, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sbk_sqlite_vwarnd(ctx->db, fmt, ap);
va_end(ap);
}