FreeSWITCH API Documentation  1.7.0
Data Structures | Macros | Functions | Variables
switch_core_sqldb.c File Reference
#include <switch.h>
#include "private/switch_core_pvt.h"
+ Include dependency graph for switch_core_sqldb.c:

Go to the source code of this file.

Data Structures

struct  switch_cache_db_handle
 
struct  helper
 
struct  switch_sql_queue_manager
 
struct  db_job
 

Macros

#define SWITCH_SQL_QUEUE_LEN   100000
 
#define SWITCH_SQL_QUEUE_PAUSE_LEN   90000
 
#define SQL_CACHE_TIMEOUT   30
 
#define SQL_REG_TIMEOUT   15
 
#define EXEC_NOW
 
#define MAX_SQL   5
 
#define new_sql()   switch_assert(sql_idx+1 < MAX_SQL); if (exists) sql[sql_idx++]
 
#define new_sql_a()   switch_assert(sql_idx+1 < MAX_SQL); sql[sql_idx++]
 

Functions

static void switch_core_sqldb_start_thread (void)
 
static void switch_core_sqldb_stop_thread (void)
 
static switch_cache_db_handle_tcreate_handle (switch_cache_db_handle_type_t type)
 
static void add_handle (switch_cache_db_handle_t *dbh, const char *db_str, const char *db_callsite_str, const char *thread_str)
 
static void del_handle (switch_cache_db_handle_t *dbh)
 
static switch_cache_db_handle_tget_handle (const char *db_str, const char *user_str, const char *thread_str)
 
switch_status_t _switch_core_db_handle (switch_cache_db_handle_t **dbh, const char *file, const char *func, int line)
 Open the default system database. More...
 
static void sql_close (time_t prune)
 
switch_cache_db_handle_type_t switch_cache_db_get_type (switch_cache_db_handle_t *dbh)
 
void switch_cache_db_flush_handles (void)
 
void switch_cache_db_release_db_handle (switch_cache_db_handle_t **dbh)
 Returns the handle to the pool, handle is NOT available to other threads until the allocating thread actually terminates. More...
 
void switch_cache_db_dismiss_db_handle (switch_cache_db_handle_t **dbh)
 Returns the handle to the pool, immediately available for other threads to use. More...
 
switch_status_t _switch_cache_db_get_db_handle_dsn (switch_cache_db_handle_t **dbh, const char *dsn, const char *file, const char *func, int line)
 
switch_status_t _switch_cache_db_get_db_handle (switch_cache_db_handle_t **dbh, switch_cache_db_handle_type_t type, switch_cache_db_connection_options_t *connection_options, const char *file, const char *func, int line)
 Gets a new cached handle from the pool, potentially creating a new connection. The connection is bound to the thread until it (the thread) terminates unless you dismiss rather than release. More...
 
static switch_status_t switch_cache_db_execute_sql_real (switch_cache_db_handle_t *dbh, const char *sql, char **err)
 
static switch_status_t switch_cache_db_execute_sql_chunked (switch_cache_db_handle_t *dbh, char *sql, uint32_t chunk_size, char **err)
 
switch_status_t switch_cache_db_execute_sql (switch_cache_db_handle_t *dbh, char *sql, char **err)
 Executes the sql. More...
 
int switch_cache_db_affected_rows (switch_cache_db_handle_t *dbh)
 Get the affected rows of the last performed query. More...
 
int switch_cache_db_load_extension (switch_cache_db_handle_t *dbh, const char *extension)
 load an external extension to db More...
 
char * switch_cache_db_execute_sql2str (switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err)
 Executes the sql and returns the result as a string. More...
 
switch_status_t switch_cache_db_persistant_execute (switch_cache_db_handle_t *dbh, const char *sql, uint32_t retries)
 
switch_status_t switch_cache_db_persistant_execute_trans_full (switch_cache_db_handle_t *dbh, char *sql, uint32_t retries, const char *pre_trans_execute, const char *post_trans_execute, const char *inner_pre_trans_execute, const char *inner_post_trans_execute)
 
static int helper_callback (void *pArg, int argc, char **argv, char **columnNames)
 
switch_status_t switch_cache_db_execute_sql_event_callback (switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_event_callback_func_t callback, void *pdata, char **err)
 
switch_status_t switch_cache_db_execute_sql_event_callback_err (switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_event_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata, char **err)
 
