Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_core_db.c File Reference


Functions

int switch_core_db_open (const char *filename, switch_core_db_t **ppDb)
int switch_core_db_close (switch_core_db_t *db)
const unsigned char * switch_core_db_column_text (switch_core_db_stmt_t *stmt, int iCol)
const char * switch_core_db_column_name (switch_core_db_stmt_t *stmt, int N)
int switch_core_db_column_count (switch_core_db_stmt_t *pStmt)
const char * switch_core_db_errmsg (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)
int switch_core_db_finalize (switch_core_db_stmt_t *pStmt)
int switch_core_db_prepare (switch_core_db_t *db, const char *zSql, int nBytes, switch_core_db_stmt_t **ppStmt, const char **pzTail)
int switch_core_db_step (switch_core_db_stmt_t *stmt)
int switch_core_db_reset (switch_core_db_stmt_t *pStmt)
int switch_core_db_bind_int (switch_core_db_stmt_t *pStmt, int i, int iValue)
int switch_core_db_bind_int64 (switch_core_db_stmt_t *pStmt, int i, int64_t iValue)
int switch_core_db_bind_text (switch_core_db_stmt_t *pStmt, int i, const char *zData, int nData, switch_core_db_destructor_type_t xDel)
int switch_core_db_bind_double (switch_core_db_stmt_t *pStmt, int i, double dValue)
int64_t switch_core_db_last_insert_rowid (switch_core_db_t *db)
int switch_core_db_get_table (switch_core_db_t *db, const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg)
void switch_core_db_free_table (char **result)
void switch_core_db_free (char *z)
int switch_core_db_changes (switch_core_db_t *db)
char * switch_mprintf (const char *zFormat,...)
switch_core_db_tswitch_core_db_open_file (char *filename)
 Open a core db (SQLite) file.
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

char* switch_mprintf const char *  zFormat,
  ...
 

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

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

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

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

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

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

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

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

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

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

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

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

00173 {
00174         va_list ap;
00175         char *z;
00176         va_start(ap, zFormat);
00177         z = sqlite3_vmprintf(zFormat, ap);
00178         va_end(ap);
00179         return z;
00180 }


Generated on Mon May 26 22:06:51 2008 for FreeSWITCH by  doxygen 1.3.9.1