📄 ocistmt.cpp
字号:
#ifndef _COCIStatement
#include "ocistmt.h"
#endif
#ifndef _COCISession
#include "ocisess.h"
#endif
#ifndef _COCILob
#include "ocilob.h"
#endif
#ifndef _COCIRef
#include "ociref.h"
#endif
#ifndef _COCIObject
#include "ociobj.h"
#endif
#ifndef _COCIBinary
#include "ocibin.h"
#endif
#ifndef _COCINumber
#include "ocinum.h"
#endif
#ifndef _COCIDate
#include "ocidate.h"
#endif
#ifndef _COCIString
#include "ocistr.h"
#endif
#ifndef _COCIVArray
#include "ocivry.h"
#endif
#ifndef _COCINestedTable
#include "ocintab.h"
#endif
COCIStatement::COCIStatement(const COCISession& Sess)
: stmthp(0)
, m_Session(Sess)
, sqlstmt(0)
, m_pDefine(0)
, m_pBind(0)
, m_Rows(1)
, m_RowsFetched(0)
, m_TotalRowsFetched(0)
, m_pObject(0)
, m_Object_Count(0)
, m_Ref_Count(0)
, m_VArray_Number_Count(0)
, m_VArray_String_Count(0)
, m_VArray_Object_Count(0)
, m_pObjIndStruct(0)
{
}
COCIStatement::~COCIStatement()
{
if(sqlstmt)
delete [] sqlstmt;
CHECK(m_Session.get_error(),OCIHandleFree(stmthp,OCI_HTYPE_STMT));
stmthp = 0;
clean_up();
}
void COCIStatement::clean_up(void)
{
if(m_pObjIndStruct.size() > 0)
{
ub4 size = m_pObjIndStruct.size();
for(ub4 i = 0; i < size; i++)
{
delete m_pObjIndStruct[i];
m_pObjIndStruct.pop_back();
}
}
if(m_pRefIndStruct.size() > 0)
{
ub4 size = m_pRefIndStruct.size();
for(ub4 i = 0; i < size; i++)
{
delete m_pRefIndStruct[i];
m_pRefIndStruct.pop_back();
}
}
if(m_pVArrayNumberIndStruct.size() > 0)
{
ub4 size = m_pVArrayNumberIndStruct.size();
for(ub4 i = 0; i < size; i++)
{
delete m_pVArrayNumberIndStruct[i];
m_pVArrayNumberIndStruct.pop_back();
}
}
if(m_pVArrayStringIndStruct.size() > 0)
{
ub4 size = m_pVArrayStringIndStruct.size();
for(ub4 i = 0; i < size; i++)
{
delete m_pVArrayStringIndStruct[i];
m_pVArrayStringIndStruct.pop_back();
}
}
if(m_pVArrayObjectIndStruct.size() > 0)
{
ub4 size = m_pVArrayObjectIndStruct.size();
for(ub4 i = 0; i < size; i++)
{
delete m_pVArrayObjectIndStruct[i];
m_pVArrayObjectIndStruct.pop_back();
}
}
m_Rows = 1;
m_RowsFetched = 0;
m_TotalRowsFetched = 0;
m_Object_Count = 0;
m_Ref_Count = 0;
m_VArray_Number_Count = 0;
m_VArray_String_Count = 0;
}
COCIStatement& COCIStatement::operator = (char * str)
{
if(!stmthp)
CHECK(m_Session.get_error(),OCIHandleAlloc((dvoid *)m_Session.get_env(), (dvoid **)&stmthp, OCI_HTYPE_STMT, 0, 0));
if(sqlstmt)
{
delete [] sqlstmt;
sqlstmt = (char*)0;
clean_up();
}
ub4 len = strlen(str);
sqlstmt = new char[len + 1];
sprintf(sqlstmt,str);
CHECK(m_Session.get_error(),OCIStmtPrepare(stmthp, m_Session.get_error(), (unsigned char *)sqlstmt, strlen(sqlstmt), OCI_NTV_SYNTAX, 0));
return *this;
}
bool COCIStatement::execute(void)
{
bool ret_val = true;
sb4 status = OCIStmtExecute(m_Session.get_svc(), stmthp, m_Session.get_error(), (ub4) m_Rows, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
if(status == OCI_NO_DATA)
{
// Check to see if we were bringing back gobbets of data (=> not all slots filled)
ub4 size = sizeof(ub4);
CHECK(m_Session.get_error(), OCIAttrGet ( (dvoid *) stmthp,
(ub4) OCI_HTYPE_STMT,
(dvoid*)&m_RowsFetched, &size, (ub4) OCI_ATTR_ROW_COUNT,
m_Session.get_error()));
if(m_RowsFetched == 0)
{
m_TotalRowsFetched = 0;
ret_val = false;
}
}
else
{
CHECK(m_Session.get_error(), status);
m_RowsFetched = m_Rows;
m_TotalRowsFetched += m_Rows;
}
if(ret_val == true)
{
// Non-scalar (fix-ups)
fix_up_object();
fix_up_ref();
fix_up_coll();
}
return ret_val;
}
bool COCIStatement::fetch(void)
{
bool ret_val = true;
// Has all data been retrieved? (=> can't fetch any more data)
if(m_RowsFetched < m_Rows)
{
ret_val = false;
}
else
{
sb4 status = OCIStmtFetch(stmthp, m_Session.get_error(), (ub4) m_Rows, (ub4) OCI_FETCH_NEXT,
(ub4) OCI_DEFAULT);
if ( status == OCI_NO_DATA )
{
// Check to see if we were bringing back gobbets of data (=> not all slots filled)
ub4 size = sizeof(ub4);
m_RowsFetched = 0;
CHECK(m_Session.get_error(), OCIAttrGet ( (dvoid *) stmthp,
(ub4) OCI_HTYPE_STMT,
(dvoid*)&m_RowsFetched, &size, (ub4) OCI_ATTR_ROW_COUNT,
m_Session.get_error()));
// If same => no more rows
if(m_RowsFetched == m_TotalRowsFetched)
{
m_TotalRowsFetched = 0;
ret_val = false;
}
else // Otherwise => more data
{
m_RowsFetched = m_RowsFetched - m_TotalRowsFetched;
}
}
else
{
CHECK(m_Session.get_error(), status);
m_RowsFetched = m_Rows;
m_TotalRowsFetched += m_Rows;
}
}
if(ret_val == true)
{
// Non-scalar (fix-ups)
fix_up_object();
fix_up_ref();
fix_up_coll();
}
return ret_val;
}
void COCIStatement::fix_up_object(void)
{
if(m_pObjIndStruct.size() > 0)
{
for(ub4 i = 0; i < m_pObjIndStruct.size(); i++)
{
m_pObjIndStruct[i]->fix_up_obj(m_RowsFetched);
}
}
}
void COCIStatement::fix_up_ref(void)
{
if(m_pRefIndStruct.size() > 0)
{
for(ub4 i = 0; i < m_pRefIndStruct.size(); i++)
{
m_pRefIndStruct[i]->fix_up_ref(m_RowsFetched);
}
}
}
void COCIStatement::fix_up_coll(void)
{
if(m_pVArrayNumberIndStruct.size() > 0)
{
for(ub4 i = 0; i < m_pVArrayNumberIndStruct.size(); i++)
{
m_pVArrayNumberIndStruct[i]->fix_up_coll(m_RowsFetched);
}
}
if(m_pVArrayStringIndStruct.size() > 0)
{
for(ub4 i = 0; i < m_pVArrayStringIndStruct.size(); i++)
{
m_pVArrayStringIndStruct[i]->fix_up_coll(m_RowsFetched);
}
}
if(m_pVArrayObjectIndStruct.size() > 0)
{
for(ub4 i = 0; i < m_pVArrayObjectIndStruct.size(); i++)
{
m_pVArrayObjectIndStruct[i]->fix_up_coll(m_RowsFetched);
}
}
}
void COCIStatement::define(int pos, int& valuep, signed short* is_null)
{
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep, (sb4)sizeof(int), (ub2)SQLT_INT, (is_null) ? is_null: 0,0,0,OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::define(int pos, std::vector<int>& valuep)
{
// Check to see if there is at least one element in the std::vector
if(valuep.size() == 0)
{
// Put one in there
valuep.push_back(1);
}
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep[0], (sb4)sizeof(int), (ub2)SQLT_INT, 0,0,0,OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::bind(int pos, int& valuep)
{
CHECK(m_Session.get_error(), OCIBindByPos(stmthp, &m_pBind, m_Session.get_error(), (ub4)pos, (dvoid*)&valuep, (sb4)sizeof(int), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::bind(char* name, int& valuep)
{
CHECK(m_Session.get_error(), OCIBindByName(stmthp, &m_pBind, m_Session.get_error(), (text *)name, strlen(name), (dvoid*)&valuep, (sb4)sizeof (int), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::bind(int pos, std::vector<int>& valuep)
{
if(valuep.size() == 0)
{
throw CBindException();
}
CHECK(m_Session.get_error(), OCIBindByPos(stmthp, &m_pBind, m_Session.get_error(), (ub4)pos, (dvoid*)&valuep[0], (sb4)sizeof(int), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::bind(char* name, std::vector<int>& valuep)
{
if(valuep.size() == 0)
{
throw CBindException();
}
CHECK(m_Session.get_error(), OCIBindByName(stmthp, &m_pBind, m_Session.get_error(), (text *)name, strlen(name), (dvoid*)&valuep[0], (sb4)sizeof (int), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::define(int pos, long& valuep)
{
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep, (sb4)sizeof(eb4), (ub2)SQLT_INT, 0,0,0,OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::define(int pos, std::vector<long>& valuep)
{
// Check to see if there is at least one element in the std::vector
if(valuep.size() == 0)
{
// Put one in there
valuep.push_back(1);
}
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep[0], (sb4)sizeof(eb4), (ub2)SQLT_INT, 0,0,0,OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::bind(int pos, long& valuep)
{
CHECK(m_Session.get_error(), OCIBindByPos(stmthp, &m_pBind, m_Session.get_error(), (ub4)pos, (dvoid*)&valuep, (sb4)sizeof(eb4), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::bind(char* name, long& valuep)
{
CHECK(m_Session.get_error(), OCIBindByName(stmthp, &m_pBind, m_Session.get_error(), (text *)name, strlen(name), (dvoid*)&valuep, (sb4)sizeof (eb4), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::bind(int pos, std::vector<long>& valuep)
{
if(valuep.size() == 0)
{
throw CBindException();
}
CHECK(m_Session.get_error(), OCIBindByPos(stmthp, &m_pBind, m_Session.get_error(), (ub4)pos, (dvoid*)&valuep[0], (sb4)sizeof(eb4), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::bind(char* name, std::vector<long>& valuep)
{
if(valuep.size() == 0)
{
throw CBindException();
}
CHECK(m_Session.get_error(), OCIBindByName(stmthp, &m_pBind, m_Session.get_error(), (text *)name, strlen(name), (dvoid*)&valuep[0], (sb4)sizeof (eb4), (ub2)SQLT_INT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::define(int pos, float& valuep)
{
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep, (sb4)sizeof(float), (ub2)SQLT_FLT, 0,0,0,OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::define(int pos, std::vector<float>& valuep)
{
// Check to see if there is at least one element in the std::vector
if(valuep.size() == 0)
{
// Put one in there
valuep.push_back(1);
}
CHECK(m_Session.get_error(), OCIDefineByPos(stmthp, &m_pDefine, m_Session.get_error(), (ub4)pos, &valuep[0], (sb4)sizeof(float), (ub2)SQLT_FLT, 0,0,0,OCI_DEFAULT));
m_Rows = valuep.size();
}
void COCIStatement::bind(int pos, float& valuep)
{
CHECK(m_Session.get_error(), OCIBindByPos(stmthp, &m_pBind, m_Session.get_error(), (ub4)pos, (dvoid*)&valuep, (sb4)sizeof(float), (ub2)SQLT_FLT, 0,0,0,0,0, OCI_DEFAULT));
m_Rows = 1;
}
void COCIStatement::bind(int pos, std::vector<float>& valuep)
{
if(valuep.size() == 0)
{
throw CBindException();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -