RTS API Documentation  1.10.11
Functions
Database Functions
+ Collaboration diagram for Database Functions:

Functions

switch_core_db_tswitch_core_db_open_file (const char *filename)
 Open a core db (SQLite) file. More...
 
switch_core_db_tswitch_core_db_open_in_memory (const char *uri)
 Open a core db (SQLite) in-memory. More...
 
switch_status_t switch_core_db_persistant_execute (switch_core_db_t *db, char *sql, uint32_t retries)
 Execute a sql stmt until it is accepted. More...
 
switch_status_t switch_core_db_persistant_execute_trans (switch_core_db_t *db, char *sql, uint32_t retries)
 
void switch_core_db_test_reactive (switch_core_db_t *db, char *test_sql, char *drop_sql, char *reactive_sql)
 perform a test query then perform a reactive query if the first one fails More...
 

Detailed Description

Function Documentation

◆ switch_core_db_open_file()

switch_core_db_t* switch_core_db_open_file ( const char *  filename)

Open a core db (SQLite) file.

Parameters
filenamethe path to the db file to open
Returns
the db handle

Definition at line 230 of file switch_core_db.c.

References db_pick_path(), path, SWITCH_CHANNEL_LOG, switch_core_db_close(), switch_core_db_connection_setup(), switch_core_db_errmsg(), switch_core_db_open(), SWITCH_FALSE, SWITCH_LOG_ERROR, and switch_log_printf().

Referenced by _switch_cache_db_get_db_handle().

231 {
232  switch_core_db_t *db;
233  char path[1024];
234  int db_ret;
235 
236  db_pick_path(filename, path, sizeof(path));
237  if ((db_ret = switch_core_db_open(path, &db)) != SQLITE_OK) {
238  goto end;
239  }
240  if ((db_ret = switch_core_db_connection_setup(db, SWITCH_FALSE)) != SQLITE_OK) {
241  goto end;
242  }
243 
244 end:
245  if (db_ret != SQLITE_OK) {
248  db = NULL;
249  }
250  return db;
251 }
#define SWITCH_CHANNEL_LOG
const char * switch_core_db_errmsg(switch_core_db_t *db)
int switch_core_db_close(switch_core_db_t *db)
static int switch_core_db_connection_setup(switch_core_db_t *db, switch_bool_t in_memory)
const char *const const char *const path
struct sqlite3 switch_core_db_t
int switch_core_db_open(const char *filename, switch_core_db_t **ppDb)
const char * filename
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static void db_pick_path(const char *dbname, char *buf, switch_size_t size)

◆ switch_core_db_open_in_memory()

switch_core_db_t* switch_core_db_open_in_memory ( const char *  uri)

Open a core db (SQLite) in-memory.

Parameters
urito the db to open
Returns
the db handle

Definition at line 253 of file switch_core_db.c.

References SWITCH_CHANNEL_LOG, switch_core_db_close(), switch_core_db_connection_setup(), switch_core_db_errmsg(), switch_core_db_open_v2(), SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_TRUE.

Referenced by _switch_cache_db_get_db_handle().

254 {
255  switch_core_db_t *db;
256  int db_ret;
257 
258  if ((db_ret = switch_core_db_open_v2(uri, &db)) != SQLITE_OK) {
259  goto end;
260  }
261  if ((db_ret = switch_core_db_connection_setup(db, SWITCH_TRUE)) != SQLITE_OK) {
262  goto end;
263  }
264 
265 end:
266  if (db_ret != SQLITE_OK) {
269  db = NULL;
270  }
271  return db;
272 }
#define SWITCH_CHANNEL_LOG
const char * switch_core_db_errmsg(switch_core_db_t *db)
int switch_core_db_close(switch_core_db_t *db)
int switch_core_db_open_v2(const char *filename, switch_core_db_t **ppDb)
static int switch_core_db_connection_setup(switch_core_db_t *db, switch_bool_t in_memory)
struct sqlite3 switch_core_db_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_core_db_persistant_execute()

switch_status_t switch_core_db_persistant_execute ( switch_core_db_t db,
char *  sql,
uint32_t  retries 
)

Execute a sql stmt until it is accepted.

Parameters
dbthe db handle
sqlthe sql to execute
retriesthe number of retries to use
Returns
SWITCH_STATUS_SUCCESS if successful

Definition at line 388 of file switch_core_db.c.

