SIP Express Router v0.8.8 - Developer's Guide | ||
---|---|---|
<<< Previous | Next >>> |
This is a generic database interface for modules that need to utilize a database. The interface should be used by all modules that access database. The interface will be independent of the underlying database server.
![]() | If possible, use predefined macros if you need to access any structure attributes. For additional description, see comments in sources of mysql module. If you want to see more complicated examples of how the API could be used, see sources of dbexample, usrloc or auth modules. |
There are several data types. All of them are defined in header files under db subdirectory, a client must include db.h header file to be able to use them.
This type represents a database connection, all database functions (described below) use a variable of this type as one argument. In other words, variable of db_con_t type serves as a handle for a particular database connection.
typedef struct db_con { char* table; /* Default table to use */ void* con; /* Database connection */ void* res; /* Result of previous operation */ void* row; /* Internal, not for public use */ int connected; /* 1 if connection is established */ } db_con_t; |
There are no macros defined for db_con_t type.
This type represents a database key. Every time you need to specify a key value, this type should be used. In fact, this type is identical to const char*.
typedef const char* db_key_t; |
There are no macros defined (they are not needed).
Each cell in a database table can be of a different type. To distinguish among these types, the db_type_t enumeration is used. Every value of the enumeration represents one datatype that is recognized by the database API. This enumeration is used in conjunction with db_type_t. For more information, see the next section.
typedef enum { DB_INT, /* Integer number */ DB_DOUBLE, /* Decimal number */ DB_STRING, /* String */ DB_STR, /* str structure */ DB_DATETIME /* Date and time */ DB_BLOB /* Binary large object */ } db_type_t; |
There are no macros defined.
This structure represents a value in the database. Several datatypes are recognized and converted by the database API:
DB_INT - Value in the database represents an integer number.
DB_DOUBLE - Value in the database represents a decimal number.
DB_STRING - Value in the database represents a string.
DB_STR - Value in the database represents a string.
DB_DATETIME - Value in the database represents date and time.
DB_BLOB - Value in the database represents binary large object.
typedef struct db_val { db_type_t type; /* Type of the value */ int nul; /* NULL flag */ union { int int_val; /* Integer value */ double double_val; /* Double value */ time_t time_val; /* Unix time_t value */ const char* string_val; /* Zero terminated string */ str str_val; /* str structure */ str blob_val; /* Structure describing blob */ } val; } db_val_t; |
![]() | All macros expect pinter to db_val_t variable as a parameter. |
VAL_TYPE(value) Macro.
Use this macro if you need to set/get the type of the value
Example 1. VAL_TYPE Macro
... VAL_TYPE(val) = DB_INT; if (VAL_TYPE(val) == DB_FLOAT) ... |
VAL_NULL(value) Macro.
Use this macro if you need to set/get the null flag. Non-zero flag means that the corresponding cell in the database contained no data (NULL value in MySQL terminology).
Example 2. VAL_NULL Macro
... if (VAL_NULL(val) == 1) { printf("The cell is NULL"); } ... |
VAL_INT(value) Macro.
Use this macro if you need to access integer value in db_val_t structure.
Example 3. VAL_INT Macro
... if (VAL_TYPE(val) == DB_INT) { printf("%d", VAL_INT(val)); } ... |
VAL_DOUBLE(value) Macro.
Use this macro if you need to access double value in the db_val_t structure.
Example 4. VAL_DOUBLE Macro
... if (VAL_TYPE(val) == DB_DOUBLE) { printf("%f", VAL_DOUBLE(val)); } ... |
VAL_TIME(value) Macro.
Use this macro if you need to access time_t value in db_val_t structure.
Example 5. VAL_TIME Macro
... time_t tim; if (VAL_TYPE(val) == DB_DATETIME) { tim = VAL_TIME(val); } ... |
VAL_STRING(value) Macro.
Use this macro if you need to access string value in db_val_t structure.
Example 6. VAL_STRING Macro
... if (VAL_TYPE(val) == DB_STRING) { printf("%s", VAL_STRING(val)); } ... |
VAL_STR(value) Macro.
Use this macro if you need to access str structure in db_val_t structure.
Example 7. VAL_STR Macro
... if (VAL_TYPE(val) == DB_STR) { printf("%.*s", VAL_STR(val).len, VAL_STR(val).s); } ... |
VAL_BLOB(value) Macro.
Use this macro if you need to access blob value in db_val_t structure.
Example 8. VAL_STR Macro
... if (VAL_TYPE(val) == DB_BLOB) { printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s); } ... |
This type represents one row in a database table. In other words, the row is an array of db_val_t variables, where each db_val_t variable represents exactly one cell in the table.
typedef struct db_row { db_val_t* values; /* Array of values in the row */ int n; /* Number of values in the row */ } db_val_t; |
ROW_VALUES(row) Macro.
Use this macro to get pointer to array of db_val_t structures.
Example 9. ROW_VALUES Macro
... db_val_t* v = ROW_VALUES(row); if (VAL_TYPE(v) == DB_INT) ... |
ROW_N(row) Macro.
Use this macro to get number of cells in a row.
Example 10. ROW_N Macro
... db_val_t* val = ROW_VALUES(row); for(i = 0; i < ROW_N(row); i++) { switch(VAL_TYPE(val + i)) { case DB_INT: ...; break; case DB_DOUBLE: ...; break; ... } } ... |
This type represents a result returned by db_query function (see below). The result can consist of zero or more rows (see db_row_t description).
![]() | A variable of type db_res_t returned by db_query function uses dynamically allocated memory, don't forget to call db_free_query if you don't need the variable anymore. You will encounter memory leaks if you fail to do this ! |
In addition to zero or more rows, each db_res_t object contains also an array of db_key_t objects. The objects represent keys (names of columns).
typedef struct db_res { struct { db_key_t* keys; /* Array of column names */ db_type_t* types; /* Array of column types */ int n; /* Number of columns */ } col; struct db_row* rows; /* Array of rows */ int n; /* Number of rows */ } db_res_t; |
RES_NAMES(res) Macro.
Use this macro if you want to obtain pointer to an array of cell names.
Example 11. RES_NAMES Macro
... db_key_t* column_names = ROW_NAMES(row); ... |
RES_COL_N(res) Macro.
Use this macro if you want to get the number of columns in the result.
Example 12. RES_COL_N Macro
... int ncol = RES_COL_N(res); for(i = 0; i < ncol; i++) { /* do something with the column */ } ... |
RES_ROWS(res) Macro.
Use this macro if you need to obtain pointer to array of rows.
Example 13. RES_ROWS Macro
... db_row_t* rows = RES_ROWS(res); ... |
RES_ROW_N(res) Macro.
Use this macro if you need to obtain the number of rows in the result.
Example 14. RES_ROW_N Macro
... int n = RES_ROW_N(res); ... |
<<< Previous | Home | Next >>> |
Additional Functions | Functions |