FreeSWITCH API Documentation  1.7.0
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_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_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 198 of file switch_core_db.c.

References db_pick_path(), SWITCH_CHANNEL_LOG, switch_core_db_close(), switch_core_db_errmsg(), switch_core_db_exec(), switch_core_db_open(), SWITCH_LOG_ERROR, and switch_log_printf().

Referenced by _switch_cache_db_get_db_handle().

199 {
200  switch_core_db_t *db;
201  char path[1024];
202  int db_ret;
203 
204  db_pick_path(filename, path, sizeof(path));
205  if ((db_ret = switch_core_db_open(path, &db)) != SQLITE_OK) {
206  goto end;
207  }
208  if ((db_ret = switch_core_db_exec(db, "PRAGMA synchronous=OFF;", NULL, NULL, NULL) != SQLITE_OK)) {
209  goto end;
210  }
211  if ((db_ret = switch_core_db_exec(db, "PRAGMA count_changes=OFF;", NULL, NULL, NULL) != SQLITE_OK)) {
212  goto end;
213  }
214  if ((db_ret = switch_core_db_exec(db, "PRAGMA cache_size=8000;", NULL, NULL, NULL) != SQLITE_OK)) {
215  goto end;
216  }
217  if ((db_ret = switch_core_db_exec(db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL) != SQLITE_OK)) {
218  goto end;
219  }
220 
221 end:
222  if (db_ret != SQLITE_OK) {
225  db = NULL;
226  }
227  return db;
228 }
#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_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
struct sqlite3 switch_core_db_t
int switch_core_db_open(const char *filename, switch_core_db_t **ppDb)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char * filename
static void db_pick_path(const char *dbname, char *buf, switch_size_t size)
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 344 of file switch_core_db.c.

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

345 {
346  char *errmsg;
348  uint8_t forever = 0;
349 
350  if (!retries) {
351  forever = 1;
352  retries = 1000;
353  }
354 
355  while (retries > 0) {
356  switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
357  if (errmsg) {
358  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
359  switch_core_db_free(errmsg);
360  switch_yield(100000);
361  retries--;
362  if (retries == 0 && forever) {
363  retries = 1000;
364  continue;
365  }
366  } else {
367  status = SWITCH_STATUS_SUCCESS;
368  break;
369  }
370  }
371 
372  return status;
373 }
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:908
switch_status_t
Common return values.
void switch_core_db_free(char *z)
switch_status_t switch_core_db_persistant_execute_trans ( switch_core_db_t db,
char *  sql,
uint32_t  retries 
)

Definition at line 273 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.

274 {
275  char *errmsg;
277  uint8_t forever = 0;
278  unsigned begin_retries = 100;
279  uint8_t again = 0;
280 
281  if (!retries) {
282  forever = 1;
283  retries = 1000;
284  }
285 
286  again:
287 
288  while (begin_retries > 0) {
289  again = 0;
290 
291  switch_core_db_exec(db, "BEGIN", NULL, NULL, &errmsg);
292 
293  if (errmsg) {
294  begin_retries--;
295  if (strstr(errmsg, "cannot start a transaction within a transaction")) {
296  again = 1;
297  } else {
298  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL Retry [%s]\n", errmsg);
299  }
300  switch_core_db_free(errmsg);
301  errmsg = NULL;
302 
303  if (again) {
304  switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
305  goto again;
306  }
307 
308  switch_yield(100000);
309 
310  if (begin_retries == 0) {
311  goto done;
312  }
313  } else {
314  break;
315  }
316 
317  }
318 
319  while (retries > 0) {
320  switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
321  if (errmsg) {
322  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
323  switch_core_db_free(errmsg);
324  errmsg = NULL;
325  switch_yield(100000);
326  retries--;
327  if (retries == 0 && forever) {
328  retries = 1000;
329  continue;
330  }
331  } else {
332  status = SWITCH_STATUS_SUCCESS;
333  break;
334  }
335  }
336 
337  done:
338 
339  switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
340 
341  return status;
342 }
#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:908
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.
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 230 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.

231 {
232  char *errmsg;
233 
235  return;
236  }
237 
239  switch_core_db_exec(db, test_sql, NULL, NULL, NULL);
240  return;
241  }
242 
243 
244  if (db) {
245  if (test_sql) {
246  switch_core_db_exec(db, test_sql, NULL, NULL, &errmsg);
247 
248  if (errmsg) {
249  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql);
250  switch_core_db_free(errmsg);
251  errmsg = NULL;
252  if (drop_sql) {
253  switch_core_db_exec(db, drop_sql, NULL, NULL, &errmsg);
254  }
255  if (errmsg) {
256  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
257  switch_core_db_free(errmsg);
258  errmsg = NULL;
259  }
260  switch_core_db_exec(db, reactive_sql, NULL, NULL, &errmsg);
261  if (errmsg) {
262  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
263  switch_core_db_free(errmsg);
264  errmsg = NULL;
265  }
266  }
267  }
268  }
269 
270 }
#define SWITCH_CHANNEL_LOG
struct switch_runtime runtime
Definition: switch_core.c:64
int switch_core_db_exec(switch_core_db_t *db, const char *sql, switch_core_db_callback_func_t callback, void *data, char **errmsg)
void switch_core_db_free(char *z)
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.