switch_status_t switch_cache_db_execute_sql_callback (switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
 Executes the sql and uses callback for row-by-row processing. More...
 
switch_status_t switch_cache_db_execute_sql_callback_err (switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata, char **err)
 Executes the sql and uses callback for row-by-row processing. More...
 
switch_status_t switch_cache_db_create_schema (switch_cache_db_handle_t *dbh, char *sql, char **err)
 Executes the create schema sql. More...
 
switch_bool_t switch_cache_db_test_reactive (switch_cache_db_handle_t *dbh, const char *test_sql, const char *drop_sql, const char *reactive_sql)
 Performs test_sql and if it fails performs drop_sql and reactive_sql. More...
 
static void *SWITCH_THREAD_FUNC switch_core_sql_db_thread (switch_thread_t *thread, void *obj)
 
static void *SWITCH_THREAD_FUNC switch_user_sql_thread (switch_thread_t *thread, void *obj)
 
static int qm_wake (switch_sql_queue_manager_t *qm)
 
static uint32_t qm_ttl (switch_sql_queue_manager_t *qm)
 
static void *SWITCH_THREAD_FUNC sql_in_thread (switch_thread_t *thread, void *obj)
 
static switch_thread_data_tnew_job (switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, switch_core_db_event_callback_func_t event_callback, switch_core_db_err_callback_func_t event_err_callback, void *pdata)
 
void switch_sql_queue_manager_execute_sql_callback (switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, void *pdata)
 
void switch_sql_queue_manager_execute_sql_callback_err (switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata)
 
void switch_sql_queue_manager_execute_sql_event_callback (switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_event_callback_func_t callback, void *pdata)
 
void switch_sql_queue_manager_execute_sql_event_callback_err (switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_event_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata)
 
static void do_flush (switch_sql_queue_manager_t *qm, int i, switch_cache_db_handle_t *dbh)
 
void switch_sql_queue_manager_resume (switch_sql_queue_manager_t *qm)
 
void switch_sql_queue_manager_pause (switch_sql_queue_manager_t *qm, switch_bool_t flush)
 
int switch_sql_queue_manager_size (switch_sql_queue_manager_t *qm, uint32_t index)
 
switch_status_t switch_sql_queue_manager_stop (switch_sql_queue_manager_t *qm)
 
switch_status_t switch_sql_queue_manager_start (switch_sql_queue_manager_t *qm)
 
switch_status_t switch_sql_queue_manager_destroy (switch_sql_queue_manager_t **qmp)
 
switch_status_t switch_sql_queue_manager_push (switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
 
switch_status_t switch_sql_queue_manager_push_confirm (switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
 
switch_status_t switch_sql_queue_manager_init_name (const char *name, switch_sql_queue_manager_t **qmp, uint32_t numq, const char *dsn, uint32_t max_trans, const char *pre_trans_execute, const char *post_trans_execute, const char *inner_pre_trans_execute, const char *inner_post_trans_execute)
 
static uint32_t do_trans (switch_sql_queue_manager_t *qm)
 
static char * parse_presence_data_cols (switch_event_t *event)
 
static void core_event_handler (switch_event_t *event)
 
void switch_core_recovery_flush (const char *technology, const char *profile_name)
 
static int recover_callback (void *pArg, int argc, char **argv, char **columnNames)
 
int switch_core_recovery_recover (const char *technology, const char *profile_name)
 
switch_cache_db_handle_type_t switch_core_dbtype (void)
 
void switch_core_sql_exec (const char *sql)
 
void switch_core_recovery_untrack (switch_core_session_t *session, switch_bool_t force)
 
void switch_core_recovery_track (switch_core_session_t *session)
 
switch_status_t switch_core_add_registration (const char *user, const char *realm, const char *token, const char *url, uint32_t expires, const char *network_ip, const char *network_port, const char *network_proto, const char *metadata)
 Add user registration. More...
 
switch_status_t switch_core_del_registration (const char *user, const char *realm, const char *token)
 Delete user registration. More...
 
switch_status_t switch_core_expire_registration (int force)
 Expire user registrations. More...
 
switch_status_t switch_core_sqldb_start (switch_memory_pool_t *pool, switch_bool_t manage)
 
void switch_core_sqldb_pause (void)
 
void switch_core_sqldb_resume (void)
 
void switch_core_sqldb_stop (void)
 
void switch_cache_db_status (switch_stream_handle_t *stream)
 Provides some feedback as to the status of the db connection pool. More...
 
char * switch_sql_concat (void)
 

Variables

struct {
   switch_memory_pool_t *   memory_pool
 
   switch_thread_t *   db_thread
 
   int   db_thread_running
 
   switch_bool_t   manage
 
   switch_mutex_t *   io_mutex
 
   switch_mutex_t *   dbh_mutex
 
   switch_mutex_t *   ctl_mutex
 
   switch_cache_db_handle_t *   handle_pool
 
   uint32_t   total_handles
 
   uint32_t   total_used_handles
 
   switch_cache_db_handle_t *   dbh
 
   switch_sql_queue_manager_t *   qm
 
   int   paused
 
sql_manager
 
static char create_complete_sql []
 
static char create_alias_sql []
 
static char create_channels_sql []
 
static char create_calls_sql []
 
static char create_interfaces_sql []
 
static char create_tasks_sql []
 
static char create_nat_sql []
 
static char create_registrations_sql []
 
static char detailed_calls_sql []
 
static char recovery_sql []
 
static char basic_calls_sql []
 

Macro Definition Documentation

#define EXEC_NOW
#define MAX_SQL   5

Definition at line 2203 of file switch_core_sqldb.c.

Referenced by core_event_handler().

#define new_sql ( )    switch_assert(sql_idx+1 < MAX_SQL); if (exists) sql[sql_idx++]

Definition at line 2204 of file switch_core_sqldb.c.

Referenced by core_event_handler().

#define new_sql_a ( )    switch_assert(sql_idx+1 < MAX_SQL); sql[sql_idx++]

Definition at line 2205 of file switch_core_sqldb.c.

Referenced by core_event_handler().

#define SQL_CACHE_TIMEOUT   30
#define SQL_REG_TIMEOUT   15

Definition at line 209 of file switch_core_sqldb.c.

Referenced by switch_core_sql_db_thread().

#define SWITCH_SQL_QUEUE_LEN   100000

Definition at line 39 of file switch_core_sqldb.c.

Referenced by switch_sql_queue_manager_init_name().

#define SWITCH_SQL_QUEUE_PAUSE_LEN   90000

Definition at line 40 of file switch_core_sqldb.c.

Function Documentation

switch_status_t _switch_cache_db_get_db_handle ( switch_cache_db_handle_t **  dbh,
switch_cache_db_handle_type_t  type,
switch_cache_db_connection_options_t connection_options,
const char *  file,
const char *  func,
int  line 
)

Gets a new cached handle from the pool, potentially creating a new connection. The connection is bound to the thread until it (the thread) terminates unless you dismiss rather than release.

Parameters
[out]dbhThe handle
[in]type- ODBC or SQLLITE
[in]connection_options(userid, password, etc)

Definition at line 384 of file switch_core_sqldb.c.

References add_handle(), CACHE_DB_LEN, switch_cache_db_native_handle_t::core_db_dbh, create_handle(), switch_runtime::db_handle_timeout, get_handle(), switch_cache_db_handle::last_used, switch_runtime::max_db_handles, switch_cache_db_handle::name, switch_cache_db_handle::native_handle, switch_cache_db_native_handle_t::odbc_dbh, switch_cache_db_native_handle_t::pgsql_dbh, runtime, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, sql_manager, switch_cache_db_type_name(), SWITCH_CHANNEL_ID_LOG, SWITCH_CHANNEL_LOG, switch_core_db_open_file(), switch_epoch_time_now(), SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG10, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_odbc_available(), switch_odbc_handle_connect(), switch_odbc_handle_destroy(), switch_odbc_handle_new(), SWITCH_ODBC_SUCCESS, switch_pgsql_available(), switch_pgsql_handle_connect(), switch_pgsql_handle_destroy(), switch_pgsql_handle_new(), SWITCH_PGSQL_SUCCESS, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_thread_self(), switch_yield, and switch_cache_db_handle::type.

Referenced by _switch_cache_db_get_db_handle_dsn().

388 {
390  char thread_str[CACHE_DB_LEN] = "";
391  char db_str[CACHE_DB_LEN] = "";
392  char db_callsite_str[CACHE_DB_LEN] = "";
393  switch_cache_db_handle_t *new_dbh = NULL;
394  int waiting = 0;
395  uint32_t yield_len = 100000, total_yield = 0;
396 
397  const char *db_name = NULL;
398  const char *odbc_user = NULL;
399  const char *odbc_pass = NULL;
400  const char *db_type = NULL;
401 
402  while(runtime.max_db_handles && sql_manager.total_handles >= runtime.max_db_handles && sql_manager.total_used_handles >= sql_manager.total_handles) {
403  if (!waiting++) {
404  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_WARNING, "Max handles %u exceeded, blocking....\n",
406  }
407 
408  switch_yield(yield_len);
409  total_yield += yield_len;
410 
411  if (runtime.db_handle_timeout && total_yield > runtime.db_handle_timeout) {
412  switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, "Error connecting\n");
413  *dbh = NULL;
414  return SWITCH_STATUS_FALSE;
415  }
416  }
417 
418  switch (type) {
419  case SCDB_TYPE_PGSQL:
420  {
421  db_name = connection_options->pgsql_options.dsn;
422  odbc_user = NULL;
423  odbc_pass = NULL;
424  db_type = "pgsql";
425  }
426  case SCDB_TYPE_ODBC:
427  {
428  db_name = connection_options->odbc_options.dsn;
429  odbc_user = connection_options->odbc_options.user;
430  odbc_pass = connection_options->odbc_options.pass;
431  db_type = "odbc";
432  }
433  break;
434  case SCDB_TYPE_CORE_DB:
435  {
436  db_name = connection_options->core_db_options.db_path;
437  odbc_user = NULL;
438  odbc_pass = NULL;
439  db_type = "core_db";
440  }
441  break;
442  }
443 
444  if (!db_name) {
445  return SWITCH_STATUS_FALSE;
446  }
447 
448  if (odbc_user || odbc_pass) {
449  snprintf(db_str, sizeof(db_str) - 1, "db=\"%s\";type=\"%s\"user=\"%s\";pass=\"%s\"", db_name, db_type, odbc_user, odbc_pass);
450  } else {
451  snprintf(db_str, sizeof(db_str) - 1, "db=\"%s\",type=\"%s\"", db_name, db_type);
452  }
453  snprintf(db_callsite_str, sizeof(db_callsite_str) - 1, "%s:%d", file, line);
454  snprintf(thread_str, sizeof(thread_str) - 1, "thread=\"%lu\"", (unsigned long) (intptr_t) self);
455 
456  if ((new_dbh = get_handle(db_str, db_callsite_str, thread_str))) {
458  "Reuse Unused Cached DB handle %s [%s]\n", new_dbh->name, switch_cache_db_type_name(new_dbh->type));
459  } else {
460  switch_core_db_t *db = NULL;
461  switch_odbc_handle_t *odbc_dbh = NULL;
462  switch_pgsql_handle_t *pgsql_dbh = NULL;
463 
464  switch (type) {
465  case SCDB_TYPE_PGSQL:
466  {
467  if (!switch_pgsql_available()) {
468  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure! PGSQL NOT AVAILABLE! Can't connect to DSN %s\n", connection_options->pgsql_options.dsn);
469  goto end;
470  }
471 
472  if ((pgsql_dbh = switch_pgsql_handle_new(connection_options->pgsql_options.dsn))) {
474  switch_pgsql_handle_destroy(&pgsql_dbh);
475  }
476  }
477  }
478  break;
479  case SCDB_TYPE_ODBC:
480  {
481 
482  if (!switch_odbc_available()) {
483  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure! ODBC NOT AVAILABLE! Can't connect to DSN %s\n", connection_options->odbc_options.dsn);
484  goto end;
485  }
486 
487  if ((odbc_dbh = switch_odbc_handle_new(connection_options->odbc_options.dsn,
488  connection_options->odbc_options.user, connection_options->odbc_options.pass))) {
490  switch_odbc_handle_destroy(&odbc_dbh);
491  }
492  }
493 
494 
495  }
496  break;
497  case SCDB_TYPE_CORE_DB:
498  {
499  db = switch_core_db_open_file(connection_options->core_db_options.db_path);
500  }
501  break;
502 
503  default:
504  goto end;
505  }
506 
507  if (!db && !odbc_dbh && !pgsql_dbh) {
508  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure to connect to %s %s!\n", switch_cache_db_type_name(type), db_name);
509  goto end;
510  }
511 
512  new_dbh = create_handle(type);
513 
515  "Create Cached DB handle %s [%s] %s:%d\n", new_dbh->name, switch_cache_db_type_name(type), file, line);
516 
517  if (db) {
518  new_dbh->native_handle.core_db_dbh = db;
519  } else if (odbc_dbh) {
520  new_dbh->native_handle.odbc_dbh = odbc_dbh;
521  } else {
522  new_dbh->native_handle.pgsql_dbh = pgsql_dbh;
523  }
524 
525  add_handle(new_dbh, db_str, db_callsite_str, thread_str);
526  }
527 
528  end:
529 
530  if (new_dbh) {
531  new_dbh->last_used = switch_epoch_time_now(NULL);
532  }
533 
534  *dbh = new_dbh;
535 
537 }
switch_cache_db_native_handle_t native_handle
char name[CACHE_DB_LEN]
switch_bool_t switch_pgsql_available(void)
Definition: switch_pgsql.c:819
#define SWITCH_CHANNEL_LOG
struct switch_odbc_handle switch_odbc_handle_t
static switch_cache_db_handle_t * create_handle(switch_cache_db_handle_type_t type)
switch_cache_db_handle_type_t type
switch_cache_db_core_db_options_t core_db_options
Definition: switch_core.h:2473
struct switch_pgsql_handle switch_pgsql_handle_t
void switch_odbc_handle_destroy(switch_odbc_handle_t **handlep)
Definition: switch_odbc.c:671
static const char * switch_cache_db_type_name(switch_cache_db_handle_type_t type)
Definition: switch_core.h:2481
struct switch_runtime runtime
Definition: switch_core.c:64
static void add_handle(switch_cache_db_handle_t *dbh, const char *db_str, const char *db_callsite_str, const char *thread_str)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
#define CACHE_DB_LEN
Definition: switch_core.h:2440
switch_bool_t switch_odbc_available(void)
Definition: switch_odbc.c:738
uint32_t db_handle_timeout
pthread_t switch_thread_id_t
Definition: switch_apr.h:51
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_cache_db_odbc_options_t odbc_options
Definition: switch_core.h:2474
static switch_cache_db_handle_t * get_handle(const char *db_str, const char *user_str, const char *thread_str)
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
struct sqlite3 switch_core_db_t
switch_cache_db_pgsql_options_t pgsql_options
Definition: switch_core.h:2475
switch_odbc_status_t switch_odbc_handle_connect(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:336
uint32_t max_db_handles
switch_pgsql_handle_t * switch_pgsql_handle_new(const char *dsn)
Create a new handle for the PGSQL connection.
Definition: switch_pgsql.c:66
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_thread_id_t switch_thread_self(void)
Definition: switch_apr.c:79
void switch_pgsql_handle_destroy(switch_pgsql_handle_t **handlep)
Definition: switch_pgsql.c:764
switch_odbc_handle_t * switch_odbc_handle_new(const char *dsn, const char *username, const char *password)
Definition: switch_odbc.c:65
switch_core_db_t * switch_core_db_open_file(const char *filename)
Open a core db (SQLite) file.
switch_pgsql_status_t switch_pgsql_handle_connect(switch_pgsql_handle_t *handle)
Connect to the database specified by the DSN passed to the switch_pgsql_handle_new() call which initi...
Definition: switch_pgsql.c:489
static struct @3 sql_manager
switch_status_t _switch_cache_db_get_db_handle_dsn ( switch_cache_db_handle_t **  dbh,
const char *  dsn,
const char *  file,
const char *  func,
int  line 
)

Definition at line 335 of file switch_core_sqldb.c.

References _switch_cache_db_get_db_handle(), switch_cache_db_connection_options_t::core_db_options, switch_cache_db_core_db_options_t::db_path, switch_cache_db_odbc_options_t::dsn, switch_cache_db_pgsql_options_t::dsn, switch_cache_db_connection_options_t::odbc_options, switch_cache_db_odbc_options_t::pass, switch_cache_db_connection_options_t::pgsql_options, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, switch_set_string, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_cache_db_odbc_options_t::user.

Referenced by _switch_core_db_handle().

337 {
338  switch_cache_db_connection_options_t connection_options = { {0} };
340  char tmp[256] = "";
341  char *p;
343  int i;
344 
345  if (!strncasecmp(dsn, "pgsql://", 8)) {
346  type = SCDB_TYPE_PGSQL;
347  connection_options.pgsql_options.dsn = (char *)(dsn + 8);
348  } else if (!strncasecmp(dsn, "sqlite://", 9)) {
349  type = SCDB_TYPE_CORE_DB;
350  connection_options.core_db_options.db_path = (char *)(dsn + 9);
351  } else if ((!(i = strncasecmp(dsn, "odbc://", 7))) || strchr(dsn+2, ':')) {
352  type = SCDB_TYPE_ODBC;
353 
354  if (i) {
355  switch_set_string(tmp, dsn);
356  } else {
357  switch_set_string(tmp, dsn+7);
358  }
359 
360  connection_options.odbc_options.dsn = tmp;
361 
362  if ((p = strchr(tmp, ':'))) {
363  *p++ = '\0';
364  connection_options.odbc_options.user = p;
365 
366  if ((p = strchr(connection_options.odbc_options.user, ':'))) {
367  *p++ = '\0';
368  connection_options.odbc_options.pass = p;
369  }
370  }
371  } else {
372  type = SCDB_TYPE_CORE_DB;
373  connection_options.core_db_options.db_path = (char *)dsn;
374  }
375 
376  status = _switch_cache_db_get_db_handle(dbh, type, &connection_options, file, func, line);
377 
378  if (status != SWITCH_STATUS_SUCCESS) *dbh = NULL;
379 
380  return status;
381 }
switch_cache_db_core_db_options_t core_db_options
Definition: switch_core.h:2473
switch_cache_db_handle_type_t
Definition: switch_core.h:2446
switch_cache_db_odbc_options_t odbc_options
Definition: switch_core.h:2474
switch_status_t
Common return values.
switch_cache_db_pgsql_options_t pgsql_options
Definition: switch_core.h:2475
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:665
switch_status_t _switch_cache_db_get_db_handle(switch_cache_db_handle_t **dbh, switch_cache_db_handle_type_t type, switch_cache_db_connection_options_t *connection_options, const char *file, const char *func, int line)
Gets a new cached handle from the pool, potentially creating a new connection. The connection is boun...
switch_status_t _switch_core_db_handle ( switch_cache_db_handle_t **  dbh,
const char *  file,
const char *  func,
int  line 
)

Open the default system database.

Definition at line 184 of file switch_core_sqldb.c.

References _switch_cache_db_get_db_handle_dsn(), switch_runtime::dbname, switch_runtime::odbc_dsn, runtime, sql_manager, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and zstr.

185 {
186  switch_status_t r;
187  char *dsn;
188 
189  if (!sql_manager.manage) {
190  return SWITCH_STATUS_FALSE;
191  }
192 
193  if (!zstr(runtime.odbc_dsn)) {
194  dsn = runtime.odbc_dsn;
195  } else if (!zstr(runtime.dbname)) {
196  dsn = runtime.dbname;
197  } else {
198  dsn = "core";
199  }
200 
201  if ((r = _switch_cache_db_get_db_handle_dsn(dbh, dsn, file, func, line)) != SWITCH_STATUS_SUCCESS) {
202  *dbh = NULL;
203  }
204 
205  return r;
206 }
struct switch_runtime runtime
Definition: switch_core.c:64
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t _switch_cache_db_get_db_handle_dsn(switch_cache_db_handle_t **dbh, const char *dsn, const char *file, const char *func, int line)
switch_status_t
Common return values.
static struct @3 sql_manager
static void add_handle ( switch_cache_db_handle_t dbh,
const char *  db_str,
const char *  db_callsite_str,
const char *  thread_str 
)
static

Definition at line 94 of file switch_core_sqldb.c.

References switch_cache_db_handle::creator, dbh, switch_cache_db_handle::hash, switch_cache_db_handle::mutex, switch_cache_db_handle::name, switch_cache_db_handle::next, sql_manager, switch_ci_hashfunc_default(), switch_mutex_lock(), switch_mutex_unlock(), switch_set_string, switch_cache_db_handle::thread_hash, switch_cache_db_handle::total_used_count, and switch_cache_db_handle::use_count.

Referenced by _switch_cache_db_get_db_handle().

95 {
96  switch_ssize_t hlen = -1;
97 
98  switch_mutex_lock(sql_manager.dbh_mutex);
99 
100  switch_set_string(dbh->creator, db_callsite_str);
101 
102  switch_set_string(dbh->name, db_str);
103  dbh->hash = switch_ci_hashfunc_default(db_str, &hlen);
104  dbh->thread_hash = switch_ci_hashfunc_default(thread_str, &hlen);
105 
106  dbh->use_count++;
107  dbh->total_used_count++;
108  sql_manager.total_used_handles++;
109  dbh->next = sql_manager.handle_pool;
110 
111  sql_manager.handle_pool = dbh;
112  sql_manager.total_handles++;
113  switch_mutex_lock(dbh->mutex);
114  switch_mutex_unlock(sql_manager.dbh_mutex);
115 }
char name[CACHE_DB_LEN]
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:98
char creator[CACHE_DB_LEN]
switch_mutex_t * mutex
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
struct switch_cache_db_handle * next
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
intptr_t switch_ssize_t
switch_cache_db_handle_t * dbh
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:665
static struct @3 sql_manager
static void core_event_handler ( switch_event_t event)
static

Definition at line 2207 of file switch_core_sqldb.c.

References CCS_DOWN, CCS_HANGUP, CS_DESTROY, CS_EXECUTE, CS_HANGUP, CS_INIT, CS_NEW, CS_REPORTING, CS_ROUTING, switch_event::event_id, filename, MAX_SQL, new_sql, new_sql_a, parse_presence_data_cols(), db_job::sql, sql_manager, switch_assert, SWITCH_CHANNEL_LOG, switch_core_get_hostname(), switch_core_get_switchname(), switch_epoch_time_now(), SWITCH_EVENT_ADD_SCHEDULE, SWITCH_EVENT_CALL_SECURE, SWITCH_EVENT_CALL_UPDATE, SWITCH_EVENT_CHANNEL_ANSWER, SWITCH_EVENT_CHANNEL_BRIDGE, SWITCH_EVENT_CHANNEL_CALLSTATE, SWITCH_EVENT_CHANNEL_CREATE, SWITCH_EVENT_CHANNEL_DESTROY, SWITCH_EVENT_CHANNEL_EXECUTE, SWITCH_EVENT_CHANNEL_HOLD, SWITCH_EVENT_CHANNEL_ORIGINATE, SWITCH_EVENT_CHANNEL_PROGRESS_MEDIA, SWITCH_EVENT_CHANNEL_STATE, SWITCH_EVENT_CHANNEL_UNBRIDGE, SWITCH_EVENT_CHANNEL_UNHOLD, SWITCH_EVENT_CHANNEL_UUID, SWITCH_EVENT_CODEC, SWITCH_EVENT_DEL_SCHEDULE, SWITCH_EVENT_EXE_SCHEDULE, switch_event_get_header, switch_event_get_header_nil, SWITCH_EVENT_LOG, SWITCH_EVENT_MODULE_LOAD, SWITCH_EVENT_MODULE_UNLOAD, SWITCH_EVENT_NAT, SWITCH_EVENT_RE_SCHEDULE, SWITCH_EVENT_SHUTDOWN, SWITCH_FALSE, switch_ivr_uuid_exists(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), switch_safe_free, switch_sql_queue_manager_push(), switch_str_nil, switch_stristr(), switch_true(), and zstr.

Referenced by switch_core_sqldb_start(), and switch_core_sqldb_stop().

2208 {
2209  char *sql[MAX_SQL] = { 0 };
2210  int sql_idx = 0;
2211  char *extra_cols;
2212  int exists = 1;
2213  char *uuid = NULL;
2214 
2215  switch_assert(event);
2216 
2217  switch (event->event_id) {
2232  {
2233  if ((uuid = switch_event_get_header(event, "unique-id"))) {
2234  exists = switch_ivr_uuid_exists(uuid);
2235  }
2236  }
2237  break;
2238  default:
2239  break;
2240  }
2241 
2242  switch (event->event_id) {
2244  {
2245  const char *id = switch_event_get_header(event, "task-id");
2246  const char *manager = switch_event_get_header(event, "task-sql_manager");
2247 
2248  if (id) {
2249  new_sql() = switch_mprintf("insert into tasks (task_id, task_desc, task_group, task_runtime, task_sql_manager, hostname) "
2250  "values(%q,'%q','%q',%q,%q,'%q')",
2251  id,
2252  switch_event_get_header_nil(event, "task-desc"),
2253  switch_event_get_header_nil(event, "task-group"),
2254  switch_event_get_header_nil(event, "task-runtime"),
2255  manager ? manager : "0",
2257  );
2258  }
2259  }
2260  break;
2263  new_sql() = switch_mprintf("delete from tasks where task_id=%q and hostname='%q'",
2265  break;
2267  {
2268  const char *id = switch_event_get_header(event, "task-id");
2269  const char *manager = switch_event_get_header(event, "task-sql_manager");
2270 
2271  if (id) {
2272  new_sql() = switch_mprintf("update tasks set task_desc='%q',task_group='%q', task_runtime=%q, task_sql_manager=%q where task_id=%q and hostname='%q'",
2273  switch_event_get_header_nil(event, "task-desc"),
2274  switch_event_get_header_nil(event, "task-group"),
2275  switch_event_get_header_nil(event, "task-runtime"),
2276  manager ? manager : "0",
2277  id,
2279  );
2280  }
2281  }
2282  break;
2284  {
2285  const char *uuid = switch_event_get_header(event, "unique-id");
2286 
2287  if (uuid) {
2288  new_sql() = switch_mprintf("delete from channels where uuid='%q'",
2289  uuid);
2290 
2291  new_sql() = switch_mprintf("delete from calls where (caller_uuid='%q' or callee_uuid='%q')",
2292  uuid, uuid);
2293 
2294  }
2295  }
2296  break;
2298  {
2299  new_sql() = switch_mprintf("update channels set uuid='%q' where uuid='%q'",
2300  switch_event_get_header_nil(event, "unique-id"),
2301  switch_event_get_header_nil(event, "old-unique-id")
2302  );
2303 
2304  new_sql() = switch_mprintf("update channels set call_uuid='%q' where call_uuid='%q'",
2305  switch_event_get_header_nil(event, "unique-id"),
2306  switch_event_get_header_nil(event, "old-unique-id")
2307  );
2308  break;
2309  }
2311  new_sql() = switch_mprintf("insert into channels (uuid,direction,created,created_epoch, name,state,callstate,dialplan,context,hostname,initial_cid_name,initial_cid_num,initial_ip_addr,initial_dest,initial_dialplan,initial_context) "
2312  "values('%q','%q','%q','%ld','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
2313  switch_event_get_header_nil(event, "unique-id"),
2314  switch_event_get_header_nil(event, "call-direction"),
2315  switch_event_get_header_nil(event, "event-date-local"),
2316  (long) switch_epoch_time_now(NULL),
2317  switch_event_get_header_nil(event, "channel-name"),
2318  switch_event_get_header_nil(event, "channel-state"),
2319  switch_event_get_header_nil(event, "channel-call-state"),
2320  switch_event_get_header_nil(event, "caller-dialplan"),
2321  switch_event_get_header_nil(event, "caller-context"), switch_core_get_switchname(),
2322  switch_event_get_header_nil(event, "caller-caller-id-name"),
2323  switch_event_get_header_nil(event, "caller-caller-id-number"),
2324  switch_event_get_header_nil(event, "caller-network-addr"),
2325  switch_event_get_header_nil(event, "caller-destination-number"),
2326  switch_event_get_header_nil(event, "caller-dialplan"),
2327  switch_event_get_header_nil(event, "caller-context")
2328  );
2329  break;
2332  case SWITCH_EVENT_CODEC:
2333  new_sql() =
2335  ("update channels set read_codec='%q',read_rate='%q',read_bit_rate='%q',write_codec='%q',write_rate='%q',write_bit_rate='%q' where uuid='%q'",
2336  switch_event_get_header_nil(event, "channel-read-codec-name"),
2337  switch_event_get_header_nil(event, "channel-read-codec-rate"),
2338  switch_event_get_header_nil(event, "channel-read-codec-bit-rate"),
2339  switch_event_get_header_nil(event, "channel-write-codec-name"),
2340  switch_event_get_header_nil(event, "channel-write-codec-rate"),
2341  switch_event_get_header_nil(event, "channel-write-codec-bit-rate"),
2342  switch_event_get_header_nil(event, "unique-id"));
2343  break;
2347 
2348  new_sql() = switch_mprintf("update channels set application='%q',application_data='%q',"
2349  "presence_id='%q',presence_data='%q' where uuid='%q'",
2350  switch_event_get_header_nil(event, "application"),
2351  switch_event_get_header_nil(event, "application-data"),
2352  switch_event_get_header_nil(event, "channel-presence-id"),
2353  switch_event_get_header_nil(event, "channel-presence-data"),
2354  switch_event_get_header_nil(event, "unique-id")
2355  );
2356 
2357  }
2358  break;
2359 
2361  {
2362  if ((extra_cols = parse_presence_data_cols(event))) {
2363  new_sql() = switch_mprintf("update channels set "
2364  "presence_id='%q',presence_data='%q', call_uuid='%q',%s where uuid='%q'",
2365  switch_event_get_header_nil(event, "channel-presence-id"),
2366  switch_event_get_header_nil(event, "channel-presence-data"),
2367  switch_event_get_header_nil(event, "channel-call-uuid"),
2368  extra_cols,
2369  switch_event_get_header_nil(event, "unique-id"));
2370  free(extra_cols);
2371  } else {
2372  new_sql() = switch_mprintf("update channels set "
2373  "presence_id='%q',presence_data='%q', call_uuid='%q' where uuid='%q'",
2374  switch_event_get_header_nil(event, "channel-presence-id"),
2375  switch_event_get_header_nil(event, "channel-presence-data"),
2376  switch_event_get_header_nil(event, "channel-call-uuid"),
2377  switch_event_get_header_nil(event, "unique-id"));
2378  }
2379 
2380  }
2381 
2382  break;
2384  {
2385  new_sql() = switch_mprintf("update channels set callee_name='%q',callee_num='%q',sent_callee_name='%q',sent_callee_num='%q',callee_direction='%q',"
2386  "cid_name='%q',cid_num='%q' where uuid='%s'",
2387  switch_event_get_header_nil(event, "caller-callee-id-name"),
2388  switch_event_get_header_nil(event, "caller-callee-id-number"),
2389  switch_event_get_header_nil(event, "sent-callee-id-name"),
2390  switch_event_get_header_nil(event, "sent-callee-id-number"),
2391  switch_event_get_header_nil(event, "direction"),
2392  switch_event_get_header_nil(event, "caller-caller-id-name"),
2393  switch_event_get_header_nil(event, "caller-caller-id-number"),
2394  switch_event_get_header_nil(event, "unique-id")
2395  );
2396  }
2397  break;
2399  {
2400  char *num = switch_event_get_header_nil(event, "channel-call-state-number");
2402 
2403  if (num) {
2404  callstate = atoi(num);
2405  }
2406 
2407  if (callstate != CCS_DOWN && callstate != CCS_HANGUP) {
2408  if ((extra_cols = parse_presence_data_cols(event))) {
2409  new_sql() = switch_mprintf("update channels set callstate='%q',%s where uuid='%q'",
2410  switch_event_get_header_nil(event, "channel-call-state"),
2411  extra_cols,
2412  switch_event_get_header_nil(event, "unique-id"));
2413  free(extra_cols);
2414  } else {
2415  new_sql() = switch_mprintf("update channels set callstate='%q' where uuid='%q'",
2416  switch_event_get_header_nil(event, "channel-call-state"),
2417  switch_event_get_header_nil(event, "unique-id"));
2418  }
2419  }
2420 
2421  }
2422  break;
2424  {
2425  char *state = switch_event_get_header_nil(event, "channel-state-number");
2427 
2428  if (!zstr(state)) {
2429  state_i = atoi(state);
2430  }
2431 
2432  switch (state_i) {
2433  case CS_NEW:
2434  case CS_DESTROY:
2435  case CS_REPORTING:
2436 #ifndef SWITCH_DEPRECATED_CORE_DB
2437  case CS_HANGUP: /* marked for deprication */
2438 #endif
2439  case CS_INIT:
2440  break;
2441 #ifdef SWITCH_DEPRECATED_CORE_DB
2442  case CS_HANGUP: /* marked for deprication */
2443  new_sql_a() = switch_mprintf("update channels set state='%s' where uuid='%s'",
2444  switch_event_get_header_nil(event, "channel-state"),
2445  switch_event_get_header_nil(event, "unique-id"));
2446  break;
2447 #endif
2448  case CS_EXECUTE:
2449  if ((extra_cols = parse_presence_data_cols(event))) {
2450  new_sql() = switch_mprintf("update channels set state='%s',%s where uuid='%q'",
2451  switch_event_get_header_nil(event, "channel-state"),
2452  extra_cols,
2453  switch_event_get_header_nil(event, "unique-id"));
2454  free(extra_cols);
2455 
2456  } else {
2457  new_sql() = switch_mprintf("update channels set state='%s' where uuid='%s'",
2458  switch_event_get_header_nil(event, "channel-state"),
2459  switch_event_get_header_nil(event, "unique-id"));
2460  }
2461  break;
2462  case CS_ROUTING:
2463  if ((extra_cols = parse_presence_data_cols(event))) {
2464  new_sql() = switch_mprintf("update channels set state='%s',cid_name='%q',cid_num='%q',callee_name='%q',callee_num='%q',"
2465  "sent_callee_name='%q',sent_callee_num='%q',"
2466  "ip_addr='%s',dest='%q',dialplan='%q',context='%q',presence_id='%q',presence_data='%q',%s "
2467  "where uuid='%s'",
2468  switch_event_get_header_nil(event, "channel-state"),
2469  switch_event_get_header_nil(event, "caller-caller-id-name"),
2470  switch_event_get_header_nil(event, "caller-caller-id-number"),
2471  switch_event_get_header_nil(event, "caller-callee-id-name"),
2472  switch_event_get_header_nil(event, "caller-callee-id-number"),
2473  switch_event_get_header_nil(event, "sent-callee-id-name"),
2474  switch_event_get_header_nil(event, "sent-callee-id-number"),
2475  switch_event_get_header_nil(event, "caller-network-addr"),
2476  switch_event_get_header_nil(event, "caller-destination-number"),
2477  switch_event_get_header_nil(event, "caller-dialplan"),
2478  switch_event_get_header_nil(event, "caller-context"),
2479  switch_event_get_header_nil(event, "channel-presence-id"),
2480  switch_event_get_header_nil(event, "channel-presence-data"),
2481  extra_cols,
2482  switch_event_get_header_nil(event, "unique-id"));
2483  free(extra_cols);
2484  } else {
2485  new_sql() = switch_mprintf("update channels set state='%s',cid_name='%q',cid_num='%q',callee_name='%q',callee_num='%q',"
2486  "sent_callee_name='%q',sent_callee_num='%q',"
2487  "ip_addr='%s',dest='%q',dialplan='%q',context='%q',presence_id='%q',presence_data='%q' "
2488  "where uuid='%s'",
2489  switch_event_get_header_nil(event, "channel-state"),
2490  switch_event_get_header_nil(event, "caller-caller-id-name"),
2491  switch_event_get_header_nil(event, "caller-caller-id-number"),
2492  switch_event_get_header_nil(event, "caller-callee-id-name"),
2493  switch_event_get_header_nil(event, "caller-callee-id-number"),
2494  switch_event_get_header_nil(event, "sent-callee-id-name"),
2495  switch_event_get_header_nil(event, "sent-callee-id-number"),
2496  switch_event_get_header_nil(event, "caller-network-addr"),
2497  switch_event_get_header_nil(event, "caller-destination-number"),
2498  switch_event_get_header_nil(event, "caller-dialplan"),
2499  switch_event_get_header_nil(event, "caller-context"),
2500  switch_event_get_header_nil(event, "channel-presence-id"),
2501  switch_event_get_header_nil(event, "channel-presence-data"),
2502  switch_event_get_header_nil(event, "unique-id"));
2503  }
2504  break;
2505  default:
2506  new_sql() = switch_mprintf("update channels set state='%s' where uuid='%s'",
2507  switch_event_get_header_nil(event, "channel-state"),
2508  switch_event_get_header_nil(event, "unique-id"));
2509  break;
2510  }
2511 
2512  break;
2513 
2514 
2515  }
2517  {
2518  const char *a_uuid, *b_uuid, *uuid;
2519 
2520  a_uuid = switch_event_get_header(event, "Bridge-A-Unique-ID");
2521  b_uuid = switch_event_get_header(event, "Bridge-B-Unique-ID");
2522  uuid = switch_event_get_header(event, "unique-id");
2523 
2524  if (zstr(a_uuid) || zstr(b_uuid)) {
2525  a_uuid = switch_event_get_header_nil(event, "caller-unique-id");
2526  b_uuid = switch_event_get_header_nil(event, "other-leg-unique-id");
2527  }
2528 
2529  if (uuid && (extra_cols = parse_presence_data_cols(event))) {
2530  new_sql() = switch_mprintf("update channels set %s where uuid='%s'", extra_cols, uuid);
2531  switch_safe_free(extra_cols);
2532  }
2533 
2534  new_sql() = switch_mprintf("update channels set call_uuid='%q' where uuid='%s' or uuid='%s'",
2535  switch_event_get_header_nil(event, "channel-call-uuid"), a_uuid, b_uuid);
2536 
2537 
2538  new_sql() = switch_mprintf("insert into calls (call_uuid,call_created,call_created_epoch,"
2539  "caller_uuid,callee_uuid,hostname) "
2540  "values ('%s','%s','%ld','%q','%q','%q')",
2541  switch_event_get_header_nil(event, "channel-call-uuid"),
2542  switch_event_get_header_nil(event, "event-date-local"),
2543  (long) switch_epoch_time_now(NULL),
2544  a_uuid,
2545  b_uuid,
2547  );
2548  }
2549  break;
2551  {
2552  char *cuuid = switch_event_get_header_nil(event, "caller-unique-id");
2553  char *uuid = switch_event_get_header(event, "unique-id");
2554 
2555  if (uuid && (extra_cols = parse_presence_data_cols(event))) {
2556  new_sql() = switch_mprintf("update channels set %s where uuid='%s'", extra_cols, uuid);
2557  switch_safe_free(extra_cols);
2558  }
2559 
2560  new_sql() = switch_mprintf("update channels set call_uuid=uuid where call_uuid='%s'",
2561  switch_event_get_header_nil(event, "channel-call-uuid"));
2562 
2563  new_sql() = switch_mprintf("delete from calls where (caller_uuid='%q' or callee_uuid='%q')",
2564  cuuid, cuuid);
2565  break;
2566  }
2567  case SWITCH_EVENT_SHUTDOWN:
2568  new_sql() = switch_mprintf("delete from channels where hostname='%q';"
2569  "delete from interfaces where hostname='%q';"
2570  "delete from calls where hostname='%q'",
2572  );
2573  break;
2574  case SWITCH_EVENT_LOG:
2575  return;
2577  {
2578  const char *type = switch_event_get_header_nil(event, "type");
2579  const char *name = switch_event_get_header_nil(event, "name");
2580  const char *description = switch_event_get_header_nil(event, "description");
2581  const char *syntax = switch_event_get_header_nil(event, "syntax");
2582  const char *key = switch_event_get_header_nil(event, "key");
2583  const char *filename = switch_event_get_header_nil(event, "filename");
2584  if (!zstr(type) && !zstr(name)) {
2585  new_sql() =
2587  ("insert into interfaces (type,name,description,syntax,ikey,filename,hostname) values('%q','%q','%q','%q','%q','%q','%q')", type, name,
2588  switch_str_nil(description), switch_str_nil(syntax), switch_str_nil(key), switch_str_nil(filename),
2590  );
2591  }
2592  break;
2593  }
2595  {
2596  const char *type = switch_event_get_header_nil(event, "type");
2597  const char *name = switch_event_get_header_nil(event, "name");
2598  if (!zstr(type) && !zstr(name)) {
2599  new_sql() = switch_mprintf("delete from interfaces where type='%q' and name='%q' and hostname='%q'", type, name,
2601  }
2602  break;
2603  }
2605  {
2606  const char *type = switch_event_get_header_nil(event, "secure_type");
2607  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Secure Type: %s\n", type);
2608  if (zstr(type)) {
2609  break;
2610  }
2611  new_sql() = switch_mprintf("update channels set secure='%s' where uuid='%s'",
2612  type, switch_event_get_header_nil(event, "caller-unique-id")
2613  );
2614  break;
2615  }
2616  case SWITCH_EVENT_NAT:
2617  {
2618  const char *op = switch_event_get_header_nil(event, "op");
2619  switch_bool_t sticky = switch_true(switch_event_get_header_nil(event, "sticky"));
2620  if (!strcmp("add", op)) {
2621  new_sql() = switch_mprintf("insert into nat (port, proto, sticky, hostname) values (%s, %s, %d,'%q')",
2622  switch_event_get_header_nil(event, "port"),
2623  switch_event_get_header_nil(event, "proto"), sticky, switch_core_get_hostname()
2624  );
2625  } else if (!strcmp("del", op)) {
2626  new_sql() = switch_mprintf("delete from nat where port=%s and proto=%s and hostname='%q'",
2627  switch_event_get_header_nil(event, "port"),
2629  } else if (!strcmp("status", op)) {
2630  /* call show nat api */
2631  } else if (!strcmp("status_response", op)) {
2632  /* ignore */
2633  } else {
2634  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown op for SWITCH_EVENT_NAT: %s\n", op);
2635  }
2636  break;
2637  }
2638  default:
2639  break;
2640  }
2641 
2642  if (sql_idx) {
2643  int i = 0;
2644 
2645 
2646  for (i = 0; i < sql_idx; i++) {
2647  if (switch_stristr("update channels", sql[i]) || switch_stristr("delete from channels", sql[i])) {
2649  } else {
2651  }
2652  sql[i] = NULL;
2653  }
2654  }
2655 }
#define switch_event_get_header_nil(e, h)
Definition: switch_event.h:174
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
switch_event_types_t event_id
Definition: switch_event.h:82
#define SWITCH_CHANNEL_LOG
switch_bool_t switch_ivr_uuid_exists(const char *uuid)
Definition: switch_ivr.c:3862
switch_bool_t
Definition: switch_types.h:405
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
#define zstr(x)
Definition: switch_utils.h:281
static char * parse_presence_data_cols(switch_event_t *event)
#define new_sql_a()
#define MAX_SQL
#define new_sql()
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_channel_callstate_t
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
switch_channel_state_t
Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA are ...
const char * switch_core_get_hostname(void)
Definition: switch_core.c:344
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
char * filename
#define switch_assert(expr)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
static switch_cache_db_handle_t* create_handle ( switch_cache_db_handle_type_t  type)
static

Definition at line 80 of file switch_core_sqldb.c.

References switch_cache_db_handle::mutex, switch_cache_db_handle::pool, pool, switch_core_alloc, switch_core_new_memory_pool, switch_mutex_init(), SWITCH_MUTEX_NESTED, and switch_cache_db_handle::type.

Referenced by _switch_cache_db_get_db_handle().

81 {
82  switch_cache_db_handle_t *new_dbh = NULL;
83  switch_memory_pool_t *pool = NULL;
84 
86  new_dbh = switch_core_alloc(pool, sizeof(*new_dbh));
87  new_dbh->pool = pool;
88  new_dbh->type = type;
89  switch_mutex_init(&new_dbh->mutex, SWITCH_MUTEX_NESTED, new_dbh->pool);
90 
91  return new_dbh;
92 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
switch_cache_db_handle_type_t type
switch_memory_pool_t * pool
switch_memory_pool_t * pool
switch_mutex_t * mutex
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
struct apr_pool_t switch_memory_pool_t
static void del_handle ( switch_cache_db_handle_t dbh)
static

Definition at line 117 of file switch_core_sqldb.c.

References switch_cache_db_handle::next, sql_manager, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by sql_close().

118 {
119  switch_cache_db_handle_t *dbh_ptr, *last = NULL;
120 
121  switch_mutex_lock(sql_manager.dbh_mutex);
122  for (dbh_ptr = sql_manager.handle_pool; dbh_ptr; dbh_ptr = dbh_ptr->next) {
123  if (dbh_ptr == dbh) {
124  if (last) {
125  last->next = dbh_ptr->next;
126  } else {
127  sql_manager.handle_pool = dbh_ptr->next;
128  }
129  sql_manager.total_handles--;
130  break;
131  }
132 
133  last = dbh_ptr;
134  }
135  switch_mutex_unlock(sql_manager.dbh_mutex);
136 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
struct switch_cache_db_handle * next
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static struct @3 sql_manager
static void do_flush ( switch_sql_queue_manager_t qm,
int  i,
switch_cache_db_handle_t dbh 
)
static

Definition at line 1597 of file switch_core_sqldb.c.

References switch_sql_queue_manager::mutex, switch_sql_queue_manager::sql_queue, switch_cache_db_execute_sql(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_trypop(), switch_safe_free, and SWITCH_STATUS_SUCCESS.

Referenced by switch_sql_queue_manager_destroy(), switch_sql_queue_manager_pause(), and switch_user_sql_thread().

1598 {
1599  void *pop = NULL;
1600  switch_queue_t *q = qm->sql_queue[i];
1601 
1602  switch_mutex_lock(qm->mutex);
1603  while (switch_queue_trypop(q, &pop) == SWITCH_STATUS_SUCCESS) {
1604  if (pop) {
1605  if (dbh) {
1606  switch_cache_db_execute_sql(dbh, (char *) pop, NULL);
1607  }
1608  switch_safe_free(pop);
1609  }
1610  }
1612 
1613 }
struct apr_queue_t switch_queue_t
Definition: switch_apr.h:590
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
static uint32_t do_trans ( switch_sql_queue_manager_t qm)
static

Definition at line 1902 of file switch_core_sqldb.c.

References switch_sql_queue_manager::event_db, switch_sql_queue_manager::inner_post_trans_execute, switch_sql_queue_manager::inner_pre_trans_execute, switch_cache_db_handle::io_mutex, io_mutex, switch_sql_queue_manager::max_trans, switch_sql_queue_manager::mutex, switch_cache_db_handle::native_handle, switch_sql_queue_manager::numq, switch_cache_db_native_handle_t::odbc_dbh, switch_cache_db_native_handle_t::pgsql_dbh, switch_sql_queue_manager::post_trans_execute, switch_sql_queue_manager::pre_trans_execute, switch_sql_queue_manager::pre_written, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, switch_sql_queue_manager::sql_queue, switch_cache_db_execute_sql(), switch_cache_db_execute_sql_real(), SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_SQLEndTran(), switch_odbc_SQLSetAutoCommitAttr(), SWITCH_ODBC_SUCCESS, switch_pgsql_finish_results, switch_pgsql_SQLEndTran(), switch_pgsql_SQLSetAutoCommitAttr(), SWITCH_PGSQL_SUCCESS, switch_queue_trypop(), switch_safe_free, switch_snprintfv(), SWITCH_STATUS_SUCCESS, switch_cache_db_handle::type, switch_sql_queue_manager::written, and zstr.

Referenced by switch_user_sql_thread().

1903 {
1904  char *errmsg = NULL;
1905  void *pop;
1906  switch_status_t status;
1907  uint32_t ttl = 0;
1909  uint32_t i;
1910 
1911  if (io_mutex) switch_mutex_lock(io_mutex);
1912 
1913  if (!zstr(qm->pre_trans_execute)) {
1915  if (errmsg) {
1916  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL PRE TRANS EXEC %s [%s]\n", qm->pre_trans_execute, errmsg);
1917  switch_safe_free(errmsg);
1918  }
1919  }
1920 
1921  switch(qm->event_db->type) {
1922  case SCDB_TYPE_CORE_DB:
1923  {
1924  switch_cache_db_execute_sql_real(qm->event_db, "BEGIN EXCLUSIVE", &errmsg);
1925  }
1926  break;
1927  case SCDB_TYPE_ODBC:
1928  {
1929  switch_odbc_status_t result;
1930 
1932  char tmp[100];
1933  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
1934  errmsg = strdup(tmp);
1935  }
1936  }
1937  break;
1938  case SCDB_TYPE_PGSQL:
1939  {
1940  switch_pgsql_status_t result;
1941 
1943  char tmp[100];
1944  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
1945  errmsg = strdup(tmp);
1946  }
1947  }
1948  break;
1949  }
1950 
1951  if (errmsg) {
1952  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "ERROR [%s]\n", errmsg);
1953  switch_safe_free(errmsg);
1954  goto end;
1955  }
1956 
1957 
1958  if (!zstr(qm->inner_pre_trans_execute)) {
1960  if (errmsg) {
1961  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL PRE TRANS EXEC %s [%s]\n", qm->inner_pre_trans_execute, errmsg);
1962  switch_safe_free(errmsg);
1963  }
1964  }
1965 
1966 
1967  while(qm->max_trans == 0 || ttl <= qm->max_trans) {
1968  pop = NULL;
1969 
1970  for (i = 0; (qm->max_trans == 0 || ttl <= qm->max_trans) && (i < qm->numq); i++) {
1971  switch_mutex_lock(qm->mutex);
1972  switch_queue_trypop(qm->sql_queue[i], &pop);
1974  if (pop) break;
1975  }
1976 
1977  if (pop) {
1978  if ((status = switch_cache_db_execute_sql(qm->event_db, (char *) pop, NULL)) == SWITCH_STATUS_SUCCESS) {
1979  switch_mutex_lock(qm->mutex);
1980  qm->pre_written[i]++;
1982  ttl++;
1983  }
1984  switch_safe_free(pop);
1985  if (status != SWITCH_STATUS_SUCCESS) break;
1986  } else {
1987  break;
1988  }
1989  }
1990 
1991  if (!zstr(qm->inner_post_trans_execute)) {
1993  if (errmsg) {
1994  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL POST TRANS EXEC %s [%s]\n", qm->inner_post_trans_execute, errmsg);
1995  switch_safe_free(errmsg);
1996  }
1997  }
1998 
1999 
2000  end:
2001 
2002  switch(qm->event_db->type) {
2003  case SCDB_TYPE_CORE_DB:
2004  {
2005  switch_cache_db_execute_sql_real(qm->event_db, "COMMIT", NULL);
2006  }
2007  break;
2008  case SCDB_TYPE_ODBC:
2009  {
2012  }
2013  break;
2014  case SCDB_TYPE_PGSQL:
2015  {
2019  }
2020  break;
2021  }
2022 
2023 
2024  if (!zstr(qm->post_trans_execute)) {
2026  if (errmsg) {
2027  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL POST TRANS EXEC %s [%s]\n", qm->post_trans_execute, errmsg);
2028  switch_safe_free(errmsg);
2029  }
2030  }
2031 
2032 
2033  switch_mutex_lock(qm->mutex);
2034  for (i = 0; i < qm->numq; i++) {
2035  qm->written[i] = qm->pre_written[i];
2036  }
2038 
2039 
2040  if (io_mutex) switch_mutex_unlock(io_mutex);
2041 
2042  return ttl;
2043 }
switch_pgsql_status_t
Definition: switch_pgsql.h:53
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
switch_odbc_status_t
Definition: switch_odbc.h:49
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_t * event_db
switch_cache_db_handle_type_t type
switch_pgsql_status_t switch_pgsql_SQLEndTran(switch_pgsql_handle_t *handle, switch_bool_t commit)
Definition: switch_pgsql.c:865
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_pgsql_status_t switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on)
Definition: switch_pgsql.c:828
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_pgsql_finish_results(handle)
Definition: switch_pgsql.h:107
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t *dbh, const char *sql, char **err)
switch_odbc_status_t switch_odbc_SQLEndTran(switch_odbc_handle_t *handle, switch_bool_t commit)
Definition: switch_odbc.c:760
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
switch_mutex_t * io_mutex
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr(switch_odbc_handle_t *handle, switch_bool_t on)
Definition: switch_odbc.c:747
static switch_cache_db_handle_t* get_handle ( const char *  db_str,
const char *  user_str,
const char *  thread_str 
)
static

Definition at line 138 of file switch_core_sqldb.c.

References CDF_PRUNE, switch_cache_db_handle::hash, hash, switch_cache_db_handle::last_user, switch_cache_db_handle::mutex, switch_cache_db_handle::next, SCDB_TYPE_PGSQL, sql_manager, switch_ci_hashfunc_default(), switch_mutex_lock(), switch_mutex_trylock(), switch_mutex_unlock(), switch_set_string, SWITCH_STATUS_SUCCESS, switch_test_flag, switch_cache_db_handle::thread_hash, switch_cache_db_handle::total_used_count, switch_cache_db_handle::type, and switch_cache_db_handle::use_count.

Referenced by _switch_cache_db_get_db_handle().

139 {
140  switch_ssize_t hlen = -1;
141  unsigned long hash = 0, thread_hash = 0;
142  switch_cache_db_handle_t *dbh_ptr, *r = NULL;
143 
144  hash = switch_ci_hashfunc_default(db_str, &hlen);
145  thread_hash = switch_ci_hashfunc_default(thread_str, &hlen);
146 
147  switch_mutex_lock(sql_manager.dbh_mutex);
148 
149  for (dbh_ptr = sql_manager.handle_pool; dbh_ptr; dbh_ptr = dbh_ptr->next) {
150  if (dbh_ptr->thread_hash == thread_hash && dbh_ptr->hash == hash && !dbh_ptr->use_count &&
152  r = dbh_ptr;
153  }
154  }
155 
156  if (!r) {
157  for (dbh_ptr = sql_manager.handle_pool; dbh_ptr; dbh_ptr = dbh_ptr->next) {
158  if (dbh_ptr->hash == hash && (dbh_ptr->type != SCDB_TYPE_PGSQL || !dbh_ptr->use_count) && !switch_test_flag(dbh_ptr, CDF_PRUNE) &&
160  r = dbh_ptr;
161  break;
162  }
163  }
164  }
165 
166  if (r) {
167  r->use_count++;
168  r->total_used_count++;
169  sql_manager.total_used_handles++;
170  r->hash = switch_ci_hashfunc_default(db_str, &hlen);
171  r->thread_hash = thread_hash;
172  switch_set_string(r->last_user, user_str);
173  }
174 
175  switch_mutex_unlock(sql_manager.dbh_mutex);
176 
177  return r;
178 
179 }
switch_status_t switch_mutex_trylock(switch_mutex_t *lock)
Definition: switch_apr.c:295
switch_cache_db_handle_type_t type
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:98
char last_user[CACHE_DB_LEN]
switch_hash_t * hash
Definition: switch_event.c:74
switch_mutex_t * mutex
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
struct switch_cache_db_handle * next
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
intptr_t switch_ssize_t
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:665
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
static struct @3 sql_manager
static int helper_callback ( void *  pArg,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 1027 of file switch_core_sqldb.c.

References helper::callback, helper::pdata, switch_event_create_array_pair(), and switch_event_destroy().

Referenced by switch_cache_db_execute_sql_event_callback(), and switch_cache_db_execute_sql_event_callback_err().

1028 {
1029  struct helper *h = (struct helper *) pArg;
1030  int r = 0;
1031  switch_event_t *event;
1032 
1033  switch_event_create_array_pair(&event, columnNames, argv, argc);
1034 
1035  r = h->callback(h->pdata, event);
1036 
1037  switch_event_destroy(&event);
1038 
1039  return r;
1040 }
switch_core_db_event_callback_func_t callback
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_event_create_array_pair(switch_event_t **event, char **names, char **vals, int len)
void switch_event_destroy(switch_event_t **event)
Destroy an event.
static switch_thread_data_t* new_job ( switch_sql_queue_manager_t qm,
const char *  sql,
switch_core_db_callback_func_t  callback,
switch_core_db_err_callback_func_t  err_callback,
switch_core_db_event_callback_func_t  event_callback,
switch_core_db_err_callback_func_t  event_err_callback,
void *  pdata 
)
static

Definition at line 1517 of file switch_core_sqldb.c.

References db_job::callback, db_job::err_callback, db_job::event_callback, db_job::event_err_callback, switch_thread_data_s::func, switch_thread_data_s::obj, db_job::pdata, pool, db_job::pool, qm, db_job::qm, db_job::sql, sql_in_thread(), switch_core_alloc, switch_core_new_memory_pool, and switch_core_strdup.

Referenced by switch_sql_queue_manager_execute_sql_callback(), switch_sql_queue_manager_execute_sql_callback_err(), switch_sql_queue_manager_execute_sql_event_callback(), and switch_sql_queue_manager_execute_sql_event_callback_err().

1523 {
1526  struct db_job *job;
1528 
1529  td = switch_core_alloc(pool, sizeof(*td));
1530  job = switch_core_alloc(pool, sizeof(*job));
1531 
1532  td->func = sql_in_thread;
1533  td->obj = job;
1534 
1535  job->sql = switch_core_strdup(pool, sql);
1536  job->qm = qm;
1537 
1538  if (callback) {
1539  job->callback = callback;
1540  job->err_callback = err_callback;
1541  } else if (event_callback) {
1544  }
1545 
1546  job->pdata = pdata;
1547  job->pool = pool;
1548 
1549  return td;
1550 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_memory_pool_t * pool
switch_sql_queue_manager_t * qm
switch_core_db_err_callback_func_t event_err_callback
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_thread_start_t func
Definition: switch_core.h:66
switch_core_db_err_callback_func_t err_callback
switch_sql_queue_manager_t * qm
switch_memory_pool_t * pool
switch_core_db_callback_func_t callback
struct apr_pool_t switch_memory_pool_t
switch_core_db_event_callback_func_t event_callback
static void *SWITCH_THREAD_FUNC sql_in_thread(switch_thread_t *thread, void *obj)
static char* parse_presence_data_cols ( switch_event_t event)
static

Definition at line 2157 of file switch_core_sqldb.c.

References switch_stream_handle::data, end_of, if(), switch_event_get_header, switch_event_get_header_nil, switch_safe_free, switch_snprintfv(), switch_split, SWITCH_STANDARD_STREAM, switch_stream_handle::write_function, and zstr.

Referenced by core_event_handler().

2158 {
2159  char *cols[128] = { 0 };
2160  int col_count = 0;
2161  char *data_copy;
2162  switch_stream_handle_t stream = { 0 };
2163  int i;
2164  char *r;
2165  char col_name[128] = "";
2166  const char *data = switch_event_get_header(event, "presence-data-cols");
2167 
2168  if (zstr(data)) {
2169  return NULL;
2170  }
2171 
2172  data_copy = strdup(data);
2173 
2174  col_count = switch_split(data_copy, ':', cols);
2175 
2176  SWITCH_STANDARD_STREAM(stream);
2177 
2178  for (i = 0; i < col_count; i++) {
2179  const char *val = NULL;
2180 
2181  switch_snprintfv(col_name, sizeof(col_name), "PD-%q", cols[i]);
2182  val = switch_event_get_header_nil(event, col_name);
2183  if (zstr(val)) {
2184  stream.write_function(&stream, "%q=NULL,", cols[i]);
2185  } else {
2186  stream.write_function(&stream, "%q='%q',", cols[i], val);
2187  }
2188  }
2189 
2190  r = (char *) stream.data;
2191 
2192  if (end_of(r) == ',') {
2193  end_of(r) = '\0';
2194  }
2195 
2196  switch_safe_free(data_copy);
2197 
2198  return r;
2199 
2200 }
#define switch_event_get_header_nil(e, h)
Definition: switch_event.h:174
#define switch_split(_data, _delim, _array)
Definition: switch_utils.h:342
#define end_of(_s)
Definition: switch_utils.h:616
#define zstr(x)
Definition: switch_utils.h:281
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
#define SWITCH_STANDARD_STREAM(s)
switch_stream_handle_write_function_t write_function
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
static uint32_t qm_ttl ( switch_sql_queue_manager_t qm)
static

Definition at line 1456 of file switch_core_sqldb.c.

References switch_sql_queue_manager::numq, switch_sql_queue_manager::sql_queue, and switch_queue_size().

Referenced by switch_user_sql_thread().

1457 {
1458  uint32_t ttl = 0;
1459  uint32_t i;
1460 
1461  for (i = 0; i < qm->numq; i++) {
1462  ttl += switch_queue_size(qm->sql_queue[i]);
1463  }
1464 
1465  return ttl;
1466 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
static int qm_wake ( switch_sql_queue_manager_t qm)
static

Definition at line 1429 of file switch_core_sqldb.c.

References switch_sql_queue_manager::cond, switch_sql_queue_manager::cond2_mutex, switch_sql_queue_manager::cond_mutex, switch_cond_next(), switch_mutex_trylock(), switch_mutex_unlock(), SWITCH_STATUS_SUCCESS, and switch_thread_cond_signal().

Referenced by switch_sql_queue_manager_push(), switch_sql_queue_manager_push_confirm(), switch_sql_queue_manager_resume(), and switch_sql_queue_manager_stop().

1430 {
1431  switch_status_t status;
1432  int tries = 0;
1433 
1434  top:
1435 
1436  status = switch_mutex_trylock(qm->cond_mutex);
1437 
1438  if (status == SWITCH_STATUS_SUCCESS) {
1441  return 1;
1442  } else {
1445  } else {
1446  if (++tries < 10) {
1447  switch_cond_next();
1448  goto top;
1449  }
1450  }
1451  }
1452 
1453  return 0;
1454 }
switch_status_t switch_mutex_trylock(switch_mutex_t *lock)
Definition: switch_apr.c:295
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
void switch_cond_next(void)
Definition: switch_time.c:638
switch_status_t switch_thread_cond_signal(switch_thread_cond_t *cond)
Definition: switch_apr.c:371
switch_status_t
Common return values.
switch_thread_cond_t * cond
static int recover_callback ( void *  pArg,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 2950 of file switch_core_sqldb.c.

References CF_RECOVERING, CF_RECOVERING_BRIDGE, switch_core_session::channel, CS_INIT, ep, switch_xml::next, switch_endpoint_interface::recover_callback, switch_caller_extension_add_application(), switch_caller_extension_new(), switch_channel_get_name(), switch_channel_get_partner_uuid(), switch_channel_get_variable, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_caller_extension(), switch_channel_set_flag, switch_channel_set_state, switch_channel_test_flag(), switch_core_get_secondary_recover_callback(), switch_core_media_recover_session(), switch_core_session_get_channel(), switch_core_session_request_xml(), switch_core_session_thread_launch(), switch_loadable_module_get_endpoint_interface(), SWITCH_LOG_NOTICE, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_TRUE, switch_xml_attr_soft(), switch_xml_child(), switch_xml_free(), switch_xml_parse_str_dynamic(), and UNPROTECT_INTERFACE.

Referenced by switch_core_recovery_recover().

2951 {
2952  int *rp = (int *) pArg;
2953  switch_xml_t xml;
2955  switch_core_session_t *session;
2956 
2957  if (argc < 4) {
2958  return 0;
2959  }
2960 
2961  if (!(xml = switch_xml_parse_str_dynamic(argv[4], SWITCH_TRUE))) {
2963  return 0;
2964  }
2965 
2966  if (!(ep = switch_loadable_module_get_endpoint_interface(argv[0]))) {
2968  return 0;
2969  }
2970 
2971  if (!(session = switch_core_session_request_xml(ep, NULL, xml))) {
2972  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid cdr data, call not recovered\n");
2973  goto end;
2974  }
2975 
2976  if (ep->recover_callback) {
2977  switch_caller_extension_t *extension = NULL;
2979  int r = 0;
2980 
2981  if ((r = ep->recover_callback(session)) > 0) {
2982  const char *cbname;
2983 
2985 
2986 
2987  if (switch_channel_get_partner_uuid(channel)) {
2989  }
2990 
2992 
2993  if ((cbname = switch_channel_get_variable(channel, "secondary_recovery_module"))) {
2995 
2996  if ((recover_callback = switch_core_get_secondary_recover_callback(cbname))) {
2997  r = recover_callback(session);
2998  }
2999  }
3000 
3001 
3002  }
3003 
3004  if (r > 0) {
3005 
3007  switch_xml_t callflow, param, x_extension;
3008  if ((extension = switch_caller_extension_new(session, "recovery", "recovery")) == 0) {
3009  abort();
3010  }
3011 
3012  if ((callflow = switch_xml_child(xml, "callflow")) && (x_extension = switch_xml_child(callflow, "extension"))) {
3013  for (param = switch_xml_child(x_extension, "application"); param; param = param->next) {
3014  const char *var = switch_xml_attr_soft(param, "app_name");
3015  const char *val = switch_xml_attr_soft(param, "app_data");
3016  /* skip announcement type apps */
3017  if (strcasecmp(var, "speak") && strcasecmp(var, "playback") && strcasecmp(var, "gentones") && strcasecmp(var, "say")) {
3018  switch_caller_extension_add_application(session, extension, var, val);
3019  }
3020  }
3021  }
3022 
3023  switch_channel_set_caller_extension(channel, extension);
3024  }
3025 
3028  "Resurrecting fallen channel %s\n", switch_channel_get_name(channel));
3030 
3031  *rp = (*rp) + 1;
3032 
3033  }
3034 
3035  } else {
3036  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Endpoint %s has no recovery function\n", argv[0]);
3037  }
3038 
3039 
3040  end:
3041 
3042  UNPROTECT_INTERFACE(ep);
3043 
3044  switch_xml_free(xml);
3045 
3046  return 0;
3047 }
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
const char * switch_xml_attr_soft(_In_ switch_xml_t xml, _In_z_ const char *attr)
returns the value of the requested tag attribute, or "" if not found
#define SWITCH_CHANNEL_SESSION_LOG(x)
An Abstract Representation of a dialplan extension.
#define SWITCH_CHANNEL_LOG
switch_xml_t switch_xml_parse_str_dynamic(_In_z_ char *s, _In_ switch_bool_t dup)
Parses a string into a switch_xml_t, ensuring the memory will be freed with switch_xml_free.
Abstraction of an module endpoint interface This is the glue between the abstract idea of a "channel"...
const char * switch_channel_get_partner_uuid(switch_channel_t *channel)
#define switch_channel_set_state(channel, state)
Set the current state of a channel.
switch_core_session_t * switch_core_session_request_xml(switch_endpoint_interface_t *endpoint_interface, switch_memory_pool_t **pool, switch_xml_t xml)
A representation of an XML tree.
Definition: switch_xml.h:76
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define UNPROTECT_INTERFACE(_it)
switch_xml_t next
Definition: switch_xml.h:88
switch_channel_t * channel
#define switch_channel_get_variable(_c, _v)
void switch_channel_set_caller_extension(switch_channel_t *channel, switch_caller_extension_t *caller_extension)
Assign a caller extension to a given channel.
void switch_caller_extension_add_application(_In_ switch_core_session_t *session, _In_ switch_caller_extension_t *caller_extension, _In_z_ const char *application_name, _In_z_ const char *extra_data)
Add an application (instruction) to the given extension.
switch_caller_extension_t * switch_caller_extension_new(_In_ switch_core_session_t *session, _In_z_ const char *extension_name, _In_z_ const char *extension_number)
Create a new extension with desired parameters.
switch_core_recover_callback_t recover_callback
static const char * ep
Definition: switch_json.c:36
switch_endpoint_interface_t * switch_loadable_module_get_endpoint_interface(const char *name)
Retrieve the endpoint interface by it's registered name.
static int recover_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_channel_set_flag(_c, _f)
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
int(* switch_core_recover_callback_t)(switch_core_session_t *session)
switch_core_recover_callback_t switch_core_get_secondary_recover_callback(const char *key)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
void switch_core_media_recover_session(switch_core_session_t *session)
switch_status_t switch_core_session_thread_launch(_In_ switch_core_session_t *session)
Launch the session thread (state machine) on a given session.
static void sql_close ( time_t  prune)
static

Definition at line 212 of file switch_core_sqldb.c.

References CDF_PRUNE, switch_cache_db_native_handle_t::core_db_dbh, switch_cache_db_handle::creator, dbh, del_handle(), switch_cache_db_handle::last_used, switch_cache_db_handle::last_user, switch_cache_db_handle::mutex, switch_cache_db_handle::name, switch_cache_db_handle::native_handle, switch_cache_db_handle::next, switch_cache_db_native_handle_t::odbc_dbh, switch_cache_db_native_handle_t::pgsql_dbh, switch_cache_db_handle::pool, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SQL_CACHE_TIMEOUT, sql_manager, SWITCH_CHANNEL_LOG, switch_cond_next(), switch_core_db_close(), switch_core_destroy_memory_pool, SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG10, switch_log_printf(), switch_mutex_lock(), switch_mutex_trylock(), switch_mutex_unlock(), switch_odbc_handle_destroy(), switch_pgsql_handle_destroy(), SWITCH_STATUS_SUCCESS, switch_test_flag, switch_cache_db_handle::type, and switch_cache_db_handle::use_count.

Referenced by switch_cache_db_flush_handles(), switch_core_sql_db_thread(), and switch_core_sqldb_stop().

213 {
215  int locked = 0;
216  int sanity = 10000;
217 
218  switch_mutex_lock(sql_manager.dbh_mutex);
219  top:
220  locked = 0;
221 
222  for (dbh = sql_manager.handle_pool; dbh; dbh = dbh->next) {
223  time_t diff = 0;
224 
225  if (prune > 0 && prune > dbh->last_used) {
226  diff = (time_t) prune - dbh->last_used;
227  }
228 
229  if (prune > 0 && (dbh->use_count || (diff < SQL_CACHE_TIMEOUT && !switch_test_flag(dbh, CDF_PRUNE)))) {
230  continue;
231  }
232 
234  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Dropping idle DB connection %s\n", dbh->name);
235 
236  switch (dbh->type) {
237  case SCDB_TYPE_PGSQL:
238  {
240  }
241  break;
242  case SCDB_TYPE_ODBC:
243  {
245  }
246  break;
247  case SCDB_TYPE_CORE_DB:
248  {
250  dbh->native_handle.core_db_dbh = NULL;
251  }
252  break;
253  }
254 
255  del_handle(dbh);
258  goto top;
259 
260  } else {
261  if (!prune) {
262  if (!sanity) {
263  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SANITY CHECK FAILED! Handle %s (%s;%s) was not properly released.\n",
264  dbh->name, dbh->creator, dbh->last_user);
265  } else {
266  locked++;
267  }
268  }
269  continue;
270  }
271 
272  }
273 
274  if (locked) {
275  if (!prune) {
277  if (sanity) sanity--;
278  }
279  goto top;
280  }
281 
282  switch_mutex_unlock(sql_manager.dbh_mutex);
283 }
switch_cache_db_native_handle_t native_handle
char name[CACHE_DB_LEN]
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_trylock(switch_mutex_t *lock)
Definition: switch_apr.c:295
static void del_handle(switch_cache_db_handle_t *dbh)
switch_cache_db_handle_type_t type
void switch_odbc_handle_destroy(switch_odbc_handle_t **handlep)
Definition: switch_odbc.c:671
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
#define SQL_CACHE_TIMEOUT
int switch_core_db_close(switch_core_db_t *db)
switch_memory_pool_t * pool
char creator[CACHE_DB_LEN]
char last_user[CACHE_DB_LEN]
switch_mutex_t * mutex
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
struct switch_cache_db_handle * next
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
void switch_cond_next(void)
Definition: switch_time.c:638
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_cache_db_handle_t * dbh
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
void switch_pgsql_handle_destroy(switch_pgsql_handle_t **handlep)
Definition: switch_pgsql.c:764
static struct @3 sql_manager
static void* SWITCH_THREAD_FUNC sql_in_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 1480 of file switch_core_sqldb.c.

