#include "baseoracle.h"
BaseOracle::BaseOracle()
{
char oneField[32]={0};
m_user="system";
m_passwd="xtw5633161XTW";
// m_user="sdzw";
// m_passwd="sdzw";
m_db="192.169.3.88:1521/orcl";
env = Environment::createEnvironment ("UTF8","UTF8"); //
}
BaseOracle::~BaseOracle()
{
if(env!=NULL)
{
Environment::terminateEnvironment (env); //结束环境
}
}
/**
* 查询获取表信息
* @param sql 查询语句
* @param ary 结果
* @return 获取到的记录个数
*/
int BaseOracle::queryresult (const char* sql,string& res,string& err)
{
if(env==NULL)
{
// WriteLog("创建一个Environment对象 error");
err="创建一个Environment对象 error";
return -1;
}
if (sql == NULL)
{
// WriteLog("sql error");
err="sql error";
return -2; }
int colNum = 0, icount = 0;
ResultSet *rset = NULL;
std::vector<oracle::occi::MetaData>* meta = NULL;
string onevalue;
try{
conn = env->createConnection (m_user, m_passwd, m_db); //建立连接
cout<<sql<<endl;
stmt = conn->createStatement (sql);
rset = stmt->executeQuery ();
meta = &rset->getColumnListMetaData();
colNum = meta->size();
char *charRes = new char[1024*1024*5];
memset(charRes,0,1024*1024*5);
char *pRes = charRes;
char* separator="\1";
int spLen = strlen(separator);
char* separatort="\2";
int spLent = strlen(separatort);
while (rset->next ())
{
icount ++;
for (int i = 1; i < colNum+1; i++)
{
if((int)(1024*1024*5-(pRes-charRes))< (rset->getString(i).length()+2))
{
res+=charRes;
pRes=charRes;
memset(pRes,0,1024*1024*5);
}
memcpy( pRes,rset->getString(i).c_str(),strlen(rset->getString(i).c_str())) ;
pRes +=strlen(rset->getString(i).c_str());
if(i<colNum)
{
memcpy(pRes,separatort,spLent); /// \2
pRes = pRes+spLent;
}
}
memcpy(pRes,separator,spLen); ///\1
pRes = pRes+spLen;
}
if(charRes!=NULL)
{
res+=charRes;
}
if(charRes)
delete charRes;
stmt->closeResultSet (rset);
rset = NULL;
}
catch (SQLException ex)
{
if (ex.getErrorCode() == 942)
{
cout<<"table or view does not exist"<<endl;
// WriteLog("table or view does not exist");
err="table or view does not exist";
icount = -3;
}
else if(ex.getErrorCode() == 12154)
{
cout<<"监听程序错误"<<endl;
// WriteLog("监听程序错误");
err="监听程序错误";
icount = -4;
}
else if(ex.getErrorCode() == 1017)
{
cout<<"无效的用户名密码 error"<<endl;
// WriteLog("无效的用户名密码");
err="无效的用户名密码";
icount = -5;
}
else
{
char error[1024]={0};
sprintf(error,"Error ErrorCode %d Message %s",ex.getErrorCode(),ex.getMessage().c_str());
cout<<error<<endl;
// WriteLog("error:%s",error);
err=error;
icount = -6;
}
}
if (rset!= NULL)
{
stmt->closeResultSet (rset);
rset = NULL;
}
if(stmt!=NULL)
{
printf("结束stmt\n");
conn->terminateStatement (stmt);
stmt=NULL;
}
if(conn!=NULL)
{
printf("结束连接\n");
env->terminateConnection (conn); //结束连接
conn=NULL;
}
return icount;
}
int BaseOracle::query (const char* sql, vector<FieldUnit>& ary,string& err)
{
if(env==NULL)
{
// WriteLog("创建一个Environment对象 error");
err="创建一个Environment对象 error";
return -1;
}
if (sql == NULL)
{
// WriteLog("sql error");
err="sql error";
return -2;
}
int colNum = 0, icount = 0;
ResultSet *rset = NULL;
std::vector<oracle::occi::MetaData>* meta = NULL;
string onevalue;
ary.clear();
try{
conn = env->createConnection (m_user, m_passwd, m_db); //建立连接
stmt = conn->createStatement (sql);
rset = stmt->executeQuery ();
meta = &rset->getColumnListMetaData();
colNum = meta->size();
while (rset->next ())
{
icount ++;
FieldUnit oneunit;
oneunit.clear();
for (int i = 1; i < colNum+1; i++)
{
onevalue = rset->getString(i);
oneunit.push_back (onevalue);
}
ary.push_back (oneunit);
}
stmt->closeResultSet (rset);
rset = NULL;
}
catch (SQLException ex)
{
if (ex.getErrorCode() == 942)
{
cout<<"table or view does not exist"<<endl;
// WriteLog("table or view does not exist");
FieldUnit oneunit;
oneunit.clear();
oneunit.push_back("table or view does not exist");
ary.push_back (oneunit);
err="table or view does not exist";
icount = -3;
}
else if(ex.getErrorCode() == 12154)
{
cout<<"监听程序错误"<<endl;
// WriteLog("监听程序错误");
FieldUnit oneunit;
oneunit.clear();
oneunit.push_back("监听程序错误");
ary.push_back (oneunit);
err="监听程序错误";
icount = -4;
}
else if(ex.getErrorCode() == 1017)
{
cout<<"无效的用户名密码 error"<<endl;
// WriteLog("无效的用户名密码");
FieldUnit oneunit;
oneunit.clear();
oneunit.push_back("无效的用户名密码");
ary.push_back (oneunit);
err="无效的用户名密码";
icount = -6;
}
else
{
cout<<"*** Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
// WriteLog("Error number: %d:%s",ex.getErrorCode(),ex.getMessage().c_str());
// string error=FormatStr("Error number: %d %s ",ex.getErrorCode(),ex.getMessage().c_str());
string error="Error number: %d %s "+ex.getErrorCode()+ex.getMessage();
FieldUnit oneunit;
oneunit.clear();
oneunit.push_back(error);
ary.push_back (oneunit);
err=error;
icount = -7;
}
}
if (rset!= NULL)
{
stmt->closeResultSet (rset);
rset = NULL;
}
if(stmt!=NULL)
{
printf("结束stmt\n");
conn->terminateStatement (stmt);
stmt=NULL;
}
if(conn!=NULL)
{
printf("结束连接\n");
env->terminateConnection (conn); //结束连接
conn=NULL;
}
return icount;
}
/**
* @param sql
* @return 0 success -1 -2 fail
*/
int BaseOracle::runonesql (const char* sql,string& err)
{
int icount = 0;
if (sql == NULL)
{
// WriteLog ("[occi] **runonesql(%s)", sql);
err="sql error";
return -1;
}
if(env==NULL)
{
// WriteLog("创建一个Environment对象 error");
err="创建一个Environment对象 error";
return -2;
}
try{
conn = env->createConnection (m_user, m_passwd, m_db); //建立连接
stmt = conn->createStatement (sql);
stmt->executeUpdate ();
}
catch (SQL