Database Functions
[Core Library]

Collaboration diagram for Database Functions:

Functions

switch_core_db_tswitch_core_db_open_file (const char *filename)
 Open a core db (SQLite) file.
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.
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


Function Documentation

switch_core_db_t* switch_core_db_open_file ( const char *  filename  ) 

Open a core db (SQLite) file.

Parameters:
filename the path to the db file to open
Returns:
the db handle

Definition at line 181 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().

00182 {
00183         switch_core_db_t *db;
00184         char path[1024];
00185         int db_ret;
00186 
00187         db_pick_path(filename, path, sizeof(path));
00188         if ((db_ret = switch_core_db_open(path, &db)) != SQLITE_OK) {
00189                 goto end;
00190         }
00191         if ((db_ret = switch_core_db_exec(db, "PRAGMA synchronous=OFF;", NULL, NULL, NULL) != SQLITE_OK)) {
00192                 goto end;
00193         }
00194         if ((db_ret = switch_core_db_exec(db, "PRAGMA count_changes=OFF;", NULL, NULL, NULL) != SQLITE_OK)) {
00195                 goto end;
00196         }
00197         if ((db_ret = switch_core_db_exec(db, "PRAGMA cache_size=8000;", NULL, NULL, NULL) != SQLITE_OK)) {
00198                 goto end;
00199         }
00200         if ((db_ret = switch_core_db_exec(db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL) != SQLITE_OK)) {
00201                 goto end;
00202         }
00203 
00204 end:
00205         if (db_ret != SQLITE_OK) {
00206                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", switch_core_db_errmsg(db));
00207                 switch_core_db_close(db);
00208                 db = NULL;
00209         }
00210         return db;
00211 }

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:
db the db handle
sql the sql to execute
retries the number of retries to use
Returns:
SWITCH_STATUS_SUCCESS if successful

Definition at line 327 of file switch_core_db.c.

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

00328 {
00329         char *errmsg;
00330         switch_status_t status = SWITCH_STATUS_FALSE;
00331         uint8_t forever = 0;
00332 
00333         if (!retries) {
00334                 forever = 1;
00335                 retries = 1000;
00336         }
00337 
00338         while (retries > 0) {
00339                 switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
00340                 if (errmsg) {
00341                         //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
00342                         switch_core_db_free(errmsg);
00343                         switch_yield(100000);
00344                         retries--;
00345                         if (retries == 0 && forever) {
00346                                 retries = 1000;
00347                                 continue;
00348                         }
00349                 } else {
00350                         status = SWITCH_STATUS_SUCCESS;
00351                         break;
00352                 }
00353         }
00354 
00355         return status;
00356 }

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

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

00257 {
00258         char *errmsg;
00259         switch_status_t status = SWITCH_STATUS_FALSE;
00260         uint8_t forever = 0;
00261         unsigned begin_retries = 100;
00262         uint8_t again = 0;
00263 
00264         if (!retries) {
00265                 forever = 1;
00266                 retries = 1000;
00267         }
00268 
00269   again:
00270 
00271         while (begin_retries > 0) {
00272                 again = 0;
00273 
00274                 switch_core_db_exec(db, "BEGIN", NULL, NULL, &errmsg);
00275 
00276                 if (errmsg) {
00277                         begin_retries--;
00278                         if (strstr(errmsg, "cannot start a transaction within a transaction")) {
00279                                 again = 1;
00280                         } else {
00281                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL Retry [%s]\n", errmsg);
00282                         }
00283                         switch_core_db_free(errmsg);
00284                         errmsg = NULL;
00285 
00286                         if (again) {
00287                                 switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
00288                                 goto again;
00289                         }
00290 
00291                         switch_yield(100000);
00292 
00293                         if (begin_retries == 0) {
00294                                 goto done;
00295                         }
00296                 } else {
00297                         break;
00298                 }
00299 
00300         }
00301 
00302         while (retries > 0) {
00303                 switch_core_db_exec(db, sql, NULL, NULL, &errmsg);
00304                 if (errmsg) {
00305                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", errmsg);
00306                         switch_core_db_free(errmsg);
00307                         errmsg = NULL;
00308                         switch_yield(100000);
00309                         retries--;
00310                         if (retries == 0 && forever) {
00311                                 retries = 1000;
00312                                 continue;
00313                         }
00314                 } else {
00315                         status = SWITCH_STATUS_SUCCESS;
00316                         break;
00317                 }
00318         }
00319 
00320   done:
00321 
00322         switch_core_db_exec(db, "COMMIT", NULL, NULL, NULL);
00323 
00324         return status;
00325 }

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:
db the db handle
test_sql the test sql
drop_sql the drop sql
reactive_sql the reactive sql

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

00214 {
00215         char *errmsg;
00216 
00217         if (!switch_test_flag((&runtime), SCF_CLEAR_SQL)) {
00218                 return;
00219         }
00220 
00221         if (!switch_test_flag((&runtime), SCF_AUTO_SCHEMAS)) {
00222                 switch_core_db_exec(db, test_sql, NULL, NULL, NULL);
00223                 return;
00224         }
00225 
00226 
00227         if (db) {
00228                 if (test_sql) {
00229                         switch_core_db_exec(db, test_sql, NULL, NULL, &errmsg);
00230 
00231                         if (errmsg) {
00232                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\nAuto Generating Table!\n", errmsg, test_sql);
00233                                 switch_core_db_free(errmsg);
00234                                 errmsg = NULL;
00235                                 if (drop_sql) {
00236                                         switch_core_db_exec(db, drop_sql, NULL, NULL, &errmsg);
00237                                 }
00238                                 if (errmsg) {
00239                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
00240                                         switch_core_db_free(errmsg);
00241                                         errmsg = NULL;
00242                                 }
00243                                 switch_core_db_exec(db, reactive_sql, NULL, NULL, &errmsg);
00244                                 if (errmsg) {
00245                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL ERR [%s]\n[%s]\n", errmsg, reactive_sql);
00246                                         switch_core_db_free(errmsg);
00247                                         errmsg = NULL;
00248                                 }
00249                         }
00250                 }
00251         }
00252 
00253 }


Generated on Wed May 16 04:00:18 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7