基于OCILIB的oracle数据库操作总结
1. 类图
2. 基类BaseOCIDao的设计与实现
BaseOCIDao.h头文件
#pragma once /* --------------------------------------------------------------------------------------------- *
* INCLUDES
* --------------------------------------------------------------------------------------------- */ #include "ocilib.h"
#include <string>
#include <vector>
using namespace std;
/* --------------------------------------------------------------------------------------------- *
* PLATFORMS
* --------------------------------------------------------------------------------------------- */ #ifdef _MSC_VER #if defined(OCI_CHARSET_WIDE)
#pragma comment(lib, "ocilibw.lib")
#elif defined(OCI_CHARSET_ANSI)
#pragma comment(lib, "ociliba.lib")
#endif #endif #ifndef OCI_SHARED_LIB
#if defined(_WINDOWS)
#define OCI_SHARED_LIB "oci.dll"
#elif defined(__APPLE__)
#define OCI_SHARED_LIB "libclntsh.dylib"
#elif defined(__hppa)
#define OCI_SHARED_LIB "libclntsh.sl"
#else
#define OCI_SHARED_LIB "libclntsh.so"
#endif
#endif #define print_frmt(f, x) printf(f, x)
#define print_text(x) printf(x) #if defined(OCI_CHARSET_WIDE)
#ifdef _WINDOWS
#define print_ostr(x) wprintf(OTEXT("%s"), x)
#else
#define print_ostr(x) printf("%ls", x)
#endif
#else
#define print_ostr(x) printf(OTEXT("%s"), x)
#endif class BaseOCIDao
{
protected:
string m_ip;
unsigned int m_port;
string m_inst;
string m_user;
string m_pwd;
unsigned int m_mode; OCI_Statement* m_stmt;
OCI_Connection* m_conn; bool m_useTrans;
bool m_IsConnected;
bool m_ShareConn;
POCI_ERROR m_ErrHandler;
vector<BaseOCIDao*> m_Children;
public:
BaseOCIDao();
BaseOCIDao(string ip,unsigned int port,string inst,string user,string pwd,POCI_ERROR ErrHandler,unsigned int mode=OCI_ENV_DEFAULT);
virtual ~BaseOCIDao(); BOOL Connect(BaseOCIDao* parent);
BOOL AddChildDao(BaseOCIDao* child); BOOL Connect();
BOOL DisConnect(); OCI_Resultset* QuerySql(const char* sql);
BOOL ExecuteSql(const char* sql);
OCI_Statement* PrepareBindStmt(const char* sql);
OCI_Resultset* QueryBindStmt(OCI_Statement *stmt);
BOOL ExecuteBindStmt(OCI_Statement *stmt); BOOL BeginTrans();
BOOL EndTrans();
BOOL Commit();
BOOL Rollback();
};
BaseOCIDao.cpp文件
#include "StdAfx.h"
#include "BaseOCIDao.h" BaseOCIDao::BaseOCIDao(string ip,unsigned int port,string inst,string user,string pwd,POCI_ERROR ErrHandler,unsigned int mode)
{
m_ip = ip;
m_port = port;
m_inst = inst;
m_user = user;
m_pwd = pwd;
m_mode = mode;
m_ErrHandler = ErrHandler;
m_ShareConn = false;
m_IsConnected = false;
m_useTrans = false; m_stmt = NULL;
m_conn = NULL;
} BaseOCIDao::BaseOCIDao()
{
m_ShareConn = false;
m_IsConnected = false;
m_useTrans = false; m_stmt = NULL;
m_conn = NULL;
} BaseOCIDao::~BaseOCIDao()
{
if(m_IsConnected && !m_ShareConn)
DisConnect();
} BOOL BaseOCIDao::Connect(BaseOCIDao* parent)
{
m_ShareConn = true;
return TRUE;
} BOOL BaseOCIDao::Connect()
{
m_ShareConn = false; /* Possible values for parameter mode:
* - OCI_ENV_DEFAULT : default mode
* - OCI_ENV_THREADED : multi-threading support
* - OCI_ENV_CONTEXT : thread contextual error handling
* - OCI_ENV_EVENTS : enables events for subscription, HA Events, AQ notifications
*/ /* INITIALIZE OCI ------------------------------------------------------- */
if (!OCI_Initialize(m_ErrHandler, NULL, m_mode))//the Oracle library is loaded from system environment variables
return EXIT_FAILURE; OCI_EnableWarnings(TRUE);
/* CONNECTION TO SERVER ------------------------------------------------- */
char dbs[];
sprintf_s(dbs,,"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%d)))(CONNECT_DATA=(SERVICE_NAME=%s)))",m_ip.c_str(),m_port,m_inst.c_str());
m_conn = OCI_ConnectionCreate(dbs, m_user.c_str(), m_pwd.c_str(), OCI_SESSION_DEFAULT);
if(m_conn!=NULL)
{
OCI_SetAutoCommit(m_conn,FALSE);
m_IsConnected = true; for (vector<BaseOCIDao*>::iterator itr = m_Children.begin();
itr!=m_Children.end(); itr++)
{
(*itr)->Connect(this);
} return EXIT_SUCCESS;
}
else
{
print_ostr(OCI_ErrorGetString(OCI_GetLastError()));
return EXIT_FAILURE;
}
} BOOL BaseOCIDao::DisConnect()
{
if(m_conn!=NULL)
{
if(OCI_ConnectionFree(m_conn))
{
print_ostr(OCI_ErrorGetString(OCI_GetLastError()));
OCI_Cleanup();
return TRUE;
}
else
{
print_ostr(OCI_ErrorGetString(OCI_GetLastError()));
return FALSE;
}
}
else
return FALSE;
} BOOL BaseOCIDao::AddChildDao(BaseOCIDao* child)
{
if(child!=NULL)
m_Children.push_back(child);
return TRUE;
} OCI_Statement* BaseOCIDao::PrepareBindStmt(const char* sql)
{
if(!m_IsConnected) return NULL;
m_stmt = OCI_StatementCreate(m_conn);
OCI_Prepare(m_stmt,sql);
return m_stmt;
} OCI_Resultset* BaseOCIDao::QuerySql(const char* sql)
{
if(!m_IsConnected) return NULL;
m_stmt = OCI_StatementCreate(m_conn);
OCI_ExecuteStmt(m_stmt,sql);
return OCI_GetResultset(m_stmt);
} OCI_Resultset* BaseOCIDao::QueryBindStmt(OCI_Statement *stmt)
{
if(!m_IsConnected) return NULL;
OCI_Execute(stmt);
return OCI_GetResultset(stmt);
} BOOL BaseOCIDao::ExecuteSql(const char* sql)
{
if(!m_IsConnected) return FALSE;
m_stmt = OCI_StatementCreate(m_conn);
BOOL flag = OCI_ExecuteStmt(m_stmt,sql);
if(!m_useTrans && !m_ShareConn)
{
if(flag)
OCI_Commit(m_conn);
else
OCI_Rollback(m_conn);
}
return flag;
} BOOL BaseOCIDao::ExecuteBindStmt(OCI_Statement *stmt)
{
if(!m_IsConnected) return FALSE;
BOOL flag = OCI_Execute(stmt);
if(!m_useTrans && !m_ShareConn)
{
if(flag)
OCI_Commit(m_conn);
else
OCI_Rollback(m_conn);
}
return flag;
} BOOL BaseOCIDao::BeginTrans()
{
if(!m_IsConnected) return FALSE;
m_useTrans = true;
return TRUE;
} BOOL BaseOCIDao::EndTrans()
{
if(!m_IsConnected || !m_useTrans) return FALSE;
m_useTrans = false;
return TRUE;
} BOOL BaseOCIDao::Commit()
{
if(!m_IsConnected || !m_useTrans) return FALSE;
return OCI_Commit(m_conn);
} BOOL BaseOCIDao::Rollback()
{
if(!m_IsConnected || !m_useTrans) return FALSE;
return OCI_Rollback(m_conn);
}
3. 继承实体类的使用
3.1 BaseModel定义
BaseModel
#pragma once
#include <string>
using namespace std;
class BaseModel{
public:
BaseModel(){}
virtual ~BaseModel(){}
};
3.2 例子
Model定义
#pragma once #include "BaseModel.h"
#include <string>
using namespace std; #define NameSize 100
class TDao;
class TModel : BaseModel{
friend TDao;
protected:
int m_ID;
char m_Name[NameSize];
public:
TModel(){
m_ID = ;
memset(m_Name,,NameSize);
}
TModel(const TModel& obj)
{
m_ID = obj.GetID();
SetName(obj.GetName());
} TModel& operator = (const TModel& obj)
{
if(this == &obj) return *this;
m_ID = obj.GetID();
return *this;
}
~TModel()
{ } void SetID(int ID)
{
m_ID = ID;
} int GetID() const
{
return m_ID;
}
void SetName(string Name)
{
strcpy_s(m_Name,NameSize,Name.c_str());
} string GetName() const
{
return m_Name;
}
};
Dao定义
#include "BaseOCIDao.h"
class TDao:BaseOCIDao
{
public:
TDao(){}
TDao(string ip,unsigned int port,string inst,string user,string pwd,POCI_ERROR ErrHandler,unsigned int mode):
BaseOCIDao(ip,port,inst,user,pwd,ErrHandler,mode)
{ }
~TDao(){} BOOL Insert(TModel* model)
{
char* sql = "insert into TabName(id,name) values (:id,:name)";
OCI_Statement* stmt = PrepareBindStmt(sql); OCI_BindInt(stmt,":id",&model->m_ID);
OCI_BindString(stmt,":name",model->m_Name,NameSize);
return ExecuteBindStmt(stmt);
} BOOL Update(TModel* model)
{
char* sql = "update TabName set name=:name where id=:id";
OCI_Statement* stmt = PrepareBindStmt(sql); OCI_BindInt(stmt,":id",&model->m_ID);
OCI_BindString(stmt,":name",model->m_Name,NameSize);
return ExecuteBindStmt(stmt);
} BOOL Delete(string cond)
{
string sql = "delete from TabName";
if(cond!="")
sql+=" where " + cond; return ExecuteSql(sql.c_str());
} vector<TModel*> Select(string cond)
{
vector<TModel*> resVec;
string sql = "select id,name from TabName";
if(cond!="")
sql+=" where " + cond; OCI_Resultset* rs = QuerySql(sql.c_str());
while(OCI_FetchNext(rs))
{
TModel *model = new TModel();
model->SetID(OCI_GetInt(rs,));
model->SetName(OCI_GetString(rs,));
resVec.push_back(model);
}
return resVec;
} BOOL Exist(TModel* model)
{
char* sql = "select id from TabName where name=:name";
OCI_Statement* stmt = PrepareBindStmt(sql);
OCI_BindString(stmt,":name",model->m_Name,NameSize);
OCI_Resultset* rs = QueryBindStmt(stmt);
if(OCI_FetchNext(rs))
{
model->SetID(OCI_GetInt(rs,));
return TRUE;
}
else
return FALSE;
}
};
DaoFactory定义
void err_handler(OCI_Error *err)
{
int err_type = OCI_ErrorGetType(err); print_text("\n"); if (err_type == OCI_ERR_WARNING)
{
print_text("> WARNING : ");
}
else
{
print_text("> ERROR : ");
} print_ostr(OCI_ErrorGetString(err));
print_text("\n");
}
class DaoFactory{
private:
string m_ip;
unsigned int m_port;
string m_inst;
string m_user;
string m_pwd;
unsigned int m_mode;
private:
DaoFactory(){
m_ip = "127.0.0.1";
m_port = ;
m_inst = "orcl";
m_user = "user";
m_pwd = "password";
m_mode = OCI_ENV_DEFAULT;
}
public:
static DaoFactory* GetInstance()
{
static DaoFactory factory;
return &factory;
}
~DaoFactory(){}
public:
TDao* CreateTDao()
{
TDao* dao = new TDao(m_ip,m_port,m_inst,m_user,m_pwd,err_handler,m_mode);
return dao;
}
};
简单情况
- 使用DaoFactory创建dao对象 dao
- 打开数据库连接 dao->Connect
- 执行搜索或增删改操作 dao->Select dao->Insert ….
- 关闭数据库连接 dao->DisConnect
- 删除dao对象 delete dao;
更复杂的情况
共享数据库连接进行查询
- 使用DaoFactory创建两个dao对象:DaoParent, DaoChild;
- DaoParent->AddChildDao(DaoChild);
- 打开数据库连接DaoParent->Connect();
- 搜索 DaoParent->Select();DaoChild->Select();
- 关闭数据库连接 DaoParent->DisConnect();
- 删除dao对象内存;delete DaoChild; delete DaoParent;
共享数据库事务,进行增删改操作
- 使用DaoFactory创建两个dao对象:DaoParent, DaoChild;
- DaoParent->AddChildDao(DaoChild);
- 打开数据库连接DaoParent->Connect();
- 打开事务 DaoParent-> BeginTrans
- 执行增删改操作 DaoParent->Insert();DaoChild->Update();。。。
- 提交或回滚事务 DaoParent-> Commit DaoParent-> Rollback
- 关闭事务 DaoParent-> EndTrans
- 关闭数据库连接 DaoParent->DisConnect();
- 删除dao对象内存;delete DaoChild; delete DaoParent;
4. 自动生成Model和Dao的工具设计
工具从输入流中读入一组规定格式的数据,按照此数据构建Model和Dao类代码。
输入内容格式规定
类名
数据库表名
字段个数N
字段1 类型1 数据库字段名1
…
字段N 类型N 数据库字段名N
支持的类型
类型 |
真实类型 |
数据库对应类型 |
int |
int |
number(10) |
unsigned int |
unsigned int |
number(10) |
short |
short |
number(5) |
unsigned short |
unsigned short |
number(5) |
float |
float |
binary_float |
double |
double |
binary_double |
string |
char[] |
varchar2() |
big_int |
long long |
number(14) |
big_uint |
unsigned long long |
number(14) |
例子:
User类有3个字段,ID,Name,Pwd
可输入如下:
User
UserTable
3
ID unsigned int ID
Name string Name
Pwd string Pwd
自动生成工具根据BaseMode和BaseOCIDao生成,可根据需要将生成后的Model和Dao进行修改。对于stirng类型的数据,自动生成Model后,可修改xxxSize宏,设置string类型字段的长度。
自动生成Model和Dao的工具AutoGenModelDao
AutoGenModelDao.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
using namespace std; typedef struct FieldType
{
string Field;
string FType;//string,int,unsigned int, short,unsigned short,float,double,big_int,big_uint
string DBField;
friend istream& operator >> (istream& myin,FieldType& obj)
{
myin>>obj.Field;
myin>>obj.FType;
if(obj.FType=="unsigned")
{
string tmp;
myin>>tmp;
obj.FType = obj.FType+" "+tmp;
}
myin>>obj.DBField;
return myin;
}
}FieldType; class AutoGenModelDao
{
private:
string m_ModelClass;
string m_DaoClass;
string m_DBTable;
string m_OutputDir;
vector<FieldType> m_Fields;
public:
AutoGenModelDao();
~AutoGenModelDao(); void AutoGen(istream& myin);
private:
void GenModelHeader();
ostream& GenConstructFun(ostream& myout);
ostream& GenGetSetFun(ostream& myout);
void GenModelBody();
private:
void GenDaoHeader();
void GenDaoBody();
ostream& GenInsertFun(ostream& myout);
ostream& GenInsertBatchFun(ostream& myout);
ostream& GenUpdateFun(ostream& myout);
ostream& GenUpdateBatchFun(ostream& myout);
ostream& GenDeleteFun(ostream& myout);
ostream& GenSelectFun(ostream& myout);
ostream& GenExistFun(ostream& myout);
};
AutoGenModelDao.cpp
#include "StdAfx.h"
#include "AutoGenModelDao.h"
#include <iostream>
#include <fstream>
using namespace std; AutoGenModelDao::AutoGenModelDao(void)
{
} AutoGenModelDao::~AutoGenModelDao(void)
{
} void AutoGenModelDao::AutoGen(istream& myin)
{
m_OutputDir = "..\\output";
string className;
myin>>className;//ClassName
m_ModelClass = className+"Model";
m_DaoClass = className+"Dao";
myin>>m_DBTable;
int fieldLen;
myin>>fieldLen;
m_Fields.clear();
for (int i=;i<fieldLen;i++)
{
FieldType field;
myin>>field;
m_Fields.push_back(field);
} GenModelHeader();
GenModelBody();
GenDaoHeader();
GenDaoBody();
} void AutoGenModelDao::GenModelHeader()
{
int strsize = ;
string file = m_OutputDir+"\\"+m_ModelClass+".h";
ofstream myout(file.c_str(),ios::out);
myout<<"#pragma once"<<endl;
myout<<"#include \"BaseModel.h\""<<endl;
myout<<"#include <string>"<<endl;
myout<<"using namespace std;"<<endl; myout<<endl;
//build #define string size
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType == "string")
myout<<"#define "<<itr->Field<<"Size "<<strsize<<endl;
}
myout<<endl;
myout<<"class "<<m_DaoClass<<";"<<endl;
myout<<"class "<<m_ModelClass<<" : BaseModel{"<<endl;
myout<<"friend "<<m_DaoClass<<";"<<endl; myout<<"protected:"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType == "string")
myout<<"\t char m_"<<itr->Field<<"["<<itr->Field<<"Size];"<<endl;
else
myout<<"\t "<<itr->FType<<" m_"<<itr->Field<<";"<<endl;
}
myout<<endl; myout<<"public:"<<endl;
myout<<"\t "<<m_ModelClass<<"();"<<endl;
myout<<"\t ~"<<m_ModelClass<<"();"<<endl;
myout<<"\t "<<m_ModelClass<<"(const "<<m_ModelClass<<"& obj);"<<endl;
myout<<"\t "<<m_ModelClass<<"& operator = (const "<<m_ModelClass<<"& obj);"<<endl; for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<"\t void Set"<<itr->Field<<"("<<itr->FType<<" val);"<<endl;
myout<<"\t "<<itr->FType<<" Get"<<itr->Field<<"() const;"<<endl;
} myout<<"};"<<endl;
myout.close();
} void AutoGenModelDao::GenModelBody()
{
string file = m_OutputDir+"\\"+m_ModelClass+".cpp";
ofstream myout(file.c_str(),ios::out);
myout<<"#include \"StdAfx.h\""<<endl;
myout<<"#include \""<<m_ModelClass<<".h\""<<endl;
GenConstructFun(myout);
GenGetSetFun(myout);
myout.close();
}
ostream& AutoGenModelDao::GenConstructFun(ostream& myout)
{
myout<<m_ModelClass<<"::"<<m_ModelClass<<"(){"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType!="string")
myout<<"\t m_"<<itr->Field<<" = 0;"<<endl;
else
myout<<"\t memset(m_"<<itr->Field<<",0,"<<itr->Field<<"Size);m_"<<itr->Field<<"[0]='\\0';"<<endl;
}
myout<<"}"<<endl;
myout<<endl;
myout<<m_ModelClass<<"::~"<<m_ModelClass<<"(){}"<<endl;
myout<<endl;
myout<<m_ModelClass<<"::"<<m_ModelClass<<"(const "<<m_ModelClass<<"& obj){"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType!="string")
myout<<"\t m_"<<itr->Field<<" = obj.Get"<<itr->Field<<"();"<<endl;
else
myout<<"\t Set"<<itr->Field<<"(obj.Get"<<itr->Field<<"());"<<endl;
}
myout<<"}"<<endl;
myout<<endl;
myout<<m_ModelClass<<"& "<<m_ModelClass<<"::operator = (const "<<m_ModelClass<<"& obj){"<<endl;
myout<<"\t if(this == &obj) return *this;"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType!="string")
myout<<"\t m_"<<itr->Field<<" = obj.Get"<<itr->Field<<"();"<<endl;
else
myout<<"\t Set"<<itr->Field<<"(obj.Get"<<itr->Field<<"());"<<endl;
}
myout<<"\t return *this;"<<endl;
myout<<"}"<<endl;
return myout;
}
ostream& AutoGenModelDao::GenGetSetFun(ostream& myout)
{
myout<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<"void "<<m_ModelClass<<"::Set"<<itr->Field<<"("<<itr->FType<<" val){"<<endl;
if(itr->FType != "string")
myout<<"\t m_"<<itr->Field<<" = val;"<<endl;
else
myout<<"\t strcpy_s(m_"<<itr->Field<<","<<itr->Field<<"Size,val.c_str());"<<endl;
myout<<"}"<<endl;
myout<<endl; myout<<itr->FType<<" "<<m_ModelClass<<"::Get"<<itr->Field<<"()const{"<<endl;
myout<<"\t return m_"<<itr->Field<<";"<<endl;
myout<<"}"<<endl;
}
return myout;
}
void AutoGenModelDao::GenDaoHeader()
{
string file = m_OutputDir+"\\"+m_DaoClass+".h";
ofstream myout(file.c_str(),ios::out);
myout<<"#pragma once"<<endl;
myout<<"#include \""<<m_ModelClass<<".h\""<<endl;
myout<<"#include \"BaseOCIDao.h\""<<endl;
myout<<"#include <string>"<<endl;
myout<<"#include <vector>"<<endl;
myout<<"using namespace std;"<<endl;
myout<<endl;
myout<<"class "<<m_DaoClass<<" : BaseOCIDao{"<<endl;
myout<<"public:"<<endl;
myout<<"\t "<<m_DaoClass<<"():BaseOCIDao(){}"<<endl;
myout<<"\t "<<m_DaoClass<<"(string ip,unsigned int port,string inst,string user,string pwd,POCI_ERROR ErrHandler,unsigned int mode):BaseOCIDao(ip,port,inst,user,pwd,ErrHandler,mode){}"<<endl;
myout<<"\t ~"<<m_DaoClass<<"(){}"<<endl;
myout<<"\t BOOL Insert("<<m_ModelClass<<"* model);"<<endl;
myout<<"\t BOOL InsertBatch(vector<"<<m_ModelClass<<"*> models);"<<endl;
myout<<"\t BOOL Update("<<m_ModelClass<<"* model);"<<endl;
myout<<"\t BOOL UpdateBatch(vector<"<<m_ModelClass<<"*> models);"<<endl;
myout<<"\t BOOL Exist("<<m_ModelClass<<"* model);"<<endl;
myout<<"\t BOOL Delete(string cond);"<<endl;
myout<<"\t vector<"<<m_ModelClass<<"*> Select(string cond);"<<endl;
myout<<"};"<<endl;
myout.close();
} void AutoGenModelDao::GenDaoBody()
{
string file = m_OutputDir+"\\"+m_DaoClass+".cpp";
ofstream myout(file.c_str(),ios::out);
myout<<"#include \"StdAfx.h\""<<endl;
myout<<"#include \""<<m_ModelClass<<".h\""<<endl;
myout<<"#include \""<<m_DaoClass<<".h\""<<endl;
myout<<endl;
GenInsertFun(myout);
GenInsertBatchFun(myout);
GenUpdateFun(myout);
GenUpdateBatchFun(myout);
GenDeleteFun(myout);
GenSelectFun(myout);
GenExistFun(myout);
myout.close();
}
ostream& AutoGenModelDao::GenInsertFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::Insert("<<m_ModelClass<<"* model){"<<endl;
myout<<"\t if(model==NULL)return FALSE;"<<endl;
myout<<"\t char* sql=\"insert into "<<m_DBTable<<"(";
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<itr->DBField;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<") values (";
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<":"<<itr->Field;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<")\";"<<endl;
myout<<endl; myout<<"\t OCI_Statement* stmt = PrepareBindStmt(sql);"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType=="int")
myout<<"\t OCI_BindInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned int")
myout<<"\t OCI_BindUnsignedInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "short")
myout<<"\t OCI_BindShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned short")
myout<<"\t OCI_BindUnsignedShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "float")
myout<<"\t OCI_BindFloat(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "double")
myout<<"\t OCI_BindDouble(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_int")
myout<<"\t OCI_BindBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_uint")
myout<<"\t OCI_BindUnsignedBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "string")
myout<<"\t OCI_BindString(stmt,\":"<<itr->Field<<"\",model->m_"<<itr->Field<<","<<itr->Field<<"Size);"<<endl;
}
myout<<"\t return ExecuteBindStmt(stmt);"<<endl;
myout<<"}"<<endl;
return myout;
} ostream& AutoGenModelDao::GenInsertBatchFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::InsertBatch(vector<"<<m_ModelClass<<"*> models){"<<endl;
myout<<"\t if(models.empty())return FALSE;"<<endl;
myout<<"\t char* sql=\"insert into "<<m_DBTable<<"(";
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<itr->DBField;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<") values (";
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<":"<<itr->Field;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<")\";"<<endl;
myout<<endl; myout<<"\t OCI_Statement* stmt = PrepareBindStmt(sql);"<<endl;
myout<<"\t int bindSize=models.size();"<<endl;
myout<<"\t int strOffset=0;"<<endl;
myout<<"\t OCI_BindArraySetSize(stmt, bindSize);"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType=="int")
{
myout<<"\t int* t"<<itr->Field<<"Arr = new int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "unsigned int")
{
myout<<"\t unsigned int* t"<<itr->Field<<"Arr = new unsigned int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "short")
{
myout<<"\t short* t"<<itr->Field<<"Arr = new short[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfShorts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "unsigned short")
{
myout<<"\t unsigned short* t"<<itr->Field<<"Arr = new unsigned short[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedShorts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "float")
{
myout<<"\t float* t"<<itr->Field<<"Arr = new float[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfFloats(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "double")
{
myout<<"\t double* t"<<itr->Field<<"Arr = new double[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfDoubles(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "big_int")
{
myout<<"\t big_int* t"<<itr->Field<<"Arr = new big_int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfBigInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "big_uint")
{
myout<<"\t big_uint* t"<<itr->Field<<"Arr = new big_uint[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedBigInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "string")
{
myout<<"\t strOffset=0;"<<endl;
myout<<"\t char* t"<<itr->Field<<"Arr = new char["<<itr->Field<<"Size*bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t strcpy_s(t"<<itr->Field<<"Arr+strOffset,"<<itr->Field<<"Size,models[i]->m_"<<itr->Field<<");"<<endl;
myout<<"\t strOffset+="<<itr->Field<<"Size;"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfStrings(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,"<<itr->Field<<"Size-1,bindSize);"<<endl<<endl;
}
}
myout<<"\t BOOL flag = ExecuteBindStmt(stmt);"<<endl<<endl; for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<"\t delete[] t"<<itr->Field<<"Arr;"<<endl;
}
myout<<endl<<"\t return flag;"<<endl;
myout<<"}"<<endl;
return myout;
} ostream& AutoGenModelDao::GenUpdateFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::Update("<<m_ModelClass<<"* model){"<<endl;
myout<<"\t if(model==NULL)return FALSE;"<<endl;
myout<<"\t char* sql=\"update "<<m_DBTable<<" set ";
vector<FieldType>::iterator itr = m_Fields.begin();
itr++;
for (;itr != m_Fields.end(); itr++)
{
myout<<itr->DBField<<" = :"<<itr->Field;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<" where "<<m_Fields.begin()->DBField<<"=:"<<m_Fields.begin()->Field<<"\";"<<endl;
myout<<endl; myout<<"\t OCI_Statement* stmt = PrepareBindStmt(sql);"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType=="int")
myout<<"\t OCI_BindInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned int")
myout<<"\t OCI_BindUnsignedInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "short")
myout<<"\t OCI_BindShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned short")
myout<<"\t OCI_BindUnsignedShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "float")
myout<<"\t OCI_BindFloat(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "double")
myout<<"\t OCI_BindDouble(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_int")
myout<<"\t OCI_BindBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_uint")
myout<<"\t OCI_BindUnsignedBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "string")
myout<<"\t OCI_BindString(stmt,\":"<<itr->Field<<"\",model->m_"<<itr->Field<<","<<itr->Field<<"Size);"<<endl;
} myout<<"\t return ExecuteBindStmt(stmt);"<<endl;
myout<<"}"<<endl;
return myout;
} ostream& AutoGenModelDao::GenUpdateBatchFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::UpdateBatch(vector<"<<m_ModelClass<<"*> models){"<<endl;
myout<<"\t if(models.empty())return FALSE;"<<endl;
myout<<"\t char* sql=\"update "<<m_DBTable<<" set ";
vector<FieldType>::iterator itr = m_Fields.begin();
itr++;
for (;itr != m_Fields.end(); itr++)
{
myout<<itr->DBField<<" = :"<<itr->Field;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<" where "<<m_Fields.begin()->DBField<<"=:"<<m_Fields.begin()->Field<<"\";"<<endl;
myout<<endl; myout<<"\t OCI_Statement* stmt = PrepareBindStmt(sql);"<<endl;
myout<<"\t int bindSize=models.size();"<<endl;
myout<<"\t int strOffset=0;"<<endl;
myout<<"\t OCI_BindArraySetSize(stmt, bindSize);"<<endl;
for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
if(itr->FType=="int")
{
myout<<"\t int* t"<<itr->Field<<"Arr = new int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "unsigned int")
{
myout<<"\t unsigned int* t"<<itr->Field<<"Arr = new unsigned int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "short")
{
myout<<"\t short* t"<<itr->Field<<"Arr = new short[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfShorts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "unsigned short")
{
myout<<"\t unsigned short* t"<<itr->Field<<"Arr = new unsigned short[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedShorts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "float")
{
myout<<"\t float* t"<<itr->Field<<"Arr = new float[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfFloats(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "double")
{
myout<<"\t double* t"<<itr->Field<<"Arr = new double[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfDoubles(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "big_int")
{
myout<<"\t big_int* t"<<itr->Field<<"Arr = new big_int[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfBigInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "big_uint")
{
myout<<"\t big_uint* t"<<itr->Field<<"Arr = new big_uint[bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t t"<<itr->Field<<"Arr[i]=models[i]->m_"<<itr->Field<<";"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfUnsignedBigInts(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,bindSize);"<<endl<<endl;
}
else if(itr->FType == "string")
{
myout<<"\t strOffset=0;"<<endl;
myout<<"\t char* t"<<itr->Field<<"Arr = new char["<<itr->Field<<"Size*bindSize];"<<endl;
myout<<"\t for(int i=0;i<bindSize;++i){"<<endl;
myout<<"\t strcpy_s(t"<<itr->Field<<"Arr+strOffset,"<<itr->Field<<"Size,models[i]->m_"<<itr->Field<<");"<<endl;
myout<<"\t strOffset+="<<itr->Field<<"Size;"<<endl;
myout<<"\t }"<<endl;
myout<<"\t OCI_BindArrayOfStrings(stmt,\":"<<itr->Field<<"\",t"<<itr->Field<<"Arr,"<<itr->Field<<"Size-1,bindSize);"<<endl<<endl;
}
} myout<<"\t BOOL flag = ExecuteBindStmt(stmt);"<<endl<<endl; for (vector<FieldType>::iterator itr = m_Fields.begin();
itr != m_Fields.end(); itr++)
{
myout<<"\t delete[] t"<<itr->Field<<"Arr;"<<endl;
}
myout<<endl<<"\t return flag;"<<endl;
myout<<"}"<<endl;
return myout;
} ostream& AutoGenModelDao::GenDeleteFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::Delete(string cond){"<<endl;
myout<<"\t string sql=\"delete from "<<m_DBTable<<"\";"<<endl;
myout<<"\t if(cond!=\"\") sql+=\" where \"+cond;"<<endl;
myout<<"\t return ExecuteSql(sql.c_str());"<<endl;
myout<<"}"<<endl;
return myout;
} ostream& AutoGenModelDao::GenSelectFun(ostream& myout)
{
myout<<endl;
myout<<"vector<"<<m_ModelClass<<"*> "<<m_DaoClass<<"::Select(string cond){"<<endl;
myout<<"\t vector<"<<m_ModelClass<<"*> resVec;"<<endl;
myout<<"\t string sql=\"select ";
vector<FieldType>::iterator itr = m_Fields.begin();
for (;itr != m_Fields.end(); itr++)
{
myout<<itr->DBField;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<" from "<<m_DBTable<<"\";"<<endl;
myout<<"\t if(cond!=\"\") sql+=\" where \"+cond;"<<endl;
myout<<"\t OCI_Resultset* rs = QuerySql(sql.c_str());"<<endl;
myout<<"\t while(OCI_FetchNext(rs)){"<<endl;
myout<<"\t "<<m_ModelClass<<"* model = new "<<m_ModelClass<<"();"<<endl; int fieldCnt = ;
for (itr=m_Fields.begin();itr != m_Fields.end(); itr++)
{
fieldCnt++;
myout<<"\t model->Set"<<itr->Field<<"(";
if(itr->FType=="int")
myout<<"OCI_GetInt(rs,"<<fieldCnt<<")";
else if(itr->FType == "unsigned int")
myout<<"OCI_GetUnsignedInt(rs,"<<fieldCnt<<")";
else if(itr->FType == "short")
myout<<"OCI_BindShort(rs,"<<fieldCnt<<")";
else if(itr->FType == "unsigned short")
myout<<"OCI_GetUnsignedShort(rs,"<<fieldCnt<<")";
else if(itr->FType == "float")
myout<<"OCI_GetFloat(rs,"<<fieldCnt<<")";
else if(itr->FType == "double")
myout<<"OCI_BindDouble(rs,"<<fieldCnt<<")";
else if(itr->FType == "big_int")
myout<<"OCI_GetBigInt(rs,"<<fieldCnt<<")";
else if(itr->FType == "big_uint")
myout<<"OCI_GetUnsignedBigInt(rs,"<<fieldCnt<<")";
else if(itr->FType == "string")
myout<<"OCI_GetString(rs,"<<fieldCnt<<")"; myout<<");"<<endl;
}
myout<<"\t resVec.push_back(model);"<<endl;
myout<<"\t }"<<endl;
myout<<"\t return resVec;"<<endl;
myout<<"}"<<endl;
return myout;
}
ostream& AutoGenModelDao::GenExistFun(ostream& myout)
{
myout<<endl;
myout<<"BOOL "<<m_DaoClass<<"::Exist("<<m_ModelClass<<"* model){"<<endl;
myout<<"\t if(model==NULL) return FALSE;"<<endl;
vector<FieldType>::iterator itr = m_Fields.begin();
myout<<"\t char* sql=\"select "<<itr->DBField<<" from "<<m_DBTable<<" where ";
itr++;
for (;itr != m_Fields.end(); itr++)
{
myout<<itr->DBField<<" = :"<<itr->Field;
if(itr+ != m_Fields.end())
myout<<",";
}
myout<<"\";"<<endl; myout<<"\t OCI_Statement* stmt = PrepareBindStmt(sql);"<<endl;
itr = m_Fields.begin();
itr++;
for (;itr != m_Fields.end(); itr++)
{
if(itr->FType=="int")
myout<<"\t OCI_BindInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned int")
myout<<"\t OCI_BindUnsignedInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "short")
myout<<"\t OCI_BindShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "unsigned short")
myout<<"\t OCI_BindUnsignedShort(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "float")
myout<<"\t OCI_BindFloat(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "double")
myout<<"\t OCI_BindDouble(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_int")
myout<<"\t OCI_BindBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "big_uint")
myout<<"\t OCI_BindUnsignedBigInt(stmt,\":"<<itr->Field<<"\",&model->m_"<<itr->Field<<");"<<endl;
else if(itr->FType == "string")
myout<<"\t OCI_BindString(stmt,\":"<<itr->Field<<"\",model->m_"<<itr->Field<<","<<itr->Field<<"Size);"<<endl;
}
myout<<"\t OCI_Resultset* rs = QueryBindStmt(stmt);"<<endl;
myout<<"\t if(OCI_FetchNext(rs)){"<<endl;
myout<<"\t model->Set"<<m_Fields.begin()->Field<<"(OCI_GetUnsignedInt(rs,1));"<<endl;
myout<<"\t return TRUE;"<<endl;
myout<<"\t }"<<endl;
myout<<"\t else return FALSE;"<<endl;
myout<<"}"<<endl;
return myout;
}