References switch_core_db_exec(), switch_core_db_free(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_yield.

389 {
390  char *errmsg;
392  uint8_t forever = 0;
393 
394  if (!retries) {
395  forever = 1;
396  retries = 1000;
397  }
398 
399  while (retries > 0) {
400  switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
401  if (errmsg) {
402  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
403  switch_core_db_free(errmsg);
404  switch_yield(100000);
405  retries--;
406  if (retries == 0 && forever) {
407  retries = 1000;
408  continue;
409  }
410  } else {
411  status = SWITCH_STATUS_SUCCESS;
412  break;
413  }
414  }
415 
416  return status;
417 }
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
switch_status_t
Common return values.
void switch_core_db_free(char *z)

◆ switch_core_db_persistant_execute_trans()

switch_status_t switch_core_db_persistant_execute_trans ( switch_core_db_t db,
char *  sql,
uint32_t  retries 
)

Definition at line 317 of file switch_core_db.c.

References SWITCH_CHANNEL_LOG, switch_core_db_exec(), switch_core_db_free(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_yield.

318 {
319  char *errmsg;
321  uint8_t forever = 0;
322  unsigned begin_retries = 100;
323  uint8_t again = 0;
324 
325  if (!retries) {
326  forever = 1;
327  retries = 1000;
328  }
329 
330  again:
331 
332  while (begin_retries > 0) {
333  again = 0;
334 
335  switch_core_db_exec(db, "BEGIN", NULL, NULL, &errmsg);
336 
337  if (errmsg) {
338  begin_retries--;
339  if (strstr(errmsg, "cannot start a transaction within a transaction")) {
340  again = 1;
341  } else {
342  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL Retry [%s]\n", errmsg);
343  }
344  switch_core_db_free(errmsg);
345  errmsg = NULL;
346 
347  if (again) {
348  switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
349  goto again;
350  }
351 
352  switch_yield(100000);
353 
354  if (begin_retries == 0) {
355  goto done;
356  }
357  } else {
358  break;
359  }
360 
361  }
362 
363  while (retries > 0) {
364  switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
365  if (errmsg) {
366  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
367  switch_core_db_free(errmsg);
368  errmsg = NULL;
369  switch_yield(100000);
370  retries--;
371  if (retries == 0 && forever) {
372  retries = 1000;
373  continue;
374  }
375  } else {
376  status = SWITCH_STATUS_SUCCESS;
377  break;
378  }
379  }
380 
381  done:
382 
383  switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
384 
385  return status;
386 }
#define SWITCH_CHANNEL_LOG
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
switch_status_t
Common return values.
void switch_core_db_free(char *z)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_core_db_test_reactive()

void switch_core_db_test_reactive ( switch_core_db_t db,
char *  test_sql,
char *  drop_sql,
char *  reactive_sql 
)

perform a test query then perform a reactive query if the first one fails

Parameters
dbthe db handle
test_sqlthe test sql
drop_sqlthe drop sql
reactive_sqlthe reactive sql

Definition at line 274 of file switch_core_db.c.

References runtime, SCF_AUTO_SCHEMAS, SCF_CLEAR_SQL, SWITCH_CHANNEL_LOG, switch_core_db_exec(), switch_core_db_free(), SWITCH_LOG_DEBUG, switch_log_printf(), and switch_test_flag.

275 {
276  char *errmsg;
277 
279  return;
280  }
281 
283  switch_core_db_exec(db, test_sql, NULL, NULL, NULL);
284  return;
285  }
286 
287 
288  if (db) {
289  if (test_sql) {
290  switch_core_db_exec(db, test_sql, NULL, NULL, &errmsg);
291 
292  if (errmsg) {
293  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql);
294  switch_core_db_free(errmsg);
295  errmsg = NULL;
296  if (drop_sql) {
297  switch_core_db_exec(db, drop_sql, NULL, NULL, &errmsg);
298  }
299  if (errmsg) {
300  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
301  switch_core_db_free(errmsg);
302  errmsg = NULL;
303  }
304  switch_core_db_exec(db, reactive_sql, NULL, NULL, &errmsg);
305  if (errmsg) {
306  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
307  switch_core_db_free(errmsg);
308  errmsg = NULL;
309  }
310  }
311  }
312  }
313 
314 }
#define SWITCH_CHANNEL_LOG
struct switch_runtime runtime
Definition: switch_core.c:86
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
void switch_core_db_free(char *z)
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
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.