References db_job::callback, dbh, switch_sql_queue_manager::dsn, db_job::err_callback, db_job::event_callback, db_job::event_err_callback, db_job::pdata, pool, db_job::pool, db_job::qm, db_job::sql, switch_cache_db_execute_sql_callback(), switch_cache_db_execute_sql_callback_err(), switch_cache_db_execute_sql_event_callback(), switch_cache_db_execute_sql_event_callback_err(), switch_cache_db_get_db_handle_dsn, switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, switch_core_destroy_memory_pool, SWITCH_LOG_ERROR, switch_log_printf(), switch_safe_free, and SWITCH_STATUS_SUCCESS.

Referenced by new_job().

1481 {
1482  struct db_job *job = (struct db_job *) obj;
1483  switch_memory_pool_t *pool = job->pool;
1484  char *err = NULL;
1486 
1487 
1489  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot connect DSN %s\n", job->qm->dsn);
1490  return NULL;
1491  }
1492 
1493  if (job->callback && !job->err_callback) {
1494  switch_cache_db_execute_sql_callback(dbh, job->sql, job->callback, job->pdata, &err);
1495  } else if (job->callback && job->err_callback) {
1496  switch_cache_db_execute_sql_callback_err(dbh, job->sql, job->callback, job->err_callback, job->pdata, &err);
1497  } else if (job->event_callback && !job->event_err_callback) {
1499  } else if (job->event_callback && job->event_err_callback) {
1501  }
1502 
1503  if (err) {
1504  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", job->sql, err);
1505  switch_safe_free(err);
1506  }
1507 
1509 
1510  if (pool) {
1512  }
1513 
1514  return NULL;
1515 }
#define SWITCH_CHANNEL_LOG
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
switch_status_t switch_cache_db_execute_sql_event_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_event_callback_func_t callback, void *pdata, char **err)
#define switch_cache_db_get_db_handle_dsn(_a, _b)
Definition: switch_core.h:2536
switch_core_db_err_callback_func_t event_err_callback
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_core_db_err_callback_func_t err_callback
switch_sql_queue_manager_t * qm
switch_status_t switch_cache_db_execute_sql_event_callback_err(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_event_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata, char **err)
switch_cache_db_handle_t * dbh
switch_memory_pool_t * pool
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
switch_core_db_callback_func_t callback
struct apr_pool_t switch_memory_pool_t
switch_status_t switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_core_db_event_callback_func_t event_callback
switch_status_t switch_cache_db_execute_sql_callback_err(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
int switch_cache_db_affected_rows ( switch_cache_db_handle_t dbh)

Get the affected rows of the last performed query.

Parameters
[in]dbhThe handle
[out]thenumber of affected rows

Definition at line 688 of file switch_core_sqldb.c.

References SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, switch_core_db_changes(), switch_odbc_handle_affected_rows(), and switch_pgsql_handle_affected_rows().

689 {
690  switch (dbh->type) {
691  case SCDB_TYPE_CORE_DB:
692  {
694  }
695  break;
696  case SCDB_TYPE_ODBC:
697  {
699  }
700  break;
701  case SCDB_TYPE_PGSQL:
702  {
704  }
705  break;
706  }
707  return 0;
708 }
switch_cache_db_native_handle_t native_handle
switch_cache_db_handle_type_t type
int switch_odbc_handle_affected_rows(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:729
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
int switch_pgsql_handle_affected_rows(switch_pgsql_handle_t *handle)
Definition: switch_pgsql.c:810
int switch_core_db_changes(switch_core_db_t *db)
switch_status_t switch_cache_db_create_schema ( switch_cache_db_handle_t dbh,
char *  sql,
char **  err 
)

Executes the create schema sql.

Parameters
[in]dbhThe handle
[in]sql- sql to run
[out]err- Error if it exists

Definition at line 1271 of file switch_core_sqldb.c.

References runtime, SCF_AUTO_SCHEMAS, switch_assert, switch_cache_db_execute_sql(), SWITCH_STATUS_SUCCESS, and switch_test_flag.

Referenced by switch_core_sqldb_start().

1272 {
1274 
1275  switch_assert(sql != NULL);
1276 
1278  r = switch_cache_db_execute_sql(dbh, sql, err);
1279  }
1280 
1281  return r;
1282 }
struct switch_runtime runtime
Definition: switch_core.c:64
switch_status_t
Common return values.
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
#define switch_assert(expr)
void switch_cache_db_dismiss_db_handle ( switch_cache_db_handle_t **  dbh)

Returns the handle to the pool, immediately available for other threads to use.

Parameters
[in]Thehandle

Definition at line 329 of file switch_core_sqldb.c.

References switch_cache_db_release_db_handle().

330 {
332 }
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
switch_status_t switch_cache_db_execute_sql ( switch_cache_db_handle_t dbh,
char *  sql,
char **  err 
)

Executes the sql.

Parameters
[in]dbhThe handle
[in]sql- sql to run
[out]err- Error if it exists

Definition at line 666 of file switch_core_sqldb.c.

References io_mutex, switch_cache_db_execute_sql_chunked(), switch_mutex_lock(), switch_mutex_unlock(), and SWITCH_STATUS_FALSE.

Referenced by do_flush(), do_trans(), switch_cache_db_create_schema(), switch_cache_db_persistant_execute_trans_full(), switch_cache_db_test_reactive(), switch_core_recovery_flush(), switch_core_recovery_recover(), switch_core_sqldb_start(), switch_sql_queue_manager_push_confirm(), and switch_user_sql_thread().

667 {
670 
671  if (io_mutex) switch_mutex_lock(io_mutex);
672 
673  switch (dbh->type) {
674  default:
675  {
676  status = switch_cache_db_execute_sql_chunked(dbh, (char *) sql, 32768, err);
677  }
678  break;
679  }
680 
681  if (io_mutex) switch_mutex_unlock(io_mutex);
682 
683  return status;
684 
685 }
switch_mutex_t * io_mutex
switch_cache_db_handle_type_t type
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_mutex_t * io_mutex
static switch_status_t switch_cache_db_execute_sql_chunked(switch_cache_db_handle_t *dbh, char *sql, uint32_t chunk_size, char **err)
char* switch_cache_db_execute_sql2str ( switch_cache_db_handle_t dbh,
char *  sql,
char *  str,
size_t  len,
char **  err 
)

Executes the sql and returns the result as a string.

Parameters
[in]dbhThe handle
[in]sql- sql to run
[out]str- buffer for result
[in]len- length of str buffer
[out]err- Error if it exists

Definition at line 734 of file switch_core_sqldb.c.

References io_mutex, memset(), running, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SWITCH_CHANNEL_LOG, switch_cond_next(), switch_copy_string(), SWITCH_CORE_DB_BUSY, switch_core_db_column_count(), switch_core_db_column_text(), switch_core_db_finalize(), switch_core_db_prepare(), SWITCH_CORE_DB_ROW, switch_core_db_step(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_exec_string(), switch_pgsql_handle_exec_string, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

735 {
738 
739  if (io_mutex) switch_mutex_lock(io_mutex);
740 
741  memset(str, 0, len);
742 
743  switch (dbh->type) {
744  case SCDB_TYPE_CORE_DB:
745  {
746  switch_core_db_stmt_t *stmt;
747 
748  if (switch_core_db_prepare(dbh->native_handle.core_db_dbh, sql, -1, &stmt, 0)) {
749  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
750  goto end;
751  } else {
752  int running = 1;
753  int colcount;
754 
755  while (running < 5000) {
756  int result = switch_core_db_step(stmt);
757  const unsigned char *txt;
758 
759  if (result == SWITCH_CORE_DB_ROW) {
760  if ((colcount = switch_core_db_column_count(stmt)) > 0) {
761  if ((txt = switch_core_db_column_text(stmt, 0))) {
762  switch_copy_string(str, (char *) txt, len);
763  status = SWITCH_STATUS_SUCCESS;
764  } else {
765  goto end;
766  }
767  }
768  break;
769  } else if (result == SWITCH_CORE_DB_BUSY) {
770  running++;
772  continue;
773  }
774  break;
775  }
776 
778  }
779  }
780  break;
781  case SCDB_TYPE_ODBC:
782  {
783  status = switch_odbc_handle_exec_string(dbh->native_handle.odbc_dbh, sql, str, len, err);
784  }
785  break;
786  case SCDB_TYPE_PGSQL:
787  {
788  status = switch_pgsql_handle_exec_string(dbh->native_handle.pgsql_dbh, sql, str, len, err);
789  }
790  break;
791  }
792 
793  end:
794 
795  if (io_mutex) switch_mutex_unlock(io_mutex);
796 
797  return status == SWITCH_STATUS_SUCCESS ? str : NULL;
798 
799 }
switch_cache_db_native_handle_t native_handle
switch_odbc_status_t switch_odbc_handle_exec_string(switch_odbc_handle_t *handle, const char *sql, char *resbuf, size_t len, char **err)
Definition: switch_odbc.c:405
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
#define SWITCH_CORE_DB_BUSY
int switch_core_db_finalize(switch_core_db_stmt_t *pStmt)
int switch_core_db_step(switch_core_db_stmt_t *stmt)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_pgsql_handle_exec_string(handle, sql, resbuf, len, err)
Definition: switch_pgsql.h:121
void switch_cond_next(void)
Definition: switch_time.c:638
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
static int32_t running
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
struct sqlite3_stmt switch_core_db_stmt_t
#define SWITCH_CORE_DB_ROW
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
int switch_core_db_column_count(switch_core_db_stmt_t *pStmt)
const unsigned char * switch_core_db_column_text(switch_core_db_stmt_t *stmt, int iCol)
switch_mutex_t * io_mutex
memset(buf, 0, buflen)
int switch_core_db_prepare(switch_core_db_t *db, const char *zSql, int nBytes, switch_core_db_stmt_t **ppStmt, const char **pzTail)
switch_status_t switch_cache_db_execute_sql_callback ( switch_cache_db_handle_t dbh,
const char *  sql,
switch_core_db_callback_func_t  callback,
void *  pdata,
char **  err 
)

Executes the sql and uses callback for row-by-row processing.

Parameters
[in]dbhThe handle
[in]sql- sql to run
[in]callback- function pointer to callback
[in]pdata- data to pass to callback
[out]err- Error if it exists

Definition at line 1161 of file switch_core_sqldb.c.

References io_mutex, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SQL_CACHE_TIMEOUT, SWITCH_CHANNEL_LOG, SWITCH_CORE_DB_ABORT, switch_core_db_exec(), switch_core_db_free(), SWITCH_CORE_DB_OK, switch_epoch_time_now(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_callback_exec, switch_pgsql_handle_callback_exec, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by sql_in_thread(), switch_console_complete(), switch_console_expand_alias(), switch_console_list_uuid(), and switch_core_recovery_recover().

1163 {
1165  char *errmsg = NULL;
1167 
1168  if (err) {
1169  *err = NULL;
1170  }
1171 
1172  if (io_mutex) switch_mutex_lock(io_mutex);
1173 
1174 
1175  switch (dbh->type) {
1176  case SCDB_TYPE_PGSQL:
1177  {
1179  }
1180  break;
1181  case SCDB_TYPE_ODBC:
1182  {
1184  }
1185  break;
1186  case SCDB_TYPE_CORE_DB:
1187  {
1188  int ret = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, callback, pdata, &errmsg);
1189 
1190  if (ret == SWITCH_CORE_DB_OK || ret == SWITCH_CORE_DB_ABORT) {
1191  status = SWITCH_STATUS_SUCCESS;
1192  }
1193 
1194  if (errmsg) {
1195  dbh->last_used = switch_epoch_time_now(NULL) - (SQL_CACHE_TIMEOUT * 2);
1196  if (!strstr(errmsg, "query abort")) {
1197  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
1198  }
1199  switch_core_db_free(errmsg);
1200  }
1201  }
1202  break;
1203  }
1204 
1205  if (io_mutex) switch_mutex_unlock(io_mutex);
1206 
1207  return status;
1208 }
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
#define SQL_CACHE_TIMEOUT
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define SWITCH_CORE_DB_OK
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
#define switch_odbc_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_odbc.h:92
#define switch_pgsql_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_pgsql.h:151
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
switch_core_db_callback_func_t callback
#define SWITCH_CORE_DB_ABORT
void switch_core_db_free(char *z)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_mutex_t * io_mutex
switch_status_t switch_cache_db_execute_sql_callback_err ( switch_cache_db_handle_t dbh,
const char *  sql,
switch_core_db_callback_func_t  callback,
switch_core_db_err_callback_func_t  err_callback,
void *  pdata,
char **  err 
)

Executes the sql and uses callback for row-by-row processing.

Parameters
[in]dbhThe handle
[in]sql- sql to run
[in]callback- function pointer to callback
[in]err_callback- function pointer to callback when error occurs
[in]pdata- data to pass to callback
[out]err- Error if it exists

Definition at line 1210 of file switch_core_sqldb.c.

References io_mutex, helper::pdata, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SQL_CACHE_TIMEOUT, SWITCH_CHANNEL_LOG, SWITCH_CORE_DB_ABORT, switch_core_db_exec(), switch_core_db_free(), SWITCH_CORE_DB_OK, switch_epoch_time_now(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_callback_exec, switch_pgsql_handle_callback_exec, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by sql_in_thread().

1213 {
1215  char *errmsg = NULL;
1217 
1218  if (err) {
1219  *err = NULL;
1220  }
1221 
1222  if (io_mutex) switch_mutex_lock(io_mutex);
1223 
1224 
1225  switch (dbh->type) {
1226  case SCDB_TYPE_PGSQL:
1227  {
1229  if (err && *err) {
1230  (*err_callback)(pdata, (const char*)*err);
1231  }
1232  }
1233  break;
1234  case SCDB_TYPE_ODBC:
1235  {
1237  if (err && *err) {
1238  (*err_callback)(pdata, (const char*)*err);
1239  }
1240  }
1241  break;
1242  case SCDB_TYPE_CORE_DB:
1243  {
1244  int ret = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, callback, pdata, &errmsg);
1245 
1246  if (ret == SWITCH_CORE_DB_OK || ret == SWITCH_CORE_DB_ABORT) {
1247  status = SWITCH_STATUS_SUCCESS;
1248  }
1249 
1250  if (errmsg) {
1251  dbh->last_used = switch_epoch_time_now(NULL) - (SQL_CACHE_TIMEOUT * 2);
1252  if (!strstr(errmsg, "query abort")) {
1253  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
1254  }
1255  }
1256  if ((ret == SWITCH_CORE_DB_ABORT || errmsg) && err_callback) {
1257  (*err_callback)(pdata, errmsg);
1258  }
1259  if (errmsg) {
1260  switch_core_db_free(errmsg);
1261  }
1262  }
1263  break;
1264  }
1265 
1266  if (io_mutex) switch_mutex_unlock(io_mutex);
1267 
1268  return status;
1269 }
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
#define SQL_CACHE_TIMEOUT
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define SWITCH_CORE_DB_OK
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_core_db_err_callback_func_t err_callback
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
#define switch_odbc_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_odbc.h:92
#define switch_pgsql_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_pgsql.h:151
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
switch_core_db_callback_func_t callback
#define SWITCH_CORE_DB_ABORT
void switch_core_db_free(char *z)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_mutex_t * io_mutex
static switch_status_t switch_cache_db_execute_sql_chunked ( switch_cache_db_handle_t dbh,
char *  sql,
uint32_t  chunk_size,
char **  err 
)
static

OMFG you cruel bastards. Who chooses 64k as a max buffer len for a sql statement, have you ever heard of transactions?

Definition at line 605 of file switch_core_sqldb.c.

References end_of_p, switch_assert, switch_cache_db_execute_sql_real(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_cache_db_execute_sql().

606 {
608  char *p, *s, *e;
609  switch_size_t chunk_count;
610  switch_size_t len;
611 
612  switch_assert(chunk_size);
613 
614  if (err)
615  *err = NULL;
616 
617  len = strlen(sql);
618 
619  if (chunk_size > len) {
620  return switch_cache_db_execute_sql_real(dbh, sql, err);
621  }
622 
623  if (!(chunk_count = strlen(sql) / chunk_size)) {
624  return SWITCH_STATUS_FALSE;
625  }
626 
627  e = end_of_p(sql);
628  s = sql;
629 
630  while (s && s < e) {
631  p = s + chunk_size;
632 
633  if (p > e) {
634  p = e;
635  }
636 
637  while (p > s) {
638  if (*p == '\n' && *(p - 1) == ';') {
639  *p = '\0';
640  *(p - 1) = '\0';
641  p++;
642  break;
643  }
644 
645  p--;
646  }
647 
648  if (p <= s)
649  break;
650 
651 
652  status = switch_cache_db_execute_sql_real(dbh, s, err);
653  if (status != SWITCH_STATUS_SUCCESS || (err && *err)) {
654  break;
655  }
656 
657  s = p;
658 
659  }
660 
661  return status;
662 
663 }
#define end_of_p(_s)
Definition: switch_utils.h:617
uintptr_t switch_size_t
switch_status_t
Common return values.
static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t *dbh, const char *sql, char **err)
#define switch_assert(expr)
switch_status_t switch_cache_db_execute_sql_event_callback ( switch_cache_db_handle_t dbh,
const char *  sql,
switch_core_db_event_callback_func_t  callback,
void *  pdata,
char **  err 
)

Definition at line 1042 of file switch_core_sqldb.c.

References helper::callback, helper_callback(), io_mutex, helper::pdata, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SQL_CACHE_TIMEOUT, SWITCH_CHANNEL_LOG, SWITCH_CORE_DB_ABORT, switch_core_db_exec(), switch_core_db_free(), SWITCH_CORE_DB_OK, switch_epoch_time_now(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_callback_exec, switch_pgsql_handle_callback_exec, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by sql_in_thread().

1044 {
1046  char *errmsg = NULL;
1048  struct helper h = {0};
1049 
1050 
1051  if (err) {
1052  *err = NULL;
1053  }
1054 
1055  if (io_mutex) switch_mutex_lock(io_mutex);
1056 
1057  h.callback = callback;
1058  h.pdata = pdata;
1059 
1060  switch (dbh->type) {
1061  case SCDB_TYPE_PGSQL:
1062  {
1064  }
1065  break;
1066  case SCDB_TYPE_ODBC:
1067  {
1069  }
1070  break;
1071  case SCDB_TYPE_CORE_DB:
1072  {
1073  int ret = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, helper_callback, &h, &errmsg);
1074 
1075  if (ret == SWITCH_CORE_DB_OK || ret == SWITCH_CORE_DB_ABORT) {
1076  status = SWITCH_STATUS_SUCCESS;
1077  }
1078 
1079  if (errmsg) {
1080  dbh->last_used = switch_epoch_time_now(NULL) - (SQL_CACHE_TIMEOUT * 2);
1081  if (!strstr(errmsg, "query abort")) {
1082  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
1083  }
1084  switch_core_db_free(errmsg);
1085  }
1086  }
1087  break;
1088  }
1089 
1090  if (io_mutex) switch_mutex_unlock(io_mutex);
1091 
1092  return status;
1093 }
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
switch_core_db_event_callback_func_t callback
#define SQL_CACHE_TIMEOUT
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define SWITCH_CORE_DB_OK
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
#define switch_odbc_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_odbc.h:92
#define switch_pgsql_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_pgsql.h:151
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
static int helper_callback(void *pArg, int argc, char **argv, char **columnNames)
#define SWITCH_CORE_DB_ABORT
void switch_core_db_free(char *z)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_mutex_t * io_mutex
switch_status_t switch_cache_db_execute_sql_event_callback_err ( switch_cache_db_handle_t dbh,
const char *  sql,
switch_core_db_event_callback_func_t  callback,
switch_core_db_err_callback_func_t  err_callback,
void *  pdata,
char **  err 
)

Definition at line 1095 of file switch_core_sqldb.c.

References helper::callback, helper_callback(), io_mutex, helper::pdata, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SQL_CACHE_TIMEOUT, SWITCH_CHANNEL_LOG, SWITCH_CORE_DB_ABORT, switch_core_db_exec(), switch_core_db_free(), SWITCH_CORE_DB_OK, switch_epoch_time_now(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_callback_exec, switch_pgsql_handle_callback_exec, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by sql_in_thread().

1099 {
1101  char *errmsg = NULL;
1103  struct helper h;
1104 
1105 
1106  if (err) {
1107  *err = NULL;
1108  }
1109 
1110  if (io_mutex) switch_mutex_lock(io_mutex);
1111 
1112  h.callback = callback;
1113  h.pdata = pdata;
1114 
1115  switch (dbh->type) {
1116  case SCDB_TYPE_PGSQL:
1117  {
1119  if (err && *err) {
1120  (*err_callback)(pdata, (const char*)*err);
1121  }
1122  }
1123  break;
1124  case SCDB_TYPE_ODBC:
1125  {
1127  if (err && *err) {
1128  (*err_callback)(pdata, (const char*)*err);
1129  }
1130  }
1131  break;
1132  case SCDB_TYPE_CORE_DB:
1133  {
1134  int ret = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, helper_callback, &h, &errmsg);
1135 
1136  if (ret == SWITCH_CORE_DB_OK || ret == SWITCH_CORE_DB_ABORT) {
1137  status = SWITCH_STATUS_SUCCESS;
1138  }
1139 
1140  if (errmsg) {
1141  dbh->last_used = switch_epoch_time_now(NULL) - (SQL_CACHE_TIMEOUT * 2);
1142  if (!strstr(errmsg, "query abort")) {
1143  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
1144  }
1145  }
1146  if ((ret == SWITCH_CORE_DB_ABORT || errmsg) && err_callback) {
1147  (*err_callback)(pdata, errmsg);
1148  }
1149  if (errmsg) {
1150  switch_core_db_free(errmsg);
1151  }
1152  }
1153  break;
1154  }
1155 
1156  if (io_mutex) switch_mutex_unlock(io_mutex);
1157 
1158  return status;
1159 }
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
switch_core_db_event_callback_func_t callback
#define SQL_CACHE_TIMEOUT
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define SWITCH_CORE_DB_OK
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
#define switch_odbc_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_odbc.h:92
#define switch_pgsql_handle_callback_exec(handle,sql,callback, pdata, err)
Execute the sql query and issue a callback for each row returned.
Definition: switch_pgsql.h:151
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
static int helper_callback(void *pArg, int argc, char **argv, char **columnNames)
#define SWITCH_CORE_DB_ABORT
void switch_core_db_free(char *z)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_mutex_t * io_mutex
static switch_status_t switch_cache_db_execute_sql_real ( switch_cache_db_handle_t dbh,
const char *  sql,
char **  err 
)
static

Definition at line 540 of file switch_core_sqldb.c.

References switch_cache_db_native_handle_t::core_db_dbh, switch_cache_db_handle::io_mutex, io_mutex, switch_cache_db_handle::native_handle, switch_cache_db_native_handle_t::odbc_dbh, switch_cache_db_native_handle_t::pgsql_dbh, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SWITCH_CHANNEL_LOG, switch_core_db_exec(), switch_core_db_free(), SWITCH_CORE_DB_OK, SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_exec(), switch_pgsql_handle_exec, switch_safe_free, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_strdup, switch_stristr(), and switch_cache_db_handle::type.

Referenced by do_trans(), switch_cache_db_execute_sql_chunked(), switch_cache_db_persistant_execute(), switch_cache_db_persistant_execute_trans_full(), and switch_core_sqldb_start().

541 {
543  char *errmsg = NULL;
544  char *tmp = NULL;
545  char *type = NULL;
547 
548  if (io_mutex) switch_mutex_lock(io_mutex);
549 
550  if (err) {
551  *err = NULL;
552  }
553 
554  switch (dbh->type) {
555  case SCDB_TYPE_PGSQL:
556  {
557  type = "PGSQL";
558  status = switch_pgsql_handle_exec(dbh->native_handle.pgsql_dbh, sql, &errmsg);
559  }
560  break;
561  case SCDB_TYPE_ODBC:
562  {
563  type = "ODBC";
564  status = switch_odbc_handle_exec(dbh->native_handle.odbc_dbh, sql, NULL, &errmsg);
565  }
566  break;
567  case SCDB_TYPE_CORE_DB:
568  {
569  int ret = switch_core_db_exec(dbh->native_handle.core_db_dbh, sql, NULL, NULL, &errmsg);
570  type = "NATIVE";
571 
572  if (ret == SWITCH_CORE_DB_OK) {
573  status = SWITCH_STATUS_SUCCESS;
574  }
575 
576  if (errmsg) {
577  switch_strdup(tmp, errmsg);
578  switch_core_db_free(errmsg);
579  errmsg = tmp;
580  }
581  }
582  break;
583  }
584 
585  if (errmsg) {
586  if (!switch_stristr("already exists", errmsg) && !switch_stristr("duplicate key name", errmsg)) {
587  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s SQL ERR [%s]\n%s\n", (type ? type : "Unknown"), errmsg, sql);
588  }
589  if (err) {
590  *err = errmsg;
591  } else {
592  switch_safe_free(errmsg);
593  }
594  }
595 
596 
597  if (io_mutex) switch_mutex_unlock(io_mutex);
598 
599  return status;
600 }
#define switch_strdup(ptr, s)
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
switch_odbc_status_t switch_odbc_handle_exec(switch_odbc_handle_t *handle, const char *sql, switch_odbc_statement_handle_t *rstmt, char **err)
Definition: switch_odbc.c:449
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define SWITCH_CORE_DB_OK
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_pgsql_handle_exec(handle, sql, err)
Definition: switch_pgsql.h:117
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
void switch_core_db_free(char *z)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
switch_mutex_t * io_mutex
void switch_cache_db_flush_handles ( void  )

Definition at line 291 of file switch_core_sqldb.c.

References SQL_CACHE_TIMEOUT, sql_close(), and switch_epoch_time_now().

Referenced by switch_core_session_ctl(), and switch_core_sqldb_stop().

292 {
294 }
#define SQL_CACHE_TIMEOUT
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
static void sql_close(time_t prune)
switch_cache_db_handle_type_t switch_cache_db_get_type ( switch_cache_db_handle_t dbh)

Definition at line 286 of file switch_core_sqldb.c.

Referenced by switch_console_complete(), switch_console_expand_alias(), and switch_console_set_alias().

287 {
288  return dbh->type;
289 }
switch_cache_db_handle_type_t type
int switch_cache_db_load_extension ( switch_cache_db_handle_t dbh,
const char *  extension 
)

load an external extension to db

Parameters
[in]dbhThe handle
[out]thepath to the extension

Definition at line 710 of file switch_core_sqldb.c.

References SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SWITCH_CHANNEL_LOG, switch_core_db_load_extension(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, and switch_log_printf().

711 {
712  switch (dbh->type) {
713  case SCDB_TYPE_CORE_DB:
714  {
715  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "try to load extension [%s]!\n", extension);
717  }
718  break;
719  case SCDB_TYPE_ODBC:
720  {
721  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "load extension not supported by type ODBC!\n");
722  }
723  break;
724  case SCDB_TYPE_PGSQL:
725  {
726  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "load extension not supported by type PGSQL!\n");
727  }
728  break;
729  }
730  return 0;
731 }
switch_cache_db_native_handle_t native_handle
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
int switch_core_db_load_extension(switch_core_db_t *db, const char *extension)
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_persistant_execute ( switch_cache_db_handle_t dbh,
const char *  sql,
uint32_t  retries 
)

