| 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 |
#include "asterisk.h" |
| 44 |
|
| 45 |
#include <sqlite3.h> |
| 46 |
|
| 47 |
#include "asterisk/module.h" |
| 48 |
#include "asterisk/config.h" |
| 49 |
#include "asterisk/paths.h" |
| 50 |
#include "asterisk/astobj2.h" |
| 51 |
#include "asterisk/lock.h" |
| 52 |
#include "asterisk/utils.h" |
| 53 |
#include "asterisk/app.h" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static int has_explicit_like_escaping; |
| 59 |
|
| 60 |
static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked); |
| 61 |
static struct ast_variable *realtime_sqlite3(const char *database, const char *table, const struct ast_variable *fields); |
| 62 |
static struct ast_config *realtime_sqlite3_multi(const char *database, const char *table, const struct ast_variable *fields); |
| 63 |
static int realtime_sqlite3_update(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields); |
| 64 |
static int realtime_sqlite3_update2(const char *database, const char *table, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields); |
| 65 |
static int realtime_sqlite3_store(const char *database, const char *table, const struct ast_variable *fields); |
| 66 |
static int realtime_sqlite3_destroy(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields); |
| 67 |
static int realtime_sqlite3_require(const char *database, const char *table, va_list ap); |
| 68 |
static int realtime_sqlite3_unload(const char *database, const char *table); |
| 69 |
|
| 70 |
struct ast_config_engine sqlite3_config_engine = { |
| 71 |
.name = "sqlite3", |
| 72 |
.load_func = realtime_sqlite3_load, |
| 73 |
.realtime_func = realtime_sqlite3, |
| 74 |
.realtime_multi_func = realtime_sqlite3_multi, |
| 75 |
.update_func = realtime_sqlite3_update, |
| 76 |
.update2_func = realtime_sqlite3_update2, |
| 77 |
.store_func = realtime_sqlite3_store, |
| 78 |
.destroy_func = realtime_sqlite3_destroy, |
| 79 |
.require_func = realtime_sqlite3_require, |
| 80 |
.unload_func = realtime_sqlite3_unload, |
| 81 |
}; |
| 82 |
|
| 83 |
enum { |
| 84 |
REALTIME_SQLITE3_REQ_WARN, |
| 85 |
REALTIME_SQLITE3_REQ_CLOSE, |
| 86 |
REALTIME_SQLITE3_REQ_CHAR, |
| 87 |
}; |
| 88 |
|
| 89 |
struct realtime_sqlite3_db { |
| 90 |
AST_DECLARE_STRING_FIELDS( |
| 91 |
AST_STRING_FIELD(name); |
| 92 |
AST_STRING_FIELD(filename); |
| 93 |
); |
| 94 |
sqlite3 *handle; |
| 95 |
pthread_t syncthread; |
| 96 |
ast_cond_t cond; |
| 97 |
unsigned int requirements:2; |
| 98 |
unsigned int dirty:1; |
| 99 |
unsigned int debug:1; |
| 100 |
unsigned int exiting:1; |
| 101 |
unsigned int wakeup:1; |
| 102 |
unsigned int has_batch_thread:1; |
| 103 |
unsigned int batch; |
| 104 |
int busy_timeout; |
| 105 |
}; |
| 106 |
|
| 107 |
struct ao2_container *databases; |
| 108 |
#define DB_BUCKETS 7 |
| 109 |
|
| 110 |
AST_MUTEX_DEFINE_STATIC(config_lock); |
| 111 |
|
| 112 |
|
| 113 |
AST_THREADSTORAGE(escape_table_buf); |
| 114 |
AST_THREADSTORAGE(escape_column_buf); |
| 115 |
AST_THREADSTORAGE(escape_value_buf); |
| 116 |
|
| 117 |
typedef int (*callback_t)(void*, int, char **, char **); |
| 118 |
|
| 119 |
static int realtime_sqlite3_exec_query_with_handle(struct realtime_sqlite3_db *, const char *, callback_t, void *); |
| 120 |
static int realtime_sqlite3_exec_query(const char *, const char *, callback_t, void *); |
| 121 |
static int realtime_sqlite3_exec_update_with_handle(struct realtime_sqlite3_db *, const char *); |
| 122 |
static int realtime_sqlite3_exec_update(const char *, const char *); |
| 123 |
|
| 124 |
void db_start_batch(struct realtime_sqlite3_db *db); |
| 125 |
void db_stop_batch(struct realtime_sqlite3_db *db); |
| 126 |
|
| 127 |
static inline const char *sqlite3_escape_string_helper(struct ast_threadstorage *ts, const char *param) |
| 128 |
{ |
| 129 |
size_t maxlen = strlen(param) * 2 + sizeof("\"\""); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
struct ast_str *buf = ast_str_thread_get(ts, maxlen); |
| 135 |
char q = ts == &escape_value_buf ? '\'' : '"'; |
| 136 |
char *tmp; |
| 137 |
|
| 138 |
if (ast_str_size(buf) < maxlen) { |
| 139 |
|
| 140 |
ast_str_make_space(&buf, maxlen); |
| 141 |
} |
| 142 |
tmp = ast_str_buffer(buf); |
| 143 |
|
| 144 |
ast_str_reset(buf); |
| 145 |
*tmp++ = q; |
| 146 |
while ((*tmp++ = *param++)) { |
| 147 |
|
| 148 |
if (*(tmp - 1) == q) { |
| 149 |
*tmp++ = q; |
| 150 |
} |
| 151 |
} |
| 152 |
*tmp = '\0'; |
| 153 |
*(tmp - 1) = q; |
| 154 |
ast_str_update(buf); |
| 155 |
|
| 156 |
return ast_str_buffer(buf); |
| 157 |
} |
| 158 |
|
| 159 |
static inline const char *sqlite3_escape_table(const char *param) |
| 160 |
{ |
| 161 |
return sqlite3_escape_string_helper(&escape_table_buf, param); |
| 162 |
} |
| 163 |
|
| 164 |
static inline const char *sqlite3_escape_column(const char *param) |
| 165 |
{ |
| 166 |
return sqlite3_escape_string_helper(&escape_column_buf, param); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
static const char *sqlite3_escape_column_op(const char *param) |
| 171 |
{ |
| 172 |
size_t maxlen = strlen(param) * 2 + sizeof("\"\" ="); |
| 173 |
struct ast_str *buf = ast_str_thread_get(&escape_column_buf, maxlen); |
| 174 |
char *tmp; |
| 175 |
int space = 0; |
| 176 |
|
| 177 |
if (ast_str_size(buf) < maxlen) { |
| 178 |
|
| 179 |
ast_str_make_space(&buf, maxlen); |
| 180 |
} |
| 181 |
tmp = ast_str_buffer(buf); |
| 182 |
|
| 183 |
ast_str_reset(buf); |
| 184 |
*tmp++ = '"'; |
| 185 |
while ((*tmp++ = *param++)) { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if (space) { |
| 190 |
continue; |
| 191 |
} |
| 192 |
if (*(tmp - 1) == ' ') { |
| 193 |
*(tmp - 1) = '"'; |
| 194 |
*tmp++ = ' '; |
| 195 |
space = 1; |
| 196 |
} else if (*(tmp - 1) == '"') { |
| 197 |
*tmp++ = '"'; |
| 198 |
} |
| 199 |
} |
| 200 |
if (!space) { |
| 201 |
strcpy(tmp - 1, "\" ="); |
| 202 |
} |
| 203 |
|
| 204 |
ast_str_update(buf); |
| 205 |
|
| 206 |
return ast_str_buffer(buf); |
| 207 |
} |
| 208 |
|
| 209 |
static inline const char *sqlite3_escape_value(const char *param) |
| 210 |
{ |
| 211 |
return sqlite3_escape_string_helper(&escape_value_buf, param); |
| 212 |
} |
| 213 |
|
| 214 |
static int db_hash_fn(const void *obj, const int flags) |
| 215 |
{ |
| 216 |
const struct realtime_sqlite3_db *db = obj; |
| 217 |
|
| 218 |
return ast_str_hash(flags & OBJ_KEY ? (const char *) obj : db->name); |
| 219 |
} |
| 220 |
|
| 221 |
static int db_cmp_fn(void *obj, void *arg, int flags) { |
| 222 |
struct realtime_sqlite3_db *db = obj, *other = arg; |
| 223 |
const char *name = arg; |
| 224 |
|
| 225 |
return !strcasecmp(db->name, flags & OBJ_KEY ? name : other->name) ? CMP_MATCH | CMP_STOP : 0; |
| 226 |
} |
| 227 |
|
| 228 |
static void db_destructor(void *obj) |
| 229 |
{ |
| 230 |
struct realtime_sqlite3_db *db = obj; |
| 231 |
|
| 232 |
ast_debug(1, "Destroying db: %s\n", db->name); |
| 233 |
ast_string_field_free_memory(db); |
| 234 |
db_stop_batch(db); |
| 235 |
if (db->handle) { |
| 236 |
ao2_lock(db); |
| 237 |
sqlite3_close(db->handle); |
| 238 |
ao2_unlock(db); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
static struct realtime_sqlite3_db *find_database(const char *database) |
| 243 |
{ |
| 244 |
return ao2_find(databases, database, OBJ_KEY); |
| 245 |
} |
| 246 |
|
| 247 |
static void unref_db(struct realtime_sqlite3_db **db) |
| 248 |
{ |
| 249 |
ao2_ref(*db, -1); |
| 250 |
*db = NULL; |
| 251 |
} |
| 252 |
|
| 253 |
static int stop_batch_cb(void *obj, void *arg, int flags) |
| 254 |
{ |
| 255 |
struct realtime_sqlite3_db *db = obj; |
| 256 |
|
| 257 |
db_stop_batch(db); |
| 258 |
return CMP_MATCH; |
| 259 |
} |
| 260 |
|
| 261 |
static int mark_dirty_cb(void *obj, void *arg, int flags) |
| 262 |
{ |
| 263 |
struct realtime_sqlite3_db *db = obj; |
| 264 |
db->dirty = 1; |
| 265 |
return CMP_MATCH; |
| 266 |
} |
| 267 |
|
| 268 |
static void mark_all_databases_dirty(void) |
| 269 |
{ |
| 270 |
ao2_callback(databases, OBJ_MULTIPLE | OBJ_NODATA, mark_dirty_cb, NULL); |
| 271 |
} |
| 272 |
|
| 273 |
static int is_dirty_cb(void *obj, void *arg, int flags) |
| 274 |
{ |
| 275 |
struct realtime_sqlite3_db *db = obj; |
| 276 |
if (db->dirty) { |
| 277 |
db_stop_batch(db); |
| 278 |
return CMP_MATCH; |
| 279 |
} |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static void unlink_dirty_databases(void) |
| 284 |
{ |
| 285 |
ao2_callback(databases, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, is_dirty_cb, NULL); |
| 286 |
} |
| 287 |
|
| 288 |
static int str_to_requirements(const char *data) |
| 289 |
{ |
| 290 |
if (!strcasecmp(data, "createclose")) { |
| 291 |
return REALTIME_SQLITE3_REQ_CLOSE; |
| 292 |
} else if (!strcasecmp(data, "createchar")) { |
| 293 |
return REALTIME_SQLITE3_REQ_CHAR; |
| 294 |
} |
| 295 |
|
| 296 |
return REALTIME_SQLITE3_REQ_WARN; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
static void trace_cb(void *arg, const char *sql) |
| 301 |
{ |
| 302 |
struct realtime_sqlite3_db *db = arg; |
| 303 |
ast_debug(3, "DB: %s SQL: %s\n", db->name, sql); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
static void *db_sync_thread(void *data) |
| 308 |
{ |
| 309 |
struct realtime_sqlite3_db *db = data; |
| 310 |
ao2_lock(db); |
| 311 |
realtime_sqlite3_exec_query_with_handle(db, "BEGIN TRANSACTION", NULL, NULL); |
| 312 |
for (;;) { |
| 313 |
if (!db->wakeup) { |
| 314 |
ast_cond_wait(&db->cond, ao2_object_get_lockaddr(db)); |
| 315 |
} |
| 316 |
db->wakeup = 0; |
| 317 |
if (realtime_sqlite3_exec_query_with_handle(db, "COMMIT", NULL, NULL) < 0) { |
| 318 |
realtime_sqlite3_exec_query_with_handle(db, "ROLLBACK", NULL, NULL); |
| 319 |
} |
| 320 |
if (db->exiting) { |
| 321 |
ao2_unlock(db); |
| 322 |
break; |
| 323 |
} |
| 324 |
realtime_sqlite3_exec_query_with_handle(db, "BEGIN TRANSACTION", NULL, NULL); |
| 325 |
ao2_unlock(db); |
| 326 |
usleep(1000 * db->batch); |
| 327 |
ao2_lock(db); |
| 328 |
} |
| 329 |
|
| 330 |
unref_db(&db); |
| 331 |
|
| 332 |
return NULL; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
static int db_open(struct realtime_sqlite3_db *db) |
| 337 |
{ |
| 338 |
ao2_lock(db); |
| 339 |
if (sqlite3_open(db->filename, &db->handle) != SQLITE_OK) { |
| 340 |
ast_log(LOG_WARNING, "Could not open %s: %s\n", db->filename, sqlite3_errmsg(db->handle)); |
| 341 |
ao2_unlock(db); |
| 342 |
return -1; |
| 343 |
} |
| 344 |
sqlite3_busy_timeout(db->handle, db->busy_timeout); |
| 345 |
|
| 346 |
if (db->debug) { |
| 347 |
sqlite3_trace(db->handle, trace_cb, db); |
| 348 |
} else { |
| 349 |
sqlite3_trace(db->handle, NULL, NULL); |
| 350 |
} |
| 351 |
|
| 352 |
ao2_unlock(db); |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
static void db_sync(struct realtime_sqlite3_db *db) |
| 358 |
{ |
| 359 |
db->wakeup = 1; |
| 360 |
ast_cond_signal(&db->cond); |
| 361 |
} |
| 362 |
|
| 363 |
void db_start_batch(struct realtime_sqlite3_db *db) |
| 364 |
{ |
| 365 |
if (db->batch) { |
| 366 |
ast_cond_init(&db->cond, NULL); |
| 367 |
ao2_ref(db, +1); |
| 368 |
db->has_batch_thread = !ast_pthread_create_background(&db->syncthread, NULL, db_sync_thread, db); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void db_stop_batch(struct realtime_sqlite3_db *db) |
| 373 |
{ |
| 374 |
if (db->has_batch_thread) { |
| 375 |
db->has_batch_thread = 0; |
| 376 |
db->exiting = 1; |
| 377 |
db_sync(db); |
| 378 |
pthread_join(db->syncthread, NULL); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static struct realtime_sqlite3_db *new_realtime_sqlite3_db(struct ast_config *config, const char *cat) |
| 386 |
{ |
| 387 |
struct ast_variable *var; |
| 388 |
struct realtime_sqlite3_db *db; |
| 389 |
|
| 390 |
if (!(db = ao2_alloc(sizeof(*db), db_destructor))) { |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
if (ast_string_field_init(db, 64)) { |
| 395 |
unref_db(&db); |
| 396 |
return NULL; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
db->requirements = REALTIME_SQLITE3_REQ_WARN; |
| 401 |
db->batch = 100; |
| 402 |
ast_string_field_set(db, name, cat); |
| 403 |
db->busy_timeout = 1000; |
| 404 |
|
| 405 |
for (var = ast_variable_browse(config, cat); var; var = var->next) { |
| 406 |
if (!strcasecmp(var->name, "dbfile")) { |
| 407 |
ast_string_field_set(db, filename, var->value); |
| 408 |
} else if (!strcasecmp(var->name, "requirements")) { |
| 409 |
db->requirements = str_to_requirements(var->value); |
| 410 |
} else if (!strcasecmp(var->name, "batch")) { |
| 411 |
ast_app_parse_timelen(var->value, (int *) &db->batch, TIMELEN_MILLISECONDS); |
| 412 |
} else if (!strcasecmp(var->name, "debug")) { |
| 413 |
db->debug = ast_true(var->value); |
| 414 |
} else if (!strcasecmp(var->name, "busy_timeout")) { |
| 415 |
if (ast_parse_arg(var->value, PARSE_INT32|PARSE_DEFAULT, &(db->busy_timeout), 1000) != 0) { |
| 416 |
ast_log(LOG_WARNING, "Invalid busy_timeout value '%s' at res_config_sqlite3.conf:%d. Using 1000 instead.\n", var->value, var->lineno); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (ast_strlen_zero(db->filename)) { |
| 422 |
ast_log(LOG_WARNING, "Must specify dbfile in res_config_sqlite3.conf\n"); |
| 423 |
unref_db(&db); |
| 424 |
return NULL; |
| 425 |
} |
| 426 |
|
| 427 |
return db; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static int update_realtime_sqlite3_db(struct realtime_sqlite3_db *db, struct ast_config *config, const char *cat) |
| 436 |
{ |
| 437 |
struct realtime_sqlite3_db *new; |
| 438 |
|
| 439 |
if (!(new = new_realtime_sqlite3_db(config, cat))) { |
| 440 |
return -1; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
db->requirements = new->requirements; |
| 445 |
|
| 446 |
|
| 447 |
if (db->debug != new->debug) { |
| 448 |
if (db->debug) { |
| 449 |
sqlite3_trace(db->handle, NULL, NULL); |
| 450 |
} else { |
| 451 |
sqlite3_trace(db->handle, trace_cb, db); |
| 452 |
} |
| 453 |
db->debug = new->debug; |
| 454 |
} |
| 455 |
|
| 456 |
if (strcmp(db->filename, new->filename)) { |
| 457 |
sqlite3_close(db->handle); |
| 458 |
ast_string_field_set(db, filename, new->filename); |
| 459 |
db_open(db); |
| 460 |
} |
| 461 |
|
| 462 |
if (db->busy_timeout != new->busy_timeout) { |
| 463 |
db->busy_timeout = new->busy_timeout; |
| 464 |
sqlite3_busy_timeout(db->handle, db->busy_timeout); |
| 465 |
} |
| 466 |
|
| 467 |
if (db->batch != new->batch) { |
| 468 |
if (db->batch == 0) { |
| 469 |
db->batch = new->batch; |
| 470 |
db_start_batch(db); |
| 471 |
} else if (new->batch == 0) { |
| 472 |
db->batch = new->batch; |
| 473 |
db_stop_batch(db); |
| 474 |
} |
| 475 |
db->batch = new->batch; |
| 476 |
} |
| 477 |
|
| 478 |
db->dirty = 0; |
| 479 |
unref_db(&new); |
| 480 |
|
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
static int row_to_varlist(void *arg, int num_columns, char **values, char **columns) |
| 486 |
{ |
| 487 |
struct ast_variable **head = arg, *tail; |
| 488 |
int i; |
| 489 |
struct ast_variable *new; |
| 490 |
|
| 491 |
if (!(new = ast_variable_new(columns[0], S_OR(values[0], ""), ""))) { |
| 492 |
return SQLITE_ABORT; |
| 493 |
} |
| 494 |
*head = tail = new; |
| 495 |
|
| 496 |
for (i = 1; i < num_columns; i++) { |
| 497 |
if (!(new = ast_variable_new(columns[i], S_OR(values[i], ""), ""))) { |
| 498 |
ast_variables_destroy(*head); |
| 499 |
*head = NULL; |
| 500 |
return SQLITE_ABORT; |
| 501 |
} |
| 502 |
tail->next = new; |
| 503 |
tail = new; |
| 504 |
} |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
static int append_row_to_cfg(void *arg, int num_columns, char **values, char **columns) |
| 511 |
{ |
| 512 |
struct ast_config *cfg = arg; |
| 513 |
struct ast_category *cat; |
| 514 |
int i; |
| 515 |
|
| 516 |
cat = ast_category_new_anonymous(); |
| 517 |
if (!cat) { |
| 518 |
return SQLITE_ABORT; |
| 519 |
} |
| 520 |
|
| 521 |
for (i = 0; i < num_columns; i++) { |
| 522 |
struct ast_variable *var; |
| 523 |
if (!(var = ast_variable_new(columns[i], S_OR(values[i], ""), ""))) { |
| 524 |
ast_log(LOG_ERROR, "Could not create new variable for '%s: %s', throwing away list\n", columns[i], values[i]); |
| 525 |
continue; |
| 526 |
} |
| 527 |
ast_variable_append(cat, var); |
| 528 |
} |
| 529 |
ast_category_append(cfg, cat); |
| 530 |
|
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
struct cfg_entry_args { |
| 540 |
struct ast_config *cfg; |
| 541 |
struct ast_category *cat; |
| 542 |
char *cat_name; |
| 543 |
struct ast_flags flags; |
| 544 |
const char *who_asked; |
| 545 |
}; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
struct row_counter_args { |
| 551 |
callback_t wrapped_callback; |
| 552 |
void *wrapped_arg; |
| 553 |
int row_count; |
| 554 |
}; |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
static int row_counter_wrapper(void *arg, int num_columns, char **values, char **columns) |
| 574 |
{ |
| 575 |
struct row_counter_args *wrapped = arg; |
| 576 |
wrapped->row_count++; |
| 577 |
if (wrapped->wrapped_callback) { |
| 578 |
return wrapped->wrapped_callback(wrapped->wrapped_arg, num_columns, values, columns); |
| 579 |
} |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
static int realtime_sqlite3_exec_query_with_handle(struct realtime_sqlite3_db *db, const char *sql, callback_t callback, void *arg) |
| 597 |
{ |
| 598 |
int res = 0; |
| 599 |
char *errmsg; |
| 600 |
struct row_counter_args wrapper = { |
| 601 |
.wrapped_callback = callback, |
| 602 |
.wrapped_arg = arg, |
| 603 |
.row_count = 0, |
| 604 |
}; |
| 605 |
|
| 606 |
ao2_lock(db); |
| 607 |
if (sqlite3_exec(db->handle, sql, row_counter_wrapper, &wrapper, &errmsg) != SQLITE_OK) { |
| 608 |
ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg); |
| 609 |
sqlite3_free(errmsg); |
| 610 |
res = -1; |
| 611 |
} |
| 612 |
ao2_unlock(db); |
| 613 |
|
| 614 |
return res == 0 ? wrapper.row_count : res; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
static int realtime_sqlite3_exec_query(const char *database, const char *sql, callback_t callback, void *arg) |
| 631 |
{ |
| 632 |
struct realtime_sqlite3_db *db; |
| 633 |
int res; |
| 634 |
|
| 635 |
if (!(db = find_database(database))) { |
| 636 |
ast_log(LOG_WARNING, "Could not find database: %s\n", database); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
|
| 640 |
res = realtime_sqlite3_exec_query_with_handle(db, sql, callback, arg); |
| 641 |
ao2_ref(db, -1); |
| 642 |
|
| 643 |
return res; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
static int realtime_sqlite3_exec_update_with_handle(struct realtime_sqlite3_db *db, const char *sql) |
| 660 |
{ |
| 661 |
int res = 0; |
| 662 |
char *errmsg; |
| 663 |
|
| 664 |
ao2_lock(db); |
| 665 |
if (sqlite3_exec(db->handle, sql, NULL, NULL, &errmsg) != SQLITE_OK) { |
| 666 |
ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg); |
| 667 |
sqlite3_free(errmsg); |
| 668 |
res = -1; |
| 669 |
} else { |
| 670 |
res = sqlite3_changes(db->handle); |
| 671 |
} |
| 672 |
ao2_unlock(db); |
| 673 |
|
| 674 |
db_sync(db); |
| 675 |
|
| 676 |
return res; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
static int realtime_sqlite3_exec_update(const char *database, const char *sql) |
| 693 |
{ |
| 694 |
struct realtime_sqlite3_db *db; |
| 695 |
int res; |
| 696 |
|
| 697 |
if (!(db = find_database(database))) { |
| 698 |
ast_log(LOG_WARNING, "Could not find database: %s\n", database); |
| 699 |
return -1; |
| 700 |
} |
| 701 |
|
| 702 |
res = realtime_sqlite3_exec_update_with_handle(db, sql); |
| 703 |
ao2_ref(db, -1); |
| 704 |
|
| 705 |
return res; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
static const char *static_sql = "SELECT category, var_name, var_val FROM \"%q\" WHERE filename = %Q AND commented = 0 ORDER BY cat_metric ASC, var_metric ASC"; |
| 710 |
enum { |
| 711 |
COL_CATEGORY, |
| 712 |
COL_VAR_NAME, |
| 713 |
COL_VAR_VAL, |
| 714 |
COL_COLUMNS, |
| 715 |
}; |
| 716 |
|
| 717 |
static int static_realtime_cb(void *arg, int num_columns, char **values, char **columns) |
| 718 |
{ |
| 719 |
struct cfg_entry_args *args = arg; |
| 720 |
struct ast_variable *var; |
| 721 |
|
| 722 |
if (!strcmp(values[COL_VAR_NAME], "#include")) { |
| 723 |
struct ast_config *cfg; |
| 724 |
char *val; |
| 725 |
|
| 726 |
val = values[COL_VAR_VAL]; |
| 727 |
if (!(cfg = ast_config_internal_load(val, args->cfg, args->flags, "", args->who_asked))) { |
| 728 |
ast_log(LOG_WARNING, "Unable to include %s\n", val); |
| 729 |
return SQLITE_ABORT; |
| 730 |
} else { |
| 731 |
args->cfg = cfg; |
| 732 |
return 0; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
if (!args->cat_name || strcmp(args->cat_name, values[COL_CATEGORY])) { |
| 737 |
args->cat = ast_category_new_dynamic(values[COL_CATEGORY]); |
| 738 |
if (!args->cat) { |
| 739 |
return SQLITE_ABORT; |
| 740 |
} |
| 741 |
|
| 742 |
ast_free(args->cat_name); |
| 743 |
|
| 744 |
if (!(args->cat_name = ast_strdup(values[COL_CATEGORY]))) { |
| 745 |
ast_category_destroy(args->cat); |
| 746 |
return SQLITE_ABORT; |
| 747 |
} |
| 748 |
|
| 749 |
ast_category_append(args->cfg, args->cat); |
| 750 |
} |
| 751 |
|
| 752 |
if (!(var = ast_variable_new(values[COL_VAR_NAME], values[COL_VAR_VAL], ""))) { |
| 753 |
ast_log(LOG_WARNING, "Unable to allocate variable\n"); |
| 754 |
return SQLITE_ABORT; |
| 755 |
} |
| 756 |
|
| 757 |
ast_variable_append(args->cat, var); |
| 758 |
|
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked) |
| 766 |
{ |
| 767 |
char *sql; |
| 768 |
struct cfg_entry_args args; |
| 769 |
|
| 770 |
if (ast_strlen_zero(table)) { |
| 771 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 772 |
return NULL; |
| 773 |
} |
| 774 |
|
| 775 |
if (!(sql = sqlite3_mprintf(static_sql, table, configfile))) { |
| 776 |
ast_log(LOG_WARNING, "Couldn't allocate query\n"); |
| 777 |
return NULL; |
| 778 |
}; |
| 779 |
|
| 780 |
args.cfg = config; |
| 781 |
args.cat = NULL; |
| 782 |
args.cat_name = NULL; |
| 783 |
args.flags = flags; |
| 784 |
args.who_asked = who_asked; |
| 785 |
|
| 786 |
realtime_sqlite3_exec_query(database, sql, static_realtime_cb, &args); |
| 787 |
|
| 788 |
sqlite3_free(sql); |
| 789 |
|
| 790 |
return config; |
| 791 |
} |
| 792 |
|
| 793 |
#define IS_SQL_LIKE_CLAUSE(x) ((x) && ast_ends_with(x, " LIKE")) |
| 794 |
|
| 795 |
|
| 796 |
static int realtime_sqlite3_helper(const char *database, const char *table, const struct ast_variable *fields, int is_multi, void *arg) |
| 797 |
{ |
| 798 |
struct ast_str *sql; |
| 799 |
const struct ast_variable *field; |
| 800 |
int first = 1; |
| 801 |
|
| 802 |
if (ast_strlen_zero(table)) { |
| 803 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 804 |
return -1; |
| 805 |
} |
| 806 |
|
| 807 |
if (!(sql = ast_str_create(128))) { |
| 808 |
return -1; |
| 809 |
} |
| 810 |
|
| 811 |
for (field = fields; field; field = field->next) { |
| 812 |
if (first) { |
| 813 |
ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s %s", sqlite3_escape_table(table), |
| 814 |
sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); |
| 815 |
first = 0; |
| 816 |
} else { |
| 817 |
ast_str_append(&sql, 0, " AND %s %s", sqlite3_escape_column_op(field->name), |
| 818 |
sqlite3_escape_value(field->value)); |
| 819 |
} |
| 820 |
|
| 821 |
if (has_explicit_like_escaping && IS_SQL_LIKE_CLAUSE(field->name)) { |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
ast_str_append(&sql, 0, " ESCAPE '\\'"); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
if (!is_multi) { |
| 832 |
ast_str_append(&sql, 0, "%s", " LIMIT 1"); |
| 833 |
} |
| 834 |
|
| 835 |
if (realtime_sqlite3_exec_query(database, ast_str_buffer(sql), is_multi ? append_row_to_cfg : row_to_varlist, arg) < 0) { |
| 836 |
ast_free(sql); |
| 837 |
return -1; |
| 838 |
} |
| 839 |
|
| 840 |
ast_free(sql); |
| 841 |
|
| 842 |
return 0; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static struct ast_variable *realtime_sqlite3(const char *database, const char *table, const struct ast_variable *fields) |
| 849 |
{ |
| 850 |
struct ast_variable *result_row = NULL; |
| 851 |
|
| 852 |
realtime_sqlite3_helper(database, table, fields, 0, &result_row); |
| 853 |
|
| 854 |
return result_row; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
static struct ast_config *realtime_sqlite3_multi(const char *database, const char *table, const struct ast_variable *fields) |
| 861 |
{ |
| 862 |
struct ast_config *cfg; |
| 863 |
|
| 864 |
if (!(cfg = ast_config_new())) { |
| 865 |
return NULL; |
| 866 |
} |
| 867 |
|
| 868 |
if (realtime_sqlite3_helper(database, table, fields, 1, cfg)) { |
| 869 |
ast_config_destroy(cfg); |
| 870 |
return NULL; |
| 871 |
} |
| 872 |
|
| 873 |
return cfg; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
static int realtime_sqlite3_update(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields) |
| 880 |
{ |
| 881 |
struct ast_str *sql; |
| 882 |
const struct ast_variable *field; |
| 883 |
int first = 1, res; |
| 884 |
|
| 885 |
if (ast_strlen_zero(table)) { |
| 886 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 887 |
return -1; |
| 888 |
} |
| 889 |
|
| 890 |
if (!(sql = ast_str_create(128))) { |
| 891 |
return -1; |
| 892 |
} |
| 893 |
|
| 894 |
for (field = fields; field; field = field->next) { |
| 895 |
if (first) { |
| 896 |
ast_str_set(&sql, 0, "UPDATE %s SET %s = %s", |
| 897 |
sqlite3_escape_table(table), sqlite3_escape_column(field->name), sqlite3_escape_value(field->value)); |
| 898 |
first = 0; |
| 899 |
} else { |
| 900 |
ast_str_append(&sql, 0, ", %s = %s", sqlite3_escape_column(field->name), sqlite3_escape_value(field->value)); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
ast_str_append(&sql, 0, " WHERE %s %s", sqlite3_escape_column_op(keyfield), sqlite3_escape_value(entity)); |
| 905 |
|
| 906 |
res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); |
| 907 |
ast_free(sql); |
| 908 |
|
| 909 |
return res; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
static int realtime_sqlite3_update2(const char *database, const char *table, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields) |
| 916 |
{ |
| 917 |
struct ast_str *sql; |
| 918 |
struct ast_str *where_clause; |
| 919 |
const struct ast_variable *field; |
| 920 |
int first = 1, res; |
| 921 |
|
| 922 |
if (ast_strlen_zero(table)) { |
| 923 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 924 |
return -1; |
| 925 |
} |
| 926 |
|
| 927 |
if (!(sql = ast_str_create(128))) { |
| 928 |
return -1; |
| 929 |
} |
| 930 |
|
| 931 |
if (!(where_clause = ast_str_create(128))) { |
| 932 |
ast_free(sql); |
| 933 |
return -1; |
| 934 |
} |
| 935 |
|
| 936 |
for (field = lookup_fields; field; field = field->next) { |
| 937 |
if (first) { |
| 938 |
ast_str_set(&where_clause, 0, " WHERE %s %s", sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); |
| 939 |
first = 0; |
| 940 |
} else { |
| 941 |
ast_str_append(&where_clause, 0, " AND %s %s", sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
first = 1; |
| 946 |
for (field = update_fields; field; field = field->next) { |
| 947 |
if (first) { |
| 948 |
ast_str_set(&sql, 0, "UPDATE %s SET %s = %s", sqlite3_escape_table(table), sqlite3_escape_column(field->name), sqlite3_escape_value(field->value)); |
| 949 |
first = 0; |
| 950 |
} else { |
| 951 |
ast_str_append(&sql, 0, ", %s = %s", sqlite3_escape_column(field->name), sqlite3_escape_value(field->value)); |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
ast_str_append(&sql, 0, "%s", ast_str_buffer(where_clause)); |
| 956 |
|
| 957 |
res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); |
| 958 |
|
| 959 |
ast_free(sql); |
| 960 |
ast_free(where_clause); |
| 961 |
|
| 962 |
return res; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
static int realtime_sqlite3_store(const char *database, const char *table, const struct ast_variable *fields) |
| 969 |
{ |
| 970 |
struct ast_str *sql, *values; |
| 971 |
const struct ast_variable *field; |
| 972 |
int first = 1, res; |
| 973 |
|
| 974 |
if (ast_strlen_zero(table)) { |
| 975 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 976 |
return -1; |
| 977 |
} |
| 978 |
|
| 979 |
if (!(sql = ast_str_create(128))) { |
| 980 |
return -1; |
| 981 |
} |
| 982 |
|
| 983 |
if (!(values = ast_str_create(128))) { |
| 984 |
ast_free(sql); |
| 985 |
return -1; |
| 986 |
} |
| 987 |
|
| 988 |
for (field = fields; field; field = field->next) { |
| 989 |
if (first) { |
| 990 |
ast_str_set(&sql, 0, "INSERT INTO %s (%s", sqlite3_escape_table(table), sqlite3_escape_column(field->name)); |
| 991 |
ast_str_set(&values, 0, ") VALUES (%s", sqlite3_escape_value(field->value)); |
| 992 |
first = 0; |
| 993 |
} else { |
| 994 |
ast_str_append(&sql, 0, ", %s", sqlite3_escape_column(field->name)); |
| 995 |
ast_str_append(&values, 0, ", %s", sqlite3_escape_value(field->value)); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
ast_str_append(&sql, 0, "%s)", ast_str_buffer(values)); |
| 1000 |
|
| 1001 |
res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); |
| 1002 |
|
| 1003 |
ast_free(sql); |
| 1004 |
ast_free(values); |
| 1005 |
|
| 1006 |
return res; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
static int realtime_sqlite3_destroy(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields) |
| 1013 |
{ |
| 1014 |
struct ast_str *sql; |
| 1015 |
const struct ast_variable *field; |
| 1016 |
int first = 1, res; |
| 1017 |
|
| 1018 |
if (ast_strlen_zero(table)) { |
| 1019 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 1020 |
return -1; |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (!(sql = ast_str_create(128))) { |
| 1024 |
return -1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
for (field = fields; field; field = field->next) { |
| 1028 |
if (first) { |
| 1029 |
ast_str_set(&sql, 0, "DELETE FROM %s WHERE %s %s", sqlite3_escape_table(table), |
| 1030 |
sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); |
| 1031 |
first = 0; |
| 1032 |
} else { |
| 1033 |
ast_str_append(&sql, 0, " AND %s %s", sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); |
| 1038 |
|
| 1039 |
ast_free(sql); |
| 1040 |
|
| 1041 |
return res; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
static const char *get_sqlite_column_type(int type) |
| 1051 |
{ |
| 1052 |
switch(type) { |
| 1053 |
case RQ_INTEGER1 : |
| 1054 |
case RQ_UINTEGER1 : |
| 1055 |
case RQ_INTEGER2 : |
| 1056 |
case RQ_UINTEGER2 : |
| 1057 |
case RQ_INTEGER3 : |
| 1058 |
case RQ_UINTEGER3 : |
| 1059 |
case RQ_INTEGER4 : |
| 1060 |
case RQ_UINTEGER4 : |
| 1061 |
case RQ_INTEGER8 : |
| 1062 |
return "INTEGER"; |
| 1063 |
case RQ_UINTEGER8 : |
| 1064 |
case RQ_CHAR : |
| 1065 |
case RQ_DATE : |
| 1066 |
case RQ_DATETIME : |
| 1067 |
return "TEXT"; |
| 1068 |
case RQ_FLOAT : |
| 1069 |
return "REAL"; |
| 1070 |
default : |
| 1071 |
return "TEXT"; |
| 1072 |
} |
| 1073 |
|
| 1074 |
return "TEXT"; |
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
static int handle_missing_table(struct realtime_sqlite3_db *db, const char *table, va_list ap) |
| 1080 |
{ |
| 1081 |
const char *column; |
| 1082 |
int type, first = 1, res; |
| 1083 |
size_t sz; |
| 1084 |
struct ast_str *sql; |
| 1085 |
|
| 1086 |
if (!(sql = ast_str_create(128))) { |
| 1087 |
return -1; |
| 1088 |
} |
| 1089 |
|
| 1090 |
while ((column = va_arg(ap, typeof(column)))) { |
| 1091 |
type = va_arg(ap, typeof(type)); |
| 1092 |
sz = va_arg(ap, typeof(sz)); |
| 1093 |
if (first) { |
| 1094 |
ast_str_set(&sql, 0, "CREATE TABLE IF NOT EXISTS %s (%s %s", sqlite3_escape_table(table), |
| 1095 |
sqlite3_escape_column(column), get_sqlite_column_type(type)); |
| 1096 |
first = 0; |
| 1097 |
} else { |
| 1098 |
ast_str_append(&sql, 0, ", %s %s", sqlite3_escape_column(column), get_sqlite_column_type(type)); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
ast_str_append(&sql, 0, ")"); |
| 1103 |
|
| 1104 |
res = realtime_sqlite3_exec_update_with_handle(db, ast_str_buffer(sql)) < 0 ? -1 : 0; |
| 1105 |
ast_free(sql); |
| 1106 |
|
| 1107 |
return res; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
static int handle_missing_column(struct realtime_sqlite3_db *db, const char *table, const char *column, int type, size_t sz) |
| 1113 |
{ |
| 1114 |
char *sql; |
| 1115 |
const char *sqltype = get_sqlite_column_type(type); |
| 1116 |
int res; |
| 1117 |
|
| 1118 |
if (db->requirements == REALTIME_SQLITE3_REQ_WARN) { |
| 1119 |
ast_log(LOG_WARNING, "Missing column '%s' of type '%s' in %s.%s\n", column, sqltype, db->name, table); |
| 1120 |
return -1; |
| 1121 |
} else if (db->requirements == REALTIME_SQLITE3_REQ_CHAR) { |
| 1122 |
sqltype = "TEXT"; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (!(sql = sqlite3_mprintf("ALTER TABLE \"%q\" ADD COLUMN \"%q\" %s", table, column, sqltype))) { |
| 1126 |
return -1; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (!(res = (realtime_sqlite3_exec_update_with_handle(db, sql) < 0 ? -1 : 0))) { |
| 1130 |
ast_log(LOG_NOTICE, "Creating column '%s' type %s for table %s\n", column, sqltype, table); |
| 1131 |
} |
| 1132 |
|
| 1133 |
sqlite3_free(sql); |
| 1134 |
|
| 1135 |
return res; |
| 1136 |
} |
| 1137 |
|
| 1138 |
static int str_hash_fn(const void *obj, const int flags) |
| 1139 |
{ |
| 1140 |
return ast_str_hash((const char *) obj); |
| 1141 |
} |
| 1142 |
|
| 1143 |
static int str_cmp_fn(void *obj, void *arg, int flags) { |
| 1144 |
return !strcasecmp((const char *) obj, (const char *) arg); |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
static int add_column_name(void *arg, int num_columns, char **values, char **columns) |
| 1150 |
{ |
| 1151 |
char *column; |
| 1152 |
struct ao2_container *cnames = arg; |
| 1153 |
|
| 1154 |
|
| 1155 |
if (!(column = ao2_alloc(strlen(values[1]) + 1, NULL))) { |
| 1156 |
return -1; |
| 1157 |
} |
| 1158 |
|
| 1159 |
strcpy(column, values[1]); |
| 1160 |
|
| 1161 |
ao2_link(cnames, column); |
| 1162 |
ao2_ref(column, -1); |
| 1163 |
|
| 1164 |
return 0; |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
static int realtime_sqlite3_require(const char *database, const char *table, va_list ap) |
| 1172 |
{ |
| 1173 |
const char *column; |
| 1174 |
char *sql; |
| 1175 |
int type; |
| 1176 |
int res; |
| 1177 |
size_t sz; |
| 1178 |
struct ao2_container *columns; |
| 1179 |
struct realtime_sqlite3_db *db; |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
if (ast_strlen_zero(table)) { |
| 1188 |
ast_log(LOG_WARNING, "Must have a table to query!\n"); |
| 1189 |
return -1; |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (!(db = find_database(database))) { |
| 1193 |
return -1; |
| 1194 |
} |
| 1195 |
|
| 1196 |
columns = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 31, |
| 1197 |
str_hash_fn, NULL, str_cmp_fn); |
| 1198 |
if (!columns) { |
| 1199 |
unref_db(&db); |
| 1200 |
return -1; |
| 1201 |
} |
| 1202 |
|
| 1203 |
if (!(sql = sqlite3_mprintf("PRAGMA table_info(\"%q\")", table))) { |
| 1204 |
unref_db(&db); |
| 1205 |
ao2_ref(columns, -1); |
| 1206 |
return -1; |
| 1207 |
} |
| 1208 |
|
| 1209 |
if ((res = realtime_sqlite3_exec_query_with_handle(db, sql, add_column_name, columns)) < 0) { |
| 1210 |
unref_db(&db); |
| 1211 |
ao2_ref(columns, -1); |
| 1212 |
sqlite3_free(sql); |
| 1213 |
return -1; |
| 1214 |
} else if (res == 0) { |
| 1215 |
|
| 1216 |
sqlite3_free(sql); |
| 1217 |
res = handle_missing_table(db, table, ap); |
| 1218 |
ao2_ref(columns, -1); |
| 1219 |
unref_db(&db); |
| 1220 |
return res; |
| 1221 |
} |
| 1222 |
|
| 1223 |
sqlite3_free(sql); |
| 1224 |
|
| 1225 |
while ((column = va_arg(ap, typeof(column)))) { |
| 1226 |
char *found; |
| 1227 |
type = va_arg(ap, typeof(type)); |
| 1228 |
sz = va_arg(ap, typeof(sz)); |
| 1229 |
if (!(found = ao2_find(columns, column, OBJ_POINTER | OBJ_UNLINK))) { |
| 1230 |
if (handle_missing_column(db, table, column, type, sz)) { |
| 1231 |
unref_db(&db); |
| 1232 |
ao2_ref(columns, -1); |
| 1233 |
return -1; |
| 1234 |
} |
| 1235 |
} else { |
| 1236 |
ao2_ref(found, -1); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
ao2_ref(columns, -1); |
| 1241 |
unref_db(&db); |
| 1242 |
|
| 1243 |
return 0; |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
static int realtime_sqlite3_unload(const char *database, const char *table) |
| 1252 |
{ |
| 1253 |
|
| 1254 |
return -1; |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
static int parse_config(int reload) |
| 1260 |
{ |
| 1261 |
struct ast_config *config; |
| 1262 |
struct ast_flags config_flags = { CONFIG_FLAG_NOREALTIME | (reload ? CONFIG_FLAG_FILEUNCHANGED : 0) }; |
| 1263 |
static const char *config_filename = "res_config_sqlite3.conf"; |
| 1264 |
|
| 1265 |
config = ast_config_load(config_filename, config_flags); |
| 1266 |
|
| 1267 |
if (config == CONFIG_STATUS_FILEUNCHANGED) { |
| 1268 |
ast_debug(1, "%s was unchanged, skipping parsing\n", config_filename); |
| 1269 |
return 0; |
| 1270 |
} |
| 1271 |
|
| 1272 |
ast_mutex_lock(&config_lock); |
| 1273 |
|
| 1274 |
if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) { |
| 1275 |
ast_log(LOG_ERROR, "%s config file '%s'\n", |
| 1276 |
config == CONFIG_STATUS_FILEMISSING ? "Missing" : "Invalid", config_filename); |
| 1277 |
ast_mutex_unlock(&config_lock); |
| 1278 |
return 0; |
| 1279 |
} else { |
| 1280 |
const char *cat; |
| 1281 |
struct realtime_sqlite3_db *db; |
| 1282 |
|
| 1283 |
mark_all_databases_dirty(); |
| 1284 |
for (cat = ast_category_browse(config, NULL); cat; cat = ast_category_browse(config, cat)) { |
| 1285 |
if (!strcasecmp(cat, "general")) { |
| 1286 |
continue; |
| 1287 |
} |
| 1288 |
if (!(db = find_database(cat))) { |
| 1289 |
if (!(db = new_realtime_sqlite3_db(config, cat))) { |
| 1290 |
ast_log(LOG_WARNING, "Could not allocate new db for '%s' - skipping.\n", cat); |
| 1291 |
continue; |
| 1292 |
} |
| 1293 |
if (db_open(db)) { |
| 1294 |
unref_db(&db); |
| 1295 |
continue; |
| 1296 |
} |
| 1297 |
db_start_batch(db); |
| 1298 |
ao2_link(databases, db); |
| 1299 |
unref_db(&db); |
| 1300 |
} else { |
| 1301 |
if (update_realtime_sqlite3_db(db, config, cat)) { |
| 1302 |
unref_db(&db); |
| 1303 |
continue; |
| 1304 |
} |
| 1305 |
unref_db(&db); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
unlink_dirty_databases(); |
| 1309 |
} |
| 1310 |
|
| 1311 |
ast_mutex_unlock(&config_lock); |
| 1312 |
|
| 1313 |
ast_config_destroy(config); |
| 1314 |
|
| 1315 |
return 0; |
| 1316 |
} |
| 1317 |
|
| 1318 |
static int reload(void) |
| 1319 |
{ |
| 1320 |
parse_config(1); |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
|
| 1324 |
static int unload_module(void) |
| 1325 |
{ |
| 1326 |
ast_mutex_lock(&config_lock); |
| 1327 |
ao2_callback(databases, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, stop_batch_cb, NULL); |
| 1328 |
ao2_ref(databases, -1); |
| 1329 |
databases = NULL; |
| 1330 |
ast_config_engine_deregister(&sqlite3_config_engine); |
| 1331 |
ast_mutex_unlock(&config_lock); |
| 1332 |
|
| 1333 |
return 0; |
| 1334 |
} |
| 1335 |
|
| 1336 |
static void discover_sqlite3_caps(void) |
| 1337 |
{ |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
#if defined(SQLITE_VERSION_NUMBER) |
| 1350 |
has_explicit_like_escaping = 1; |
| 1351 |
#else |
| 1352 |
has_explicit_like_escaping = 0; |
| 1353 |
#endif |
| 1354 |
|
| 1355 |
ast_debug(3, "SQLite3 has 'LIKE ... ESCAPE ...' support? %s\n", |
| 1356 |
has_explicit_like_escaping ? "Yes" : "No"); |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
static int load_module(void) |
| 1370 |
{ |
| 1371 |
discover_sqlite3_caps(); |
| 1372 |
|
| 1373 |
databases = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, DB_BUCKETS, |
| 1374 |
db_hash_fn, NULL, db_cmp_fn); |
| 1375 |
if (!databases) { |
| 1376 |
return AST_MODULE_LOAD_DECLINE; |
| 1377 |
} |
| 1378 |
|
| 1379 |
if (parse_config(0)) { |
| 1380 |
ao2_ref(databases, -1); |
| 1381 |
return AST_MODULE_LOAD_DECLINE; |
| 1382 |
} |
| 1383 |
|
| 1384 |
if (!(ast_config_engine_register(&sqlite3_config_engine))) { |
| 1385 |
ast_log(LOG_ERROR, "The config API must have changed, this shouldn't happen.\n"); |
| 1386 |
ao2_ref(databases, -1); |
| 1387 |
return AST_MODULE_LOAD_DECLINE; |
| 1388 |
} |
| 1389 |
|
| 1390 |
return AST_MODULE_LOAD_SUCCESS; |
| 1391 |
} |
| 1392 |
|
| 1393 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite 3 realtime config engine", |
| 1394 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1395 |
.load = load_module, |
| 1396 |
.unload = unload_module, |
| 1397 |
.reload = reload, |
| 1398 |
.load_pri = AST_MODPRI_REALTIME_DRIVER, |
| 1399 |
.requires = "extconfig", |
| 1400 |
); |