FreeSWITCH API Documentation  1.7.0
switch_core_sqldb.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  * Emmanuel Schmidbauer <eschmidbauer@gmail.com>
30  *
31  *
32  * switch_core_sqldb.c -- Main Core Library (statistics tracker)
33  *
34  */
35 
36 #include <switch.h>
38 
39 #define SWITCH_SQL_QUEUE_LEN 100000
40 #define SWITCH_SQL_QUEUE_PAUSE_LEN 90000
41 
46  time_t last_used;
50  int32_t flags;
51  unsigned long hash;
52  unsigned long thread_hash;
55  uint32_t use_count;
56  uint64_t total_used_count;
58 };
59 
60 static struct {
69  uint32_t total_handles;
73  int paused;
74 } sql_manager;
75 
76 
77 static void switch_core_sqldb_start_thread(void);
78 static void switch_core_sqldb_stop_thread(void);
79 
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 }
93 
94 static void add_handle(switch_cache_db_handle_t *dbh, const char *db_str, const char *db_callsite_str, const char *thread_str)
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 }
116 
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 }
137 
138 static switch_cache_db_handle_t *get_handle(const char *db_str, const char *user_str, const char *thread_str)
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 }
180 
181 /*!
182  \brief Open the default system database
183 */
184 SWITCH_DECLARE(switch_status_t) _switch_core_db_handle(switch_cache_db_handle_t **dbh, const char *file, const char *func, int line)
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 }
207 
208 #define SQL_CACHE_TIMEOUT 30
209 #define SQL_REG_TIMEOUT 15
210 
211 
212 static void sql_close(time_t prune)
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 }
284 
285 
287 {
288  return dbh->type;
289 }
290 
292 {
294 }
295 
296 
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 }
327 
328 
330 {
332 }
333 
334 
336  const char *file, const char *func, int line)
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 }
382 
383 
386  switch_cache_db_connection_options_t *connection_options,
387  const char *file, const char *func, int line)
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 }
538 
539 
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 }
601 
602 /**
603  OMFG you cruel bastards. Who chooses 64k as a max buffer len for a sql statement, have you ever heard of transactions?
604 **/
605 static switch_status_t switch_cache_db_execute_sql_chunked(switch_cache_db_handle_t *dbh, char *sql, uint32_t chunk_size, char **err)
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 }
664 
665 
667 {
669  switch_mutex_t *io_mutex = dbh->io_mutex;
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 }
686 
687 
689 {
690  switch (dbh->type) {
691  case SCDB_TYPE_CORE_DB:
692  {
693  return switch_core_db_changes(dbh->native_handle.core_db_dbh);
694  }
695  break;
696  case SCDB_TYPE_ODBC:
697  {
698  return switch_odbc_handle_affected_rows(dbh->native_handle.odbc_dbh);
699  }
700  break;
701  case SCDB_TYPE_PGSQL:
702  {
703  return switch_pgsql_handle_affected_rows(dbh->native_handle.pgsql_dbh);
704  }
705  break;
706  }
707  return 0;
708 }
709 
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);
716  return switch_core_db_load_extension(dbh->native_handle.core_db_dbh, 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 }
732 
733 
734 SWITCH_DECLARE(char *) switch_cache_db_execute_sql2str(switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err)
735 {
737  switch_mutex_t *io_mutex = dbh->io_mutex;
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 }
800 
802 {
803  char *errmsg = NULL;
805  uint8_t forever = 0;
806  switch_mutex_t *io_mutex = dbh->io_mutex;
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 }
837 
838 
840  char *sql, uint32_t retries,
841  const char *pre_trans_execute,
842  const char *post_trans_execute,
843  const char *inner_pre_trans_execute,
844  const char *inner_post_trans_execute)
845 {
846  char *errmsg = NULL;
848  uint8_t forever = 0;
849  unsigned begin_retries = 100;
850  uint8_t again = 0;
851  switch_mutex_t *io_mutex = dbh->io_mutex;
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 
883  if ((result = switch_odbc_SQLSetAutoCommitAttr(dbh->native_handle.odbc_dbh, 0)) != SWITCH_ODBC_SUCCESS) {
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 
894  if ((result = switch_pgsql_SQLSetAutoCommitAttr(dbh->native_handle.pgsql_dbh, 0)) != SWITCH_PGSQL_SUCCESS) {
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  {
921  switch_odbc_SQLEndTran(dbh->native_handle.odbc_dbh, 1);
922  switch_odbc_SQLSetAutoCommitAttr(dbh->native_handle.odbc_dbh, 1);
923  }
924  break;
925  case SCDB_TYPE_PGSQL:
926  {
927  switch_pgsql_SQLEndTran(dbh->native_handle.pgsql_dbh, 1);
928  switch_pgsql_SQLSetAutoCommitAttr(dbh->native_handle.pgsql_dbh, 1);
929  switch_pgsql_finish_results(dbh->native_handle.pgsql_dbh);
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  {
996  switch_odbc_SQLEndTran(dbh->native_handle.odbc_dbh, 1);
997  switch_odbc_SQLSetAutoCommitAttr(dbh->native_handle.odbc_dbh, 1);
998  }
999  break;
1000  case SCDB_TYPE_PGSQL:
1001  {
1002  switch_pgsql_SQLEndTran(dbh->native_handle.pgsql_dbh, 1);
1003  switch_pgsql_SQLSetAutoCommitAttr(dbh->native_handle.pgsql_dbh, 1);
1004  switch_pgsql_finish_results(dbh->native_handle.pgsql_dbh);
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 }
1021 
1022 struct helper {
1024  void *pdata;
1025 };
1026 
1027 static int helper_callback(void *pArg, int argc, char **argv, char **columnNames)
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 }
1041 
1043  const char *sql, switch_core_db_event_callback_func_t callback, void *pdata, char **err)
1044 {
1046  char *errmsg = NULL;
1047  switch_mutex_t *io_mutex = dbh->io_mutex;
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  {
1063  status = switch_pgsql_handle_callback_exec(dbh->native_handle.pgsql_dbh, sql, helper_callback, &h, err);
1064  }
1065  break;
1066  case SCDB_TYPE_ODBC:
1067  {
1068  status = switch_odbc_handle_callback_exec(dbh->native_handle.odbc_dbh, sql, helper_callback, &h, err);
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 }
1094 
1098  void *pdata, char **err)
1099 {
1101  char *errmsg = NULL;
1102  switch_mutex_t *io_mutex = dbh->io_mutex;
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  {
1118  status = switch_pgsql_handle_callback_exec(dbh->native_handle.pgsql_dbh, sql, helper_callback, &h, err);
1119  if (err && *err) {
1120  (*err_callback)(pdata, (const char*)*err);
1121  }
1122  }
1123  break;
1124  case SCDB_TYPE_ODBC:
1125  {
1126  status = switch_odbc_handle_callback_exec(dbh->native_handle.odbc_dbh, sql, helper_callback, &h, err);
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 }
1160 
1162  const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
1163 {
1165  char *errmsg = NULL;
1166  switch_mutex_t *io_mutex = dbh->io_mutex;
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  {
1178  status = switch_pgsql_handle_callback_exec(dbh->native_handle.pgsql_dbh, sql, callback, pdata, err);
1179  }
1180  break;
1181  case SCDB_TYPE_ODBC:
1182  {
1183  status = switch_odbc_handle_callback_exec(dbh->native_handle.odbc_dbh, sql, callback, pdata, err);
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 }
1209 
1212  switch_core_db_err_callback_func_t err_callback, void *pdata, char **err)
1213 {
1215  char *errmsg = NULL;
1216  switch_mutex_t *io_mutex = dbh->io_mutex;
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  {
1228  status = switch_pgsql_handle_callback_exec(dbh->native_handle.pgsql_dbh, sql, callback, pdata, err);
1229  if (err && *err) {
1230  (*err_callback)(pdata, (const char*)*err);
1231  }
1232  }
1233  break;
1234  case SCDB_TYPE_ODBC:
1235  {
1236  status = switch_odbc_handle_callback_exec(dbh->native_handle.odbc_dbh, sql, callback, pdata, err);
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 }
1270 
1272 {
1274 
1275  switch_assert(sql != NULL);
1276 
1278  r = switch_cache_db_execute_sql(dbh, sql, err);
1279  }
1280 
1281  return r;
1282 }
1283 
1284 /*!
1285  * \brief Performs test_sql and if it fails performs drop_sql and reactive_sql.
1286  *
1287  * If auto-clear-sql is disabled, then this function will do nothing and it is
1288  * assumed that the queries are not needed. If auto-create-schemas is disabled,
1289  * then just test_sql is executed, but drop_sql and reactive_sql are not.
1290  *
1291  * Otherwise, test_sql gets executed. If that succeeds, then there is nothing to
1292  * do. Otherwise drop_sql is executed (its result is ignored) and then finally
1293  * reactive_sql is executed.
1294  *
1295  * \return If auto-create-schemas is enabled, SWITCH_TRUE is returned if
1296  * test_sql succeeds, SWITCH_FALSE otherwise. If reactive_sql is executed
1297  * successfully SWITCH_TRUE is returned, otherwise SWITCH_FALSE is returned.
1298  */
1300  const char *test_sql, const char *drop_sql, const char *reactive_sql)
1301 {
1303  switch_mutex_t *io_mutex = dbh->io_mutex;
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  {
1323  if (switch_pgsql_handle_exec(dbh->native_handle.pgsql_dbh, test_sql, NULL) != SWITCH_PGSQL_SUCCESS) {
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 }
1377 
1378 
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 }
1401 
1402 
1404 
1406  const char *name;
1409  uint32_t *pre_written;
1410  uint32_t *written;
1411  uint32_t numq;
1412  char *dsn;
1424  uint32_t max_trans;
1425  uint32_t confirm;
1426  uint8_t paused;
1427 };
1428 
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 }
1455 
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 }
1467 
1468 struct db_job {
1470  char *sql;
1475  void *pdata;
1476  int event;
1478 };
1479 
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 }
1516 
1522  void *pdata)
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 }
1551 
1552 
1554  const char *sql, switch_core_db_callback_func_t callback, void *pdata)
1555 {
1556 
1558  if ((td = new_job(qm, sql, callback, NULL, NULL, NULL, pdata))) {
1560  }
1561 }
1562 
1566 {
1567 
1569  if ((td = new_job(qm, sql, callback, err_callback, NULL, NULL, pdata))) {
1571  }
1572 }
1573 
1576 {
1577 
1579  if ((td = new_job(qm, sql, NULL, NULL, callback, NULL, pdata))) {
1581  }
1582 }
1583 
1587  void *pdata)
1588 {
1589 
1591  if ((td = new_job(qm, sql, NULL, NULL, callback, err_callback, pdata))) {
1593  }
1594 }
1595 
1596 
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 }
1614 
1615 
1617 {
1618  switch_mutex_lock(qm->mutex);
1619  qm->paused = 0;
1620  switch_mutex_unlock(qm->mutex);
1621 
1622  qm_wake(qm);
1623 
1624 }
1625 
1627 {
1628  uint32_t i;
1629 
1630  switch_mutex_lock(qm->mutex);
1631  qm->paused = 1;
1632  switch_mutex_unlock(qm->mutex);
1633 
1634  if (flush) {
1635  for(i = 0; i < qm->numq; i++) {
1636  do_flush(qm, i, NULL);
1637  }
1638  }
1639 
1640 }
1641 
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  }
1650  switch_mutex_unlock(qm->mutex);
1651 
1652  return size;
1653 }
1654 
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);
1666  switch_queue_interrupt_all(qm->sql_queue[i]);
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 }
1687 
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 }
1703 
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 }
1730 
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);
1758  switch_mutex_unlock(qm->mutex);
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 }
1771 
1772 
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;
1819  switch_mutex_unlock(qm->mutex);
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--;
1837  switch_mutex_unlock(qm->mutex);
1838 #endif
1839 
1840  return SWITCH_STATUS_SUCCESS;
1841 }
1842 
1843 
1844 
1845 
1846 
1849  uint32_t numq, const char *dsn, uint32_t max_trans,
1850  const char *pre_trans_execute,
1851  const char *post_trans_execute,
1852  const char *inner_pre_trans_execute,
1853  const char *inner_post_trans_execute)
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 }
1901 
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 }
2044 
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 }
2155 
2156 
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 }
2201 
2202 
2203 #define MAX_SQL 5
2204 #define new_sql() switch_assert(sql_idx+1 < MAX_SQL); if (exists) sql[sql_idx++]
2205 #define new_sql_a() switch_assert(sql_idx+1 < MAX_SQL); sql[sql_idx++]
2206 
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 }
2656 
2657 
2658 static char create_complete_sql[] =
2659  "CREATE TABLE complete (\n"
2660  " sticky INTEGER,\n"
2661  " a1 VARCHAR(128),\n"
2662  " a2 VARCHAR(128),\n"
2663  " a3 VARCHAR(128),\n"
2664  " a4 VARCHAR(128),\n"
2665  " a5 VARCHAR(128),\n"
2666  " a6 VARCHAR(128),\n"
2667  " a7 VARCHAR(128),\n"
2668  " a8 VARCHAR(128),\n"
2669  " a9 VARCHAR(128),\n"
2670  " a10 VARCHAR(128),\n"
2671  " hostname VARCHAR(256)\n"
2672  ");\n";
2673 
2674 static char create_alias_sql[] =
2675  "CREATE TABLE aliases (\n"
2676  " sticky INTEGER,\n"
2677  " alias VARCHAR(128),\n"
2678  " command VARCHAR(4096),\n"
2679  " hostname VARCHAR(256)\n"
2680  ");\n";
2681 
2682 static char create_channels_sql[] =
2683  "CREATE TABLE channels (\n"
2684  " uuid VARCHAR(256),\n"
2685  " direction VARCHAR(32),\n"
2686  " created VARCHAR(128),\n"
2687  " created_epoch INTEGER,\n"
2688  " name VARCHAR(1024),\n"
2689  " state VARCHAR(64),\n"
2690  " cid_name VARCHAR(1024),\n"
2691  " cid_num VARCHAR(256),\n"
2692  " ip_addr VARCHAR(256),\n"
2693  " dest VARCHAR(1024),\n"
2694  " application VARCHAR(128),\n"
2695  " application_data VARCHAR(4096),\n"
2696  " dialplan VARCHAR(128),\n"
2697  " context VARCHAR(128),\n"
2698  " read_codec VARCHAR(128),\n"
2699  " read_rate VARCHAR(32),\n"
2700  " read_bit_rate VARCHAR(32),\n"
2701  " write_codec VARCHAR(128),\n"
2702  " write_rate VARCHAR(32),\n"
2703  " write_bit_rate VARCHAR(32),\n"
2704  " secure VARCHAR(64),\n"
2705  " hostname VARCHAR(256),\n"
2706  " presence_id VARCHAR(4096),\n"
2707  " presence_data VARCHAR(4096),\n"
2708  " callstate VARCHAR(64),\n"
2709  " callee_name VARCHAR(1024),\n"
2710  " callee_num VARCHAR(256),\n"
2711  " callee_direction VARCHAR(5),\n"
2712  " call_uuid VARCHAR(256),\n"
2713  " sent_callee_name VARCHAR(1024),\n"
2714  " sent_callee_num VARCHAR(256),\n"
2715  " initial_cid_name VARCHAR(1024),\n"
2716  " initial_cid_num VARCHAR(256),\n"
2717  " initial_ip_addr VARCHAR(256),\n"
2718  " initial_dest VARCHAR(1024),\n"
2719  " initial_dialplan VARCHAR(128),\n"
2720  " initial_context VARCHAR(128)\n"
2721  ");\n";
2722 
2723 static char create_calls_sql[] =
2724  "CREATE TABLE calls (\n"
2725  " call_uuid VARCHAR(255),\n"
2726  " call_created VARCHAR(128),\n"
2727  " call_created_epoch INTEGER,\n"
2728  " caller_uuid VARCHAR(256),\n"
2729  " callee_uuid VARCHAR(256),\n"
2730  " hostname VARCHAR(256)\n"
2731  ");\n";
2732 
2733 static char create_interfaces_sql[] =
2734  "CREATE TABLE interfaces (\n"
2735  " type VARCHAR(128),\n"
2736  " name VARCHAR(1024),\n"
2737  " description VARCHAR(4096),\n"
2738  " ikey VARCHAR(1024),\n"
2739  " filename VARCHAR(4096),\n"
2740  " syntax VARCHAR(4096),\n"
2741  " hostname VARCHAR(256)\n"
2742  ");\n";
2743 
2744 static char create_tasks_sql[] =
2745  "CREATE TABLE tasks (\n"
2746  " task_id INTEGER,\n"
2747  " task_desc VARCHAR(4096),\n"
2748  " task_group VARCHAR(1024),\n"
2749  " task_runtime BIGINT,\n"
2750  " task_sql_manager INTEGER,\n"
2751  " hostname VARCHAR(256)\n"
2752  ");\n";
2753 
2754 static char create_nat_sql[] =
2755  "CREATE TABLE nat (\n"
2756  " sticky INTEGER,\n"
2757  " port INTEGER,\n"
2758  " proto INTEGER,\n"
2759  " hostname VARCHAR(256)\n"
2760  ");\n";
2761 
2762 
2764  "CREATE TABLE registrations (\n"
2765  " reg_user VARCHAR(256),\n"
2766  " realm VARCHAR(256),\n"
2767  " token VARCHAR(256),\n"
2768  /* If url is modified please check for code in switch_core_sqldb_start for dependencies for MSSQL" */
2769  " url TEXT,\n"
2770  " expires INTEGER,\n"
2771  " network_ip VARCHAR(256),\n"
2772  " network_port VARCHAR(256),\n"
2773  " network_proto VARCHAR(256),\n"
2774  " hostname VARCHAR(256),\n"
2775  " metadata VARCHAR(256)\n"
2776  ");\n";
2777 
2778 
2779 
2780 
2781 static char detailed_calls_sql[] =
2782  "create view detailed_calls as select "
2783  "a.uuid as uuid,"
2784  "a.direction as direction,"
2785  "a.created as created,"
2786  "a.created_epoch as created_epoch,"
2787  "a.name as name,"
2788  "a.state as state,"
2789  "a.cid_name as cid_name,"
2790  "a.cid_num as cid_num,"
2791  "a.ip_addr as ip_addr,"
2792  "a.dest as dest,"
2793  "a.application as application,"
2794  "a.application_data as application_data,"
2795  "a.dialplan as dialplan,"
2796  "a.context as context,"
2797  "a.read_codec as read_codec,"
2798  "a.read_rate as read_rate,"
2799  "a.read_bit_rate as read_bit_rate,"
2800  "a.write_codec as write_codec,"
2801  "a.write_rate as write_rate,"
2802  "a.write_bit_rate as write_bit_rate,"
2803  "a.secure as secure,"
2804  "a.hostname as hostname,"
2805  "a.presence_id as presence_id,"
2806  "a.presence_data as presence_data,"
2807  "a.callstate as callstate,"
2808  "a.callee_name as callee_name,"
2809  "a.callee_num as callee_num,"
2810  "a.callee_direction as callee_direction,"
2811  "a.call_uuid as call_uuid,"
2812  "a.sent_callee_name as sent_callee_name,"
2813  "a.sent_callee_num as sent_callee_num,"
2814  "b.uuid as b_uuid,"
2815  "b.direction as b_direction,"
2816  "b.created as b_created,"
2817  "b.created_epoch as b_created_epoch,"
2818  "b.name as b_name,"
2819  "b.state as b_state,"
2820  "b.cid_name as b_cid_name,"
2821  "b.cid_num as b_cid_num,"
2822  "b.ip_addr as b_ip_addr,"
2823  "b.dest as b_dest,"
2824  "b.application as b_application,"
2825  "b.application_data as b_application_data,"
2826  "b.dialplan as b_dialplan,"
2827  "b.context as b_context,"
2828  "b.read_codec as b_read_codec,"
2829  "b.read_rate as b_read_rate,"
2830  "b.read_bit_rate as b_read_bit_rate,"
2831  "b.write_codec as b_write_codec,"
2832  "b.write_rate as b_write_rate,"
2833  "b.write_bit_rate as b_write_bit_rate,"
2834  "b.secure as b_secure,"
2835  "b.hostname as b_hostname,"
2836  "b.presence_id as b_presence_id,"
2837  "b.presence_data as b_presence_data,"
2838  "b.callstate as b_callstate,"
2839  "b.callee_name as b_callee_name,"
2840  "b.callee_num as b_callee_num,"
2841  "b.callee_direction as b_callee_direction,"
2842  "b.call_uuid as b_call_uuid,"
2843  "b.sent_callee_name as b_sent_callee_name,"
2844  "b.sent_callee_num as b_sent_callee_num,"
2845  "c.call_created_epoch as call_created_epoch "
2846  "from channels a "
2847  "left join calls c on a.uuid = c.caller_uuid and a.hostname = c.hostname "
2848  "left join channels b on b.uuid = c.callee_uuid and b.hostname = c.hostname "
2849  "where a.uuid = c.caller_uuid or a.uuid not in (select callee_uuid from calls)";
2850 
2851 
2852 static char recovery_sql[] =
2853  "CREATE TABLE recovery (\n"
2854  " runtime_uuid VARCHAR(255),\n"
2855  " technology VARCHAR(255),\n"
2856  " profile_name VARCHAR(255),\n"
2857  " hostname VARCHAR(255),\n"
2858  " uuid VARCHAR(255),\n"
2859  " metadata text\n"
2860  ");\n";
2861 
2862 static char basic_calls_sql[] =
2863  "create view basic_calls as select "
2864  "a.uuid as uuid,"
2865  "a.direction as direction,"
2866  "a.created as created,"
2867  "a.created_epoch as created_epoch,"
2868  "a.name as name,"
2869  "a.state as state,"
2870  "a.cid_name as cid_name,"
2871  "a.cid_num as cid_num,"
2872  "a.ip_addr as ip_addr,"
2873  "a.dest as dest,"
2874 
2875  "a.presence_id as presence_id,"
2876  "a.presence_data as presence_data,"
2877  "a.callstate as callstate,"
2878  "a.callee_name as callee_name,"
2879  "a.callee_num as callee_num,"
2880  "a.callee_direction as callee_direction,"
2881  "a.call_uuid as call_uuid,"
2882  "a.hostname as hostname,"
2883  "a.sent_callee_name as sent_callee_name,"
2884  "a.sent_callee_num as sent_callee_num,"
2885 
2886 
2887  "b.uuid as b_uuid,"
2888  "b.direction as b_direction,"
2889  "b.created as b_created,"
2890  "b.created_epoch as b_created_epoch,"
2891  "b.name as b_name,"
2892  "b.state as b_state,"
2893  "b.cid_name as b_cid_name,"
2894  "b.cid_num as b_cid_num,"
2895  "b.ip_addr as b_ip_addr,"
2896  "b.dest as b_dest,"
2897 
2898  "b.presence_id as b_presence_id,"
2899  "b.presence_data as b_presence_data,"
2900  "b.callstate as b_callstate,"
2901  "b.callee_name as b_callee_name,"
2902  "b.callee_num as b_callee_num,"
2903  "b.callee_direction as b_callee_direction,"
2904  "b.sent_callee_name as b_sent_callee_name,"
2905  "b.sent_callee_num as b_sent_callee_num,"
2906  "c.call_created_epoch as call_created_epoch "
2907 
2908  "from channels a "
2909  "left join calls c on a.uuid = c.caller_uuid and a.hostname = c.hostname "
2910  "left join channels b on b.uuid = c.callee_uuid and b.hostname = c.hostname "
2911  "where a.uuid = c.caller_uuid or a.uuid not in (select callee_uuid from calls)";
2912 
2913 
2914 
2915 SWITCH_DECLARE(void) switch_core_recovery_flush(const char *technology, const char *profile_name)
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 }
2948 
2949 
2950 static int recover_callback(void *pArg, int argc, char **argv, char **columnNames)
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 }
3048 
3049 SWITCH_DECLARE(int) switch_core_recovery_recover(const char *technology, const char *profile_name)
3050 
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 }
3128 
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 }
3141 
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 }
3155 
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 }
3193 
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 }
3241 
3242 
3243 
3244 SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, const char *realm, const char *token, const char *url, uint32_t expires,
3245  const char *network_ip, const char *network_port, const char *network_proto,
3246  const char *metadata)
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 }
3298 
3299 SWITCH_DECLARE(switch_status_t) switch_core_del_registration(const char *user, const char *realm, const char *token)
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 }
3319 
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 }
3343 
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 }
3634 
3636 {
3637  if (sql_manager.paused) {
3638  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SQL is already paused.\n");
3639  }
3640  sql_manager.paused = 1;
3641 }
3642 
3644 {
3645  if (!sql_manager.paused) {
3646  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SQL is already running.\n");
3647  }
3648  sql_manager.paused = 0;
3649 }
3650 
3651 
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 }
3665 
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 }
3698 
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 }
3715 
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 }
3789 
3791 {
3793  return "+";
3794 
3795  return "||";
3796 }
3797 
3798 /* For Emacs:
3799  * Local Variables:
3800  * mode:c
3801  * indent-tabs-mode:t
3802  * tab-width:4
3803  * c-basic-offset:4
3804  * End:
3805  * For VIM:
3806  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
3807  */
struct apr_queue_t switch_queue_t
Definition: switch_apr.h:590
#define SWITCH_SQL_QUEUE_LEN
#define switch_event_get_header_nil(e, h)
Definition: switch_event.h:174
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_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
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
#define switch_strdup(ptr, s)
switch_event_types_t event_id
Definition: switch_event.h:82
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
char * core_db_inner_post_trans_execute
switch_cache_db_native_handle_t native_handle
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.
char name[CACHE_DB_LEN]
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
switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage)
switch_status_t switch_thread_cond_create(switch_thread_cond_t **cond, switch_memory_pool_t *pool)
Definition: switch_apr.c:350
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
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_mutex_t * io_mutex
An Abstract Representation of a dialplan extension.
#define SWITCH_THREAD_FUNC
switch_odbc_status_t
Definition: switch_odbc.h:49
switch_bool_t switch_pgsql_available(void)
Definition: switch_pgsql.c:819
#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.
switch_status_t switch_mutex_trylock(switch_mutex_t *lock)
Definition: switch_apr.c:295
switch_cache_db_handle_t * event_db
static char create_interfaces_sql[]
struct switch_odbc_handle switch_odbc_handle_t
static switch_cache_db_handle_t * create_handle(switch_cache_db_handle_type_t type)
Abstraction of an module endpoint interface This is the glue between the abstract idea of a "channel"...
uint32_t total_handles
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_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.
static void del_handle(switch_cache_db_handle_t *dbh)
switch_bool_t switch_ivr_uuid_exists(const char *uuid)
Definition: switch_ivr.c:3862
char * core_db_post_trans_execute
switch_cache_db_handle_type_t type
switch_cache_db_core_db_options_t core_db_options
Definition: switch_core.h:2473
int switch_odbc_handle_affected_rows(switch_odbc_handle_t *handle)
Definition: switch_odbc.c:729
switch_bool_t
Definition: switch_types.h:405
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)
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_core_db_event_callback_func_t callback
struct switch_pgsql_handle switch_pgsql_handle_t
#define switch_split(_data, _delim, _array)
Definition: switch_utils.h:342
void switch_odbc_handle_destroy(switch_odbc_handle_t **handlep)
Definition: switch_odbc.c:671
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_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
#define SWITCH_EVENT_SUBCLASS_ANY
Definition: switch_event.h:128
switch_memory_pool_t * pool
Representation of an event.
Definition: switch_event.h:80
#define SQL_CACHE_TIMEOUT
switch_core_session_t * switch_core_session_request_xml(switch_endpoint_interface_t *endpoint_interface, switch_memory_pool_t **pool, switch_xml_t xml)
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
static char create_tasks_sql[]
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
void switch_sql_queue_manager_pause(switch_sql_queue_manager_t *qm, switch_bool_t flush)
static const char * switch_cache_db_type_name(switch_cache_db_handle_type_t type)
Definition: switch_core.h:2481
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:98
void switch_sql_queue_manager_resume(switch_sql_queue_manager_t *qm)
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)
int switch_core_db_close(switch_core_db_t *db)
A representation of an XML tree.
Definition: switch_xml.h:76
#define switch_cache_db_get_db_handle_dsn(_a, _b)
Definition: switch_core.h:2536
#define end_of(_s)
Definition: switch_utils.h:616
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.
switch_status_t switch_thread_cond_wait(switch_thread_cond_t *cond, switch_mutex_t *mutex)
Definition: switch_apr.c:355
switch_status_t switch_core_del_registration(const char *user, const char *realm, const char *token)
Delete user registration.
switch_pgsql_status_t switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on)
Definition: switch_pgsql.c:828
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.
switch_memory_pool_t * pool
char creator[CACHE_DB_LEN]
switch_memory_pool_t * memory_pool
#define SWITCH_CORE_DB_BUSY
switch_status_t switch_sql_queue_manager_stop(switch_sql_queue_manager_t *qm)
void switch_core_recovery_track(switch_core_session_t *session)
int switch_core_recovery_recover(const char *technology, const char *profile_name)
int(* switch_core_db_callback_func_t)(void *pArg, int argc, char **argv, char **columnNames)
static switch_thread_t * thread
Definition: switch_log.c:279
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
int switch_core_db_finalize(switch_core_db_stmt_t *pStmt)
void switch_core_recovery_flush(const char *technology, const char *profile_name)
struct switch_runtime runtime
Definition: switch_core.c:64
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
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)
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.
char last_user[CACHE_DB_LEN]
switch_sql_queue_manager_t * qm
static void switch_core_sqldb_start_thread(void)
switch_hash_t * hash
Definition: switch_event.c:74
struct apr_thread_cond_t switch_thread_cond_t
Definition: switch_apr.h:463
#define end_of_p(_s)
Definition: switch_utils.h:617
#define zstr(x)
Definition: switch_utils.h:281
int(* switch_core_db_event_callback_func_t)(void *pArg, switch_event_t *event)
Definition: switch_core.h:2438
int switch_core_db_step(switch_core_db_stmt_t *stmt)
switch_mutex_t * dbh_mutex
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_mutex_t * mutex
int(* switch_core_db_err_callback_func_t)(void *pArg, const 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_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, const char *sql, uint32_t retries)
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
_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.
static void add_handle(switch_cache_db_handle_t *dbh, const char *db_str, const char *db_callsite_str, const char *thread_str)
struct switch_cache_db_handle * next
#define UNPROTECT_INTERFACE(_it)
#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
int switch_sql_queue_manager_size(switch_sql_queue_manager_t *qm, uint32_t index)
void switch_cache_db_flush_handles(void)
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)
static char recovery_sql[]
static char * parse_presence_data_cols(switch_event_t *event)
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
void switch_core_sqldb_stop(void)
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
switch_xml_t next
Definition: switch_xml.h:88
#define new_sql_a()
switch_cache_db_handle_type_t switch_cache_db_get_type(switch_cache_db_handle_t *dbh)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
#define SQL_REG_TIMEOUT
switch_channel_t * channel
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 uint32_t do_trans(switch_sql_queue_manager_t *qm)
#define MAX_SQL
char * switch_core_get_uuid(void)
Retrieve the unique identifier from the core.
Definition: switch_core.c:482
switch_core_db_err_callback_func_t event_err_callback
static char create_channels_sql[]
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
int switch_cache_db_affected_rows(switch_cache_db_handle_t *dbh)
Get the affected rows of the last performed query.
intptr_t switch_ssize_t
switch_cache_db_handle_type_t
Definition: switch_core.h:2446
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
#define switch_channel_get_variable(_c, _v)
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:551
#define new_sql()
int switch_core_db_load_extension(switch_core_db_t *db, const char *extension)
#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[]
switch_thread_start_t func
Definition: switch_core.h:66
#define switch_pgsql_finish_results(handle)
Definition: switch_pgsql.h:107
switch_core_db_err_callback_func_t err_callback
#define CACHE_DB_LEN
Definition: switch_core.h:2440
switch_memory_pool_t * pool
static void *SWITCH_THREAD_FUNC switch_user_sql_thread(switch_thread_t *thread, void *obj)
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
void switch_core_sqldb_resume(void)
switch_sql_queue_manager_t * qm
switch_bool_t manage
#define switch_pgsql_handle_exec_string(handle, sql, resbuf, len, err)
Definition: switch_pgsql.h:121
switch_bool_t switch_odbc_available(void)
Definition: switch_odbc.c:738
void switch_core_sql_exec(const char *sql)
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)
char * switch_snprintfv(char *zBuf, int n, const char *zFormat,...)
void switch_channel_set_caller_extension(switch_channel_t *channel, switch_caller_extension_t *caller_extension)
Assign a caller extension to a given channel.
switch_pgsql_status_t switch_pgsql_flush(switch_pgsql_handle_t *handle)
Definition: switch_pgsql.c:842
#define switch_pgsql_handle_exec(handle, sql, err)
Definition: switch_pgsql.h:117
int switch_cache_db_load_extension(switch_cache_db_handle_t *dbh, const char *extension)
load an external extension to db
uintptr_t switch_size_t
uint32_t db_handle_timeout
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.
static char create_calls_sql[]
pthread_t switch_thread_id_t
Definition: switch_apr.h:51
void switch_cond_next(void)
Definition: switch_time.c:638
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)
static char detailed_calls_sql[]
int paused
void switch_cache_db_status(switch_stream_handle_t *stream)
Provides some feedback as to the status of the db connection pool.
#define SWITCH_STANDARD_STREAM(s)
switch_odbc_handle_t * odbc_dbh
Definition: switch_core.h:2454
switch_channel_callstate_t
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
#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
switch_cache_db_odbc_options_t odbc_options
Definition: switch_core.h:2474
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.
static int32_t running
switch_cache_db_handle_t * handle_pool
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
switch_cache_db_handle_t * dbh
apr_pool_t * pool
Definition: switch_apr.c:626
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
#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
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
void switch_core_sqldb_pause(void)
switch_memory_pool_t * pool
switch_status_t switch_thread_cond_signal(switch_thread_cond_t *cond)
Definition: switch_apr.c:371
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_core_recovery_untrack(switch_core_session_t *session, switch_bool_t force)
switch_core_recover_callback_t recover_callback
switch_channel_state_t
Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA are ...
static switch_cache_db_handle_t * get_handle(const char *db_str, const char *user_str, const char *thread_str)
uint32_t total_used_handles
static const char * ep
Definition: switch_json.c:36
switch_core_db_t * core_db_dbh
Definition: switch_core.h:2453
switch_stream_handle_write_function_t write_function
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
static char basic_calls_sql[]
switch_pgsql_handle_t * pgsql_dbh
Definition: switch_core.h:2455
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)
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)
switch_endpoint_interface_t * switch_loadable_module_get_endpoint_interface(const char *name)
Retrieve the endpoint interface by it's registered name.
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)
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_status_t switch_core_expire_registration(int force)
Expire user registrations.
static int helper_callback(void *pArg, int argc, char **argv, char **columnNames)
struct sqlite3 switch_core_db_t
switch_status_t switch_queue_interrupt_all(switch_queue_t *queue)
Definition: switch_apr.c:1145
struct apr_thread_t switch_thread_t
Definition: switch_apr.h:941
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
Main Library Header.
static int recover_callback(void *pArg, int argc, char **argv, char **columnNames)
switch_core_db_callback_func_t callback
#define SWITCH_DECLARE(type)
int switch_pgsql_handle_affected_rows(switch_pgsql_handle_t *handle)
Definition: switch_pgsql.c:810
uint32_t max_db_handles
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
switch_odbc_status_t switch_odbc_SQLEndTran(switch_odbc_handle_t *handle, switch_bool_t commit)
Definition: switch_odbc.c:760
#define switch_channel_set_flag(_c, _f)
char * switch_xml_toxml_nolock(switch_xml_t xml, _In_ switch_bool_t prn_header)
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 ...
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.
#define SWITCH_CORE_DB_ABORT
switch_pgsql_handle_t * switch_pgsql_handle_new(const char *dsn)
Create a new handle for the PGSQL connection.
Definition: switch_pgsql.c:66
static char create_registrations_sql[]
static int qm_wake(switch_sql_queue_manager_t *qm)
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:665
switch_thread_cond_t * cond
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1155
switch_status_t switch_event_create_array_pair(switch_event_t **event, char **names, char **vals, int len)
char * core_db_pre_trans_execute
static void switch_core_sqldb_stop_thread(void)
static const char * skip(const char *in)
Definition: switch_json.c:270
void switch_core_db_free(char *z)
int(* switch_core_recover_callback_t)(switch_core_session_t *session)
struct sqlite3_stmt switch_core_db_stmt_t
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
switch_status_t switch_sql_queue_manager_destroy(switch_sql_queue_manager_t **qmp)
#define SWITCH_CORE_DB_ROW
switch_core_recover_callback_t switch_core_get_secondary_recover_callback(const char *key)
switch_status_t switch_sql_queue_manager_start(switch_sql_queue_manager_t *qm)
#define SWITCH_MAX_TRANS
Definition: switch_types.h:231
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
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.
switch_status_t switch_event_unbind_callback(switch_event_callback_t callback)
#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)
int switch_core_db_changes(switch_core_db_t *db)
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_sql_queue_manager_push(switch_sql_queue_manager_t *qm, const char *sql, uint32_t pos, switch_bool_t dup)
int switch_core_db_column_count(switch_core_db_stmt_t *pStmt)
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
static uint32_t qm_ttl(switch_sql_queue_manager_t *qm)
switch_thread_id_t switch_thread_self(void)
Definition: switch_apr.c:79
void switch_event_destroy(switch_event_t **event)
Destroy an event.
char * switch_sql_concat(void)
switch_mutex_t * ctl_mutex
switch_status_t switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
Executes the sql.
switch_core_db_event_callback_func_t event_callback
const unsigned char * switch_core_db_column_text(switch_core_db_stmt_t *stmt, int iCol)
static char create_complete_sql[]
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_pgsql_handle_destroy(switch_pgsql_handle_t **handlep)
Definition: switch_pgsql.c:764
char * filename
static void core_event_handler(switch_event_t *event)
char * core_db_inner_pre_trans_execute
void switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Clear given flag(s) from a channel.
#define switch_assert(expr)
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)
switch_odbc_handle_t * switch_odbc_handle_new(const char *dsn, const char *username, const char *password)
Definition: switch_odbc.c:65
switch_mutex_t * io_mutex
static void sql_close(time_t prune)
switch_thread_t * db_thread
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_core_db_t * switch_core_db_open_file(const char *filename)
Open a core db (SQLite) file.
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
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.
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 void *SWITCH_THREAD_FUNC sql_in_thread(switch_thread_t *thread, void *obj)
memset(buf, 0, buflen)
void switch_core_media_recover_session(switch_core_session_t *session)
switch_odbc_status_t switch_odbc_SQLSetAutoCommitAttr(switch_odbc_handle_t *handle, switch_bool_t on)
Definition: switch_odbc.c:747
int switch_core_db_prepare(switch_core_db_t *db, const char *zSql, int nBytes, switch_core_db_stmt_t **ppStmt, const char **pzTail)
static void do_flush(switch_sql_queue_manager_t *qm, int i, switch_cache_db_handle_t *dbh)
static char create_alias_sql[]
static struct @3 sql_manager
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.
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_session_thread_launch(_In_ switch_core_session_t *session)
Launch the session thread (state machine) on a given session.
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
int db_thread_running
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_cache_db_handle_type_t switch_core_dbtype(void)
switch_status_t switch_threadattr_priority_set(switch_threadattr_t *attr, switch_thread_priority_t priority)
Definition: switch_apr.c:665