- 论坛徽章:
- 0
|
原帖由 ningth 于 2006-11-29 16:01 发表
如何屏蔽数据库间的差异
unixODBC没用过,不知道怎么样
如果不想自己封装,unixodbc应该是个不错的选择。
以前做过类似的工作,连接里有个文档你可参考。 http://www.onlyit.cn/bbs/doc/over_db.rar
不过那个是很早的版本了。现在在用的版本做了些改进,多了对多线程和连接池的支持。贴上和连接打交道的头文件供参考。
- #ifndef itf_db_h
- #define itf_db_h
- #include <system.h>
- #include <dbdatamgr.h>
- #include <dbdatamgr_itf.h>
- #include <memlist.h>
- #include <stdio.h>
- #include <thread_ext.h>
- //这个是后台数据库,决定了sql语句的适用性
- #define Db_Serv_Undef 0
- #define Db_Serv_Oracle 1
- #define Db_Serv_Access 2
- #define Db_Serv_Sybase 3
- #define Db_Serv_Mysql 4
- #define Db_Serv_Informix 5
- #define Db_Serv_Odb 6
- //这个是客户端连接用的,不用于决定sql语句
- #define Db_Type_Oracle 1 //oracle
- #define Db_Type_Bde 2 //bde
- #define Db_Type_Exp_Oracle 3 //exp_oracle
- #define Db_Type_Exp_Mssql 4 //exp_mssql
- #define Db_Type_Vc 5 //vc_odbc
- #define Db_Type_Ado_Access 6 //ado_access
- #define Db_Type_Itf_Sql 7 //itf_sql
- #define Db_Type_Mysql 8 //itf_mysql
- #define Db_Type_Odb_V01 10 //odb
- class TDb_Connect
- {
- protected:
- int mServ_Type;
- public:
- int mDb_Type;
- char mDb_Name[60];
- char mUser_Name[60];
- char mPassword[60];
- char mGrp_Name[60];
- char mHost[60];
- int mPort;
- int mInitlized;
- int mTid;
- int mCreate_Tid;
- void * mAss_Ptr;
- void (* mRelease_Obj_Handle)(void * conn);
- DBDataMgr_Itf_Stru mData_Oper_Funcs;
- protected:
- int (* _Set_Data_Oper_Handle)(DBDataMgr_Itf_Stru * oper_ptr);
- int (* _Connect_Handle)(void * ddm,int db_type,char * db_name,char * user_name,
- char * password,char * host,int port);
- int (* _Disconnect_Handle)(void * ddm);
- int (* _Commit_Handle)(void * ddm);
- int (* _Rollback_Handle)(void * ddm);
- int (* _ExecuteSql_Handle)(void * ddm,char * sqlcmd);
- int (* _ExecuteProc_Handle)(void * ddm,char * sqlcmd);
- int (* _SelectRow_Handle)(void * ddm,char * sqlcmd,int (* data_handle)(void *,void *),
- void * dbioptr,void * assptr);
- int (* _BatchInsert_Handle)(void * ddm);
- public:
- TDb_Connect();
- ~TDb_Connect();
- virtual int Set_Serv_Type(int db_serv_type);
- virtual int Get_Serv_Type();
- int SetLocalTid();
- virtual int Load_Handle(char * lib_file);
- virtual int Connect(int db_type,char * db_name, char * user_name,char * password,
- char * host=NULL,int port=0);
- virtual int Disconnect();
- virtual int Commit(DBDataMgr * ddm);
- virtual int Rollback(DBDataMgr * ddm);
- virtual int ExecuteSql(DBDataMgr * ddm,char * sqlcmd);
- virtual int ExecuteProc(DBDataMgr * ddm,char * sqlcmd);
- virtual int SelectRow(DBDataMgr * ddm,char * sqlcmd,int (* data_handle)(void *,void *),
- void * dbioptr,void * assptr);
- virtual int BatchInsert(DBDataMgr * ddm);
- virtual int Get_Sequence_Id(DBDataMgr * ddm,char * seq_name,int * seq_val);
- virtual int Get_Sequence_Cur_Val(DBDataMgr * ddm,char * seq_name,int * seq_val);
- virtual int Set_Sequence_Id(DBDataMgr * ddm,char * seq_name,int seq_val);
- };
- class TDb_Connect_Mgr
- {
- public:
- TMemList mConn_Lst;
- TThread_Lock * mLock;
- protected:
- int Check_Lock();
- public:
- TDb_Connect_Mgr();
- ~TDb_Connect_Mgr();
- int Add_Connect(TDb_Connect * conn);
- int Remove_Connect(TDb_Connect * conn);
- int End_Session(char * grp_name=NULL);
- int Get_Free_Count(char * grp_name=NULL);
- int Get_Count(char * grp_name=NULL);
- TDb_Connect * Get_Session(char * grp_name=NULL);
- int Disconnect(char * grp_name=NULL); //关闭一个数据库连接
- int Disconnect(TDb_Connect * conn); //关闭一个数据库连接
- int Disconnect_Self_Thread(); //关闭本线程创建的数据库连接
- };
- //建立数据库连接
- //无需指定后台
- extern int Mts_Ora(char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL);
- //最后一个参数指定后台
- extern int Mts_Bde(char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL,
- int db_serv_type=0);
- //无需指定后台
- extern int Mts_Exp_Oracle(char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL);
- //无需指定后台
- extern int Mts_Exp_Mssql(char * hostname,
- char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL);
- //无需指定后台
- extern int Mts_Odb(char * dbname,
- char * username,
- char * passwd,
- int connnum,
- char * grpname,
- char * host,
- int port);
- //无需指定后台
- extern int Mts_Ado_Access(
- char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL);
- //无需指定后台
- extern int Mts_Mysql(char * dbname,
- char * username,
- char * passwd,
- char * host=NULL,
- int port=0,
- int connnum=1,
- char * grpname=NULL);
- //最后一个参数指定后台
- extern int Mts_Vc(char * dbname,
- char * username,
- char * passwd,
- int connnum=1,
- char * grp_name=NULL,
- int db_serv_type=0);
- extern int Mts_Get_Free_Count(char * grp_name=NULL); //获得未被占用的连接数量
- extern int Mts_Get_Count(char * grp_name=NULL); //获得连接数量
- extern TDb_Connect * Mts_Get_Session(char * grp_name=NULL); //获得一个未被占用的连接,或者是本线程正在使用的连接
- extern int Mts_End_Session(char * grp_name=NULL); //释放一个数据库会话
- extern int Mts_Disconnect(char * grp_name=NULL); //关闭一个数据库连接
- extern int Mts_Disconnect(TDb_Connect * conn); //关闭一个数据库连接
- extern int Mts_Disconnect_Self_Thread();
- extern TDb_Connect * New_Bde_Connect();
- extern TDb_Connect * New_Ora_Connect();
- extern TDb_Connect * New_Net_Connect();
- extern TDb_Connect * New_Odb_Connect();
- extern TDb_Connect * New_Exp_Connect();
- extern TDb_Connect * New_Mysql_Connect();
- //下面2个函数被各数据库驱动程序调用,应用程序不应该调用
- extern int Mts_Add_Connect(TDb_Connect * conn);
- extern int Mts_Remove_Connect(TDb_Connect * conn);
- extern TDb_Connect_Mgr * Db_Connect_Mgr;
- extern int Global_Init_Db_Connect_Mgr();
- extern int Init_Db_Connect_Mgr();
- extern void * Mts_Get_Mgr_Ptr();
- extern int Mts_Set_Mgr_Ptr(void * ptr);
- #endif
复制代码 |
|