| 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 |
#define MOD_SQL_SQLITE_VERSION "mod_sql_sqlite/0.4" |
| 27 |
|
| 28 |
#include "conf.h" |
| 29 |
#include "privs.h" |
| 30 |
#include "mod_sql.h" |
| 31 |
|
| 32 |
#include <sqlite3.h> |
| 33 |
|
| 34 |
|
| 35 |
#if PROFTPD_VERSION_NUMBER < 0x0001030602 |
| 36 |
# error "ProFTPD 1.3.6rc2 or later required" |
| 37 |
#endif |
| 38 |
|
| 39 |
module sql_sqlite_module; |
| 40 |
|
| 41 |
#define SQL_SQLITE_START_FL_NOW 1 |
| 42 |
#define SQL_SQLITE_START_FL_EXCL 2 |
| 43 |
|
| 44 |
typedef struct db_conn_struct { |
| 45 |
char *dsn; |
| 46 |
char *user; |
| 47 |
char *pass; |
| 48 |
|
| 49 |
sqlite3 *dbh; |
| 50 |
|
| 51 |
} db_conn_t; |
| 52 |
|
| 53 |
typedef struct conn_entry_struct { |
| 54 |
char *name; |
| 55 |
void *data; |
| 56 |
|
| 57 |
|
| 58 |
int timer; |
| 59 |
int ttl; |
| 60 |
|
| 61 |
|
| 62 |
unsigned int nconn; |
| 63 |
|
| 64 |
} conn_entry_t; |
| 65 |
|
| 66 |
#define DEF_CONN_POOL_SIZE 10 |
| 67 |
|
| 68 |
static pool *conn_pool = NULL; |
| 69 |
static array_header *conn_cache = NULL; |
| 70 |
|
| 71 |
#define SQLITE_TRACE_LEVEL 12 |
| 72 |
static const char *trace_channel = "sql.sqlite"; |
| 73 |
|
| 74 |
MODRET sql_sqlite_close(cmd_rec *); |
| 75 |
|
| 76 |
static void db_err(void *user_data, int err_code, const char *err_msg) { |
| 77 |
pr_trace_msg(trace_channel, 1, "(sqlite3): [error %d] %s", err_code, err_msg); |
| 78 |
} |
| 79 |
|
| 80 |
static void db_trace(void *user_data, const char *trace_msg) { |
| 81 |
pr_trace_msg(trace_channel, SQLITE_TRACE_LEVEL, "(sqlite3): %s", trace_msg); |
| 82 |
} |
| 83 |
|
| 84 |
static conn_entry_t *sql_sqlite_get_conn(char *name) { |
| 85 |
register unsigned int i = 0; |
| 86 |
|
| 87 |
if (name == NULL) { |
| 88 |
errno = EINVAL; |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
for (i = 0; i < conn_cache->nelts; i++) { |
| 93 |
conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i]; |
| 94 |
|
| 95 |
if (strcmp(name, entry->name) == 0) { |
| 96 |
return entry; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
errno = ENOENT; |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
static void *sql_sqlite_add_conn(pool *p, char *name, db_conn_t *conn) { |
| 105 |
conn_entry_t *entry = NULL; |
| 106 |
|
| 107 |
if (p == NULL || |
| 108 |
name == NULL || |
| 109 |
conn == NULL) { |
| 110 |
errno = EINVAL; |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
if (sql_sqlite_get_conn(name) != NULL) { |
| 115 |
errno = EEXIST; |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
entry = (conn_entry_t *) pcalloc(p, sizeof(conn_entry_t)); |
| 120 |
entry->name = name; |
| 121 |
entry->data = conn; |
| 122 |
|
| 123 |
*((conn_entry_t **) push_array(conn_cache)) = entry; |
| 124 |
return entry; |
| 125 |
} |
| 126 |
|
| 127 |
static int sql_sqlite_timer_cb(CALLBACK_FRAME) { |
| 128 |
register unsigned int i = 0; |
| 129 |
|
| 130 |
for (i = 0; i < conn_cache->nelts; i++) { |
| 131 |
conn_entry_t *entry; |
| 132 |
|
| 133 |
entry = ((conn_entry_t **) conn_cache->elts)[i]; |
| 134 |
if ((unsigned long) entry->timer == p2) { |
| 135 |
cmd_rec *cmd = NULL; |
| 136 |
|
| 137 |
sql_log(DEBUG_INFO, "timer expired for connection '%s'", entry->name); |
| 138 |
|
| 139 |
cmd = pr_cmd_alloc(conn_pool, 2, entry->name, "1"); |
| 140 |
sql_sqlite_close(cmd); |
| 141 |
destroy_pool(cmd->pool); |
| 142 |
|
| 143 |
entry->timer = 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static int result_ncols = 0; |
| 154 |
static array_header *result_list = NULL; |
| 155 |
|
| 156 |
static int exec_cb(void *n, int ncols, char **cols, |
| 157 |
char **colnames) { |
| 158 |
register int i; |
| 159 |
char ***row; |
| 160 |
cmd_rec *cmd = n; |
| 161 |
|
| 162 |
if (result_list == NULL) { |
| 163 |
result_ncols = ncols; |
| 164 |
result_list = make_array(cmd->tmp_pool, ncols, sizeof(char **)); |
| 165 |
} |
| 166 |
|
| 167 |
row = push_array(result_list); |
| 168 |
*row = pcalloc(cmd->tmp_pool, sizeof(char *) * ncols); |
| 169 |
|
| 170 |
for (i = 0; i < ncols; i++) { |
| 171 |
char *val; |
| 172 |
|
| 173 |
val = cols[i]; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (val != NULL) { |
| 179 |
(*row)[i] = pstrdup(cmd->tmp_pool, val); |
| 180 |
|
| 181 |
} else { |
| 182 |
(*row)[i] = NULL; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
static int exec_stmt(cmd_rec *cmd, db_conn_t *conn, char *stmt, char **errstr) { |
| 190 |
int res; |
| 191 |
char *ptr = NULL; |
| 192 |
unsigned int nretries = 0; |
| 193 |
|
| 194 |
PRIVS_ROOT |
| 195 |
res = sqlite3_exec(conn->dbh, stmt, exec_cb, cmd, &ptr); |
| 196 |
PRIVS_RELINQUISH |
| 197 |
|
| 198 |
while (res != SQLITE_OK) { |
| 199 |
if (res == SQLITE_BUSY) { |
| 200 |
struct timeval tv; |
| 201 |
|
| 202 |
sqlite3_free(ptr); |
| 203 |
|
| 204 |
nretries++; |
| 205 |
sql_log(DEBUG_FUNC, "attempt #%u, database busy, trying '%s' again", |
| 206 |
nretries, stmt); |
| 207 |
|
| 208 |
|
| 209 |
tv.tv_sec = 0; |
| 210 |
tv.tv_usec = 500000L; |
| 211 |
|
| 212 |
if (select(0, NULL, NULL, NULL, &tv) < 0) { |
| 213 |
if (errno == EINTR) { |
| 214 |
pr_signals_handle(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
PRIVS_ROOT |
| 219 |
res = sqlite3_exec(conn->dbh, stmt, exec_cb, cmd, &ptr); |
| 220 |
PRIVS_RELINQUISH |
| 221 |
|
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
*errstr = pstrdup(cmd->pool, ptr); |
| 226 |
sqlite3_free(ptr); |
| 227 |
|
| 228 |
sql_log(DEBUG_FUNC, "error executing '%s': (%d) %s", stmt, res, *errstr); |
| 229 |
return -1; |
| 230 |
} |
| 231 |
|
| 232 |
if (ptr) |
| 233 |
sqlite3_free(ptr); |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static int query_start(cmd_rec *cmd, db_conn_t *conn, int flags, |
| 239 |
char **errstr) { |
| 240 |
char *start_txn = NULL; |
| 241 |
|
| 242 |
switch (flags) { |
| 243 |
case SQL_SQLITE_START_FL_NOW: |
| 244 |
start_txn = pstrdup(cmd->tmp_pool, "BEGIN IMMEDIATE"); |
| 245 |
break; |
| 246 |
|
| 247 |
case SQL_SQLITE_START_FL_EXCL: |
| 248 |
start_txn = pstrdup(cmd->tmp_pool, "BEGIN EXCLUSIVE"); |
| 249 |
break; |
| 250 |
|
| 251 |
default: |
| 252 |
start_txn = pstrdup(cmd->tmp_pool, "BEGIN"); |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
return exec_stmt(cmd, conn, start_txn, errstr); |
| 257 |
} |
| 258 |
|
| 259 |
static int query_run(cmd_rec *cmd, db_conn_t *conn, char *query, |
| 260 |
char **errstr) { |
| 261 |
return exec_stmt(cmd, conn, query, errstr); |
| 262 |
} |
| 263 |
|
| 264 |
static int query_finish(cmd_rec *cmd, db_conn_t *conn, char **errstr) { |
| 265 |
return exec_stmt(cmd, conn, pstrdup(cmd->tmp_pool, "COMMIT"), errstr); |
| 266 |
} |
| 267 |
|
| 268 |
static modret_t *sql_sqlite_get_data(cmd_rec *cmd) { |
| 269 |
register unsigned int i; |
| 270 |
unsigned int count, k = 0; |
| 271 |
char **data; |
| 272 |
sql_data_t *sd = pcalloc(cmd->tmp_pool, sizeof(sql_data_t)); |
| 273 |
|
| 274 |
if (result_list == NULL) { |
| 275 |
return mod_create_data(cmd, sd); |
| 276 |
} |
| 277 |
|
| 278 |
sd->rnum = result_list->nelts; |
| 279 |
sd->fnum = result_ncols; |
| 280 |
count = sd->rnum * sd->fnum; |
| 281 |
data = pcalloc(cmd->tmp_pool, sizeof(char *) * (count + 1)); |
| 282 |
|
| 283 |
for (i = 0; i < result_list->nelts; i++) { |
| 284 |
register int j; |
| 285 |
char **row; |
| 286 |
|
| 287 |
row = ((char ***) result_list->elts)[i]; |
| 288 |
for (j = 0; j < result_ncols; j++) { |
| 289 |
data[k++] = pstrdup(cmd->tmp_pool, row[j]); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
data[k] = NULL; |
| 294 |
sd->data = data; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
result_ncols = 0; |
| 300 |
result_list = NULL; |
| 301 |
|
| 302 |
return mod_create_data(cmd, sd); |
| 303 |
} |
| 304 |
|
| 305 |
MODRET sql_sqlite_open(cmd_rec *cmd) { |
| 306 |
conn_entry_t *entry = NULL; |
| 307 |
db_conn_t *conn = NULL; |
| 308 |
const char *stmt = NULL; |
| 309 |
int res, xerrno = 0; |
| 310 |
unsigned int nretries = 0; |
| 311 |
|
| 312 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_open"); |
| 313 |
|
| 314 |
if (cmd->argc < 1) { |
| 315 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_open"); |
| 316 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 321 |
if (entry == NULL) { |
| 322 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_open"); |
| 323 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 324 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 325 |
} |
| 326 |
|
| 327 |
conn = (db_conn_t *) entry->data; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
if (entry->nconn > 0) { |
| 333 |
entry->nconn++; |
| 334 |
|
| 335 |
if (entry->timer) { |
| 336 |
pr_timer_reset(entry->timer, &sql_sqlite_module); |
| 337 |
} |
| 338 |
|
| 339 |
sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name, |
| 340 |
entry->nconn); |
| 341 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_open"); |
| 342 |
return PR_HANDLED(cmd); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
PRIVS_ROOT |
| 349 |
res = sqlite3_open_v2(conn->dsn, &(conn->dbh), SQLITE_OPEN_READWRITE, NULL); |
| 350 |
xerrno = errno; |
| 351 |
PRIVS_RELINQUISH |
| 352 |
|
| 353 |
if (res != SQLITE_OK) { |
| 354 |
char *errstr; |
| 355 |
|
| 356 |
errstr = pstrcat(cmd->pool, sqlite3_errmsg(conn->dbh), |
| 357 |
" (", strerror(xerrno), ")", NULL); |
| 358 |
sql_log(DEBUG_FUNC, "error opening SQLite database '%s': %s", conn->dsn, |
| 359 |
errstr); |
| 360 |
|
| 361 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_open"); |
| 362 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 363 |
} |
| 364 |
|
| 365 |
if (pr_trace_get_level(trace_channel) >= SQLITE_TRACE_LEVEL) { |
| 366 |
sqlite3_trace(conn->dbh, db_trace, NULL); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
stmt = "PRAGMA journal_mode = MEMORY;"; |
| 375 |
res = sqlite3_exec(conn->dbh, stmt, NULL, NULL, NULL); |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
while (res == SQLITE_BUSY) { |
| 381 |
struct timeval tv; |
| 382 |
|
| 383 |
nretries++; |
| 384 |
sql_log(DEBUG_FUNC, "attempt #%u, database busy, trying '%s' again", |
| 385 |
nretries, stmt); |
| 386 |
|
| 387 |
|
| 388 |
tv.tv_sec = 0; |
| 389 |
tv.tv_usec = 500000L; |
| 390 |
|
| 391 |
if (select(0, NULL, NULL, NULL, &tv) < 0) { |
| 392 |
if (errno == EINTR) { |
| 393 |
pr_signals_handle(); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
res = sqlite3_exec(conn->dbh, stmt, NULL, NULL, NULL); |
| 398 |
} |
| 399 |
|
| 400 |
if (res != SQLITE_OK) { |
| 401 |
sql_log(DEBUG_FUNC, "error setting MEMORY journal mode: %s", |
| 402 |
sqlite3_errmsg(conn->dbh)); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
sql_log(DEBUG_INFO, MOD_SQL_SQLITE_VERSION ": SQLite version: %s", |
| 407 |
sqlite3_libversion()); |
| 408 |
|
| 409 |
entry->nconn++; |
| 410 |
|
| 411 |
if (pr_sql_conn_policy == SQL_CONN_POLICY_PERSESSION) { |
| 412 |
|
| 413 |
if (entry->nconn == 1) { |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
entry->nconn++; |
| 419 |
} |
| 420 |
|
| 421 |
} else if (entry->ttl > 0) { |
| 422 |
|
| 423 |
entry->timer = pr_timer_add(entry->ttl, -1, &sql_sqlite_module, |
| 424 |
sql_sqlite_timer_cb, "sqlite connection ttl"); |
| 425 |
|
| 426 |
sql_log(DEBUG_INFO, "'%s' connection: %d second timer started", |
| 427 |
entry->name, entry->ttl); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
entry->nconn++; |
| 433 |
} |
| 434 |
|
| 435 |
sql_log(DEBUG_INFO, "'%s' connection opened", entry->name); |
| 436 |
sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name, |
| 437 |
entry->nconn); |
| 438 |
pr_event_generate("mod_sql.db.connection-opened", &sql_sqlite_module); |
| 439 |
|
| 440 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_open"); |
| 441 |
return PR_HANDLED(cmd); |
| 442 |
} |
| 443 |
|
| 444 |
MODRET sql_sqlite_close(cmd_rec *cmd) { |
| 445 |
conn_entry_t *entry = NULL; |
| 446 |
db_conn_t *conn = NULL; |
| 447 |
|
| 448 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_close"); |
| 449 |
|
| 450 |
if (cmd->argc < 1 || cmd->argc > 2) { |
| 451 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_close"); |
| 452 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 457 |
if (entry == NULL) { |
| 458 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_close"); |
| 459 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 460 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 461 |
} |
| 462 |
|
| 463 |
conn = (db_conn_t *) entry->data; |
| 464 |
|
| 465 |
|
| 466 |
if (entry->nconn == 0) { |
| 467 |
sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name, |
| 468 |
entry->nconn); |
| 469 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_close"); |
| 470 |
return PR_HANDLED(cmd); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
if ((--entry->nconn) == 0 || |
| 477 |
(cmd->argc == 2 && cmd->argv[1])) { |
| 478 |
|
| 479 |
if (conn->dbh) { |
| 480 |
if (sqlite3_close(conn->dbh) != SQLITE_OK) { |
| 481 |
sql_log(DEBUG_FUNC, "error closing SQLite database: %s", |
| 482 |
sqlite3_errmsg(conn->dbh)); |
| 483 |
} |
| 484 |
|
| 485 |
conn->dbh = NULL; |
| 486 |
} |
| 487 |
|
| 488 |
entry->nconn = 0; |
| 489 |
|
| 490 |
if (entry->timer) { |
| 491 |
pr_timer_remove(entry->timer, &sql_sqlite_module); |
| 492 |
entry->timer = 0; |
| 493 |
sql_log(DEBUG_INFO, "'%s' connection timer stopped", entry->name); |
| 494 |
} |
| 495 |
|
| 496 |
sql_log(DEBUG_INFO, "'%s' connection closed", entry->name); |
| 497 |
pr_event_generate("mod_sql.db.connection-closed", &sql_sqlite_module); |
| 498 |
} |
| 499 |
|
| 500 |
sql_log(DEBUG_INFO, "'%s' connection count is now %u", entry->name, |
| 501 |
entry->nconn); |
| 502 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_close"); |
| 503 |
|
| 504 |
return PR_HANDLED(cmd); |
| 505 |
} |
| 506 |
|
| 507 |
MODRET sql_sqlite_cleanup(cmd_rec *cmd) { |
| 508 |
destroy_pool(conn_pool); |
| 509 |
conn_pool = NULL; |
| 510 |
conn_cache = NULL; |
| 511 |
|
| 512 |
return mod_create_data(cmd, NULL); |
| 513 |
} |
| 514 |
|
| 515 |
MODRET sql_sqlite_def_conn(cmd_rec *cmd) { |
| 516 |
char *name = NULL; |
| 517 |
conn_entry_t *entry = NULL; |
| 518 |
db_conn_t *conn = NULL; |
| 519 |
|
| 520 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_defineconnection"); |
| 521 |
|
| 522 |
if (cmd->argc < 4 || |
| 523 |
cmd->argc > 10 || |
| 524 |
!cmd->argv[0]) { |
| 525 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_defineconnection"); |
| 526 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 527 |
} |
| 528 |
|
| 529 |
if (conn_pool == NULL) { |
| 530 |
pr_log_pri(PR_LOG_WARNING, "WARNING: the mod_sql_sqlite module has not " |
| 531 |
"been properly initialized. Please make sure your --with-modules " |
| 532 |
"configure option lists mod_sql *before* mod_sql_sqlite, and recompile."); |
| 533 |
|
| 534 |
sql_log(DEBUG_FUNC, "%s", "The mod_sql_sqlite module has not been properly " |
| 535 |
"initialized. Please make sure your --with-modules configure option " |
| 536 |
"lists mod_sql *before* mod_sql_sqlite, and recompile."); |
| 537 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_defineconnection"); |
| 538 |
|
| 539 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "uninitialized module"); |
| 540 |
} |
| 541 |
|
| 542 |
conn = (db_conn_t *) palloc(conn_pool, sizeof(db_conn_t)); |
| 543 |
|
| 544 |
name = pstrdup(conn_pool, cmd->argv[0]); |
| 545 |
conn->user = pstrdup(conn_pool, cmd->argv[1]); |
| 546 |
conn->pass = pstrdup(conn_pool, cmd->argv[2]); |
| 547 |
conn->dsn = pstrdup(conn_pool, cmd->argv[3]); |
| 548 |
|
| 549 |
|
| 550 |
entry = sql_sqlite_add_conn(conn_pool, name, (void *) conn); |
| 551 |
if (entry == NULL && |
| 552 |
errno == EEXIST) { |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
if (strcmp(name, "default") != 0) { |
| 558 |
sql_log(DEBUG_FUNC, "named connection '%s' already exists", name); |
| 559 |
} |
| 560 |
|
| 561 |
entry = sql_sqlite_get_conn(name); |
| 562 |
} |
| 563 |
|
| 564 |
if (entry == NULL) { |
| 565 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_defineconnection"); |
| 566 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 567 |
"error adding named connection"); |
| 568 |
} |
| 569 |
|
| 570 |
if (cmd->argc >= 5) { |
| 571 |
entry->ttl = (int) strtol(cmd->argv[4], (char **) NULL, 10); |
| 572 |
if (entry->ttl >= 1) { |
| 573 |
pr_sql_conn_policy = SQL_CONN_POLICY_TIMER; |
| 574 |
|
| 575 |
} else { |
| 576 |
entry->ttl = 0; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
entry->timer = 0; |
| 581 |
entry->nconn = 0; |
| 582 |
|
| 583 |
sql_log(DEBUG_INFO, " name: '%s'", entry->name); |
| 584 |
sql_log(DEBUG_INFO, " dsn: '%s'", conn->dsn); |
| 585 |
sql_log(DEBUG_INFO, " ttl: '%d'", entry->ttl); |
| 586 |
|
| 587 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_defineconnection"); |
| 588 |
return PR_HANDLED(cmd); |
| 589 |
} |
| 590 |
|
| 591 |
MODRET sql_sqlite_exit(cmd_rec *cmd) { |
| 592 |
register unsigned int i = 0; |
| 593 |
|
| 594 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_exit"); |
| 595 |
|
| 596 |
for (i = 0; i < conn_cache->nelts; i++) { |
| 597 |
conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i]; |
| 598 |
|
| 599 |
if (entry->nconn > 0) { |
| 600 |
cmd_rec *tmp = pr_cmd_alloc(conn_pool, 2, entry->name, "1"); |
| 601 |
sql_sqlite_close(tmp); |
| 602 |
destroy_pool(tmp->pool); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_exit"); |
| 607 |
|
| 608 |
return PR_HANDLED(cmd); |
| 609 |
} |
| 610 |
|
| 611 |
MODRET sql_sqlite_select(cmd_rec *cmd) { |
| 612 |
conn_entry_t *entry = NULL; |
| 613 |
db_conn_t *conn = NULL; |
| 614 |
modret_t *mr = NULL; |
| 615 |
char *errstr = NULL, *query = NULL; |
| 616 |
cmd_rec *close_cmd; |
| 617 |
|
| 618 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_select"); |
| 619 |
|
| 620 |
if (cmd->argc < 2) { |
| 621 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 622 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 627 |
if (entry == NULL) { |
| 628 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 629 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 630 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 631 |
} |
| 632 |
|
| 633 |
conn = (db_conn_t *) entry->data; |
| 634 |
|
| 635 |
mr = sql_sqlite_open(cmd); |
| 636 |
if (MODRET_ERROR(mr)) { |
| 637 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 638 |
return mr; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
if (cmd->argc == 2) { |
| 643 |
query = pstrcat(cmd->tmp_pool, "SELECT ", cmd->argv[1], NULL); |
| 644 |
|
| 645 |
} else { |
| 646 |
query = pstrcat(cmd->tmp_pool, cmd->argv[2], " FROM ", cmd->argv[1], NULL); |
| 647 |
|
| 648 |
if (cmd->argc > 3 && cmd->argv[3]) |
| 649 |
query = pstrcat(cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL); |
| 650 |
|
| 651 |
if (cmd->argc > 4 && cmd->argv[4]) |
| 652 |
query = pstrcat(cmd->tmp_pool, query, " LIMIT ", cmd->argv[4], NULL); |
| 653 |
|
| 654 |
if (cmd->argc > 5) { |
| 655 |
register unsigned int i = 0; |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
for (i = 5; i < cmd->argc; i++) { |
| 664 |
if (cmd->argv[i] && |
| 665 |
strcasecmp("DISTINCT", cmd->argv[i]) == 0) { |
| 666 |
query = pstrcat(cmd->tmp_pool, "DISTINCT ", query, NULL); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
query = pstrcat(cmd->tmp_pool, "SELECT ", query, NULL); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
sql_log(DEBUG_INFO, "query \"%s\"", query); |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
if (query_start(cmd, conn, 0, &errstr) < 0) { |
| 682 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 683 |
sql_sqlite_close(close_cmd); |
| 684 |
destroy_pool(close_cmd->pool); |
| 685 |
|
| 686 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 687 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 688 |
} |
| 689 |
|
| 690 |
if (query_run(cmd, conn, query, &errstr) < 0) { |
| 691 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 692 |
sql_sqlite_close(close_cmd); |
| 693 |
destroy_pool(close_cmd->pool); |
| 694 |
|
| 695 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 696 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 697 |
} |
| 698 |
|
| 699 |
if (query_finish(cmd, conn, &errstr) < 0) { |
| 700 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 701 |
sql_sqlite_close(close_cmd); |
| 702 |
destroy_pool(close_cmd->pool); |
| 703 |
|
| 704 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 705 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 706 |
} |
| 707 |
|
| 708 |
mr = sql_sqlite_get_data(cmd); |
| 709 |
|
| 710 |
|
| 711 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 712 |
sql_sqlite_close(close_cmd); |
| 713 |
destroy_pool(close_cmd->pool); |
| 714 |
|
| 715 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_select"); |
| 716 |
return mr; |
| 717 |
} |
| 718 |
|
| 719 |
MODRET sql_sqlite_insert(cmd_rec *cmd) { |
| 720 |
conn_entry_t *entry = NULL; |
| 721 |
db_conn_t *conn = NULL; |
| 722 |
modret_t *mr = NULL; |
| 723 |
char *errstr = NULL, *query = NULL; |
| 724 |
cmd_rec *close_cmd; |
| 725 |
|
| 726 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_insert"); |
| 727 |
|
| 728 |
if (cmd->argc != 2 && |
| 729 |
cmd->argc != 4) { |
| 730 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 731 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 736 |
if (entry == NULL) { |
| 737 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 738 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 739 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 740 |
} |
| 741 |
|
| 742 |
conn = (db_conn_t *) entry->data; |
| 743 |
|
| 744 |
mr = sql_sqlite_open(cmd); |
| 745 |
if (MODRET_ERROR(mr)) { |
| 746 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 747 |
return mr; |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
if (cmd->argc == 2) { |
| 752 |
query = pstrcat(cmd->tmp_pool, "INSERT ", cmd->argv[1], NULL); |
| 753 |
|
| 754 |
} else { |
| 755 |
query = pstrcat(cmd->tmp_pool, "INSERT INTO ", cmd->argv[1], |
| 756 |
" (", cmd->argv[2], ") VALUES (", cmd->argv[3], ")", NULL); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
sql_log(DEBUG_INFO, "query \"%s\"", query); |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
if (query_start(cmd, conn, SQL_SQLITE_START_FL_NOW, &errstr) < 0) { |
| 768 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 769 |
sql_sqlite_close(close_cmd); |
| 770 |
destroy_pool(close_cmd->pool); |
| 771 |
|
| 772 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 773 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 774 |
} |
| 775 |
|
| 776 |
if (query_run(cmd, conn, query, &errstr) < 0) { |
| 777 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 778 |
sql_sqlite_close(close_cmd); |
| 779 |
destroy_pool(close_cmd->pool); |
| 780 |
|
| 781 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 782 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 783 |
} |
| 784 |
|
| 785 |
if (query_finish(cmd, conn, &errstr) < 0) { |
| 786 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 787 |
sql_sqlite_close(close_cmd); |
| 788 |
destroy_pool(close_cmd->pool); |
| 789 |
|
| 790 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 791 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
result_ncols = 0; |
| 798 |
result_list = NULL; |
| 799 |
|
| 800 |
|
| 801 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 802 |
sql_sqlite_close(close_cmd); |
| 803 |
destroy_pool(close_cmd->pool); |
| 804 |
|
| 805 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_insert"); |
| 806 |
return PR_HANDLED(cmd); |
| 807 |
} |
| 808 |
|
| 809 |
MODRET sql_sqlite_update(cmd_rec *cmd) { |
| 810 |
conn_entry_t *entry = NULL; |
| 811 |
db_conn_t *conn = NULL; |
| 812 |
modret_t *mr = NULL; |
| 813 |
char *errstr = NULL, *query = NULL; |
| 814 |
cmd_rec *close_cmd; |
| 815 |
|
| 816 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_update"); |
| 817 |
|
| 818 |
if (cmd->argc < 2 || cmd->argc > 4) { |
| 819 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 820 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 825 |
if (entry == NULL) { |
| 826 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 827 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 828 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 829 |
} |
| 830 |
|
| 831 |
conn = (db_conn_t *) entry->data; |
| 832 |
|
| 833 |
mr = sql_sqlite_open(cmd); |
| 834 |
if (MODRET_ERROR(mr)) { |
| 835 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 836 |
return mr; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
if (cmd->argc == 2) { |
| 841 |
query = pstrcat(cmd->tmp_pool, "UPDATE ", cmd->argv[1], NULL); |
| 842 |
|
| 843 |
} else { |
| 844 |
query = pstrcat(cmd->tmp_pool, "UPDATE ", cmd->argv[1], " SET ", |
| 845 |
cmd->argv[2], NULL); |
| 846 |
|
| 847 |
if (cmd->argc > 3 && |
| 848 |
cmd->argv[3]) { |
| 849 |
query = pstrcat(cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
sql_log(DEBUG_INFO, "query \"%s\"", query); |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
if (query_start(cmd, conn, SQL_SQLITE_START_FL_NOW, &errstr) < 0) { |
| 861 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 862 |
sql_sqlite_close(close_cmd); |
| 863 |
destroy_pool(close_cmd->pool); |
| 864 |
|
| 865 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 866 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 867 |
} |
| 868 |
|
| 869 |
if (query_run(cmd, conn, query, &errstr) < 0) { |
| 870 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 871 |
sql_sqlite_close(close_cmd); |
| 872 |
destroy_pool(close_cmd->pool); |
| 873 |
|
| 874 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 875 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 876 |
} |
| 877 |
|
| 878 |
if (query_finish(cmd, conn, &errstr) < 0) { |
| 879 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 880 |
sql_sqlite_close(close_cmd); |
| 881 |
destroy_pool(close_cmd->pool); |
| 882 |
|
| 883 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 884 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
result_ncols = 0; |
| 891 |
result_list = NULL; |
| 892 |
|
| 893 |
|
| 894 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 895 |
sql_sqlite_close(close_cmd); |
| 896 |
destroy_pool(close_cmd->pool); |
| 897 |
|
| 898 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_update"); |
| 899 |
return PR_HANDLED(cmd); |
| 900 |
} |
| 901 |
|
| 902 |
MODRET sql_sqlite_prepare(cmd_rec *cmd) { |
| 903 |
if (cmd->argc != 1) { |
| 904 |
return PR_ERROR(cmd); |
| 905 |
} |
| 906 |
|
| 907 |
conn_pool = (pool *) cmd->argv[0]; |
| 908 |
|
| 909 |
if (conn_cache == NULL) { |
| 910 |
conn_cache = make_array(conn_pool, DEF_CONN_POOL_SIZE, |
| 911 |
sizeof(conn_entry_t *)); |
| 912 |
} |
| 913 |
|
| 914 |
return mod_create_data(cmd, NULL); |
| 915 |
} |
| 916 |
|
| 917 |
MODRET sql_sqlite_procedure(cmd_rec *cmd) { |
| 918 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_procedure"); |
| 919 |
|
| 920 |
if (cmd->argc != 3) { |
| 921 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_procedure"); |
| 922 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 923 |
} |
| 924 |
|
| 925 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_procedure"); |
| 926 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 927 |
"backend does not support procedures"); |
| 928 |
} |
| 929 |
|
| 930 |
MODRET sql_sqlite_query(cmd_rec *cmd) { |
| 931 |
conn_entry_t *entry = NULL; |
| 932 |
db_conn_t *conn = NULL; |
| 933 |
modret_t *mr = NULL; |
| 934 |
char *errstr = NULL, *query = NULL; |
| 935 |
cmd_rec *close_cmd; |
| 936 |
|
| 937 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_query"); |
| 938 |
|
| 939 |
if (cmd->argc != 2) { |
| 940 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 941 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 946 |
if (entry == NULL) { |
| 947 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 948 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 949 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 950 |
} |
| 951 |
|
| 952 |
conn = (db_conn_t *) entry->data; |
| 953 |
|
| 954 |
mr = sql_sqlite_open(cmd); |
| 955 |
if (MODRET_ERROR(mr)) { |
| 956 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 957 |
return mr; |
| 958 |
} |
| 959 |
|
| 960 |
query = pstrdup(cmd->tmp_pool, cmd->argv[1]); |
| 961 |
|
| 962 |
|
| 963 |
sql_log(DEBUG_INFO, "query \"%s\"", query); |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
if (query_start(cmd, conn, 0, &errstr) < 0) { |
| 970 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 971 |
sql_sqlite_close(close_cmd); |
| 972 |
destroy_pool(close_cmd->pool); |
| 973 |
|
| 974 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 975 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 976 |
} |
| 977 |
|
| 978 |
if (query_run(cmd, conn, query, &errstr) < 0) { |
| 979 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 980 |
sql_sqlite_close(close_cmd); |
| 981 |
destroy_pool(close_cmd->pool); |
| 982 |
|
| 983 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 984 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 985 |
} |
| 986 |
|
| 987 |
if (query_finish(cmd, conn, &errstr) < 0) { |
| 988 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 989 |
sql_sqlite_close(close_cmd); |
| 990 |
destroy_pool(close_cmd->pool); |
| 991 |
|
| 992 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 993 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, errstr); |
| 994 |
} |
| 995 |
|
| 996 |
mr = sql_sqlite_get_data(cmd); |
| 997 |
|
| 998 |
|
| 999 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 1000 |
sql_sqlite_close(close_cmd); |
| 1001 |
destroy_pool(close_cmd->pool); |
| 1002 |
|
| 1003 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_query"); |
| 1004 |
return mr; |
| 1005 |
} |
| 1006 |
|
| 1007 |
MODRET sql_sqlite_quote(cmd_rec *cmd) { |
| 1008 |
conn_entry_t *entry = NULL; |
| 1009 |
modret_t *mr = NULL; |
| 1010 |
char *unescaped = NULL, *escaped = NULL, *ptr = NULL; |
| 1011 |
cmd_rec *close_cmd; |
| 1012 |
|
| 1013 |
sql_log(DEBUG_FUNC, "%s", "entering \tsqlite cmd_escapestring"); |
| 1014 |
|
| 1015 |
if (cmd->argc != 2) { |
| 1016 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_escapestring"); |
| 1017 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, "badly formed request"); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
entry = sql_sqlite_get_conn(cmd->argv[0]); |
| 1022 |
if (entry == NULL) { |
| 1023 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_escapestring"); |
| 1024 |
return PR_ERROR_MSG(cmd, MOD_SQL_SQLITE_VERSION, |
| 1025 |
pstrcat(cmd->tmp_pool, "unknown named connection: ", cmd->argv[0], NULL)); |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
mr = sql_sqlite_open(cmd); |
| 1030 |
if (MODRET_ERROR(mr)) { |
| 1031 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_escapestring"); |
| 1032 |
return mr; |
| 1033 |
} |
| 1034 |
|
| 1035 |
unescaped = cmd->argv[1]; |
| 1036 |
ptr = sqlite3_mprintf("%q", unescaped); |
| 1037 |
escaped = pstrdup(cmd->pool, ptr); |
| 1038 |
pr_trace_msg(trace_channel, 17, "quoted '%s' to '%s'", unescaped, escaped); |
| 1039 |
sqlite3_free(ptr); |
| 1040 |
|
| 1041 |
close_cmd = pr_cmd_alloc(cmd->tmp_pool, 1, entry->name); |
| 1042 |
sql_sqlite_close(close_cmd); |
| 1043 |
destroy_pool(close_cmd->pool); |
| 1044 |
|
| 1045 |
sql_log(DEBUG_FUNC, "%s", "exiting \tsqlite cmd_escapestring"); |
| 1046 |
return mod_create_data(cmd, escaped); |
| 1047 |
} |
| 1048 |
|
| 1049 |
MODRET sql_sqlite_identify(cmd_rec *cmd) { |
| 1050 |
sql_data_t *sd = NULL; |
| 1051 |
|
| 1052 |
sd = (sql_data_t *) pcalloc(cmd->tmp_pool, sizeof(sql_data_t)); |
| 1053 |
sd->data = (char **) pcalloc(cmd->tmp_pool, sizeof(char *) * 2); |
| 1054 |
|
| 1055 |
sd->rnum = 1; |
| 1056 |
sd->fnum = 2; |
| 1057 |
|
| 1058 |
sd->data[0] = MOD_SQL_SQLITE_VERSION; |
| 1059 |
sd->data[1] = MOD_SQL_API_V1; |
| 1060 |
|
| 1061 |
return mod_create_data(cmd, (void *) sd); |
| 1062 |
} |
| 1063 |
|
| 1064 |
static cmdtable sql_sqlite_cmdtable[] = { |
| 1065 |
{ CMD, "sql_close", G_NONE, sql_sqlite_close, FALSE, FALSE }, |
| 1066 |
{ CMD, "sql_cleanup", G_NONE, sql_sqlite_cleanup, FALSE, FALSE }, |
| 1067 |
{ CMD, "sql_defineconnection",G_NONE, sql_sqlite_def_conn, FALSE, FALSE }, |
| 1068 |
{ CMD, "sql_escapestring", G_NONE, sql_sqlite_quote, FALSE, FALSE }, |
| 1069 |
{ CMD, "sql_exit", G_NONE, sql_sqlite_exit, FALSE, FALSE }, |
| 1070 |
{ CMD, "sql_identify", G_NONE, sql_sqlite_identify, FALSE, FALSE }, |
| 1071 |
{ CMD, "sql_insert", G_NONE, sql_sqlite_insert, FALSE, FALSE }, |
| 1072 |
{ CMD, "sql_open", G_NONE, sql_sqlite_open, FALSE, FALSE }, |
| 1073 |
{ CMD, "sql_prepare", G_NONE, sql_sqlite_prepare, FALSE, FALSE }, |
| 1074 |
{ CMD, "sql_procedure", G_NONE, sql_sqlite_procedure, FALSE, FALSE }, |
| 1075 |
{ CMD, "sql_query", G_NONE, sql_sqlite_query, FALSE, FALSE }, |
| 1076 |
{ CMD, "sql_select", G_NONE, sql_sqlite_select, FALSE, FALSE }, |
| 1077 |
{ CMD, "sql_update", G_NONE, sql_sqlite_update, FALSE, FALSE }, |
| 1078 |
{ 0, NULL } |
| 1079 |
}; |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
static void sql_sqlite_mod_load_ev(const void *event_data, void *user_data) { |
| 1085 |
if (strcmp("mod_sql_sqlite.c", (const char *) event_data) == 0) { |
| 1086 |
|
| 1087 |
if (sql_register_backend("sqlite3", sql_sqlite_cmdtable) < 0) { |
| 1088 |
pr_log_pri(PR_LOG_NOTICE, MOD_SQL_SQLITE_VERSION |
| 1089 |
": notice: error registering backend: %s", strerror(errno)); |
| 1090 |
pr_session_end(0); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
static void sql_sqlite_mod_unload_ev(const void *event_data, void *user_data) { |
| 1096 |
if (strcmp("mod_sql_sqlite.c", (const char *) event_data) == 0) { |
| 1097 |
|
| 1098 |
if (sql_unregister_backend("sqlite3") < 0) { |
| 1099 |
pr_log_pri(PR_LOG_NOTICE, MOD_SQL_SQLITE_VERSION |
| 1100 |
": notice: error unregistering backend: %s", strerror(errno)); |
| 1101 |
pr_session_end(0); |
| 1102 |
} |
| 1103 |
|
| 1104 |
pr_event_unregister(&sql_sqlite_module, NULL, NULL); |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
static int sql_sqlite_init(void) { |
| 1116 |
|
| 1117 |
|
| 1118 |
pr_event_register(&sql_sqlite_module, "core.module-load", |
| 1119 |
sql_sqlite_mod_load_ev, NULL); |
| 1120 |
pr_event_register(&sql_sqlite_module, "core.module-unload", |
| 1121 |
sql_sqlite_mod_unload_ev, NULL); |
| 1122 |
|
| 1123 |
#if defined(SQLITE_CONFIG_SINGLETHREAD) |
| 1124 |
|
| 1125 |
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
| 1126 |
#endif |
| 1127 |
|
| 1128 |
#if defined(SQLITE_CONFIG_LOG) |
| 1129 |
sqlite3_config(SQLITE_CONFIG_LOG, db_err, NULL); |
| 1130 |
#endif |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
if (strcmp(sqlite3_libversion(), SQLITE_VERSION) != 0) { |
| 1138 |
pr_log_pri(PR_LOG_INFO, MOD_SQL_SQLITE_VERSION |
| 1139 |
": compiled using SQLite version '%s' headers, but linked to " |
| 1140 |
"SQLite version '%s' library", SQLITE_VERSION, sqlite3_libversion()); |
| 1141 |
} |
| 1142 |
|
| 1143 |
pr_log_debug(DEBUG3, MOD_SQL_SQLITE_VERSION ": using SQLite %s", |
| 1144 |
sqlite3_libversion()); |
| 1145 |
|
| 1146 |
return 0; |
| 1147 |
} |
| 1148 |
|
| 1149 |
static int sql_sqlite_sess_init(void) { |
| 1150 |
if (conn_pool != NULL) { |
| 1151 |
destroy_pool(conn_pool); |
| 1152 |
conn_cache = NULL; |
| 1153 |
} |
| 1154 |
|
| 1155 |
conn_pool = make_sub_pool(session.pool); |
| 1156 |
pr_pool_tag(conn_pool, "SQLite connection pool"); |
| 1157 |
|
| 1158 |
if (conn_cache == NULL) { |
| 1159 |
conn_cache = make_array(conn_pool, DEF_CONN_POOL_SIZE, |
| 1160 |
sizeof(conn_entry_t *)); |
| 1161 |
} |
| 1162 |
|
| 1163 |
return 0; |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
module sql_sqlite_module = { |
| 1170 |
NULL, NULL, |
| 1171 |
|
| 1172 |
|
| 1173 |
0x20, |
| 1174 |
|
| 1175 |
|
| 1176 |
"sql_sqlite", |
| 1177 |
|
| 1178 |
|
| 1179 |
NULL, |
| 1180 |
|
| 1181 |
|
| 1182 |
NULL, |
| 1183 |
|
| 1184 |
|
| 1185 |
NULL, |
| 1186 |
|
| 1187 |
|
| 1188 |
sql_sqlite_init, |
| 1189 |
|
| 1190 |
|
| 1191 |
sql_sqlite_sess_init, |
| 1192 |
|
| 1193 |
|
| 1194 |
MOD_SQL_SQLITE_VERSION |
| 1195 |
}; |