Definition at line 801 of file switch_core_sqldb.c.

References io_mutex, switch_cache_db_execute_sql_real(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_safe_free, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_yield.

Referenced by switch_console_set_alias().

802 {
803  char *errmsg = NULL;
805  uint8_t forever = 0;
807 
808  if (!retries) {
809  forever = 1;
810  retries = 1000;
811  }
812 
813  while (retries > 0) {
814 
815  if (io_mutex) switch_mutex_lock(io_mutex);
816  switch_cache_db_execute_sql_real(dbh, sql, &errmsg);
817  if (io_mutex) switch_mutex_unlock(io_mutex);
818 
819 
820  if (errmsg) {
821  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
822  switch_safe_free(errmsg);
823  switch_yield(100000);
824  retries--;
825  if (retries == 0 && forever) {
826  retries = 1000;
827  continue;
828  }
829  } else {
830  status = SWITCH_STATUS_SUCCESS;
831  break;
832  }
833  }
834 
835  return status;
836 }
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t *dbh, const char *sql, char **err)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_mutex_t * io_mutex
switch_status_t switch_cache_db_persistant_execute_trans_full ( switch_cache_db_handle_t dbh,
char *  sql,
uint32_t  retries,
const char *  pre_trans_execute,
const char *  post_trans_execute,
const char *  inner_pre_trans_execute,
const char *  inner_post_trans_execute 
)

