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

switch_core_db.h File Reference


Detailed Description

Core DB Header.


Defines

#define SWITCH_CORE_DB_STATIC   ((switch_core_db_destructor_type_t)0)
#define SWITCH_CORE_DB_TRANSIENT   ((switch_core_db_destructor_type_t)-1)
#define SWITCH_CORE_DB_OK   0
#define SWITCH_CORE_DB_ERROR   1
#define SWITCH_CORE_DB_INTERNAL   2
#define SWITCH_CORE_DB_PERM   3
#define SWITCH_CORE_DB_ABORT   4
#define SWITCH_CORE_DB_BUSY   5
#define SWITCH_CORE_DB_LOCKED   6
#define SWITCH_CORE_DB_NOMEM   7
#define SWITCH_CORE_DB_READONLY   8
#define SWITCH_CORE_DB_INTERRUPT   9
#define SWITCH_CORE_DB_IOERR   10
#define SWITCH_CORE_DB_CORRUPT   11
#define SWITCH_CORE_DB_NOTFOUND   12
#define SWITCH_CORE_DB_FULL   13
#define SWITCH_CORE_DB_CANTOPEN   14
#define SWITCH_CORE_DB_PROTOCOL   15
#define SWITCH_CORE_DB_EMPTY   16
#define SWITCH_CORE_DB_SCHEMA   17
#define SWITCH_CORE_DB_TOOBIG   18
#define SWITCH_CORE_DB_CONSTRAINT   19
#define SWITCH_CORE_DB_MISMATCH   20
#define SWITCH_CORE_DB_MISUSE   21
#define SWITCH_CORE_DB_NOLFS   22
#define SWITCH_CORE_DB_AUTH   23
#define SWITCH_CORE_DB_FORMAT   24
#define SWITCH_CORE_DB_RANGE   25
#define SWITCH_CORE_DB_NOTADB   26
#define SWITCH_CORE_DB_ROW   100
#define SWITCH_CORE_DB_DONE   101

Typedefs

typedef sqlite3 switch_core_db_t
typedef sqlite3_stmt switch_core_db_stmt_t
typedef int(* switch_core_db_callback_func_t )(void *pArg, int argc, char **argv, char **columnNames)
typedef void(* switch_core_db_destructor_type_t )(void *)

Functions

int switch_core_db_close (switch_core_db_t *db)
int switch_core_db_open (const char *filename, switch_core_db_t **ppDb)
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,...)


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