您的位置:首页 > 其它

OTL简介 - cdone的专栏 - 博客频道 - CSDN.NET

2013-03-06 17:09 459 查看
OTL简介 - cdone的专栏 - 博客频道 - CSDN.NET

OTL简介

2008-12-31 14:39 1419人阅读 评论(0) 收藏 举报exceptionstreamdb2数据库oraclesql server目录(?)[+]

特点
类介绍otl_connect
otl_stream
otl_exception

example
OOTL简介

1 特点:

Ø 跨数据库平台:

n 支持 oracle 的OCI

n 支持DB2的CLI,

n 支持ODBC(通过ODBC,可支持SQL SERVER,MySQL等)

Ø 跨OS平台:

n 标准C++语言,支持 unix/linux/windows

Ø 使用简单:

n 只有一个头文件;

n 接口简洁.otl_stream, otl_connect, otl_exception等就可以完成大部分工作;

n 相对 ProC等嵌入式开发,代码能相应减少;

Ø 性能:

n 直接访问数据库API接口,具有API接口的高效率,可靠性和线程线安全;

Ø 稳定性:

n 开源代码,唯一的代码文件otlv4.h,可以了解所有基于数据库API的实现细节;

n 从1996年开始,到今已10余年.

Ø 可读性及可维护性:

n 标准C++代码,不需要任何预处理;

n 使用流的形式,输入输出异常简洁;

n 减少大量代码,

n 代码结构更加简洁

参考资料:
http://otl.sourceforge.net/

2 类介绍

下面以常用的和重要的几个类进行简单介绍,更详细的资料建议大家参考:
http://otl.sourceforge.net/

2.1 otl_connect

Ø static int otl_initialize(const int threaded_mode=0);

静态函数,在联接数据库前调用;

Ø void rlogon(const char* connect_str,const int auto_commit=0);

此函数在oracle 和 DB2上的实现有差异,但语法是统一的.其中,DB2只要指定实例名,不会判断用户名和密码.

n OTL 4.0/OCIx style

u "USER/PASSWORD" (for local Oracle connections)

u "USER/PASSWORD@TNS_ALIAS" (for remote Oracle connections via SQL*Net)

n OTL 4.0/ODBC and OTL 4.0/DB2-CLI style

u "USER/PASSWORD/@DSN" (Oracle-like style for ODBC or DB2-CLI connections)

u "DSN=value;UID=value;PWD=value" (ODBC-like style for ODBC or DB2-CLI connection)

Ø void logoff(void);

断开数据库联接;

2.2 otl_stream

Ø void open(const int arr_size, // stream buffer size
const char* sqlstm, // SQL statemnet or stored procedure call
otl_connect& db, // connect object)

此函数还有两个参数,根据ORACLE OCI和DB2 CLI的不同而有所不一样,基本不用关心;

Ø int eof(void); //判断记录是否处理完毕, 返回0表示处理完;

Ø long get_rpc(void); //处理的记录数,尽量少用,目前在各种数据库下会有不同;在正常与异常下也会有所不同;

Ø void close(void); //关闭记录集;

Ø void set_commit(int auto_commit=0); //设置数据库提交模式,缺省为不自动提交;

Ø void flush(void); // 将output buffer的内容提交

2.3 otl_exception

有如下成员变量,可供查询异常时的信息

Ø char stm_text[2048 or OTL_EXCEPTION_STM_TEXT_SIZE]; // 发生异常时的SQL语句

Ø unsigned char msg[1000]; // 异常信息;

Ø int code; // 异常代码;

3 example:

#include <iostream>

#define OTL_DB2_CLI

#include "otlv4.h"

using namespace std;

char temp[2][128];

char strCode[16];

char g_strDBConnectPara[128];

char strSQL[2048];

otl_connect db;

void test1();

void test2();

void test3();

int main()

{

strcpy(g_strDBConnectPara,"db2inst1/db2inst1@db282");

otl_connect::otl_initialize();

try

{

db.rlogon( g_strDBConnectPara );

printf("rlogon ok./n");

test1();

test2();

test3();

}

catch(otl_exception& e)

{

printf("ERROR code=[%d]/n msg=[%s]/nSQL=[%s]/n",

e.code,

e.msg,

e.stm_text);

}

db.logoff();

return 0;

}

void test1()

{

otl_stream c(1,"select code,code_name from meta_code_name ",db);

int idx = 0;

while(!c.eof())

{

c >> temp[0] >> temp[1];

idx++;

cout << idx << " code=[" << temp[0]

<< "] code_name is:" << temp[1]

<< " last_eof_rc=" << c.last_eof_rc

<< " end_marker=" << c.end_marker

<< " oper_int_called=" << c.oper_int_called

<< endl;

}

printf("dealed [%d] records./n",c.get_rpc());

}

void test2()

{

sprintf(strSQL,"select code,code_name from meta_code_name where code =:f1<char[16]>");

strcpy(strCode,"10");

otl_stream o(100,strSQL,db);

o << strCode;

int idx = 0;

while(!o.eof())

{

o >> temp[0] >> temp[1];

idx++;

cout << idx << " code=[" << temp[0]

<< "] code_name is:" << temp[1]

<< " last_eof_rc=" << o.last_eof_rc

<< " end_marker=" << o.end_marker

<< " oper_int_called=" << o.oper_int_called

<< endl;

}

}

void test3()

{

cout << endl << "Please input SQL:" << endl;

cin.getline(strSQL,1024);

cout << strSQL << endl;

otl_stream d;

d.open(100,strSQL,db);

double fResult = 0;

int iResult = 0;

int desc_len = 0;

//根据查询的字段类型,用不同的数据类型获取数据

otl_column_desc* desc;

desc=d.describe_select(desc_len);

otl_datetime tResult;

while(!d.eof())

{

int idx = 0;

for(int i = 0;i < desc_len; i++)

{

idx++;

switch(desc[i].otl_var_dbtype)

{

case 1: //otl_var_char

d>>temp[0];

break;

case 2: //otl_var_double

case 3: //otl_var_float

d>>fResult;

sprintf(temp[0],"%f",fResult);

break;

case 4: //otl_var_int

case 5: //otl_var_unsigned_int

case 6: //otl_var_short

case 7: //otl_var_long_int

d>>iResult;

sprintf(temp[0],"%d",iResult);

break;

case 9: //otl_var_varchar_long

case 10: //otl_var_raw_long

d>>iResult;

sprintf(temp[0],"%ld",iResult);

break;

case 8: //otl_var_timestamp

case 16: //otl_var_db2time

case 17: //otl_var_db2date

case 18: //otl_var_tz_timestamp

case 19: //otl_var_ltz_timestamp

d>>tResult;

sprintf(temp[0],"%d-%d-%d %d:%d:%d",

tResult.year,tResult.month,tResult.day,tResult.hour,tResult.minute,tResult.second);

break;

default:

d>>temp[0];

break;

}

cout << temp[0] << "/t";

}

cout << endl;

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