* Execute a raw SQL query. * \param _h handle for the database * \param _s raw query string * \param _r result set for storage * \return zero on success, negative value on failure */
| 337 | * \return zero on success, negative value on failure |
| 338 | */ |
| 339 | int db_sqlite_raw_query(const db_con_t* _h, const str* _s, db_res_t** _r) |
| 340 | { |
| 341 | static char sql_str[SQL_BUF_LEN]; |
| 342 | int ret=-1, i=0; |
| 343 | char* errmsg; |
| 344 | str select_str={"select", 6}; |
| 345 | |
| 346 | str _scpy; |
| 347 | |
| 348 | CON_RESET_CURR_PS(_h); |
| 349 | while (i < _s->len && !isalpha(_s->s[i])) i++; |
| 350 | |
| 351 | /* if any blank spaces or anything else before the actual query */ |
| 352 | if (i) { |
| 353 | _scpy.s = _s->s+i; |
| 354 | _scpy.len = _s->len-i; |
| 355 | } else { |
| 356 | _scpy = *_s; |
| 357 | } |
| 358 | |
| 359 | if (_scpy.len >= select_str.len && |
| 360 | str_strncasecmp(&_scpy, &select_str, select_str.len)) { |
| 361 | /* not a select statement; can execute the query and exit*/ |
| 362 | if (_s->len + 1 > SQL_BUF_LEN) { |
| 363 | LM_ERR("query too big! try reducing the size of your query!" |
| 364 | "Current max size [%d]!\n", SQL_BUF_LEN); |
| 365 | return -1; |
| 366 | } |
| 367 | memcpy(sql_str, _s->s, _s->len); |
| 368 | sql_str[_s->len] = '\0'; |
| 369 | if (sqlite3_exec(CON_CONNECTION(_h), |
| 370 | sql_str, NULL, NULL, &errmsg)) { |
| 371 | LM_ERR("query failed: %s\n", errmsg); |
| 372 | sqlite3_free(errmsg); |
| 373 | return -2; |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | CON_RAW_QUERY(_h) = 1; |
| 380 | |
| 381 | if (db_copy_rest_of_count(&_scpy, &count_str)) { |
| 382 | LM_ERR("failed to build count str!\n"); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | ret=sqlite3_prepare_v2(CON_CONNECTION(_h), |
| 387 | _s->s, _s->len, &CON_SQLITE_PS(_h), NULL); |
| 388 | if (ret!=SQLITE_OK) |
| 389 | LM_ERR("failed to prepare: (%s)\n", |
| 390 | sqlite3_errmsg(CON_CONNECTION(_h))); |
| 391 | |
| 392 | if (_r) { |
| 393 | ret = db_sqlite_store_result(_h, _r, NULL, 0); |
| 394 | } else { |
| 395 | /* need to fetch now the total number of rows in query |
| 396 | * because later won't have the query string */ |
nothing calls this directly
no test coverage detected