libzdb概述¶
Libzdb是一个具有线程安全连接池的数据库驱动库。这个库可以透明地连接到多个数据库系统(原生支持Oracle、Mysql、PG、Sqlite),其他数据库基于libzdb的模板进行扩充实现(简称方言),当前libzdb对于各个数据库的方言放在了src/db目录下。
由于Libzdb原生不支持神通数据库,神通公司研发支持了神通数据库,方言放置在src/db/shentong目录下。libzdb通过神通的aci库与神通数据库进行交互。
libzdb的功能¶
libzdb提供以下功能:
- 用于设计可伸缩的、多线程的应用程序,可以安全地支持大量用户
- SQL访问函数,用于管理数据库访问、处理SQL语句和操作从神通数据库服务器检索到的对象
- SQL和PL/SQL查询的语句缓存
- 透明的应用程序故障转移支持
- 快速迁移ORACLE数据库中的数据至神通数据库中
libzdb运行要求¶
需要有C++编译环境和运行时库。
libzdb使用¶
由于libzdb在使用shentong方言时,需要依赖aci库,因此在应用程序连接libzdb库时,需要同时依赖aci库,且aci依赖系统的pthread库,这些都需要加入到连接选项中去,比如:
-laci -lzdb -lpthread -lm
编译时需要指定-L参数,以变能找到aci相关的库文件。
Libzdb的shentong方言,连接字符串为:
shentong://localhost:2003/osrdb?user=sysdba&password=szoscar55
我们可以基于libzdb提供的test文件进行测试是否可以正确访问神通数据库,在libzdb/test目录下,有一个select.c文件,内容如下:
#include <stdio.h>
#include <assert.h>
#include <zdb.h>
/*
This example demonstrate most of the functionality of libzdb and can be compiled with a C, OBJ-C(++) or a C++ compiler.
Compile: [gcc -std=c99|g++|clang|clang++] -o select select.c -L/<libzdb>/lib -lzdb -I/<libzdb>/include/zdb
gcc -o select select.c -L. -laci -lzdb -I ../zdb/ -lpthread -Wl,-rpath=. -std=c99
*/
int main(void) {
URL_T url = URL_new("shentong://localhost:2003/osrdb?user=sysdba&password=szoscar55");
assert(url);
ConnectionPool_T pool = ConnectionPool_new(url);
ConnectionPool_start(pool);
Connection_T con = ConnectionPool_getConnection(pool);
TRY
{
Connection_execute(con, "create table bleach(name varchar(255))");
PreparedStatement_T p = Connection_prepareStatement(con, "insert into bleach values (?)");
const char *bleach[] = {
"Ichigo Kurosaki", "Rukia Kuchiki", "Orihime Inoue", "Yasutora \"Chad\" Sado",
"Kisuke Urahara", "Uryū Ishida", "Renji Abarai", 0
};
for (int i = 0; bleach[i]; i++) {
PreparedStatement_setString(p, 1, bleach[i]);
PreparedStatement_execute(p);
}
ResultSet_T r = Connection_executeQuery(con, "select name from bleach");
while (ResultSet_next(r))
printf("%s\n", ResultSet_getString(r, 1));
Connection_execute(con, "drop table bleach;");
}
CATCH(SQLException)
{
printf("SQLException -- %s\n", Exception_frame.message);
}
FINALLY
{
Connection_close(con);
}
END_TRY;
ConnectionPool_free(&pool);
URL_free(&url);
return 0;
}
将aci库和libzdb库拷贝到与select.c相同目录,执行编译:
gcc -o select select.c -L. -laci -lzdb -I ../zdb/ -lpthread -Wl,-rpath=. -std=c99
如果正确编译并执行,即正确。
注解
部分麒麟4.0的环境在引用aci库时,可能无法链接,需要调整aci在gcc/g++的链接顺序就可以正常链接。