Definition at line 839 of file switch_core_sqldb.c.

References io_mutex, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, switch_cache_db_execute_sql(), switch_cache_db_execute_sql_real(), SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_SQLEndTran(), switch_odbc_SQLSetAutoCommitAttr(), SWITCH_ODBC_SUCCESS, switch_pgsql_finish_results, switch_pgsql_SQLEndTran(), switch_pgsql_SQLSetAutoCommitAttr(), SWITCH_PGSQL_SUCCESS, switch_safe_free, switch_snprintfv(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_yield, and zstr.

845 {
846  char *errmsg = NULL;
848  uint8_t forever = 0;
849  unsigned begin_retries = 100;
850  uint8_t again = 0;
852 
853  if (!retries) {
854  forever = 1;
855  retries = 1000;
856  }
857 
858  if (io_mutex) switch_mutex_lock(io_mutex);
859 
860  if (!zstr(pre_trans_execute)) {
861  switch_cache_db_execute_sql_real(dbh, pre_trans_execute, &errmsg);
862  if (errmsg) {
863  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL PRE TRANS EXEC %s [%s]\n", pre_trans_execute, errmsg);
864  switch_safe_free(errmsg);
865  }
866  }
867 
868  again:
869 
870  while (begin_retries > 0) {
871  again = 0;
872 
873  switch(dbh->type) {
874  case SCDB_TYPE_CORE_DB:
875  {
876  switch_cache_db_execute_sql_real(dbh, "BEGIN EXCLUSIVE", &errmsg);
877  }
878  break;
879  case SCDB_TYPE_ODBC:
880  {
881  switch_odbc_status_t result;
882 
884  char tmp[100];
885  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
886  errmsg = strdup(tmp);
887  }
888  }
889  break;
890  case SCDB_TYPE_PGSQL:
891  {
892  switch_pgsql_status_t result;
893 
895  char tmp[100];
896  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
897  errmsg = strdup(tmp);
898  }
899  }
900  break;
901  }
902 
903  if (errmsg) {
904  begin_retries--;
905  if (strstr(errmsg, "cannot start a transaction within a transaction")) {
906  again = 1;
907  } else {
908  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL Retry [%s]\n", errmsg);
909  }
910  switch_safe_free(errmsg);
911 
912  if (again) {
913  switch(dbh->type) {
914  case SCDB_TYPE_CORE_DB:
915  {
916  switch_cache_db_execute_sql_real(dbh, "COMMIT", NULL);
917  }
918  break;
919  case SCDB_TYPE_ODBC:
920  {
923  }
924  break;
925  case SCDB_TYPE_PGSQL:
926  {
930  }
931  break;
932  }
933 
934  goto again;
935  }
936 
937  switch_yield(100000);
938 
939  if (begin_retries == 0) {
940  goto done;
941  }
942 
943  continue;
944  }
945 
946  break;
947  }
948 
949 
950  if (!zstr(inner_pre_trans_execute)) {
951  switch_cache_db_execute_sql_real(dbh, inner_pre_trans_execute, &errmsg);
952  if (errmsg) {
953  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL PRE TRANS EXEC %s [%s]\n", inner_pre_trans_execute, errmsg);
954  switch_safe_free(errmsg);
955  }
956  }
957 
958  while (retries > 0) {
959 
960  switch_cache_db_execute_sql(dbh, sql, &errmsg);
961 
962  if (errmsg) {
963  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
964  switch_safe_free(errmsg);
965  errmsg = NULL;
966  switch_yield(100000);
967  retries--;
968  if (retries == 0 && forever) {
969  retries = 1000;
970  continue;
971  }
972  } else {
973  status = SWITCH_STATUS_SUCCESS;
974  break;
975  }
976  }
977 
978  if (!zstr(inner_post_trans_execute)) {
979  switch_cache_db_execute_sql_real(dbh, inner_post_trans_execute, &errmsg);
980  if (errmsg) {
981  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL POST TRANS EXEC %s [%s]\n", inner_post_trans_execute, errmsg);
982  switch_safe_free(errmsg);
983  }
984  }
985 
986  done:
987 
988  switch(dbh->type) {
989  case SCDB_TYPE_CORE_DB:
990  {
991  switch_cache_db_execute_sql_real(dbh, "COMMIT", NULL);
992  }
993  break;
994  case SCDB_TYPE_ODBC:
995  {
998  }
999  break;
1000  case SCDB_TYPE_PGSQL:
1001  {
1005  }
1006  break;
1007  }
1008 
1009  if (!zstr(post_trans_execute)) {
1010  switch_cache_db_execute_sql_real(dbh, post_trans_execute, &errmsg);
1011  if (errmsg) {
1012  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL POST TRANS EXEC %s [%s]\n", post_trans_execute, errmsg);
1013  switch_safe_free(errmsg);
1014  }
1015  }
1016 
1017  if (io_mutex) switch_mutex_unlock(io_mutex);
1018 
1019  return status;
1020 }
switch_pgsql_status_t
Definition: switch_pgsql.h:53
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
switch_odbc_status_t
Definition: switch_odbc.h:49
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
switch_pgsql_status_t switch_pgsql_SQLEndTran(switch_pgsql_handle_t *handle, switch_bool_t commit)
Definition: switch_pgsql.c:865
switch_pgsql_status_t switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on)
Definition: switch_pgsql.c:828
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_pgsql_finish_results(handle)
Definition: switch_pgsql.h:107
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t *dbh, const char *sql, char **err)
switch_odbc_status_t switch_odbc_SQLEndTran(switch_odbc_handle_t *handle, switch_bool_t commit)
Definition: switch_odbc.c:760
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
switch_mutex_t * io_mutex
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr(switch_odbc_handle_t *handle, switch_bool_t on)
Definition: switch_odbc.c:747
void switch_cache_db_release_db_handle ( switch_cache_db_handle_t **  dbh)

Returns the handle to the pool, handle is NOT available to other threads until the allocating thread actually terminates.

Parameters
[in]Thehandle

Definition at line 297 of file switch_core_sqldb.c.

References SCDB_TYPE_PGSQL, sql_manager, switch_epoch_time_now(), switch_mutex_lock(), switch_mutex_unlock(), and switch_pgsql_flush().

Referenced by sql_in_thread(), switch_cache_db_dismiss_db_handle(), switch_console_complete(), switch_console_expand_alias(), switch_console_list_uuid(), switch_console_set_alias(), switch_core_recovery_flush(), switch_core_recovery_recover(), switch_core_sqldb_start(), switch_sql_queue_manager_push_confirm(), and switch_user_sql_thread().

298 {
299  if (dbh && *dbh) {
300 
301  switch((*dbh)->type) {
302  case SCDB_TYPE_PGSQL:
303  {
304  switch_pgsql_flush((*dbh)->native_handle.pgsql_dbh);
305  }
306  break;
307  default:
308  break;
309  }
310 
311  switch_mutex_lock(sql_manager.dbh_mutex);
312  (*dbh)->last_used = switch_epoch_time_now(NULL);
313 
314  (*dbh)->io_mutex = NULL;
315 
316  if ((*dbh)->use_count) {
317  if (--(*dbh)->use_count == 0) {
318  (*dbh)->thread_hash = 1;
319  }
320  }
321  switch_mutex_unlock((*dbh)->mutex);
322  sql_manager.total_used_handles--;
323  *dbh = NULL;
324  switch_mutex_unlock(sql_manager.dbh_mutex);
325  }
326 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_pgsql_status_t switch_pgsql_flush(switch_pgsql_handle_t *handle)
Definition: switch_pgsql.c:842
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
static struct @3 sql_manager
void switch_cache_db_status ( switch_stream_handle_t stream)

Provides some feedback as to the status of the db connection pool.

Parameters
[in]streamstream for status

Definition at line 3716 of file switch_core_sqldb.c.

References CACHE_DB_LEN, switch_cache_db_handle::creator, dbh, switch_cache_db_handle::last_used, switch_cache_db_handle::last_user, memset(), switch_cache_db_handle::mutex, switch_cache_db_handle::name, switch_cache_db_handle::next, sql_manager, switch_cache_db_type_name(), switch_epoch_time_now(), SWITCH_FALSE, switch_mutex_lock(), switch_mutex_trylock(), switch_mutex_unlock(), SWITCH_STATUS_SUCCESS, SWITCH_TRUE, switch_cache_db_handle::total_used_count, switch_cache_db_handle::type, and switch_cache_db_handle::use_count.

3717 {
3718  /* return some status info suitable for the cli */
3719  switch_cache_db_handle_t *dbh = NULL;
3720  switch_bool_t locked = SWITCH_FALSE;
3721  time_t now = switch_epoch_time_now(NULL);
3722  char cleankey_str[CACHE_DB_LEN];
3723  char *pos1 = NULL;
3724  char *pos2 = NULL;
3725  int count = 0, used = 0;
3726 
3727  switch_mutex_lock(sql_manager.dbh_mutex);
3728 
3729  for (dbh = sql_manager.handle_pool; dbh; dbh = dbh->next) {
3730  char *needles[3];
3731  time_t diff = 0;
3732  int i = 0;
3733 
3734  needles[0] = "pass=\"";
3735  needles[1] = "password=";
3736  needles[2] = "password='";
3737 
3738  diff = now - dbh->last_used;
3739 
3741  switch_mutex_unlock(dbh->mutex);
3742  locked = SWITCH_FALSE;
3743  } else {
3744  locked = SWITCH_TRUE;
3745  }
3746 
3747  /* sanitize password */
3748  memset(cleankey_str, 0, sizeof(cleankey_str));
3749  for (i = 0; i < 3; i++) {
3750  if((pos1 = strstr(dbh->name, needles[i]))) {
3751  pos1 += strlen(needles[i]);
3752 
3753  if (!(pos2 = strstr(pos1, "\""))) {
3754  if (!(pos2 = strstr(pos1, "'"))) {
3755  if (!(pos2 = strstr(pos1, " "))) {
3756  pos2 = pos1 + strlen(pos1);
3757  }
3758  }
3759  }
3760  strncpy(cleankey_str, dbh->name, pos1 - dbh->name);
3761  strcpy(&cleankey_str[pos1 - dbh->name], pos2);
3762  break;
3763  }
3764  }
3765  if (i == 3) {
3766  strncpy(cleankey_str, dbh->name, strlen(dbh->name));
3767  }
3768 
3769  count++;
3770 
3771  if (dbh->use_count) {
3772  used++;
3773  }
3774 
3775  stream->write_function(stream, "%s\n\tType: %s\n\tLast used: %d\n\tTotal used: %ld\n\tFlags: %s, %s(%d)\n"
3776  "\tCreator: %s\n\tLast User: %s\n",
3777  cleankey_str,
3779  diff,
3780  dbh->total_used_count,
3781  locked ? "Locked" : "Unlocked",
3782  dbh->use_count ? "Attached" : "Detached", dbh->use_count, dbh->creator, dbh->last_user);
3783  }
3784 
3785  stream->write_function(stream, "%d total. %d in use.\n", count, used);
3786 
3787  switch_mutex_unlock(sql_manager.dbh_mutex);
3788 }
char name[CACHE_DB_LEN]
switch_status_t switch_mutex_trylock(switch_mutex_t *lock)
Definition: switch_apr.c:295
switch_cache_db_handle_type_t type
switch_bool_t
Definition: switch_types.h:405
static const char * switch_cache_db_type_name(switch_cache_db_handle_type_t type)
Definition: switch_core.h:2481
char creator[CACHE_DB_LEN]
char last_user[CACHE_DB_LEN]
switch_mutex_t * mutex
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
struct switch_cache_db_handle * next
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define CACHE_DB_LEN
Definition: switch_core.h:2440
switch_cache_db_handle_t * dbh
switch_stream_handle_write_function_t write_function
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
memset(buf, 0, buflen)
static struct @3 sql_manager
switch_bool_t switch_cache_db_test_reactive ( switch_cache_db_handle_t dbh,
const char *  test_sql,
const char *  drop_sql,
const char *  reactive_sql 
)

Performs test_sql and if it fails performs drop_sql and reactive_sql.

If auto-clear-sql is disabled, then this function will do nothing and it is assumed that the queries are not needed. If auto-create-schemas is disabled, then just test_sql is executed, but drop_sql and reactive_sql are not.

Otherwise, test_sql gets executed. If that succeeds, then there is nothing to do. Otherwise drop_sql is executed (its result is ignored) and then finally reactive_sql is executed.

Returns
If auto-create-schemas is enabled, SWITCH_TRUE is returned if test_sql succeeds, SWITCH_FALSE otherwise. If reactive_sql is executed successfully SWITCH_TRUE is returned, otherwise SWITCH_FALSE is returned.

Definition at line 1299 of file switch_core_sqldb.c.

