这个例子比较有代表性,为什么呢?因为
(1)它用到了string,快速安全的C++开发你不用string?
(2)它用到了用户自定义的类型,介绍了如何从数据库里直接“读出一个对象”或者“将自己定义的对象写入数据库”
仅仅以上两点就足已看出此例子的代表性
如果你想知道更多适合你的示例,下载otl的时候会有全部的example和HTML形式的说明文档,相当全面,你看看就会受益良多。
自定义类型真的就万事大吉了吗?
但是,我想重点说一下但是!这种做法又是非常糟糕的,为什么这么说呢?这种做法看起来对用户自定义的任意多成员的类型都能够支持读写,好像是超出了基本类型那么多繁琐的细节。但是在一个业务比较复杂的系统中,尤其是以数据操作为主的系统中,用户会随时从数据库的多个表中取得结果,如果每种结果对应一种类型(用户要仔细设计这种类型的SQL语句,这又是针对基本类型的繁琐细节,输入输出操作符重载)要处理的又是一些细节。本质上并没有消除开发人员的工作量,本质上仅仅是让开发人员提供接口的方式把细节细化到这些接口中。程序的可扩展能力提高了,效率一点也没有提高。
理想的模型应该是什么样子的?
(1)直接返回通用结构:xml,json,等。
理想的模型是C++的开发人员使用接口的时候就如同接触SQL语句一样方便自如(不需要关注操作数据库表中的字段类型,完全在业务逻辑,也就是要哪些字段上用什么条件),说白了就是要把SQL语句放到C++语言中写出来,得到的结果也是结构化的,无类型的,如果开发人员想用类型的时候可以再转换,否则只需要字符串类型即可。
(2)连接和断开连接应该不再交给程序员。
程序员只需要在想要得到数据库数据的时候调用一次SQL语句即可,直接获取结果,而不再是每次都要调用connection,disconnect,reconnect等这些和数据库连接相关的接口。
但今天我们还只能给出自定义类型,我一边发现理想模型,一边DIY
OTL 4.0, Example 230 (std::string, STL-compliant stream iterators, MyODBC 3.50)
Example 230 (std::string, STL-compliant stream iterators, MyODBC 3.50)
This example demonstrates STL-compliant OTL/MyODBC stream itertors, std::strings, and ANSI C++ typecasts.
Source Code
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <cstring>
#include <cstdlib>
#define OTL_ODBC // Compile OTL 4.0/ODBC
// The following #define is required with MyODBC 3.51.11 and higher
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
// #define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used
#define OTL_STL // Turn on STL features
#define OTL_ANSI_CPP // Turn on ANSI C++ typecasts
#include <otlv4.h> // include the OTL 4.0 header file
using namespace std;
otl_connect db; // connect object
// row container class
class row{
public:
int f1;
string f2;
// default constructor
row(){f1=0;}
// destructor
~row(){}
// copy constructor
row(const row& row)
{
f1=row.f1;
f2=row.f2;
}
// assignment operator
row& operator=(const row& row)
{
f1=row.f1;
f2=row.f2;
return *this;
}
};
// redefined operator>> for reading row& from otl_stream
otl_stream& operator>>(otl_stream& s, row& row)
{
s>>row.f1>>row.f2;
return s;
}
// redefined operator<< for writing row& into otl_stream
otl_stream& operator<<(otl_stream& s, const row& row)
{
s<<row.f1<<row.f2;
return s;
}
// redefined operator<< writing row& into ostream
ostream& operator<<(ostream& s, const row& row)
{
s<<"f1="<<row.f1<<", f2="<<row.f2;
return s;
}
void insert()
// insert rows into table
{
otl_stream o(1, // buffer size should be == 1 on INSERT
"insert into test_tab values "
"(:f1<int>,:f2<char[31]>),"
"(:f12<int>,:f22<char[31]>),"
"(:f13<int>,:f23<char[31]>),"
"(:f14<int>,:f24<char[31]>),"
"(:f15<int>,:f25<char[31]>)",
// INSERT statement. Multiple sets of values can be used
// to work around the lack of the bulk interface
db // connect object
);
row r; // single row buffer
vector<row> vo; // vector of rows
// populate the vector
for(int i=1;i<=100;++i){
r.f1=i;
r.f2="NameXXX";
vo.push_back(r);
}
cout<<"vo.size="<<vo.size()<<endl;
// insert vector into table
copy(vo.begin(),
vo.end(),
otl_output_iterator<row>(o)
);
}
void select(const int af1)
{
otl_stream i(50, // buffer size may be > 1
"select * from test_tab "
"where f1>=:f11<int> "
" and f1<=:f12<int>*2",
// SELECT statement
db // connect object
);
// create select stream
vector<row> v; // vector of rows
i<<af1<<af1; // :f11 = af1, :f12 = af1
// copy all rows to be fetched into the vector
copy(otl_input_iterator<row,ptrdiff_t>(i),
otl_input_iterator<row,ptrdiff_t>(),
back_inserter(v));
cout<<"Size="<<v.size()<<endl;
// send the vector to cout
copy(v.begin(), v.end(), ostream_iterator<row>(cout, "\n"));
}
int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try{
db.rlogon("UID=scott;PWD=tiger;DSN=mysql35"); // connect to ODBC
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // drop table
otl_cursor::direct_exec
(
db,
"create table test_tab(f1 int, f2 varchar(30)) type=innoDB"
); // create table
insert(); // insert records into table
select(8); // select records from table
}
catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.var_info<<endl; // print out the variable that caused the error
}
db.logoff(); // disconnect from ODBC
return 0;
}
Output
vo.size=100
Size=9
f1=8, f2=NameXXX
f1=9, f2=NameXXX
f1=10, f2=NameXXX
f1=11, f2=NameXXX
f1=12, f2=NameXXX
f1=13, f2=NameXXX
f1=14, f2=NameXXX
f1=15, f2=NameXXX
f1=16, f2=NameXXX