#include "atlstr.h"
#include "GenDB.h"
CGenDb::CGenDb(void){}
CGenDb::~CGenDb(void){}
void CGenDb::CloseDb(void)
{
imp->CloseDbImp();
imp.Release();
#if defined(COMTEST)
CoUninitialize();
#endif
};
void CGenDb::Select(const std::string& sql,bool bchange)
{
imp->OpenIoImp(select_type,_bstr_t(sql.c_str()),bchange);
}
void CGenDb::Insert(const std::string& sql)
{
imp->OpenIoImp(insert_type,_bstr_t(sql.c_str()),false);
}
void CGenDb::CallStorProc(const std::string& sql)
{
imp->OpenIoImp(storpro_type,_bstr_t(sql.c_str()),0);
}
void CGenDb::Delete(const std::string& sql,bool nopara)
{
imp->OpenIoImp(delete_type,_bstr_t(sql.c_str()),nopara);
}
void CGenDb::Excute(const std::string& sql,bool nothrowexception)
{
imp->OpenIoImp(unknown_type,_bstr_t(sql.c_str()),nothrowexception);
}
void CGenDb::CleanErrorTag()
{
imp->CleanErrorImp();
}
void CGenDb::CloseStream()
{
imp->CloseIoImp();
};
bool CGenDb::NextRow(void)
{
int tag(0);
imp->HaveNextImp(&tag);
if(tag==0)
return true;
return false;
};
void CGenDb::GetOutputType(std::vector<columnsinfo>& vec)
{
int fdsize(0);
imp->GetFieldCountImp(&fdsize);
vec.resize(fdsize);
FieldInfo fi;
Field f;
CComBSTR bstr;
f.name = &bstr;
f.nullok = &fi.nullok;
f.prec = &fi.prec;
f.scale = &fi.scale;
f.size = &fi.size;
f.type = &fi.type;
int pos(0);
char* sour = NULL;
USES_CONVERSION;
for(int i(0);i<fdsize;++i)
{
imp->GetFieldInfoImp(i,&pos,&f);
#if (_MSC_VER >= 1400)
strcpy_s(fi.name,MAX_NAME_LEN,OLE2A(bstr));
#else
strcpy(fi.name,OLE2A(bstr));
#endif
delete []sour;
vec[pos-1] = fi;
bstr.Empty();
}
}
void CGenDb::BuildSql(const std::vector<FieldInfo>& vec,
const std::string& wherestr,std::string& sql)
{
CString info;
std::vector<FieldInfo>::const_iterator
star(vec.begin()),end(vec.end());
for(;star!=end;++star)
{
info.AppendFormat(_T("%d~%d~%d~%d~%d~%s|"), star->type,
star->nullok,star->size,star->scale,star->prec,CA2T(star->name));
}
_bstr_t binfo(info),bwhere(wherestr.c_str()),bsql(sql.c_str());
CComBSTR csql(sql.c_str());
imp->BuildSqlImp(binfo,bwhere,bsql,&csql);
USES_CONVERSION;
sql = OLE2A(csql);
}
void CGenDb::SetALLRowdata(C2DVector<std::string>& csa)
{
SAFEARRAY* psa= csa.Detach();
try{imp->SetAllRowDataImp(&psa);}
catch(_com_error &e){csa.Attach(psa);throw e;}
csa.Attach(psa);
}
int CGenDb::SetOneRowdata(C2DVector<std::string>& csa)
{
int num(0);
SAFEARRAY* psa= csa.Detach();
try{imp->SetOneRowDataImp(&num,&psa);}
catch(_com_error &e){csa.Attach(psa);throw e;}
csa.Attach(psa);
return num;
}
void CGenDb::GetAllRowdata(C2DVector<std::string>& csa)
{
SAFEARRAY* psa= NULL;
imp->GetAllRowDataImp(&psa);
if(psa != NULL)csa.Attach(psa);
}
int CGenDb::GetOneRowdata(C2DVector<std::string>& csa)
{
int num(0);
SAFEARRAY* psa= NULL;
imp->GetOneRowDataImp(&num,&psa);
if(psa != NULL)csa.Attach(psa);
return num;
}
bool CGenDb::GetChunk(unsigned char *& buffer,int& len)
{
int left_len(0);
imp->GetChunkImp(&buffer,&len,&left_len);
if(left_len==0)
return true;
return false;
}
void CGenDb::AppendChunk(unsigned char *& buffer,int input_len,int len)
{
imp->AppendChunkImp(buffer,input_len,len);
}
bool CGenDb::BindtoParameter(int& t)
{
int tag(0);
imp->BindtoParameterImp(&tag,&t);
if(tag==0)
return false;
return true;
}
void CGenDb::ConnecTodbcdb(DataBase_Type type,int timeout,const char* address,
const char* user,const char* pwd,const char* db)
{
HRESULT hr = S_OK;
#if defined(COMTEST)
hr = CoInitialize(NULL);
if (FAILED(hr)) _com_issue_error(hr);
#endif
switch(type)
{
//case SQL_Unknown:
case SQL_Server_2000:
case SQL_Server_2005:
case SQL_Server_2008:
case MySQL_ODBC_5_1_Driver:
case MySQL_ODBC_3_51_Driver:
hr = imp.CreateInstance(__uuidof(GenOdbcImp));
break;
case Oracle_Driver_9I:
hr = imp.CreateInstance(__uuidof(GenOci9IImp));
break;
case Oracle_Driver_10G:
hr = imp.CreateInstance(__uuidof(GenOci10GImp));
break;
//case Oracle_Driver_10G_R2:
case Oracle_Driver_11G:
hr = imp.CreateInstance(__uuidof(GenOci11GImp));
break;
default:
hr = ERROR_INVALID_DRIVE;
_com_issue_error(hr);
break;
}
if (FAILED(hr)) _com_issue_error(hr);
imp->ConnectDbImp(type,timeout,_bstr_t(address),
_bstr_t(user),_bstr_t(pwd),_bstr_t(db));
}
void CGenDb::SetTransaction()
{
imp->SetTransactionImp();
};
void CGenDb::CommitTransaction()
{
imp->CommitTransactionImp();
};
void CGenDb::RollbackTransaction()
{
imp->RollbackTransactionImp();
};
//void CGenDb::AddParameter(int t)
//{
// //imp->AddParameter(t);
//}
//void CGenDb::AddParameter(std::string& t)
//{
// //imp->AddParameter(t);
//}
//bool CGenDb::BindtoParameter(std::string& t)
//{
// return true;//imp->BindtoParameter(t);
//}