# $Id: db-api.txt 5903 2009-07-21 08:49:04Z bogdan_iancu $
#
Generic Database Interface
--------------------------
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.
Notes:
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.
1 Data types
There are several new data types. All of them are defined in header file db.h,
a client must include the header file to be able to use them.
1.1 Type db_con_t
1.1.1 Description
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.
1.1.2 Definition
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;
1.1.3 Macros
There are no macros for db_con_t type.
1.2 Type db_key_t
1.2.1 Description
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*.
1.2.2 Definition
typedef const char* db_key_t;
1.2.3 Macros
There are no macros (It is not needed).
1.3 Type db_type_t
1.3.1 Description
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.
1.3.2 Definition
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_BITMAP /* Bitmap, one-dimensional array of flags */
} db_type_t;
1.3.3 Macros
There are no macros.
1.4 Type db_val_t
1.4.1 Description
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
DB_BITMAP - Value in the database represents an array of flags
These datatypes are automaticaly recognized, converted from internal database
representation and stored in the variable of corresponding type.
1.4.2 Definition
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 */
unsigned int bitmap_val; /* Array of flags */
} val;
} db_val_t;
1.4.3 Macros
Note: All macros expect reference to db_val_t variable as the parameter.
1.4.3.1 VAL_TYPE(value) Macro
Use this macro if you need to set/get the type of the value
Example: VAL_TYPE(val) = DB_INT;
if (VAL_TYPE(val) == DB_FLOAT) ...
1.4.3.2 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: if (VAL_NULL(val) == 1) {
printf("The cell is NULL");
}
1.4.3.3 VAL_INT(value) Macro
Use this macro if you need to access integer value in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_INT) {
printf("%d", VAL_INT(val));
}
1.4.3.4 VAL_DOUBLE(value) Macro
Use this macro if you need to access double value in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_DOUBLE) {
printf("%f", VAL_DOUBLE(val));
}
1.4.3.5 VAL_TIME(value) Macro
Use this macro if you need to access time_t value in the db_val_t structure.
Example: time_t tim;
if (VAL_TYPE(val) == DB_DATETIME) {
tim = VAL_TIME(val);
}
1.4.3.6 VAL_STRING(value) Macro
Use this macro if you need to access string value in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_STRING) {
printf("%s", VAL_STRING(val));
}
1.4.3.7 VAL_STR(value) Macro
Use this macro if you need to access str structure in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_STR) {
printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
}
1.4.3.8 VAL_BLOB(value) Macro
Use this macro if you need to access blob value in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_BLOB) {
printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s);
}
1.4.3.9 VAL_BITMAP(value) Macro
Use this macro if you need to access bitmap value in the db_val_t structure.
Example: if (VAL_TYPE(val) == DB_BITMAP) {
printf("%d", VAL_BITMAP(val));
}
1.5 Type db_row_t
1.5.1 Description
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.
1.5.2 Definition
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;
1.5.3 Macros
1.5.3.1 ROW_VALUES(row) Macro
Use this macro to get pointer to the array of db_val_t structures.
Example: db_val_t* v = ROW_VALUES(row);
if (VAL_TYPE(v) == DB_INT) ....
1.5.3.2 ROW_N(row) Macro
Use this macro to get number of cells in the row.
Example: 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;
...
}
}
1.6 Type db_res_t
1.6.1 Description
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).
Note: A variable of type db_res_t returned by db_query function uses dynamicaly
allocated memory, don't forget to call db_free_result 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).
1.6.2 Definition
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;
1.6.3 Macros
1.6.3.1 RES_NAMES(res) Macro
Use this macro if you want to obtain pointer to the array of cell names.
Example: db_key_t* column_names = ROW_NAMES(row);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共2000个文件
h:598个
c:579个
xml:340个
OpenSIPS是一个成熟的开源SIP服务器,除了提供基本的SIP代理及SIP路由功能外,还提供了一些应用级的功能。OpenSIPS的结构非常灵活,其核心路由功能完全通过脚本来实现,可灵活定制各种路由策略,可灵活应用于语音、视频通信、IM以及Presence等多种应用。同时OpenSIPS性能上是目前最快的SIP服务器之一,可用于电信级产品构建。
资源推荐
资源详情
资源评论
收起资源包目录
opensips-1.6.4-2-notls_src.tar.gz (2000个子文件)
00list 1B
00list 1B
opensips.cfg.5 1KB
osipsconsole.8 10KB
opensipsctl.8 4KB
opensips.8 4KB
bdb_recover.8 3KB
opensipsdbctl.8 2KB
opensipsunix.8 1KB
acc 275B
acc 164B
active_watchers 491B
active_watchers 372B
address 211B
address 115B
aliases 413B
aliases 254B
register.allow 581B
permissions.allow 509B
AUTHORS 4KB
b2b_entities 532B
b2b_entities 473B
b2b_logic 515B
b2b_logic 411B
bad_table_auto_dupe 111B
bad_table_int 114B
bad_table_long_row 112B
bad_table_null 109B
bad_table_short_row 108B
bad_table_wrong_type 84B
opensipsctl.base 17KB
opensipsdbctl.base 5KB
base-pkginfo 252B
base-Prototype 18KB
berkeley-pkginfo 303B
berkeley-postinstall 169B
berkeley-preinstall 501B
berkeley-Prototype 4KB
pvar.c 97KB
nathelper.c 97KB
logic.c 78KB
subscribe.c 57KB
lcr_mod.c 57KB
mediaproxy.c 57KB
msg_translator.c 55KB
nat_traversal.c 55KB
notify.c 53KB
dlg.c 53KB
b2b_logic.c 52KB
transformations.c 50KB
t_reply.c 48KB
abyss_server.c 47KB
route.c 47KB
seas_action.c 45KB
tcp_main.c 45KB
textops.c 44KB
presentity.c 43KB
parse_via.c 42KB
siptrace.c 41KB
cpl_parser.c 41KB
avpops_impl.c 40KB
tree234.c 40KB
tm.c 39KB
ratelimit.c 39KB
identity.c 38KB
abyss_xmlrpc_server.c 37KB
dlg_handlers.c 37KB
t_lookup.c 37KB
drouting.c 36KB
resolve.c 36KB
msilo.c 36KB
dispatch.c 35KB
xjab_worker.c 35KB
main.c 35KB
sipheader.c 33KB
acc.c 33KB
sdp.c 33KB
stun.c 33KB
socket_info.c 33KB
action.c 32KB
event_dispatcher.c 32KB
dialog.c 31KB
call_control.c 30KB
sst_handlers.c 29KB
imc_cmd.c 29KB
dbase.c 29KB
dlg_hash.c 29KB
enum.c 29KB
avpops.c 28KB
timer.c 28KB
route_fifo.c 28KB
iniparser.c 28KB
presence.c 27KB
loose.c 27KB
b2b_entities.c 27KB
parse_fline.c 27KB
cpl_run.c 27KB
sip_msg.c 27KB
t_msgbuilder.c 26KB
xsnprintf.c 26KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
hopelin66
- 粉丝: 7
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功