References io_mutex, runtime, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SCF_AUTO_SCHEMAS, SCF_CLEAR_SQL, switch_assert, switch_cache_db_execute_sql(), SWITCH_CHANNEL_LOG, switch_core_db_exec(), switch_core_db_free(), SWITCH_FALSE, SWITCH_LOG_DEBUG, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_odbc_handle_exec(), SWITCH_ODBC_SUCCESS, switch_pgsql_handle_exec, SWITCH_PGSQL_SUCCESS, SWITCH_STATUS_SUCCESS, switch_test_flag, and SWITCH_TRUE.

Referenced by switch_core_sqldb_start().

1301 {
1304 
1305  switch_assert(test_sql != NULL);
1306  switch_assert(reactive_sql != NULL);
1307 
1309  return SWITCH_TRUE;
1310  }
1311 
1313  switch_status_t status = switch_cache_db_execute_sql(dbh, (char *)test_sql, NULL);
1314 
1315  return (status == SWITCH_STATUS_SUCCESS) ? SWITCH_TRUE : SWITCH_FALSE;
1316  }
1317 
1318  if (io_mutex) switch_mutex_lock(io_mutex);
1319 
1320  switch (dbh->type) {
1321  case SCDB_TYPE_PGSQL:
1322  {
1324  if (drop_sql) {
1325  switch_pgsql_handle_exec(dbh->native_handle.pgsql_dbh, drop_sql, NULL);
1326  }
1327  r = switch_pgsql_handle_exec(dbh->native_handle.pgsql_dbh, reactive_sql, NULL) == SWITCH_PGSQL_SUCCESS;
1328  }
1329  }
1330  break;
1331  case SCDB_TYPE_ODBC:
1332  {
1333  if (switch_odbc_handle_exec(dbh->native_handle.odbc_dbh, test_sql, NULL, NULL) != SWITCH_ODBC_SUCCESS) {
1334  if (drop_sql) {
1335  switch_odbc_handle_exec(dbh->native_handle.odbc_dbh, drop_sql, NULL, NULL);
1336  }
1337  r = switch_odbc_handle_exec(dbh->native_handle.odbc_dbh, reactive_sql, NULL, NULL) == SWITCH_ODBC_SUCCESS;
1338  }
1339  }
1340  break;
1341  case SCDB_TYPE_CORE_DB:
1342  {
1343  char *errmsg = NULL;
1344  switch_core_db_exec(dbh->native_handle.core_db_dbh, test_sql, NULL, NULL, &errmsg);
1345 
1346  if (errmsg) {
1347  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql);
1348  switch_core_db_free(errmsg);
1349  errmsg = NULL;
1350  if (drop_sql) {
1351  switch_core_db_exec(dbh->native_handle.core_db_dbh, drop_sql, NULL, NULL, &errmsg);
1352  }
1353  if (errmsg) {
1354  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Ignoring SQL ERR [%s]\n[%s]\n", errmsg, drop_sql);
1355  switch_core_db_free(errmsg);
1356  errmsg = NULL;
1357  }
1358  switch_core_db_exec(dbh->native_handle.core_db_dbh, reactive_sql, NULL, NULL, &errmsg);
1359  if (errmsg) {
1360  r = SWITCH_FALSE;
1361  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
1362  switch_core_db_free(errmsg);
1363  errmsg = NULL;
1364  } else {
1365  r = SWITCH_TRUE;
1366  }
1367  }
1368  }
1369  break;
1370  }
1371 
1372 
1373  if (io_mutex) switch_mutex_unlock(io_mutex);
1374 
1375  return r;
1376 }
switch_cache_db_native_handle_t native_handle
switch_mutex_t * io_mutex
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_type_t type
switch_bool_t
Definition: switch_types.h:405
switch_odbc_status_t switch_odbc_handle_exec(switch_odbc_handle_t *handle, const char *sql, switch_odbc_statement_handle_t *rstmt, char **err)
Definition: switch_odbc.c:449
struct switch_runtime runtime
Definition: switch_core.c:64
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define switch_pgsql_handle_exec(handle, sql, err)
Definition: switch_pgsql.h:117
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
void switch_core_db_free(char *z)
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
#define switch_assert(expr)
switch_mutex_t * io_mutex
switch_status_t switch_core_add_registration ( const char *  user,
const char *  realm,
const char *  token,
const char *  url,
uint32_t  expires,
const char *  network_ip,
const char *  network_port,
const char *  network_proto,
const char *  metadata 
)

Add user registration.

Parameters
[in]user
[in]realm
[in]token
[in]url- a freeswitch dial string
[in]expires
[in]network_ip
[in]network_port
[in]network_proto- one of tls, tcp, udp
[in]metadata- generic metadata supplied by module
[out]err- Error if it exists

Definition at line 3244 of file switch_core_sqldb.c.

References switch_runtime::multiple_registrations, runtime, SCF_USE_SQL, db_job::sql, sql_manager, switch_core_get_switchname(), SWITCH_FALSE, switch_mprintf(), switch_sql_queue_manager_push(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_str_nil, switch_test_flag, and zstr.

3247 {
3248  char *sql;
3249 
3250  if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
3251  return SWITCH_STATUS_FALSE;
3252  }
3253 
3255  sql = switch_mprintf("delete from registrations where hostname='%q' and (url='%q' or token='%q')",
3257  } else {
3258  sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'",
3259  user, realm, switch_core_get_switchname());
3260  }
3261 
3263 
3264  if ( !zstr(metadata) ) {
3265  sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname,metadata) "
3266  "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q','%q')",
3267  switch_str_nil(user),
3268  switch_str_nil(realm),
3269  switch_str_nil(token),
3270  switch_str_nil(url),
3271  expires,
3272  switch_str_nil(network_ip),
3273  switch_str_nil(network_port),
3274  switch_str_nil(network_proto),
3276  metadata
3277  );
3278  } else {
3279  sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname) "
3280  "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q')",
3281  switch_str_nil(user),
3282  switch_str_nil(realm),
3283  switch_str_nil(token),
3284  switch_str_nil(url),
3285  expires,
3286  switch_str_nil(network_ip),
3287  switch_str_nil(network_port),
3288  switch_str_nil(network_proto),
3290  );
3291  }
3292 
3293 
3295 
3296  return SWITCH_STATUS_SUCCESS;
3297 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
struct switch_runtime runtime
Definition: switch_core.c:64
#define zstr(x)
Definition: switch_utils.h:281
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
switch_cache_db_handle_type_t switch_core_dbtype ( void  )

Definition at line 3129 of file switch_core_sqldb.c.

References SCDB_TYPE_CORE_DB, sql_manager, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by switch_console_set_complete().

3130 {
3132 
3133  switch_mutex_lock(sql_manager.ctl_mutex);
3134  if (sql_manager.qm && sql_manager.qm->event_db) {
3135  type = sql_manager.qm->event_db->type;
3136  }
3137  switch_mutex_unlock(sql_manager.ctl_mutex);
3138 
3139  return type;
3140 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_cache_db_handle_type_t
Definition: switch_core.h:2446
static struct @3 sql_manager
switch_status_t switch_core_del_registration ( const char *  user,
const char *  realm,
const char *  token 
)

Delete user registration.

Parameters
[in]user
[in]realm
[in]token
[out]err- Error if it exists

Definition at line 3299 of file switch_core_sqldb.c.

References switch_runtime::multiple_registrations, runtime, SCF_USE_SQL, db_job::sql, sql_manager, switch_core_get_switchname(), SWITCH_FALSE, switch_mprintf(), switch_sql_queue_manager_push(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_test_flag, and zstr.

3300 {
3301 
3302  char *sql;
3303 
3304  if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
3305  return SWITCH_STATUS_FALSE;
3306  }
3307 
3308  if (!zstr(token) && runtime.multiple_registrations) {
3309  sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q' and token='%q'", user, realm, switch_core_get_switchname(), token);
3310  } else {
3311  sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'", user, realm, switch_core_get_switchname());
3312  }
3313 
3315 
3316 
3317  return SWITCH_STATUS_SUCCESS;
3318 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
struct switch_runtime runtime
Definition: switch_core.c:64
#define zstr(x)
Definition: switch_utils.h:281
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
switch_status_t switch_core_expire_registration ( int  force)

Expire user registrations.

Parameters
[in]forcedelete all registrations
[out]err- Error if it exists

Definition at line 3320 of file switch_core_sqldb.c.

References runtime, SCF_USE_SQL, db_job::sql, sql_manager, switch_core_get_switchname(), switch_epoch_time_now(), SWITCH_FALSE, switch_mprintf(), switch_sql_queue_manager_push(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_test_flag.

Referenced by switch_core_sql_db_thread().

3321 {
3322 
3323  char *sql;
3324  time_t now;
3325 
3326  if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
3327  return SWITCH_STATUS_FALSE;
3328  }
3329 
3330  now = switch_epoch_time_now(NULL);
3331 
3332  if (force) {
3333  sql = switch_mprintf("delete from registrations where hostname='%q'", switch_core_get_switchname());
3334  } else {
3335  sql = switch_mprintf("delete from registrations where expires > 0 and expires <= %ld and hostname='%q'", now, switch_core_get_switchname());
3336  }
3337 
3339 
3340  return SWITCH_STATUS_SUCCESS;
3341 
3342 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
struct switch_runtime runtime
Definition: switch_core.c:64
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
void switch_core_recovery_flush ( const char *  technology,
const char *  profile_name 
)

Definition at line 2915 of file switch_core_sqldb.c.

References dbh, db_job::sql, switch_cache_db_execute_sql(), switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, switch_core_db_handle, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_mprintf(), switch_safe_free, SWITCH_STATUS_SUCCESS, and zstr.

Referenced by switch_core_session_ctl().

2916 {
2917  char *sql = NULL;
2919 
2921  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
2922  return;
2923  }
2924 
2925  if (zstr(technology)) {
2926 
2927  if (zstr(profile_name)) {
2928  sql = switch_mprintf("delete from recovery");
2929  } else {
2931  }
2932 
2933  } else {
2934  if (zstr(profile_name)) {
2935  sql = switch_mprintf("delete from recovery where technology='%q' ", technology);
2936  } else {
2937  sql = switch_mprintf("delete from recovery where technology='%q' and profile_name='%q'", technology, profile_name);
2938  }
2939  }
2940 
2941  if (sql) {
2942  switch_cache_db_execute_sql(dbh, sql, NULL);
2943  switch_safe_free(sql);
2944  }
2945 
2947 }
#define SWITCH_CHANNEL_LOG
#define zstr(x)
Definition: switch_utils.h:281
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_cache_db_handle_t * dbh
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
int switch_core_recovery_recover ( const char *  technology,
const char *  profile_name 
)

Definition at line 3049 of file switch_core_sqldb.c.

References dbh, recover_callback(), db_job::sql, sql_manager, switch_cache_db_execute_sql(), switch_cache_db_execute_sql_callback(), switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, switch_core_db_handle, switch_core_get_uuid(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), switch_safe_free, SWITCH_STATUS_SUCCESS, and zstr.

Referenced by switch_core_session_ctl().

3051 {
3052  char *sql = NULL;
3053  char *errmsg = NULL;
3055  int r = 0;
3056 
3057  if (!sql_manager.manage) {
3058  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DATABASE NOT AVAIALBLE, REVCOVERY NOT POSSIBLE\n");
3059  return 0;
3060  }
3061 
3063  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
3064  return 0;
3065  }
3066 
3067  if (zstr(technology)) {
3068 
3069  if (zstr(profile_name)) {
3070  sql = switch_mprintf("select technology, profile_name, hostname, uuid, metadata "
3071  "from recovery where runtime_uuid!='%q'",
3073  } else {
3074  sql = switch_mprintf("select technology, profile_name, hostname, uuid, metadata "
3075  "from recovery where runtime_uuid!='%q' and profile_name='%q'",
3076  switch_core_get_uuid(), profile_name);
3077  }
3078 
3079  } else {
3080 
3081  if (zstr(profile_name)) {
3082  sql = switch_mprintf("select technology, profile_name, hostname, uuid, metadata "
3083  "from recovery where technology='%q' and runtime_uuid!='%q'",
3084  technology, switch_core_get_uuid());
3085  } else {
3086  sql = switch_mprintf("select technology, profile_name, hostname, uuid, metadata "
3087  "from recovery where technology='%q' and runtime_uuid!='%q' and profile_name='%q'",
3088  technology, switch_core_get_uuid(), profile_name);
3089  }
3090  }
3091 
3092 
3094 
3095  if (errmsg) {
3096  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
3097  switch_safe_free(errmsg);
3098  }
3099 
3100  switch_safe_free(sql);
3101 
3102  if (zstr(technology)) {
3103  if (zstr(profile_name)) {
3104  sql = switch_mprintf("delete from recovery where runtime_uuid!='%q'",
3106  } else {
3107  sql = switch_mprintf("delete from recovery where runtime_uuid!='%q' and profile_name='%q'",
3108  switch_core_get_uuid(), profile_name);
3109  }
3110  } else {
3111  if (zstr(profile_name)) {
3112  sql = switch_mprintf("delete from recovery where runtime_uuid!='%q' and technology='%q' ",
3113  switch_core_get_uuid(), technology);
3114  } else {
3115  sql = switch_mprintf("delete from recovery where runtime_uuid!='%q' and technology='%q' and profile_name='%q'",
3116  switch_core_get_uuid(), technology, profile_name);
3117  }
3118  }
3119 
3120  switch_cache_db_execute_sql(dbh, sql, NULL);
3121  switch_safe_free(sql);
3122 
3124 
3125  return r;
3126 
3127 }
#define SWITCH_CHANNEL_LOG
#define zstr(x)
Definition: switch_utils.h:281
char * switch_core_get_uuid(void)
Retrieve the unique identifier from the core.
Definition: switch_core.c:482
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_cache_db_handle_t * dbh
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
static int recover_callback(void *pArg, int argc, char **argv, char **columnNames)
switch_status_t switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
void switch_core_recovery_track ( switch_core_session_t session)

Definition at line 3194 of file switch_core_sqldb.c.

References CF_ANSWERED, CF_RECOVERING, CF_TRACKABLE, CF_TRACKED, CS_SOFT_EXECUTE, db_job::sql, sql_manager, switch_channel_get_state(), switch_channel_get_variable_dup(), switch_channel_set_flag, switch_channel_test_flag(), switch_core_get_switchname(), switch_core_get_uuid(), switch_core_session_get_channel(), switch_core_session_get_uuid(), SWITCH_FALSE, switch_ivr_generate_xml_cdr(), switch_mprintf(), switch_safe_free, switch_sql_queue_manager_push(), SWITCH_STATUS_SUCCESS, switch_str_nil, switch_xml_free(), and switch_xml_toxml_nolock().

Referenced by switch_channel_clear_flag(), switch_channel_perform_mark_answered(), switch_core_media_activate_rtp(), and switch_core_session_perform_receive_message().

3195 {
3196  switch_xml_t cdr = NULL;
3197  char *xml_cdr_text = NULL;
3198  char *sql = NULL;
3200  const char *profile_name;
3201  const char *technology;
3202 
3203  if (!sql_manager.manage) {
3204  return;
3205  }
3206 
3208  return;
3209  }
3210 
3212  return;
3213  }
3214 
3215  profile_name = switch_channel_get_variable_dup(channel, "recovery_profile_name", SWITCH_FALSE, -1);
3216  technology = session->endpoint_interface->interface_name;
3217 
3218  if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
3219  xml_cdr_text = switch_xml_toxml_nolock(cdr, SWITCH_FALSE);
3220  switch_xml_free(cdr);
3221  }
3222 
3223  if (xml_cdr_text) {
3224  if (switch_channel_test_flag(channel, CF_TRACKED)) {
3225  sql = switch_mprintf("update recovery set metadata='%q' where uuid='%q'", xml_cdr_text, switch_core_session_get_uuid(session));
3226  } else {
3227  sql = switch_mprintf("insert into recovery (runtime_uuid, technology, profile_name, hostname, uuid, metadata) "
3228  "values ('%q','%q','%q','%q','%q','%q')",
3229  switch_core_get_uuid(), switch_str_nil(technology),
3230  switch_str_nil(profile_name), switch_core_get_switchname(), switch_core_session_get_uuid(session), xml_cdr_text);
3231  }
3232 
3234 
3235  switch_safe_free(xml_cdr_text);
3237 
3238  }
3239 
3240 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
switch_channel_state_t switch_channel_get_state(switch_channel_t *channel)
Get the current state of a channel in the state engine.
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
A representation of an XML tree.
Definition: switch_xml.h:76
const char * switch_channel_get_variable_dup(switch_channel_t *channel, const char *varname, switch_bool_t dup, int idx)
Retrieve a variable from a given channel.
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
char * switch_core_get_uuid(void)
Retrieve the unique identifier from the core.
Definition: switch_core.c:482
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
#define switch_channel_set_flag(_c, _f)
char * switch_xml_toxml_nolock(switch_xml_t xml, _In_ switch_bool_t prn_header)
switch_endpoint_interface_t * endpoint_interface
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
switch_status_t switch_ivr_generate_xml_cdr(switch_core_session_t *session, switch_xml_t *xml_cdr)
Generate an XML CDR report.
Definition: switch_ivr.c:2713
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
void switch_core_recovery_untrack ( switch_core_session_t session,
switch_bool_t  force 
)

Definition at line 3156 of file switch_core_sqldb.c.

References CF_ANSWERED, CF_RECOVERING, CF_TRACKABLE, CF_TRACKED, CS_SOFT_EXECUTE, db_job::sql, sql_manager, switch_channel_clear_flag(), switch_channel_get_state(), switch_channel_test_flag(), switch_core_get_uuid(), switch_core_session_get_channel(), switch_core_session_get_uuid(), SWITCH_FALSE, switch_mprintf(), and switch_sql_queue_manager_push().

Referenced by switch_core_standard_on_hangup().

3157 {
3158  char *sql = NULL;
3160 
3161  if (!sql_manager.manage) {
3162  return;
3163  }
3164 
3166  return;
3167  }
3168 
3169  if (!switch_channel_test_flag(channel, CF_TRACKABLE)) {
3170  return;
3171  }
3172 
3173  if ((switch_channel_test_flag(channel, CF_RECOVERING))) {
3174  return;
3175  }
3176 
3177  if (switch_channel_test_flag(channel, CF_TRACKED) || force) {
3178 
3179  if (force) {
3180  sql = switch_mprintf("delete from recovery where uuid='%q'", switch_core_session_get_uuid(session));
3181 
3182  } else {
3183  sql = switch_mprintf("delete from recovery where runtime_uuid='%q' and uuid='%q'",
3185  }
3186 
3188 
3190  }
3191 
3192 }
switch_channel_state_t switch_channel_get_state(switch_channel_t *channel)
Get the current state of a channel in the state engine.
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
char * switch_core_get_uuid(void)
Retrieve the unique identifier from the core.
Definition: switch_core.c:482
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
void switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Clear given flag(s) from a channel.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static struct @3 sql_manager
static void* SWITCH_THREAD_FUNC switch_core_sql_db_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 1379 of file switch_core_sqldb.c.

References runtime, SCF_USE_SQL, SQL_CACHE_TIMEOUT, sql_close(), sql_manager, SQL_REG_TIMEOUT, switch_core_expire_registration(), switch_epoch_time_now(), switch_test_flag, and switch_yield.

Referenced by switch_core_sqldb_start().

1380 {
1381  int sec = 0, reg_sec = 0;;
1382 
1383  sql_manager.db_thread_running = 1;
1384 
1385  while (sql_manager.db_thread_running == 1) {
1386  if (++sec == SQL_CACHE_TIMEOUT) {
1388  sec = 0;
1389  }
1390 
1391  if (switch_test_flag((&runtime), SCF_USE_SQL) && ++reg_sec == SQL_REG_TIMEOUT) {
1393  reg_sec = 0;
1394  }
1395  switch_yield(1000000);
1396  }
1397 
1398 
1399  return NULL;
1400 }
#define SQL_CACHE_TIMEOUT
struct switch_runtime runtime
Definition: switch_core.c:64
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
#define SQL_REG_TIMEOUT
switch_status_t switch_core_expire_registration(int force)
Expire user registrations.
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
static void sql_close(time_t prune)
static struct @3 sql_manager
void switch_core_sql_exec ( const char *  sql)

Definition at line 3142 of file switch_core_sqldb.c.

References runtime, SCF_USE_SQL, sql_manager, switch_sql_queue_manager_push(), switch_test_flag, and SWITCH_TRUE.

Referenced by switch_console_set_complete().

3143 {
3144  if (!sql_manager.manage) {
3145  return;
3146  }
3147 
3148  if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
3149  return;
3150  }
3151 
3152 
3154 }
struct switch_runtime runtime
Definition: switch_core.c:64
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
static struct @3 sql_manager
switch_status_t switch_core_sqldb_start ( switch_memory_pool_t pool,
switch_bool_t  manage 
)

Definition at line 3344 of file switch_core_sqldb.c.

References basic_calls_sql, core_event_handler(), create_alias_sql, create_calls_sql, create_channels_sql, create_complete_sql, create_interfaces_sql, create_nat_sql, create_registrations_sql, create_tasks_sql, DBTYPE_DEFAULT, detailed_calls_sql, manage, switch_runtime::odbc_dbtype, switch_runtime::odbc_dsn, pool, recovery_sql, runtime, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SCF_CLEAR_SQL, SCF_CORE_NON_SQLITE_DB_REQ, SCF_USE_SQL, skip(), db_job::sql, sql_manager, switch_cache_db_create_schema(), switch_cache_db_execute_sql(), switch_cache_db_execute_sql_real(), switch_cache_db_release_db_handle(), switch_cache_db_test_reactive(), SWITCH_CHANNEL_LOG, switch_clear_flag, switch_core_db_handle, switch_core_get_hostname(), switch_core_get_switchname(), switch_core_sql_db_thread(), switch_core_sqldb_start_thread(), SWITCH_EVENT_ADD_SCHEDULE, SWITCH_EVENT_ALL, switch_event_bind(), SWITCH_EVENT_CALL_SECURE, SWITCH_EVENT_CALL_UPDATE, SWITCH_EVENT_CHANNEL_ANSWER, SWITCH_EVENT_CHANNEL_BRIDGE, SWITCH_EVENT_CHANNEL_CALLSTATE, SWITCH_EVENT_CHANNEL_CREATE, SWITCH_EVENT_CHANNEL_DESTROY, SWITCH_EVENT_CHANNEL_EXECUTE, SWITCH_EVENT_CHANNEL_HOLD, SWITCH_EVENT_CHANNEL_ORIGINATE, SWITCH_EVENT_CHANNEL_PROGRESS_MEDIA, SWITCH_EVENT_CHANNEL_STATE, SWITCH_EVENT_CHANNEL_UNBRIDGE, SWITCH_EVENT_CHANNEL_UNHOLD, SWITCH_EVENT_CHANNEL_UUID, SWITCH_EVENT_CODEC, SWITCH_EVENT_DEL_SCHEDULE, SWITCH_EVENT_EXE_SCHEDULE, SWITCH_EVENT_LOG, SWITCH_EVENT_MODULE_LOAD, SWITCH_EVENT_MODULE_UNLOAD, SWITCH_EVENT_NAT, SWITCH_EVENT_RE_SCHEDULE, SWITCH_EVENT_SHUTDOWN, SWITCH_EVENT_SUBCLASS_ANY, SWITCH_LOG_CRIT, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_odbc_SQLEndTran(), switch_odbc_SQLSetAutoCommitAttr(), SWITCH_ODBC_SUCCESS, switch_pgsql_finish_results, switch_pgsql_SQLEndTran(), switch_pgsql_SQLSetAutoCommitAttr(), SWITCH_PGSQL_SUCCESS, SWITCH_PRI_REALTIME, switch_safe_free, switch_snprintfv(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_string_replace(), switch_stristr(), switch_test_flag, switch_thread_create(), SWITCH_THREAD_STACKSIZE, switch_threadattr_create(), switch_threadattr_priority_set(), and switch_threadattr_stacksize_set().

Referenced by switch_core_init().

3345 {
3346  switch_threadattr_t *thd_attr;
3347 
3348  sql_manager.memory_pool = pool;
3349  sql_manager.manage = manage;
3350 
3354 
3355  if (!sql_manager.manage) goto skip;
3356 
3357  top:
3358 
3359  /* Activate SQL database */
3361  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
3362 
3364  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure! ODBC IS REQUIRED!\n");
3365  return SWITCH_STATUS_FALSE;
3366  }
3367 
3368  if (runtime.odbc_dsn) {
3369  runtime.odbc_dsn = NULL;
3371  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Falling back to core_db.\n");
3372  goto top;
3373  }
3374 
3375 
3377  return SWITCH_STATUS_FALSE;
3378  }
3379 
3380 
3382 
3383  switch (sql_manager.dbh->type) {
3384  case SCDB_TYPE_PGSQL:
3385  case SCDB_TYPE_ODBC:
3387  char sql[512] = "";
3388  char *tables[] = { "channels", "calls", "tasks", NULL };
3389  int i;
3390  const char *hostname = switch_core_get_switchname();
3391 
3392  for (i = 0; tables[i]; i++) {
3393  switch_snprintfv(sql, sizeof(sql), "delete from %q where hostname='%q'", tables[i], hostname);
3394  switch_cache_db_execute_sql(sql_manager.dbh, sql, NULL);
3395  }
3396  }
3397  break;
3398  case SCDB_TYPE_CORE_DB:
3399  {
3400  switch_cache_db_execute_sql(sql_manager.dbh, "drop table channels", NULL);
3401  switch_cache_db_execute_sql(sql_manager.dbh, "drop table calls", NULL);
3402  switch_cache_db_execute_sql(sql_manager.dbh, "drop view detailed_calls", NULL);
3403  switch_cache_db_execute_sql(sql_manager.dbh, "drop view basic_calls", NULL);
3404  switch_cache_db_execute_sql(sql_manager.dbh, "drop table interfaces", NULL);
3405  switch_cache_db_execute_sql(sql_manager.dbh, "drop table tasks", NULL);
3406  switch_cache_db_execute_sql(sql_manager.dbh, "PRAGMA synchronous=OFF;", NULL);
3407  switch_cache_db_execute_sql(sql_manager.dbh, "PRAGMA count_changes=OFF;", NULL);
3408  switch_cache_db_execute_sql(sql_manager.dbh, "PRAGMA default_cache_size=8000", NULL);
3409  switch_cache_db_execute_sql(sql_manager.dbh, "PRAGMA temp_store=MEMORY;", NULL);
3410  switch_cache_db_execute_sql(sql_manager.dbh, "PRAGMA journal_mode=OFF;", NULL);
3411  }
3412  break;
3413  }
3414 
3415  switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from aliases", "DROP TABLE aliases", create_alias_sql);
3416  switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from complete", "DROP TABLE complete", create_complete_sql);
3417  switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from nat", "DROP TABLE nat", create_nat_sql);
3418  switch_cache_db_test_reactive(sql_manager.dbh, "delete from registrations where reg_user=''",
3419  "DROP TABLE registrations", create_registrations_sql);
3420 
3421  switch_cache_db_test_reactive(sql_manager.dbh, "select metadata from registrations", NULL, "ALTER TABLE registrations ADD COLUMN metadata VARCHAR(256)");
3422 
3423 
3424  switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from recovery", "DROP TABLE recovery", recovery_sql);
3425  switch_cache_db_create_schema(sql_manager.dbh, "create index recovery1 on recovery(technology)", NULL);
3426  switch_cache_db_create_schema(sql_manager.dbh, "create index recovery2 on recovery(profile_name)", NULL);
3427  switch_cache_db_create_schema(sql_manager.dbh, "create index recovery3 on recovery(uuid)", NULL);
3428  switch_cache_db_create_schema(sql_manager.dbh, "create index recovery3 on recovery(runtime_uuid)", NULL);
3429 
3430 
3431 
3432 
3433  switch (sql_manager.dbh->type) {
3434  case SCDB_TYPE_PGSQL:
3435  case SCDB_TYPE_ODBC:
3436  {
3437  char *err;
3438  int result = 0;
3439 
3440  switch_cache_db_test_reactive(sql_manager.dbh, "select call_uuid, read_bit_rate, sent_callee_name, initial_cid_name, initial_cid_num, initial_ip_addr, initial_dest, initial_dialplan, initial_context from channels", "DROP TABLE channels", create_channels_sql);
3441  switch_cache_db_test_reactive(sql_manager.dbh, "select call_uuid from calls", "DROP TABLE calls", create_calls_sql);
3442  switch_cache_db_test_reactive(sql_manager.dbh, "select * from basic_calls where sent_callee_name=''", "DROP VIEW basic_calls", basic_calls_sql);
3443  switch_cache_db_test_reactive(sql_manager.dbh, "select * from detailed_calls where sent_callee_name=''", "DROP VIEW detailed_calls", detailed_calls_sql);
3445  switch_cache_db_test_reactive(sql_manager.dbh, "delete from registrations where reg_user=''",
3446  "DROP TABLE registrations", create_registrations_sql);
3447  } else {
3448  char *tmp = switch_string_replace(create_registrations_sql, "url TEXT", "url VARCHAR(max)");
3449  switch_cache_db_test_reactive(sql_manager.dbh, "delete from registrations where reg_user=''",
3450  "DROP TABLE registrations", tmp);
3451  free(tmp);
3452  }
3453  switch_cache_db_test_reactive(sql_manager.dbh, "select ikey from interfaces", "DROP TABLE interfaces", create_interfaces_sql);
3454  switch_cache_db_test_reactive(sql_manager.dbh, "select task_id, task_desc, task_group, task_runtime, task_sql_manager, hostname from tasks",
3455  "DROP TABLE tasks", create_tasks_sql);
3456 
3457 
3458  switch(sql_manager.dbh->type) {
3459  case SCDB_TYPE_CORE_DB:
3460  {
3461  switch_cache_db_execute_sql_real(sql_manager.dbh, "BEGIN EXCLUSIVE", &err);
3462  }
3463  break;
3464  case SCDB_TYPE_ODBC:
3465  {
3466  switch_odbc_status_t result;
3467 
3468  if ((result = switch_odbc_SQLSetAutoCommitAttr(sql_manager.dbh->native_handle.odbc_dbh, 0)) != SWITCH_ODBC_SUCCESS) {
3469  char tmp[100];
3470  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
3471  err = strdup(tmp);
3472  }
3473  }
3474  break;
3475  case SCDB_TYPE_PGSQL:
3476  {
3477  switch_pgsql_status_t result;
3478 
3479  if ((result = switch_pgsql_SQLSetAutoCommitAttr(sql_manager.dbh->native_handle.pgsql_dbh, 0)) != SWITCH_PGSQL_SUCCESS) {
3480  char tmp[100];
3481  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to Set AutoCommit Off", result);
3482  err = strdup(tmp);
3483  }
3484  }
3485  break;
3486  }
3487 
3488  switch_cache_db_execute_sql(sql_manager.dbh, "delete from channels where hostname=''", &err);
3489  if (!err) {
3490  switch_cache_db_execute_sql(sql_manager.dbh, "delete from channels where hostname=''", &err);
3491 
3492  switch(sql_manager.dbh->type) {
3493  case SCDB_TYPE_CORE_DB:
3494  {
3495  switch_cache_db_execute_sql_real(sql_manager.dbh, "COMMIT", &err);
3496  }
3497  break;
3498  case SCDB_TYPE_ODBC:
3499  {
3500  if (switch_odbc_SQLEndTran(sql_manager.dbh->native_handle.odbc_dbh, 1) != SWITCH_ODBC_SUCCESS ||
3501  switch_odbc_SQLSetAutoCommitAttr(sql_manager.dbh->native_handle.odbc_dbh, 1) != SWITCH_ODBC_SUCCESS) {
3502  char tmp[100];
3503  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to commit transaction.", result);
3504  err = strdup(tmp);
3505  }
3506  }
3507  break;
3508  case SCDB_TYPE_PGSQL:
3509  {
3510  if (switch_pgsql_SQLEndTran(sql_manager.dbh->native_handle.pgsql_dbh, 1) != SWITCH_PGSQL_SUCCESS ||
3511  switch_pgsql_SQLSetAutoCommitAttr(sql_manager.dbh->native_handle.pgsql_dbh, 1) != SWITCH_PGSQL_SUCCESS ||
3512  switch_pgsql_finish_results(sql_manager.dbh->native_handle.pgsql_dbh) != SWITCH_PGSQL_SUCCESS) {
3513  char tmp[100];
3514  switch_snprintfv(tmp, sizeof(tmp), "%q-%i", "Unable to commit transaction.", result);
3515  err = strdup(tmp);
3516  }
3517  }
3518  break;
3519  }
3520  }
3521 
3522 
3523  if (err) {
3524  //runtime.odbc_dsn = NULL;
3525  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error [%s]\n", err);
3526  //switch_cache_db_release_db_handle(&sql_manager.dbh);
3527  if (switch_stristr("read-only", err)) {
3528  switch_safe_free(err);
3529  } else {
3530  switch_safe_free(err);
3531  goto top;
3532  }
3533  }
3534  }
3535  break;
3536  case SCDB_TYPE_CORE_DB:
3537  {
3544  }
3545  break;
3546  }
3547 
3549  char sql[512] = "";
3550  char *tables[] = { "complete", "aliases", "nat", NULL };
3551  int i;
3552  const char *hostname = switch_core_get_hostname();
3553 
3554  for (i = 0; tables[i]; i++) {
3555  switch_snprintfv(sql, sizeof(sql), "delete from %q where sticky=0 and hostname='%q'", tables[i], hostname);
3556  switch_cache_db_execute_sql(sql_manager.dbh, sql, NULL);
3557  }
3558 
3559  switch_snprintfv(sql, sizeof(sql), "delete from interfaces where hostname='%q'", hostname);
3560  switch_cache_db_execute_sql(sql_manager.dbh, sql, NULL);
3561  }
3562 
3563  switch_cache_db_create_schema(sql_manager.dbh, "create index alias1 on aliases (alias)", NULL);
3564  switch_cache_db_create_schema(sql_manager.dbh, "create index tasks1 on tasks (hostname,task_id)", NULL);
3565  switch_cache_db_create_schema(sql_manager.dbh, "create index complete1 on complete (a1,hostname)", NULL);
3566  switch_cache_db_create_schema(sql_manager.dbh, "create index complete2 on complete (a2,hostname)", NULL);
3567  switch_cache_db_create_schema(sql_manager.dbh, "create index complete3 on complete (a3,hostname)", NULL);
3568  switch_cache_db_create_schema(sql_manager.dbh, "create index complete4 on complete (a4,hostname)", NULL);
3569  switch_cache_db_create_schema(sql_manager.dbh, "create index complete5 on complete (a5,hostname)", NULL);
3570  switch_cache_db_create_schema(sql_manager.dbh, "create index complete6 on complete (a6,hostname)", NULL);
3571  switch_cache_db_create_schema(sql_manager.dbh, "create index complete7 on complete (a7,hostname)", NULL);
3572  switch_cache_db_create_schema(sql_manager.dbh, "create index complete8 on complete (a8,hostname)", NULL);
3573  switch_cache_db_create_schema(sql_manager.dbh, "create index complete9 on complete (a9,hostname)", NULL);
3574  switch_cache_db_create_schema(sql_manager.dbh, "create index complete10 on complete (a10,hostname)", NULL);
3575  switch_cache_db_create_schema(sql_manager.dbh, "create index complete11 on complete (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,hostname)", NULL);
3576  switch_cache_db_create_schema(sql_manager.dbh, "create index nat_map_port_proto on nat (port,proto,hostname)", NULL);
3577  switch_cache_db_create_schema(sql_manager.dbh, "create index channels1 on channels(hostname)", NULL);
3578  switch_cache_db_create_schema(sql_manager.dbh, "create index calls1 on calls(hostname)", NULL);
3579  switch_cache_db_create_schema(sql_manager.dbh, "create index chidx1 on channels (hostname)", NULL);
3580  switch_cache_db_create_schema(sql_manager.dbh, "create index uuindex on channels (uuid, hostname)", NULL);
3581  switch_cache_db_create_schema(sql_manager.dbh, "create index uuindex2 on channels (call_uuid)", NULL);
3582  switch_cache_db_create_schema(sql_manager.dbh, "create index callsidx1 on calls (hostname)", NULL);
3583  switch_cache_db_create_schema(sql_manager.dbh, "create index eruuindex on calls (caller_uuid, hostname)", NULL);
3584  switch_cache_db_create_schema(sql_manager.dbh, "create index eeuuindex on calls (callee_uuid)", NULL);
3585  switch_cache_db_create_schema(sql_manager.dbh, "create index eeuuindex2 on calls (call_uuid)", NULL);
3586  switch_cache_db_create_schema(sql_manager.dbh, "create index regindex1 on registrations (reg_user,realm,hostname)", NULL);
3587 
3588 
3589  skip:
3590 
3591  if (sql_manager.manage) {
3592 #ifdef SWITCH_SQL_BIND_EVERY_EVENT
3594 #else
3620 #endif
3621 
3622  switch_threadattr_create(&thd_attr, sql_manager.memory_pool);
3626  switch_thread_create(&sql_manager.db_thread, thd_attr, switch_core_sql_db_thread, NULL, sql_manager.memory_pool);
3627 
3628  }
3629 
3631 
3632  return SWITCH_STATUS_SUCCESS;
3633 }
switch_status_t switch_cache_db_create_schema(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the create schema sql.
switch_pgsql_status_t
Definition: switch_pgsql.h:53
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
switch_odbc_status_t
Definition: switch_odbc.h:49
#define SWITCH_CHANNEL_LOG
static char create_interfaces_sql[]
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:660
switch_pgsql_status_t switch_pgsql_SQLEndTran(switch_pgsql_handle_t *handle, switch_bool_t commit)
Definition: switch_pgsql.c:865
#define SWITCH_EVENT_SUBCLASS_ANY
Definition: switch_event.h:128
switch_memory_pool_t * pool
static char create_tasks_sql[]
switch_pgsql_status_t switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on)
Definition: switch_pgsql.c:828
struct switch_runtime runtime
Definition: switch_core.c:64
static void switch_core_sqldb_start_thread(void)
#define switch_clear_flag(obj, flag)
Clear a flag on an arbitrary object while locked.
Definition: switch_utils.h:655
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
static char recovery_sql[]
switch_status_t switch_event_bind(const char *id, switch_event_types_t event, const char *subclass_name, switch_event_callback_t callback, void *user_data)
Bind an event callback to a specific event.
switch_dbtype_t odbc_dbtype
static char create_channels_sql[]
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:551
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
static char create_nat_sql[]
#define switch_pgsql_finish_results(handle)
Definition: switch_pgsql.h:107
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
switch_bool_t manage
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
static char create_calls_sql[]
static char detailed_calls_sql[]
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
static char basic_calls_sql[]
char * switch_string_replace(const char *string, const char *search, const char *replace)
static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t *dbh, const char *sql, char **err)
static void *SWITCH_THREAD_FUNC switch_core_sql_db_thread(switch_thread_t *thread, void *obj)
const char * switch_core_get_hostname(void)
Definition: switch_core.c:344
switch_odbc_status_t switch_odbc_SQLEndTran(switch_odbc_handle_t *handle, switch_bool_t commit)
Definition: switch_odbc.c:760
switch_bool_t switch_cache_db_test_reactive(switch_cache_db_handle_t *dbh, const char *test_sql, const char *drop_sql, const char *reactive_sql)
Performs test_sql and if it fails performs drop_sql and reactive_sql.
static char create_registrations_sql[]
static const char * skip(const char *in)
Definition: switch_json.c:270
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:642
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:675
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
static char create_complete_sql[]
static void core_event_handler(switch_event_t *event)
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr(switch_odbc_handle_t *handle, switch_bool_t on)
Definition: switch_odbc.c:747
static char create_alias_sql[]
static struct @3 sql_manager
switch_status_t switch_threadattr_priority_set(switch_threadattr_t *attr, switch_thread_priority_t priority)
Definition: switch_apr.c:665
static void switch_core_sqldb_start_thread ( void  )
static

Definition at line 3666 of file switch_core_sqldb.c.

References switch_runtime::core_db_inner_post_trans_execute, switch_runtime::core_db_inner_pre_trans_execute, switch_runtime::core_db_post_trans_execute, switch_runtime::core_db_pre_trans_execute, switch_runtime::dbname, switch_runtime::odbc_dsn, runtime, sql_manager, SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_MAX_TRANS, switch_mutex_lock(), switch_mutex_unlock(), switch_sql_queue_manager_init_name(), switch_sql_queue_manager_start(), and zstr.

Referenced by switch_core_sqldb_start().

3667 {
3668 
3669  switch_mutex_lock(sql_manager.ctl_mutex);
3670  if (sql_manager.manage) {
3671  if (!sql_manager.qm) {
3672  char *dbname = runtime.odbc_dsn;
3673 
3674  if (zstr(dbname)) {
3675  dbname = runtime.dbname;
3676  if (zstr(dbname)) {
3677  dbname = "core";
3678  }
3679  }
3680 
3682  &sql_manager.qm,
3683  4,
3684  dbname,
3690 
3691  }
3693  } else {
3694  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL is not enabled\n");
3695  }
3696  switch_mutex_unlock(sql_manager.ctl_mutex);
3697 }
char * core_db_inner_post_trans_execute
#define SWITCH_CHANNEL_LOG
char * core_db_post_trans_execute
struct switch_runtime runtime
Definition: switch_core.c:64
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t switch_sql_queue_manager_init_name(const char *name, switch_sql_queue_manager_t **qmp, uint32_t numq, const char *dsn, uint32_t max_trans, const char *pre_trans_execute, const char *post_trans_execute, const char *inner_pre_trans_execute, const char *inner_post_trans_execute)
char * core_db_pre_trans_execute
switch_status_t switch_sql_queue_manager_start(switch_sql_queue_manager_t *qm)
#define SWITCH_MAX_TRANS
Definition: switch_types.h:231
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char * core_db_inner_pre_trans_execute
static struct @3 sql_manager
void switch_core_sqldb_stop ( void  )

Definition at line 3699 of file switch_core_sqldb.c.

References core_event_handler(), sql_close(), sql_manager, switch_cache_db_flush_handles(), switch_core_sqldb_stop_thread(), switch_event_unbind_callback(), and switch_thread_join().

Referenced by switch_core_destroy().

3700 {
3701  switch_status_t st;
3702 
3704 
3705  if (sql_manager.db_thread && sql_manager.db_thread_running) {
3706  sql_manager.db_thread_running = -1;
3707  switch_thread_join(&st, sql_manager.db_thread);
3708  }
3709 
3711 
3713  sql_close(0);
3714 }
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
void switch_cache_db_flush_handles(void)
switch_status_t
Common return values.
static void switch_core_sqldb_stop_thread(void)
switch_status_t switch_event_unbind_callback(switch_event_callback_t callback)
static void core_event_handler(switch_event_t *event)
static void sql_close(time_t prune)
static struct @3 sql_manager
static void switch_core_sqldb_stop_thread ( void  )
static

Definition at line 3652 of file switch_core_sqldb.c.

References sql_manager, SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), and switch_sql_queue_manager_destroy().

Referenced by switch_core_sqldb_stop().

3653 {
3654  switch_mutex_lock(sql_manager.ctl_mutex);
3655  if (sql_manager.manage) {
3656  if (sql_manager.qm) {
3658  }
3659  } else {
3660  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL is not enabled\n");
3661  }
3662 
3663  switch_mutex_unlock(sql_manager.ctl_mutex);
3664 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t switch_sql_queue_manager_destroy(switch_sql_queue_manager_t **qmp)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static struct @3 sql_manager
char* switch_sql_concat ( void  )

This routine is a variant of the "sprintf()" from the standard C library. The resulting string is written into memory obtained from malloc() so that there is never a possiblity of buffer overflow. This routine also implement some additional formatting options that are useful for constructing SQL statements.

The strings returned by this routine should be freed by calling switch_core_db_free().

All of the usual printf formatting options apply. In addition, there is a "%q" option. q works like s in that it substitutes a null-terminated string from the argument list. But q also doubles every '\'' character. q is designed for use inside a string literal. By doubling each '\'' character it escapes that character and allows it to be inserted into the string.

For example, so some string variable contains text as follows:

 char *zText = "It's a happy day!";

We can use this text in an SQL statement as follows:

 char *z = switch_core_db_mprintf("INSERT INTO TABLES('%q')", zText);
 switch_core_db_exec(db, z, callback1, 0, 0);
 switch_core_db_free(z);

Because the q format string is used, the '\'' character in zText is escaped and the SQL generated is as follows:

 INSERT INTO table1 VALUES('It''s a happy day!')

This is correct. Had we used s instead of q, the generated SQL would have looked like this:

 INSERT INTO table1 VALUES('It's a happy day!');

This second example is an SQL syntax error. As a general rule you should always use q instead of s when inserting text into a string literal.

Definition at line 3790 of file switch_core_sqldb.c.

References DBTYPE_MSSQL, switch_runtime::odbc_dbtype, and runtime.

3791 {
3793  return "+";
3794 
3795  return "||";
3796 }
struct switch_runtime runtime
Definition: switch_core.c:64
switch_dbtype_t odbc_dbtype
switch_status_t switch_sql_queue_manager_destroy ( switch_sql_queue_manager_t **  qmp)

Definition at line 1704 of file switch_core_sqldb.c.

References do_flush(), switch_sql_queue_manager::name, switch_sql_queue_manager::numq, pool, switch_sql_queue_manager::pool, qm, switch_assert, SWITCH_CHANNEL_LOG, switch_core_destroy_memory_pool, SWITCH_LOG_INFO, switch_log_printf(), switch_sql_queue_manager_stop(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_core_sqldb_stop_thread().

1705 {
1709  uint32_t i;
1710 
1711  switch_assert(qmp);
1712  qm = *qmp;
1713  *qmp = NULL;
1714 
1715  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s Destroying SQL queue.\n", qm->name);
1716 
1718 
1719 
1720 
1721  for(i = 0; i < qm->numq; i++) {
1722  do_flush(qm, i, NULL);
1723  }
1724 
1725  pool = qm->pool;
1727 
1728  return status;
1729 }
#define SWITCH_CHANNEL_LOG
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
switch_status_t switch_sql_queue_manager_stop(switch_sql_queue_manager_t *qm)
switch_sql_queue_manager_t * qm
switch_memory_pool_t * pool
switch_status_t
Common return values.
struct apr_pool_t switch_memory_pool_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
static void do_flush(switch_sql_queue_manager_t *qm, int i, switch_cache_db_handle_t *dbh)
void switch_sql_queue_manager_execute_sql_callback ( switch_sql_queue_manager_t qm,
const char *  sql,
switch_core_db_callback_func_t  callback,
void *  pdata 
)

Definition at line 1553 of file switch_core_sqldb.c.

References new_job(), and switch_thread_pool_launch_thread().

1555 {
1556 
1558  if ((td = new_job(qm, sql, callback, NULL, NULL, NULL, pdata))) {
1560  }
1561 }
switch_core_db_event_callback_func_t callback
static switch_thread_data_t * new_job(switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, switch_core_db_event_callback_func_t event_callback, switch_core_db_err_callback_func_t event_err_callback, void *pdata)
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
void switch_sql_queue_manager_execute_sql_callback_err ( switch_sql_queue_manager_t qm,
const char *  sql,
switch_core_db_callback_func_t  callback,
switch_core_db_err_callback_func_t  err_callback,
void *  pdata 
)

Definition at line 1563 of file switch_core_sqldb.c.

References new_job(), and switch_thread_pool_launch_thread().

1566 {
1567 
1569  if ((td = new_job(qm, sql, callback, err_callback, NULL, NULL, pdata))) {
1571  }
1572 }
switch_core_db_event_callback_func_t callback
static switch_thread_data_t * new_job(switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, switch_core_db_event_callback_func_t event_callback, switch_core_db_err_callback_func_t event_err_callback, void *pdata)
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
void switch_sql_queue_manager_execute_sql_event_callback ( switch_sql_queue_manager_t qm,
const char *  sql,
switch_core_db_event_callback_func_t  callback,
void *  pdata 
)

Definition at line 1574 of file switch_core_sqldb.c.

References new_job(), and switch_thread_pool_launch_thread().

1576 {
1577 
1579  if ((td = new_job(qm, sql, NULL, NULL, callback, NULL, pdata))) {
1581  }
1582 }
switch_core_db_event_callback_func_t callback
static switch_thread_data_t * new_job(switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, switch_core_db_event_callback_func_t event_callback, switch_core_db_err_callback_func_t event_err_callback, void *pdata)
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
void switch_sql_queue_manager_execute_sql_event_callback_err ( switch_sql_queue_manager_t qm,
const char *  sql,
switch_core_db_event_callback_func_t  callback,
switch_core_db_err_callback_func_t  err_callback,
void *  pdata 
)

Definition at line 1584 of file switch_core_sqldb.c.

References new_job(), and switch_thread_pool_launch_thread().

1588 {
1589 
1591  if ((td = new_job(qm, sql, NULL, NULL, callback, err_callback, pdata))) {
1593  }
1594 }
switch_core_db_event_callback_func_t callback
static switch_thread_data_t * new_job(switch_sql_queue_manager_t *qm, const char *sql, switch_core_db_callback_func_t callback, switch_core_db_err_callback_func_t err_callback, switch_core_db_event_callback_func_t event_callback, switch_core_db_err_callback_func_t event_err_callback, void *pdata)
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
switch_status_t switch_sql_queue_manager_init_name ( const char *  name,
switch_sql_queue_manager_t **  qmp,
uint32_t  numq,
const char *  dsn,
uint32_t  max_trans,
const char *  pre_trans_execute,
const char *  post_trans_execute,
const char *  inner_pre_trans_execute,
const char *  inner_post_trans_execute 
)

Definition at line 1847 of file switch_core_sqldb.c.

References switch_sql_queue_manager::cond, switch_sql_queue_manager::cond2_mutex, switch_sql_queue_manager::cond_mutex, switch_sql_queue_manager::dsn, switch_sql_queue_manager::inner_post_trans_execute, switch_sql_queue_manager::inner_pre_trans_execute, switch_sql_queue_manager::max_trans, switch_sql_queue_manager::mutex, switch_sql_queue_manager::name, switch_sql_queue_manager::numq, pool, switch_sql_queue_manager::pool, switch_sql_queue_manager::post_trans_execute, switch_sql_queue_manager::pre_trans_execute, switch_sql_queue_manager::pre_written, qm, switch_sql_queue_manager::sql_queue, switch_core_alloc, switch_core_new_memory_pool, switch_core_strdup, switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), SWITCH_SQL_QUEUE_LEN, SWITCH_STATUS_SUCCESS, switch_thread_cond_create(), and switch_sql_queue_manager::written.

Referenced by switch_core_sqldb_start_thread().

1854 {
1857  uint32_t i;
1858 
1859  if (!numq) numq = 1;
1860 
1862  qm = switch_core_alloc(pool, sizeof(*qm));
1863 
1864  qm->pool = pool;
1865  qm->numq = numq;
1866  qm->dsn = switch_core_strdup(qm->pool, dsn);
1867  qm->name = switch_core_strdup(qm->pool, name);
1868  qm->max_trans = max_trans;
1869 
1873  switch_thread_cond_create(&qm->cond, qm->pool);
1874 
1875  qm->sql_queue = switch_core_alloc(qm->pool, sizeof(switch_queue_t *) * numq);
1876  qm->written = switch_core_alloc(qm->pool, sizeof(uint32_t) * numq);
1877  qm->pre_written = switch_core_alloc(qm->pool, sizeof(uint32_t) * numq);
1878 
1879  for (i = 0; i < qm->numq; i++) {
1881  }
1882 
1883  if (pre_trans_execute) {
1884  qm->pre_trans_execute = switch_core_strdup(qm->pool, pre_trans_execute);
1885  }
1886  if (post_trans_execute) {
1887  qm->post_trans_execute = switch_core_strdup(qm->pool, post_trans_execute);
1888  }
1889  if (inner_pre_trans_execute) {
1890  qm->inner_pre_trans_execute = switch_core_strdup(qm->pool, inner_pre_trans_execute);
1891  }
1892  if (inner_post_trans_execute) {
1893  qm->inner_post_trans_execute = switch_core_strdup(qm->pool, inner_post_trans_execute);
1894  }
1895 
1896  *qmp = qm;
1897 
1898  return SWITCH_STATUS_SUCCESS;
1899 
1900 }
struct apr_queue_t switch_queue_t
Definition: switch_apr.h:590
#define SWITCH_SQL_QUEUE_LEN
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
switch_status_t switch_thread_cond_create(switch_thread_cond_t **cond, switch_memory_pool_t *pool)
Definition: switch_apr.c:350
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_memory_pool_t * pool
switch_sql_queue_manager_t * qm
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_memory_pool_t * pool
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
switch_thread_cond_t * cond
struct apr_pool_t switch_memory_pool_t
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
void switch_sql_queue_manager_pause ( switch_sql_queue_manager_t qm,
switch_bool_t  flush 
)

Definition at line 1626 of file switch_core_sqldb.c.

References do_flush(), switch_mutex_lock(), and switch_mutex_unlock().

1627 {
1628  uint32_t i;
1629 
1630  switch_mutex_lock(qm->mutex);
1631  qm->paused = 1;
1633 
1634  if (flush) {
1635  for(i = 0; i < qm->numq; i++) {
1636  do_flush(qm, i, NULL);
1637  }
1638  }
1639 
1640 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static void do_flush(switch_sql_queue_manager_t *qm, int i, switch_cache_db_handle_t *dbh)
switch_status_t switch_sql_queue_manager_push ( switch_sql_queue_manager_t qm,
const char *  sql,
uint32_t  pos,
switch_bool_t  dup 
)

Definition at line 1731 of file switch_core_sqldb.c.

References qm_wake(), sql_manager, SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_trypush(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_yield.

Referenced by core_event_handler(), switch_core_add_registration(), switch_core_del_registration(), switch_core_expire_registration(), switch_core_recovery_track(), switch_core_recovery_untrack(), and switch_core_sql_exec().

1732 {
1733  char *sqlptr = NULL;
1734  switch_status_t status;
1735  int x = 0;
1736 
1737  if (sql_manager.paused || qm->thread_running != 1) {
1739  if (!dup) free((char *)sql);
1740  qm_wake(qm);
1741  return SWITCH_STATUS_SUCCESS;
1742  }
1743 
1744  if (qm->thread_running != 1) {
1745  if (!dup) free((char *)sql);
1746  return SWITCH_STATUS_FALSE;
1747  }
1748 
1749  if (pos > qm->numq - 1) {
1750  pos = 0;
1751  }
1752 
1753  sqlptr = dup ? strdup(sql) : (char *)sql;
1754 
1755  do {
1756  switch_mutex_lock(qm->mutex);
1757  status = switch_queue_trypush(qm->sql_queue[pos], sqlptr);
1759  if (status != SWITCH_STATUS_SUCCESS) {
1760  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Delay %d sending sql\n", x);
1761  if (x++) {
1762  switch_yield(1000000 * x);
1763  }
1764  }
1765  } while(status != SWITCH_STATUS_SUCCESS);
1766 
1767  qm_wake(qm);
1768 
1769  return SWITCH_STATUS_SUCCESS;
1770 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t
Common return values.
static int qm_wake(switch_sql_queue_manager_t *qm)
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1155
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static struct @3 sql_manager
switch_status_t switch_sql_queue_manager_push_confirm ( switch_sql_queue_manager_t qm,
const char *  sql,
uint32_t  pos,
switch_bool_t  dup 
)

Definition at line 1773 of file switch_core_sqldb.c.

References dbh, qm_wake(), sql_manager, switch_cache_db_execute_sql(), switch_cache_db_get_db_handle_dsn, switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_push(), switch_sql_queue_manager_size(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_yield.

1774 {
1775 #define EXEC_NOW
1776 #ifdef EXEC_NOW
1778 
1779  if (sql_manager.paused || qm->thread_running != 1) {
1781  if (!dup) free((char *)sql);
1782  qm_wake(qm);
1783  return SWITCH_STATUS_SUCCESS;
1784  }
1785 
1787  switch_cache_db_execute_sql(dbh, (char *)sql, NULL);
1789  }
1790 
1791  if (!dup) free((char *)sql);
1792 
1793 #else
1794 
1795  int size, x = 0, sanity = 0;
1796  uint32_t written, want;
1797 
1798  if (sql_manager.paused) {
1799  if (!dup) free((char *)sql);
1800  qm_wake(qm);
1801  return SWITCH_STATUS_SUCCESS;
1802  }
1803 
1804  if (qm->thread_running != 1) {
1805  if (!dup) free((char *)sql);
1806  return SWITCH_STATUS_FALSE;
1807  }
1808 
1809  if (pos > qm->numq - 1) {
1810  pos = 0;
1811  }
1812 
1813  switch_mutex_lock(qm->mutex);
1814  qm->confirm++;
1815  switch_queue_push(qm->sql_queue[pos], dup ? strdup(sql) : (char *)sql);
1816  written = qm->pre_written[pos];
1817  size = switch_sql_queue_manager_size(qm, pos);
1818  want = written + size;
1820 
1821  qm_wake(qm);
1822 
1823  while((qm->written[pos] < want) || (qm->written[pos] >= written && want < written && qm->written[pos] > want)) {
1824  switch_yield(5000);
1825 
1826  if (++x == 200) {
1827  qm_wake(qm);
1828  x = 0;
1829  if (++sanity == 20) {
1830  break;
1831  }
1832  }
1833  }
1834 
1835  switch_mutex_lock(qm->mutex);
1836  qm->confirm--;
1838 #endif
1839 
1840  return SWITCH_STATUS_SUCCESS;
1841 }
#define SWITCH_CHANNEL_LOG
#define switch_cache_db_get_db_handle_dsn(_a, _b)
Definition: switch_core.h:2536
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
int switch_sql_queue_manager_size(switch_sql_queue_manager_t *qm, uint32_t index)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_cache_db_handle_t * dbh
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
static int qm_wake(switch_sql_queue_manager_t *qm)
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
static struct @3 sql_manager
void switch_sql_queue_manager_resume ( switch_sql_queue_manager_t qm)

Definition at line 1616 of file switch_core_sqldb.c.

References qm_wake(), switch_mutex_lock(), and switch_mutex_unlock().

1617 {
1618  switch_mutex_lock(qm->mutex);
1619  qm->paused = 0;
1621 
1622  qm_wake(qm);
1623 
1624 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static int qm_wake(switch_sql_queue_manager_t *qm)
int switch_sql_queue_manager_size ( switch_sql_queue_manager_t qm,
uint32_t  index 
)

Definition at line 1642 of file switch_core_sqldb.c.

References switch_mutex_lock(), switch_mutex_unlock(), and switch_queue_size().

Referenced by switch_sql_queue_manager_push_confirm().

1643 {
1644  int size = 0;
1645 
1646  switch_mutex_lock(qm->mutex);
1647  if (index < qm->numq) {
1648  size = switch_queue_size(qm->sql_queue[index]);
1649  }
1651 
1652  return size;
1653 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t switch_sql_queue_manager_start ( switch_sql_queue_manager_t qm)

Definition at line 1688 of file switch_core_sqldb.c.

References apr_threadattr_t::pool, SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_PRI_NORMAL, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_thread_create(), SWITCH_THREAD_STACKSIZE, switch_threadattr_create(), switch_threadattr_priority_set(), switch_threadattr_stacksize_set(), and switch_user_sql_thread().

Referenced by switch_core_sqldb_start_thread().

1689 {
1690  switch_threadattr_t *thd_attr;
1691 
1692  if (!qm->thread_running) {
1693  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s Starting SQL thread.\n", qm->name);
1694  switch_threadattr_create(&thd_attr, qm->pool);
1697  switch_thread_create(&qm->thread, thd_attr, switch_user_sql_thread, qm, qm->pool);
1698  return SWITCH_STATUS_SUCCESS;
1699  }
1700 
1701  return SWITCH_STATUS_FALSE;
1702 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:660
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:551
switch_memory_pool_t * pool
static void *SWITCH_THREAD_FUNC switch_user_sql_thread(switch_thread_t *thread, void *obj)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:642
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:675
switch_status_t switch_threadattr_priority_set(switch_threadattr_t *attr, switch_thread_priority_t priority)
Definition: switch_apr.c:665
switch_status_t switch_sql_queue_manager_stop ( switch_sql_queue_manager_t qm)

Definition at line 1655 of file switch_core_sqldb.c.

References qm_wake(), SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, switch_log_printf(), switch_queue_interrupt_all(), switch_queue_push(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_thread_join(), and switch_yield.

Referenced by switch_sql_queue_manager_destroy().

1656 {
1658  uint32_t i, sanity = 100;
1659 
1660  if (qm->thread_running == 1) {
1661  qm->thread_running = -1;
1662 
1663  while(--sanity && qm->thread_running == -1) {
1664  for(i = 0; i < qm->numq; i++) {
1665  switch_queue_push(qm->sql_queue[i], NULL);
1667  }
1668  qm_wake(qm);
1669 
1670  if (qm->thread_running == -1) {
1671  switch_yield(100000);
1672  }
1673  }
1674  status = SWITCH_STATUS_SUCCESS;
1675  }
1676 
1677  if (qm->thread) {
1678  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s Stopping SQL thread.\n", qm->name);
1679  qm_wake(qm);
1680  switch_thread_join(&status, qm->thread);
1681  qm->thread = NULL;
1682  status = SWITCH_STATUS_SUCCESS;
1683  }
1684 
1685  return status;
1686 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
switch_status_t
Common return values.
switch_status_t switch_queue_interrupt_all(switch_queue_t *queue)
Definition: switch_apr.c:1145
static int qm_wake(switch_sql_queue_manager_t *qm)
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static void *SWITCH_THREAD_FUNC switch_user_sql_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 2045 of file switch_core_sqldb.c.

References switch_sql_queue_manager::cond, switch_sql_queue_manager::cond2_mutex, switch_sql_queue_manager::cond_mutex, do_flush(), do_trans(), switch_sql_queue_manager::dsn, switch_sql_queue_manager::event_db, switch_sql_queue_manager::max_trans, switch_sql_queue_manager::name, switch_sql_queue_manager::numq, switch_sql_queue_manager::paused, qm, qm_ttl(), runtime, SCDB_TYPE_CORE_DB, SCDB_TYPE_ODBC, SCDB_TYPE_PGSQL, SCF_DEBUG_SQL, sql_manager, switch_sql_queue_manager::sql_queue, switch_cache_db_execute_sql(), switch_cache_db_get_db_handle_dsn, switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_lock(), switch_mutex_unlock(), switch_queue_size(), switch_snprintf(), SWITCH_STATUS_SUCCESS, switch_test_flag, switch_thread_cond_wait(), switch_yield, switch_sql_queue_manager::thread_running, and switch_cache_db_handle::type.

Referenced by switch_sql_queue_manager_start().

2046 {
2047 
2048  uint32_t sanity = 120;
2050  uint32_t i;
2051 
2052  while (!qm->event_db) {
2054  break;
2055  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s Error getting db handle, Retrying\n", qm->name);
2056  switch_yield(500000);
2057  sanity--;
2058  }
2059 
2060  if (!qm->event_db) {
2061  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "%s Error getting db handle\n", qm->name);
2062  return NULL;
2063  }
2064 
2065  qm->thread_running = 1;
2066 
2068 
2069  switch (qm->event_db->type) {
2070  case SCDB_TYPE_PGSQL:
2071  break;
2072  case SCDB_TYPE_ODBC:
2073  break;
2074  case SCDB_TYPE_CORE_DB:
2075  {
2076  switch_cache_db_execute_sql(qm->event_db, "PRAGMA synchronous=OFF;", NULL);
2077  switch_cache_db_execute_sql(qm->event_db, "PRAGMA count_changes=OFF;", NULL);
2078  switch_cache_db_execute_sql(qm->event_db, "PRAGMA temp_store=MEMORY;", NULL);
2079  switch_cache_db_execute_sql(qm->event_db, "PRAGMA journal_mode=OFF;", NULL);
2080  }
2081  break;
2082  }
2083 
2084 
2085  while (qm->thread_running == 1) {
2086  uint32_t i, lc;
2087  uint32_t written = 0, iterations = 0;
2088 
2089  if (qm->paused) {
2090  goto check;
2091  }
2092 
2093  if (sql_manager.paused) {
2094  for (i = 0; i < qm->numq; i++) {
2095  do_flush(qm, i, NULL);
2096  }
2097  goto check;
2098  }
2099 
2100  do {
2101  if (!qm_ttl(qm)) {
2102  goto check;
2103  }
2104  written = do_trans(qm);
2105  iterations += written;
2106  } while(written == qm->max_trans);
2107 
2109  char line[128] = "";
2110  switch_size_t l;
2111 
2112  switch_snprintf(line, sizeof(line), "%s RUN QUEUE [", qm->name);
2113 
2114  for (i = 0; i < qm->numq; i++) {
2115  l = strlen(line);
2116  switch_snprintf(line + l, sizeof(line) - l, "%d%s", switch_queue_size(qm->sql_queue[i]), i == qm->numq - 1 ? "" : "|");
2117  }
2118 
2119  l = strlen(line);
2120  switch_snprintf(line + l, sizeof(line) - l, "]--[%d]\n", iterations);
2121 
2123 
2124  }
2125 
2126  check:
2127 
2128  if ((lc = qm_ttl(qm)) == 0) {
2132  }
2133 
2134  i = 40;
2135 
2136  while (--i > 0 && (lc = qm_ttl(qm)) < 500) {
2137  switch_yield(5000);
2138  }
2139 
2140 
2141  }
2142 
2144 
2145  for(i = 0; i < qm->numq; i++) {
2146  do_flush(qm, i, qm->event_db);
2147  }
2148 
2150 
2151  qm->thread_running = 0;
2152 
2153  return NULL;
2154 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
#define SWITCH_CHANNEL_LOG
switch_cache_db_handle_t * event_db
switch_cache_db_handle_type_t type
#define switch_cache_db_get_db_handle_dsn(_a, _b)
Definition: switch_core.h:2536
switch_status_t switch_thread_cond_wait(switch_thread_cond_t *cond, switch_mutex_t *mutex)
Definition: switch_apr.c:355
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
struct switch_runtime runtime
Definition: switch_core.c:64
switch_sql_queue_manager_t * qm
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
static uint32_t do_trans(switch_sql_queue_manager_t *qm)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
uintptr_t switch_size_t
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
switch_thread_cond_t * cond
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static uint32_t qm_ttl(switch_sql_queue_manager_t *qm)
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
static void do_flush(switch_sql_queue_manager_t *qm, int i, switch_cache_db_handle_t *dbh)
static struct @3 sql_manager

Variable Documentation

char basic_calls_sql[]
static

Definition at line 2862 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_alias_sql[]
static
Initial value:
=
"CREATE TABLE aliases (\n"
" sticky INTEGER,\n"
" alias VARCHAR(128),\n"
" command VARCHAR(4096),\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2674 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_calls_sql[]
static
Initial value:
=
"CREATE TABLE calls (\n"
" call_uuid VARCHAR(255),\n"
" call_created VARCHAR(128),\n"
" call_created_epoch INTEGER,\n"
" caller_uuid VARCHAR(256),\n"
" callee_uuid VARCHAR(256),\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2723 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_channels_sql[]
static

Definition at line 2682 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_complete_sql[]
static
Initial value:
=
"CREATE TABLE complete (\n"
" sticky INTEGER,\n"
" a1 VARCHAR(128),\n"
" a2 VARCHAR(128),\n"
" a3 VARCHAR(128),\n"
" a4 VARCHAR(128),\n"
" a5 VARCHAR(128),\n"
" a6 VARCHAR(128),\n"
" a7 VARCHAR(128),\n"
" a8 VARCHAR(128),\n"
" a9 VARCHAR(128),\n"
" a10 VARCHAR(128),\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2658 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_interfaces_sql[]
static
Initial value:
=
"CREATE TABLE interfaces (\n"
" type VARCHAR(128),\n"
" name VARCHAR(1024),\n"
" description VARCHAR(4096),\n"
" ikey VARCHAR(1024),\n"
" filename VARCHAR(4096),\n"
" syntax VARCHAR(4096),\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2733 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_nat_sql[]
static
Initial value:
=
"CREATE TABLE nat (\n"
" sticky INTEGER,\n"
" port INTEGER,\n"
" proto INTEGER,\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2754 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_registrations_sql[]
static
Initial value:
=
"CREATE TABLE registrations (\n"
" reg_user VARCHAR(256),\n"
" realm VARCHAR(256),\n"
" token VARCHAR(256),\n"
" url TEXT,\n"
" expires INTEGER,\n"
" network_ip VARCHAR(256),\n"
" network_port VARCHAR(256),\n"
" network_proto VARCHAR(256),\n"
" hostname VARCHAR(256),\n"
" metadata VARCHAR(256)\n"
");\n"

Definition at line 2763 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

char create_tasks_sql[]
static
Initial value:
=
"CREATE TABLE tasks (\n"
" task_id INTEGER,\n"
" task_desc VARCHAR(4096),\n"
" task_group VARCHAR(1024),\n"
" task_runtime BIGINT,\n"
" task_sql_manager INTEGER,\n"
" hostname VARCHAR(256)\n"
");\n"

Definition at line 2744 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

switch_mutex_t* ctl_mutex

Definition at line 67 of file switch_core_sqldb.c.

switch_thread_t* db_thread

Definition at line 62 of file switch_core_sqldb.c.

int db_thread_running

Definition at line 63 of file switch_core_sqldb.c.

switch_mutex_t* dbh_mutex

Definition at line 66 of file switch_core_sqldb.c.

char detailed_calls_sql[]
static

Definition at line 2781 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

Definition at line 68 of file switch_core_sqldb.c.

switch_mutex_t* io_mutex
switch_bool_t manage

Definition at line 64 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

switch_memory_pool_t* memory_pool

Definition at line 61 of file switch_core_sqldb.c.

int paused

Definition at line 73 of file switch_core_sqldb.c.

char recovery_sql[]
static
Initial value:
=
"CREATE TABLE recovery (\n"
" runtime_uuid VARCHAR(255),\n"
" technology VARCHAR(255),\n"
" profile_name VARCHAR(255),\n"
" hostname VARCHAR(255),\n"
" uuid VARCHAR(255),\n"
" metadata text\n"
");\n"

Definition at line 2852 of file switch_core_sqldb.c.

Referenced by switch_core_sqldb_start().

struct { ... } sql_manager
uint32_t total_handles

Definition at line 69 of file switch_core_sqldb.c.

uint32_t total_used_handles

Definition at line 70 of file switch_core_sqldb.c.