support for bulk operations

----------------------------------------------------------------------
This commit is contained in:
stephenhutton
2005-09-16 04:42:58 +00:00
parent e12918c5ff
commit eddb6bde52
3 changed files with 1835 additions and 67 deletions

633
soci.cpp
View File

@@ -40,9 +40,9 @@ void getErrorDetails(sword res, OCIError *errhp,
case OCI_ERROR:
OCIErrorGet(errhp, 1, 0, &errcode,
errbuf, sizeof(errbuf), OCI_HTYPE_ERROR);
msg = reinterpret_cast<char*>(errbuf);
errNum = static_cast<int>(errcode);
break;
msg = reinterpret_cast<char*>(errbuf);
errNum = static_cast<int>(errcode);
break;
case OCI_INVALID_HANDLE:
msg = "SOCI error: Invalid handle";
break;
@@ -350,21 +350,28 @@ bool Statement::execute(int num)
{
if (num > 0)
{
for (size_t i = 0; i != intos_.size(); ++i)
intos_[i]->preFetch();
for (size_t i = 0; i != uses_.size(); ++i)
uses_[i]->preUse();
preFetch();
preUse();
}
fetchSize_ = intosSize();
int bindSize = usesSize();
if (bindSize > 1 && fetchSize_ > 1)
{
throw SOCIError("Bulk insert/update and bulk select not allowed in same query");
}
if (num > 0)
num = std::max(num, std::max(fetchSize_, bindSize));
bool gotData;
sword res = OCIStmtExecute(session_.svchp_, stmtp_, session_.errhp_,
num, 0, 0, 0, OCI_DEFAULT);
static_cast<ub4>(num), 0, 0, 0, OCI_DEFAULT);
if (res == OCI_SUCCESS || res == OCI_SUCCESS_WITH_INFO)
{
if (num > 0)
gotData = true;
else
gotData = false;
gotData = num > 0 ? true : false;
}
else // res != OCI_SUCCESS
{
@@ -372,16 +379,20 @@ bool Statement::execute(int num)
{
throwSOCIError(res, session_.errhp_);
}
gotData = false;
else if (res == OCI_NO_DATA)
{
gotData = fetchSize_ > 1 ? resizeIntos() : false;
}
else
{
gotData = false;
}
}
if (num > 0)
{
for (size_t i = 0; i != intos_.size(); ++i)
intos_[i]->postFetch(gotData, false);
for (size_t i = 0; i != uses_.size(); ++i)
uses_[i]->postUse();
postFetch(gotData, false);
postUse();
}
return gotData;
@@ -389,12 +400,14 @@ bool Statement::execute(int num)
bool Statement::fetch()
{
for (size_t i = 0; i != intos_.size(); ++i)
intos_[i]->preFetch();
if (!fetchSize_)
return false;
bool gotData = false;
sword res = OCIStmtFetch(stmtp_, session_.errhp_, 1,
OCI_FETCH_NEXT, OCI_DEFAULT);
int num = fetchSize_;
sword res = OCIStmtFetch(stmtp_, session_.errhp_, num,
OCI_FETCH_NEXT, OCI_DEFAULT);
if (res == OCI_SUCCESS || res == OCI_SUCCESS_WITH_INFO)
{
gotData = true;
@@ -402,17 +415,110 @@ bool Statement::fetch()
else
{
if (res == OCI_NO_DATA)
gotData = false;
{
if (fetchSize_ > 1)
{
resizeIntos();
gotData = true;
fetchSize_ = 0;
}
else
{
gotData = false;
}
}
else
{
throwSOCIError(res, session_.errhp_);
}
}
for (size_t i = 0; i != intos_.size(); ++i)
intos_[i]->postFetch(gotData, true);
postFetch(gotData, true);
return gotData;
}
int Statement::intosSize()
{
int intosSize = 0;
for (size_t i = 0; i != intos_.size(); ++i)
{
if (i==0)
{
intosSize = intos_[i]->size();
}
else if (intosSize != intos_[i]->size())
{
std::ostringstream msg;
msg << "Bind variable size mismatch (into["<<i<<"] has size "
<< intos_[i]->size() << ", intos_[0] has size "<< intosSize <<std::endl;
throw SOCIError(msg.str());
}
}
return intosSize;
}
int Statement::usesSize()
{
int usesSize = 0;
for (size_t i = 0; i != uses_.size(); ++i)
{
if (i==0)
{
usesSize = uses_[i]->size();
}
else if (usesSize != uses_[i]->size())
{
std::ostringstream msg;
msg << "Bind variable size mismatch (use["<<i<<"] has size "
<< uses_[i]->size() << ", use[0] has size "<< usesSize <<std::endl;
throw SOCIError(msg.str());
}
}
return usesSize;
}
bool Statement::resizeIntos()
{
int rows;
// Get The Actual Number Of Rows Fetched
ub4 sizep = sizeof(ub4);
OCIAttrGet(static_cast<dvoid*>(stmtp_), static_cast<ub4>(OCI_HTYPE_STMT),
static_cast<dvoid*>(&rows), static_cast<ub4*>(&sizep),
static_cast<ub4>(OCI_ATTR_ROWS_FETCHED), session_.errhp_);
for (size_t i = 0; i != intos_.size(); ++i)
{
intos_[i]->resize(rows);
}
return rows > 0 ? true : false;
}
void Statement::preFetch()
{
for (size_t i = 0; i != intos_.size(); ++i) // todo for each?
intos_[i]->preFetch();
}
void Statement::preUse()
{
for (size_t i = 0; i != uses_.size(); ++i)
uses_[i]->preUse();
}
void Statement::postFetch(bool gotData, bool calledFromFetch)
{
for (size_t i = 0; i != intos_.size(); ++i)
intos_[i]->postFetch(gotData, calledFromFetch);
}
void Statement::postUse()
{
for (size_t i = 0; i != uses_.size(); ++i)
uses_[i]->postUse();
}
void Statement::define()
{
int definePosition = 1;
@@ -761,7 +867,6 @@ void StandardIntoType::postFetch(bool gotData, bool calledFromFetch)
// no need to set anything (fetch() will return false)
return;
}
if (ind_ != NULL)
{
if (gotData == false)
@@ -816,6 +921,85 @@ void StandardUseType::cleanUp()
}
}
// vector based types
void VectorIntoType::postFetch(bool gotData, bool calledFromFetch)
{
if(gotData)
{
convertFrom();
}
if (calledFromFetch == true && gotData == false)
{
// this is a normal end-of-rowset condition,
// no need to set anything (fetch() will return false)
return;
}
if (ind_ != NULL)
{
for(size_t i=0; i<indOCIHolderVec_.size(); ++i, ++ind_)
{
if (gotData == false)
{
*ind_ = eNoData;
}
else
{
if (indOCIHolderVec_[i] == 0)
*ind_ = eOK;
else if (indOCIHolderVec_[i] == -1)
*ind_ = eNull;
else
*ind_ = eTruncated;
}
}
}
else
{
if (gotData == false)
{
// no data fetched and no indicator - programming error!
throw SOCIError("No data fetched and no indicator defined.");
}
}
}
void VectorIntoType::cleanUp()
{
if (defnp_)
{
OCIHandleFree(defnp_, OCI_HTYPE_DEFINE);
defnp_ = NULL;
}
}
void VectorUseType::preUse()
{
convertTo();
if (ind_ != NULL)
{
for (size_t i=0; i<indOCIHolderVec_.size(); ++i, ++ind_)
{
if (*ind_ == eNull)
indOCIHolderVec_[i] = -1; // null
else
indOCIHolderVec_[i] = 0; // value is OK
}
}
}
void VectorUseType::cleanUp()
{
if (bindp_)
{
OCIHandleFree(bindp_, OCI_HTYPE_BIND);
bindp_ = NULL;
}
}
// into and use types for char
void IntoType<char>::define(Statement &st, int &position)
@@ -860,6 +1044,50 @@ void UseType<char>::bind(Statement &st, int &position)
}
}
// into and use types for std::vector<char>
void IntoType<std::vector<char> >::define(Statement &st, int &position)
{
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(char), SQLT_AFC,
indOCIHolder_, 0, 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void UseType<std::vector<char> >::bind(Statement &st, int &position)
{
st_ = &st;
sword res;
if (name_.empty())
{
// no name provided, bind by position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(char), SQLT_AFC,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
else
{
// bind by name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()),
&v_.at(0), sizeof(char), SQLT_AFC,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
// into and use types for unsigned long
@@ -904,6 +1132,49 @@ void UseType<unsigned long>::bind(Statement &st, int &position)
throwSOCIError(res, st.session_.errhp_);
}
}
// into and use types for std::vector<unsigned long>
void IntoType<std::vector<unsigned long> >::define(Statement &st, int &position)
{
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(unsigned long), SQLT_UIN,
indOCIHolder_, 0, 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void UseType<std::vector<unsigned long> >::bind(Statement &st, int &position)
{
st_ = &st;
sword res;
if (name_.empty())
{
// no name provided, bind by position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(unsigned long), SQLT_UIN,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
else
{
// bind by name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()),
&v_.at(0), sizeof(unsigned long), SQLT_UIN,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
// into and use types for double
@@ -948,6 +1219,50 @@ void UseType<double>::bind(Statement &st, int &position)
}
}
// into and use types for std::vector<double>
void IntoType<std::vector<double> >::define(Statement &st, int &position)
{
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(double), SQLT_FLT,
indOCIHolder_, 0, 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void UseType<std::vector<double> >::bind(Statement &st, int &position)
{
st_ = &st;
sword res;
if (name_.empty())
{
// no name provided, bind by position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, &v_.at(0), sizeof(double), SQLT_FLT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
else
{
// bind by name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()), &v_.at(0), sizeof(double), SQLT_FLT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
// into and use types for char*
void IntoType<char*>::define(Statement &st, int &position)
@@ -1006,7 +1321,7 @@ void IntoType<std::string>::define(Statement &st, int &position)
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, buf_, bufSize, SQLT_STR,
&indOCIHolder_, 0, &rcode_, OCI_DEFAULT);
&indOCIHolder_, 0, &rcode_, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
@@ -1025,6 +1340,8 @@ void IntoType<std::string>::cleanUp()
{
delete [] buf_;
buf_ = NULL;
StandardIntoType::cleanUp();
}
void UseType<std::string>::bind(Statement &st, int &position)
@@ -1082,6 +1399,136 @@ void UseType<std::string>::cleanUp()
buf_ = NULL;
}
// into and use types for std::vector<std::string>
size_t columnSize(Statement &st, int position)
{
// TODO move this to the Statement class?
//Do A Describe To Get The Size For This Column
//Note: we may want to optimize so that the OCI_DESCRIBE_ONLY call happens only once per statement
//Possibly use existing statement::describe() / make column prop access lazy at same time
size_t colSize(0);
sword res = OCIStmtExecute(st.session_.svchp_, st.stmtp_, st.session_.errhp_,
1, 0, 0, 0, OCI_DESCRIBE_ONLY);
if (res != OCI_SUCCESS) throwSOCIError(res, st.session_.errhp_);
// Get The Column Handle
OCIParam* colhd;
res = OCIParamGet( reinterpret_cast<dvoid*>(st.stmtp_),
static_cast<ub4>(OCI_HTYPE_STMT),
reinterpret_cast<OCIError*>(st.session_.errhp_),
reinterpret_cast<dvoid**>(&colhd),
static_cast<ub4>(position));
if (res != OCI_SUCCESS) throwSOCIError(res, st.session_.errhp_);
// Get The Data Size
res = OCIAttrGet( reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&colSize),
0,
static_cast<ub4>(OCI_ATTR_DATA_SIZE),
reinterpret_cast<OCIError*>(st.session_.errhp_));
if (res != OCI_SUCCESS) throwSOCIError(res, st.session_.errhp_);
return colSize;
}
void IntoType<std::vector<std::string> >::define(Statement &st, int &position)
{
st_ = &st;
strLen_ = columnSize(st, position) + 1;
const size_t bufSize = strLen_ * v_.size();
buf_ = new char[bufSize];
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, buf_, strLen_, SQLT_CHR,
indOCIHolder_, &sizes_[0], 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void IntoType<std::vector<std::string> >::postFetch(bool gotData, bool calledFromFetch)
{
char* pos = buf_;
if (gotData)
{
for (size_t i=0; i<v_.size(); ++i)
{
v_[i] = std::string(pos, sizes_[i]);
pos += strLen_;
}
}
VectorIntoType::postFetch(gotData, calledFromFetch);
}
void IntoType<std::vector<std::string> >::cleanUp()
{
delete [] buf_;
buf_ = NULL;
VectorIntoType::cleanUp();
}
void UseType<std::vector<std::string> >::bind(Statement &st, int &position)
{
st_ = &st;
sword res;
size_t maxSize = 0;
for (size_t i=0; i<v_.size(); ++i)
{
size_t sz = v_[i].length();
sizes_.push_back(sz);
maxSize = sz > maxSize ? sz : maxSize;
}
buf_ = new char[maxSize * v_.size()];
char* pos = buf_;
for (size_t i=0; i<v_.size(); ++i)
{
strcpy(pos, v_[i].c_str());
pos += maxSize;
}
if(name_.empty())
{
// No Name Provided, Bind By Position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, (dvoid*)buf_, (sb4)maxSize, SQLT_CHR,
indOCIHolder_, (ub2*)&sizes_.at(0), 0, 0, 0, OCI_DEFAULT);//todo static cast?
}
else
{
// Bind By Name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()), buf_, (sb4)maxSize, SQLT_CHR,//todo static cast?
indOCIHolder_, (ub2*)&sizes_.at(0), 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void UseType<std::vector<std::string> >::cleanUp()
{
delete []buf_;
buf_ = NULL;
VectorUseType::cleanUp();
}
// into and use types for date and time (struct tm)
void IntoType<std::tm>::define(Statement &st, int &position)
@@ -1114,6 +1561,7 @@ void IntoType<std::tm>::postFetch(bool gotData, bool calledFromFetch)
std::mktime(&tm_);
}
StandardIntoType::postFetch(gotData, calledFromFetch);
}
void UseType<std::tm>::bind(Statement &st, int &position)
@@ -1173,6 +1621,133 @@ void UseType<std::tm>::postUse()
StandardUseType::postUse();
}
// into and use types for std::vector<std::tm>
void IntoType<std::vector<std::tm> >::define(Statement &st, int &position)
{
st_ = &st;
const sb4 dlen = 7;
buf_ = new char[vec_.size() * dlen];
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, buf_, dlen, SQLT_DAT,
indOCIHolder_, 0, 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void IntoType<std::vector<std::tm> >::cleanUp()
{
delete []buf_;
buf_ = NULL;
VectorIntoType::cleanUp();
}
void IntoType<std::vector<std::tm> >::postFetch(bool gotData, bool calledFromFetch)
{
if (gotData)
{
char* pos = buf_;
for (size_t i=0; i < vec_.size(); ++i)
{
std::tm t;
t.tm_isdst = -1;
t.tm_year = (*pos++ - 100) * 100;
t.tm_year += *pos++ - 2000;
t.tm_mon = *pos++ - 1;
t.tm_mday = *pos++;
t.tm_hour = *pos++ - 1;
t.tm_min = *pos++ - 1;
t.tm_sec = *pos++ - 1;
// normalize and compute the remaining fields
std::mktime(&t);
vec_[i] = t;
}
}
VectorIntoType::postFetch(gotData, calledFromFetch);
}
void UseType<std::vector<std::tm> >::bind(Statement &st, int &position)
{
st_ = &st;
sword res;
const sb4 dlen = 7;
buf_ = new ub1[dlen * v_.size()];
if (name_.empty())
{
// no name provided, bind by position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, buf_, dlen, SQLT_DAT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
else
{
// bind by name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()), buf_, dlen, SQLT_DAT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
void UseType<std::vector<std::tm> >::cleanUp()
{
delete []buf_;
buf_ = NULL;
VectorUseType::cleanUp();
}
void UseType<std::vector<std::tm> >::preUse()
{
VectorUseType::preUse();
ub1* pos = buf_;
for (size_t i=0; i < v_.size(); ++i)
{
*pos++ = static_cast<ub1>(100 + (1900 + v_[i].tm_year) / 100);
*pos++ = static_cast<ub1>(100 + v_[i].tm_year % 100);
*pos++ = static_cast<ub1>(v_[i].tm_mon + 1);
*pos++ = static_cast<ub1>(v_[i].tm_mday);
*pos++ = static_cast<ub1>(v_[i].tm_hour + 1);
*pos++ = static_cast<ub1>(v_[i].tm_min + 1);
*pos++ = static_cast<ub1>(v_[i].tm_sec + 1);
}
}
void UseType<std::vector<std::tm> >::postUse()
{
ub1* pos = buf_;
for (size_t i=0; i<v_.size(); ++i)
{
v_[i].tm_isdst = -1;
v_[i].tm_year = (*pos++ - 100) * 100;
v_[i].tm_year += *pos++ - 2000;
v_[i].tm_mon = *pos++ - 1;
v_[i].tm_mday = *pos++;
v_[i].tm_hour = *pos++ - 1;
v_[i].tm_min = *pos++ - 1;
v_[i].tm_sec = *pos++ - 1;
// normalize and compute the remaining fields
std::mktime(&v_[i]);
}
VectorUseType::postUse();
}
// into and use types for Statement (for nested statements and cursors)
void IntoType<Statement>::define(Statement &st, int &position)

566
soci.h
View File

@@ -53,6 +53,9 @@ public:
virtual void preFetch() = 0;
virtual void postFetch(bool gotData, bool calledFromFetch) = 0;
virtual void cleanUp() = 0;
virtual int size(){return 1;}
virtual void resize(int sz){}
};
// this is intended to be a base class for all classes that deal with
@@ -66,6 +69,8 @@ public:
virtual void preUse() = 0;
virtual void postUse() = 0;
virtual void cleanUp() = 0;
virtual int size(){return 1;};
};
namespace details {
@@ -75,7 +80,7 @@ class TypePtr
{
public:
TypePtr(T *p) : p_(p) {}
~TypePtr() { delete p_; }
~TypePtr() { delete p_; }
T * get() const { return p_; }
void release() const { p_ = NULL; }
@@ -108,6 +113,12 @@ details::IntoTypePtr into(T &t, T1 p1)
return details::IntoTypePtr(new IntoType<T>(t, p1));
}
template <typename T>
details::IntoTypePtr into(T &t, std::vector<eIndicator> &indicator)
{
return details::IntoTypePtr(new IntoType<T>(t, indicator));
}
template <typename T>
details::IntoTypePtr into(T &t, eIndicator &indicator)
{
@@ -183,6 +194,12 @@ details::UseTypePtr use(T &t, T1 p1)
return details::UseTypePtr(new UseType<T>(t, p1));
}
template <typename T>
details::UseTypePtr use(T &t, const std::vector<eIndicator> &indicator)
{
return details::UseTypePtr(new UseType<T>(t, indicator));
}
template <typename T>
details::UseTypePtr use(T &t, eIndicator &indicator)
{
@@ -201,6 +218,12 @@ details::UseTypePtr use(T &t, eIndicator &ind, T1 p1)
return details::UseTypePtr(new UseType<T>(t, ind, p1));
}
template <typename T, typename T1>
details::UseTypePtr use(T &t, const std::vector<eIndicator> &ind, T1 p1)
{
return details::UseTypePtr(new UseType<T>(t, ind, p1));
}
template <typename T, typename T1, typename T2, typename T3>
details::UseTypePtr use(T &t, T1 p1, T2 p2, T3 p3)
{
@@ -245,7 +268,7 @@ class PrepareTempType;
} // namespace details
// default TypeConversion, acts as pass through for Row::get()
// default traits class TypeConversion, acts as pass through for Row::get()
// when no actual conversion is needed.
template<typename T>
class TypeConversion
@@ -405,6 +428,37 @@ private:
std::map<std::string, size_t> index_;
};
namespace details
{
class RefCountedPrepareInfo;
// this needs to be lightweight and copyable
class PrepareTempType
{
public:
PrepareTempType(Session &);
PrepareTempType(PrepareTempType const &);
PrepareTempType & operator=(PrepareTempType const &);
~PrepareTempType();
template <typename T>
PrepareTempType & operator<<(T const &t)
{
rcpi_->accumulate(t);
return *this;
}
PrepareTempType & operator,(IntoTypePtr const &i);
PrepareTempType & operator,(UseTypePtr const &u);
RefCountedPrepareInfo * getPrepareInfo() const { return rcpi_; }
private:
RefCountedPrepareInfo *rcpi_;
};
} // namespace details
class Statement
{
public:
@@ -428,14 +482,16 @@ public:
OCIStmt *stmtp_;
Session &session_;
protected:
std::vector<IntoTypeBase*> intos_;
std::vector<UseTypeBase*> uses_;
private:
Row* row_;
int fetchSize_;
template<typename T>
void intoRow()
{
@@ -446,6 +502,14 @@ private:
}
template<eDataType> void bindInto();
int intosSize();
int usesSize();
void preFetch();
void preUse();
void postFetch(bool gotData, bool calledFromFetch);
void postUse();
bool resizeIntos();
};
class Procedure : public Statement
@@ -560,31 +624,7 @@ private:
Session *session_;
};
// this needs to be lightweight and copyable
class PrepareTempType
{
public:
PrepareTempType(Session &);
PrepareTempType(PrepareTempType const &);
PrepareTempType & operator=(PrepareTempType const &);
~PrepareTempType();
template <typename T>
PrepareTempType & operator<<(T const &t)
{
rcpi_->accumulate(t);
return *this;
}
PrepareTempType & operator,(IntoTypePtr const &i);
PrepareTempType & operator,(UseTypePtr const &u);
RefCountedPrepareInfo * getPrepareInfo() const { return rcpi_; }
private:
RefCountedPrepareInfo *rcpi_;
};
// this needs to be lightweight and copyable
class PrepareType
@@ -607,6 +647,8 @@ private:
} // namespace details
class Session
{
public:
@@ -641,6 +683,66 @@ public:
// template specializations for bind and define operations
// into and use type base classes for vectors
class VectorIntoType : public IntoTypeBase
{
public:
VectorIntoType()
: st_(NULL), defnp_(NULL), ind_(NULL), indOCIHolder_(NULL) {}
VectorIntoType(std::vector<eIndicator>& ind)
: st_(NULL), defnp_(NULL), ind_(&ind.at(0)), indOCIHolderVec_(ind.size())
{
indOCIHolder_ = &indOCIHolderVec_.front();
}
virtual void preFetch() {}
virtual void postFetch(bool gotData, bool calledFromFetch);
virtual void cleanUp();
protected:
Statement *st_;
OCIDefine *defnp_;
eIndicator* ind_;
sb2* indOCIHolder_;
private:
virtual void convertFrom() {}
std::vector<sb2> indOCIHolderVec_;
};
class VectorUseType : public UseTypeBase
{
public:
VectorUseType(std::string const &name = std::string())
: st_(NULL), bindp_(NULL), ind_(NULL), name_(name), indOCIHolder_(NULL) {}
VectorUseType(const std::vector<eIndicator> &ind, std::string const &name = std::string())
: st_(NULL), bindp_(NULL), ind_(&ind.at(0)), indOCIHolderVec_(ind.size()), name_(name)
{
indOCIHolder_ = &indOCIHolderVec_.front();
}
virtual void preUse();
virtual void postUse() {convertTo();}
virtual void cleanUp();
protected:
Statement *st_;
OCIBind *bindp_;
private:
const eIndicator *ind_;
std::vector<sb2> indOCIHolderVec_;
protected:
std::string name_;
sb2 *indOCIHolder_;
private:
virtual void convertTo() {}
};
// standard types
@@ -649,8 +751,11 @@ class StandardIntoType : public IntoTypeBase
public:
StandardIntoType()
: st_(NULL), defnp_(NULL), ind_(NULL) {}
StandardIntoType(eIndicator &ind)
: st_(NULL), defnp_(NULL), ind_(&ind) {}
StandardIntoType(eIndicator& ind)
: st_(NULL), defnp_(NULL), ind_(&ind)
{}
StandardIntoType(std::vector<eIndicator>& ind)
: st_(NULL), defnp_(NULL), ind_(&ind.at(0)){}
virtual void preFetch() {}
virtual void postFetch(bool gotData, bool calledFromFetch);
@@ -762,6 +867,83 @@ private:
T &t_;
};
// into and use types for std::vector<T> where T is an integer
template <typename T>
class IntegerVectorIntoType : public VectorIntoType
{
public:
IntegerVectorIntoType(std::vector<T> &v) : v_(v) {}
IntegerVectorIntoType(std::vector<T> &v, std::vector<eIndicator> &vInd)
: VectorIntoType(vInd), v_(v) {}
virtual int size(){return v_.size();}
virtual void resize(int sz){v_.resize(sz);}
virtual void define(Statement &st, int &position)
{
st_ = &st;
sword res;
res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
position++, &v_.at(0), static_cast<sb4>(sizeof(T)), SQLT_INT,
indOCIHolder_, 0, 0, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
private:
std::vector<T>& v_;
};
template <typename T>
class IntegerVectorUseType : public VectorUseType
{
public:
IntegerVectorUseType(std::vector<T> &v, std::string const &name = std::string())
: VectorUseType(name), v_(v) {}
IntegerVectorUseType(std::vector<T> &v, const std::vector<eIndicator> &ind,
std::string const &name = std::string())
: VectorUseType(ind, name), v_(v) {}
virtual int size(){return v_.size();}
virtual void bind(Statement &st, int &position)
{
st_ = &st;
sword res;
if (name_.empty())
{
// no name provided, bind by position
res = OCIBindByPos(st.stmtp_, &bindp_, st.session_.errhp_,
position++, &v_.at(0), static_cast<sb4>(sizeof(T)), SQLT_INT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
else
{
// bind by name
res = OCIBindByName(st.stmtp_, &bindp_, st.session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(name_.c_str())),
static_cast<sb4>(name_.size()),
&v_.at(0), static_cast<sb4>(sizeof(T)), SQLT_INT,
indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT);
}
if (res != OCI_SUCCESS)
{
throwSOCIError(res, st.session_.errhp_);
}
}
private:
std::vector<T>& v_;
};
// into and use types for short
@@ -805,6 +987,51 @@ public:
: IntegerUseType<int>(i, ind, name) {}
};
// into and use types for std::vector<int>
template <>
class IntoType<std::vector<int> > : public IntegerVectorIntoType<int>
{
public:
IntoType(std::vector<int> &v) : IntegerVectorIntoType<int>(v) {}
IntoType(std::vector<int> &v, std::vector<eIndicator> &ind)
: IntegerVectorIntoType<int>(v, ind) {}
};
template <>
class UseType<std::vector<int> > : public IntegerVectorUseType<int>
{
public:
UseType(std::vector<int> &i, std::string const &name = std::string())
: IntegerVectorUseType<int>(i, name) {}
UseType(std::vector<int> &i, const std::vector<eIndicator> &ind,
std::string const &name = std::string())
: IntegerVectorUseType<int>(i, ind, name) {}
};
// into and use types for std::vector<short>
template <>
class IntoType<std::vector<short> > : public IntegerVectorIntoType<short>
{
public:
IntoType(std::vector<short> &v) : IntegerVectorIntoType<short>(v) {}
IntoType(std::vector<short> &v, std::vector<eIndicator> &ind)
: IntegerVectorIntoType<short>(v, ind) {}
};
template <>
class UseType<std::vector<short> > : public IntegerVectorUseType<short>
{
public:
UseType(std::vector<short> &v, std::string const &name = std::string())
: IntegerVectorUseType<short>(v, name) {}
UseType(std::vector<short> &v, const std::vector<eIndicator> &ind,
std::string const &name = std::string())
: IntegerVectorUseType<short>(v, ind, name) {}
};
// into and use types for char
template <>
@@ -837,6 +1064,39 @@ public:
char& c_;
};
// into and use types for std::vector<char>
template <>
class IntoType<std::vector<char> >: public VectorIntoType
{
public:
IntoType(std::vector<char> &v) : v_(v) {}
IntoType(std::vector<char> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v){}
void define(Statement &st, int &position);
int size(){return v_.size();}
void resize(int sz){v_.resize(sz);}
private:
std::vector<char> &v_;
};
template <>
class UseType<std::vector<char> >: public VectorUseType
{
public:
UseType(std::vector<char> &v, std::string const &name = std::string())
: VectorUseType(name), v_(v) {}
UseType(std::vector<char> &v, const std::vector<eIndicator> &vind,
std::string const &name = std::string())
: VectorUseType(vind, name), v_(v){}
void bind(Statement &st, int &position);
int size(){return v_.size();}
private:
std::vector<char> &v_;
};
// into and use types for unsigned long
@@ -870,6 +1130,37 @@ private:
unsigned long &ul_;
};
template <>
class IntoType<std::vector<unsigned long> > : public VectorIntoType
{
public:
IntoType(std::vector<unsigned long> &v) : v_(v) {}
IntoType(std::vector<unsigned long> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v) {}
void define(Statement &st, int &position);
int size(){return v_.size();}
void resize(int sz){v_.resize(sz);}
private:
std::vector<unsigned long> &v_;
};
template <>
class UseType<std::vector<unsigned long> > : public VectorUseType
{
public:
UseType(std::vector<unsigned long> &v, std::string const &name = std::string())
: VectorUseType(name), v_(v) {}
UseType(std::vector<unsigned long> &v, const std::vector<eIndicator> &ind,
std::string const &name = std::string())
: VectorUseType(ind, name), v_(v) {}
void bind(Statement &st, int &position);
int size(){return v_.size();}
private:
std::vector<unsigned long> &v_;
};
// into and use types for double
template <>
@@ -885,7 +1176,6 @@ private:
double &d_;
};
template <>
class UseType<double> : public StandardUseType
{
public:
@@ -901,6 +1191,38 @@ private:
double &d_;
};
// into and use types for std::vector<double>
template <>
class IntoType<std::vector<double> > : public VectorIntoType
{
public:
IntoType(std::vector<double> &v) : v_(v) {}
IntoType(std::vector<double> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v) {}
void define(Statement &st, int &position);
virtual int size(){return v_.size();}
virtual void resize(int sz){v_.resize(sz);}
private:
std::vector<double> &v_;
};
template <>
class UseType<std::vector<double> > : public VectorUseType
{
public:
UseType(std::vector<double> &v, std::string const &name = std::string())
: VectorUseType(name), v_(v) {}
UseType(std::vector<double> &v, const std::vector<eIndicator> &ind, std::string const &name = std::string())
: VectorUseType(ind, name), v_(v) {}
void bind(Statement &st, int &position);
int size(){return v_.size();}
private:
std::vector<double> &v_;
};
// into and use types for char*
template <>
@@ -936,6 +1258,58 @@ private:
size_t bufSize_;
};
// into and use types for std::vector<std::string>
template <>
class IntoType<std::vector<std::string> > : public VectorIntoType
{
public:
IntoType(std::vector<std::string>& v)
: VectorIntoType(), v_(v), buf_(NULL), strLen_(0), sizes_(v_.size()) {}
IntoType(std::vector<std::string>& v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v), buf_(NULL), strLen_(0), sizes_(v_.size()) {}
~IntoType(){delete [] buf_;}
int size(){return v_.size();}
void resize(int sz){v_.resize(sz);}
void cleanUp();
void define(Statement &st, int &position);
void postFetch(bool gotData, bool calledFromFetch);
private:
std::vector<std::string>& v_;
char* buf_;
size_t strLen_;
sb4 maxSize_;
std::vector<ub2> sizes_;
};
template <>
class UseType<std::vector<std::string> > : public VectorUseType
{
public:
UseType(std::vector<std::string>& v, std::string const &name = std::string())
: VectorUseType(name), v_(v) {}
UseType(std::vector<std::string>& v, const std::vector<eIndicator> &ind,
std::string const &name = std::string())
: VectorUseType(ind, name), v_(v) {}
~UseType(){delete [] buf_;}
int size(){return v_.size();}
void cleanUp();
void bind(Statement &st, int &position);
private:
std::vector<ub2> sizes_;
std::vector<std::string>& v_;
char* buf_;
sb4 maxSize_;
};
// into and use types for char arrays (with size known at compile-time)
template <size_t N>
@@ -1022,7 +1396,7 @@ class UseType<std::tm> : public StandardUseType
{
public:
UseType(std::tm &t, std::string const &name = std::string())
: StandardUseType(name), tm_(t) {}
: StandardUseType(name), tm_(t) {}
UseType(std::tm &t, eIndicator &ind,
std::string const &name = std::string())
: StandardUseType(ind, name), tm_(t) {}
@@ -1036,6 +1410,55 @@ private:
ub1 buf_[7];
};
// into and use types for std::vector<std::tm>
template <>
class IntoType<std::vector<std::tm> > : public VectorIntoType
{
public:
IntoType(std::vector<std::tm>& v)
: vec_(v), buf_(NULL) {}
IntoType(std::vector<std::tm>& v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), vec_(v), buf_(NULL) {}
~IntoType(){delete []buf_;}
void cleanUp();
void define(Statement &st, int &position);
void postFetch(bool gotData, bool calledFromFetch);
virtual int size(){return vec_.size();}
virtual void resize(int sz){vec_.resize(sz);}
private:
std::vector<std::tm>& vec_;
char* buf_;
};
template <>
class UseType<std::vector<std::tm> > : public VectorUseType
{
public:
UseType(std::vector<std::tm>& v, std::string const &name = std::string())
: VectorUseType(name), v_(v), buf_(NULL) {}
UseType(std::vector<std::tm>& v, const std::vector<eIndicator> &vind,
std::string const &name = std::string())
: VectorUseType(vind, name), v_(v), buf_(NULL) {}
~UseType(){delete [] buf_;}
int size(){return v_.size();}
void bind(Statement &st, int &position);
void cleanUp();
void preUse();
void postUse();
private:
std::vector<std::tm>& v_;
ub1* buf_;
};
// into and use types for Statement (for nested statements and cursors)
template <>
@@ -1127,6 +1550,83 @@ private:
BASE_TYPE base_value_;
};
// Automatically create a std::vector based IntoType from a TypeConversion
template <typename T>
class IntoType<std::vector<T> >: public IntoType<std::vector<typename TypeConversion<T>::base_type> >
{
public:
typedef typename std::vector<typename TypeConversion<T>::base_type> BASE_TYPE;
IntoType(std::vector<T> &value)
: IntoType<BASE_TYPE>(base_value_), value_(value)
{
base_value_.resize(value_.size());
}
IntoType(std::vector<T> &value, std::vector<eIndicator> &ind)
: IntoType<BASE_TYPE>(base_value_, ind), value_(value)
{
base_value_.resize(value_.size());
}
int size(){return base_value_.size();}
void resize(int sz){value_.resize(sz); base_value_.resize(sz);}
private:
void convertFrom()
{
size_t sz = base_value_.size();
for(size_t i=0; i<sz; ++i)
{
value_[i] = TypeConversion<T>::from(base_value_[i]);
}
}
std::vector<T> &value_;
BASE_TYPE base_value_;
};
// Automatically create a std::vector based UseType from a TypeConversion
template <typename T>
class UseType<std::vector<T> > : public UseType<std::vector<typename TypeConversion<T>::base_type> >
{
public:
typedef typename std::vector<typename TypeConversion<T>::base_type> BASE_TYPE;
UseType(std::vector<T> &value) : UseType<BASE_TYPE>(base_value_), value_(value)
{
base_value_.resize(value_.size());
}
UseType(std::vector<T> &value, const std::vector<eIndicator> &ind,
const std::string &name=std::string())
: UseType<BASE_TYPE>(base_value_, ind, name), value_(value)
{
base_value_.resize(value_.size());
}
private:
void convertFrom()
{
size_t sz = base_value_.size();
for(size_t i=0; i<sz; ++i)
{
value_[i] = TypeConversion<T>::from(base_value_[i]);
}
}
void convertTo()
{
size_t sz = value_.size();
for(size_t i=0; i<sz; ++i)
{
base_value_[i] = TypeConversion<T>::to(value_[i]);
}
}
std::vector<T> &value_;
BASE_TYPE base_value_;
};
// basic BLOB operations
class BLOB

703
test.cpp
View File

@@ -53,12 +53,13 @@ void test1()
sql.once <<
"select a from (select :b as a from dual)",
into(a), use(b);
assert(a == 5);
assert(a == 5);
eIndicator aind = eOK;
eIndicator bind = eNull;
sql.once << "select a from (select :b as a from dual)",
into(a, aind), use(b, bind);
assert(aind == eNull);
int c = 10;
@@ -153,7 +154,7 @@ void test2()
// make sure the date is stored properly in Oracle
char buf[25];
strftime(buf, 25, "%m-%d-%Y %H:%M:%S", &t2);
strftime(buf, sizeof(buf), "%m-%d-%Y %H:%M:%S", &t2);
std::string t_out;
std::string format("MM-DD-YYYY HH24:MI:SS");
@@ -728,7 +729,6 @@ void test14()
char c_in = 'Z';
sql << "insert into test14(chr1) values(:C)", use(c_in);
sql.commit();
char c_out = ' ';
sql << "select chr1 from test14", into(c_out);
@@ -737,6 +737,692 @@ void test14()
std::cout << "test 14 passed" <<std::endl;
}
// test bulk insert features
void test15()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test15"; } catch(SOCIError&) {} // ignore
sql << "create table test15 (id number(5), code number(2))";
// verify exception if thrown if vectors of unequal size are passed in
{
std::vector<int> ids;
ids.push_back(1);
ids.push_back(2);
std::vector<int> codes;
codes.push_back(1);
std::string error;
try
{
sql << "insert into test15(id,code) values(:id,:code)", use(ids), use(codes);
}
catch(SOCIError& e)
{
error = e.what();
}
assert(error.find("Bind variable size mismatch") != std::string::npos);
try
{
sql << "select from test15", into(ids), into(codes);
}
catch(std::exception& e)
{
error = e.what();
}
assert(error.find("Bind variable size mismatch") != std::string::npos);
}
// verify partial insert occurs when one of the records is bad
{
std::vector<int> ids;
ids.push_back(100);
ids.push_back(1000000); // too big for column
std::string error;
try
{
sql << "insert into test15 (id) values(:id)", use(ids, "id");
}
catch(SOCIError& e)
{
error = e.what(); //TODO e could be made to tell which row(s) failed
}
sql.commit();
assert(error.find("ORA-01438") != std::string::npos);
int count(0);
sql << "select count(*) from test15", into(count);
assert(count == 1);
sql << "delete from test15";
}
// test insert
{
std::vector<int> ids;
for (int i=0; i < 3; ++i)
{
ids.push_back(i+10);
}
Statement st = (sql.prepare << "insert into test15(id) values(:id)",
use(ids));
st.execute(1);
int count;
sql << "select count(*) from test15", into(count);
assert(count == 3);
}
//verify an exception is thrown if use vector is zero length
{
std::vector<int> ids;
bool caught(false);
try
{
sql << "select id from test15", into(ids);
}
catch (std::out_of_range& e)
{
caught = true;
}
assert(caught);
}
// verify an exception is thrown if into vector is zero length
{
std::vector<int> ids;
bool caught(false);
try
{
sql << "insert into test15(id) values(:id)", use(ids);
}
catch(std::out_of_range& e)
{
caught = true;
}
assert(caught);
}
// test eNoData indicator
{
std::vector<eIndicator> inds(3);
std::vector<int> ids_out(3);
Statement st = (sql.prepare << "select id from test15 where 1=0",
into(ids_out, inds));
assert(!st.execute(1));
assert(ids_out.size() == 0);
assert(inds.size() == 3 && inds[0] == eNoData && inds[1] == eNoData && inds[2] == eNoData);
}
// test NULL indicators
{
std::vector<int> ids(3);
sql << "select id from test15", into(ids);
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
inds_in.push_back(eOK);
std::vector<int> new_codes;
new_codes.push_back(10);
new_codes.push_back(11);
new_codes.push_back(10);
sql << "update test15 set code = :code where id = :id",
use(new_codes, inds_in), use(ids);
std::vector<eIndicator> inds_out(3);
std::vector<int> codes(3);
sql << "select code from test15", into(codes, inds_out);
assert(codes.size() == 3 && inds_out.size() == 3);
assert(codes[0] == 10 && codes[2] == 10);
assert(inds_out[0] == eOK && inds_out[1] == eNull && inds_out[2] == eOK);
}
// test basic select
{
const size_t sz = 3;
std::vector<eIndicator> inds(sz);
std::vector<int> ids_out(sz);
Statement st = (sql.prepare << "select id from test15", into(ids_out, inds));
assert(st.execute(1));
assert(ids_out.size() == sz);
assert(ids_out[0] == 10);
assert(ids_out[2] == 12);
assert(inds.size() == 3 && inds[0] == eOK && inds[1] == eOK && inds[2] == eOK);
}
// verify execute(0)
{
std::vector<int> ids_out(2);
Statement st = (sql.prepare << "select id from test15", into(ids_out));
st.execute(0);
assert(ids_out.size() == 2);
assert(st.fetch());
assert(ids_out.size() == 2 && ids_out[0] == 10 && ids_out[1] == 11);
assert(st.fetch());
assert(ids_out.size() == 1 && ids_out[0] == 12);
assert(!st.fetch());
}
// verify resizing happens if vector is larger then number of rows returned
{
std::vector<int> ids_out(4); // one too many
Statement st2 = (sql.prepare << "select id from test15", into(ids_out));
assert(st2.execute(1));
assert(ids_out.size() == 3);
assert(ids_out[0] == 10);
assert(ids_out[2] == 12);
}
// verify resizing happens properly during fetch()
{
std::vector<int> more;
more.push_back(13);
more.push_back(14);
sql << "insert into test15(id) values(:id)", use(more);
std::vector<int> ids(2);
Statement st3 = (sql.prepare << "select id from test15", into(ids));
assert(st3.execute(1));
assert(ids[0] == 10);
assert(ids[1] == 11);
assert(st3.fetch());
assert(ids[0] == 12);
assert(ids[1] == 13);
assert(st3.fetch());
assert(ids.size() == 1);
assert(ids[0] == 14);
assert(!st3.fetch());
}
std::cout << "test 15 passed"<< std::endl;
}
//test bulk operations for std::string
void test16()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test16"; } catch(SOCIError&) {} // ignore
sql << "create table test16(name varchar2(10), code varchar2(3))";
// verify an exception is thrown and nothing succeeds if string size exceeds column size
{
std::vector<std::string> codes;
codes.push_back("abc");
codes.push_back("defg"); // too big for column
std::string error;
try
{
sql << "insert into test16(code) values(:code)", use(codes);
}
catch(SOCIError& e)
{
error = e.what();
}
assert(error.find("ORA-12899") != std::string::npos);
int count(0);
sql << "select count(*) from test16", into(count);
assert(count == 1);
sql << "delete from test16";
}
std::vector<std::string> names;
std::vector<std::string> codes;
for (int i=0; i < 3; ++i)
{
std::ostringstream nStr;
nStr << "name" << i;
if (i==1)
nStr << "extra";
names.push_back(nStr.str());
std::ostringstream cStr;
cStr << i*100;
codes.push_back(cStr.str());
}
Statement st = (sql.prepare << "insert into test16(name,code) values(:name, :code)",
use(names), use(codes));
st.execute(1);
// test select, multiple string columns
{
std::vector<std::string> names_out(2);
std::vector<std::string> codes_out(2);
Statement st = (sql.prepare << "select name, code from test16", into(names_out), into(codes_out));
st.execute(1);
assert(names_out.size() == 2);
assert(names_out[0] == "name0");
assert(names_out[1] == "name1extra");
assert(codes_out[0] == "0");
assert(codes_out[1] == "100");
st.fetch();
assert(names_out.size()==1 && names_out[0] == "name2");
assert(codes_out.size()==1 && codes_out[0] == "200");
}
// test simple syntax
{
std::vector<std::string> simple;
simple.push_back("simple1");
simple.push_back("simple2");
sql << "insert into test16(name) values(:names)", use(simple, "names");
std::vector<std::string> simple_out(2);
sql << "select name from test16 where name like 'simple%'", into(simple_out);
assert(simple_out.size()==2 && simple_out[0] == "simple1" && simple_out[1] == "simple2");
}
// test indicators
{
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
inds_in.push_back(eOK);
std::vector<std::string> names_in;
names_in.push_back("indtest1");
names_in.push_back("indtest2");
names_in.push_back("indtest3");
std::vector<std::string> codes;
codes.push_back("99");
codes.push_back("99");
codes.push_back("99");
sql<<"insert into test16(name,code) values (:name, :code)", use(names_in, inds_in), use(codes);
sql.commit();
std::vector<eIndicator> inds_out(3);
inds_out[0] = eNoData;
inds_out[1] = eNoData;
inds_out[2] = eNoData;
std::vector<std::string> names_out(3);
sql<<"select name from test16 where code = :code", use(codes[0]), into(names_out, inds_out);
assert(names_out[0] == "indtest1" && names_out[2] == "indtest3");
assert(inds_out[0] == eOK && inds_out[1] == eNull && inds_out[2] == eOK);
assert(names_out[0] == "indtest1" && names_out[2] == "indtest3");
}
//verify bad sql statement causes exception
{
std::vector<std::string> names;
std::string msg;
try
{
sql << "select bogus from test16\n", into(names);
}
catch(SOCIError& e)
{
msg = e.what();
}
assert(msg.find("invalid identifier") != std::string::npos);
}
std::cout << "test 16 passed"<< std::endl;
}
// test bulk operations for unsigned long and double
void test17()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test17"; } catch(SOCIError&) {} // ignore
sql << "create table test17 (nums number(10), amts number(8,2))";
std::vector<unsigned long> nums;
nums.push_back(100 * 1000);
nums.push_back(200 * 1000);
nums.push_back(300 * 1000);
std::vector<double> amts;
amts.push_back(1.11);
amts.push_back(1.12);
amts.push_back(1.13);
sql << "insert into test17(nums, amts) values(:nums, :amts)", use(nums, "nums"), use(amts, "amts");
int count(0);
sql << "select count(*) from test17", into(count);
assert(count == 3);
std::vector<unsigned long> nums_out(2);
std::vector<double> amts_out(2);
sql << "select nums, amts from test17", into(nums_out), into(amts_out);
assert(nums_out.size()==2 && nums_out[0] == 100*1000 && nums_out[1] == 200*1000);
assert(amts_out.size()==2 && amts_out[0] == 1.11 && amts_out[1] == 1.12);
// test indicators
{
std::vector<eIndicator> indsd_in;
indsd_in.push_back(eOK);
indsd_in.push_back(eNull);
std::vector<double> vd;
vd.push_back(99.99);
vd.push_back(100.1);
std::vector<eIndicator> indsl_in;
indsl_in.push_back(eNull);
indsl_in.push_back(eOK);
std::vector<unsigned long> vl;
vl.push_back(9990);
vl.push_back(1001);
sql << "insert into test17(nums, amts) values(:num, :amt)", use(vl, indsl_in), use(vd, indsd_in);
std::vector<double> d_out(2);
std::vector<unsigned long> l_out(2);
std::vector<eIndicator> indd_out(2);
std::vector<eIndicator> indl_out(2);
sql << "select nums, amts from test17 where nums is null or amts is null",
into(l_out, indl_out), into(d_out, indd_out);
assert(l_out.size() == 2);
assert(l_out[1] == vl[1] && indl_out[1] == eOK);
assert(indl_out[0] == eNull);
assert(indd_out[1] == eNull);
assert(d_out[0] < 100 && d_out[0] > 99.98);
assert(indd_out[0] == eOK);
}
std::cout << "test 17 passed"<<std::endl;
}
// test bulk operations for std::tm
void test18()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test18"; } catch(SOCIError&) {} // ignore
sql << "create table test18 (d1 date, d2 date)";
std::vector<std::tm> times;
for (int i=0; i<4; ++i)
{
std::time_t now = std::time(0);
std::tm when = *gmtime(&now);
when.tm_year = 104;
when.tm_mon = 11;
when.tm_mday = i+10;
mktime(&when);
times.push_back(when);
}
sql << "insert into test18(d1) values(:times)", use(times);
int count(0);
sql << "select count(*) from test18", into(count);
assert(count == 4);
std::vector<std::tm> times_out(3);
sql << "select d1 from test18", into(times_out);
assert(times_out.size() == 3);
for (size_t i=0; i<times_out.size(); ++i)
{
assert(mktime(&times_out[i]) == mktime(&times[i]));
std::tm t = times_out[i];
assert(t.tm_year == 104);
assert(t.tm_mon == 11);
assert(t.tm_mday == static_cast<int>(i+10));
}
std::vector<std::tm> times_in;
// test indicators
{
for (int i=0; i<2; ++i)
{
std::time_t now = std::time(0);
std::tm when = *gmtime(&now);
when.tm_year = 125;
when.tm_mon = 2;
when.tm_mday = i + 1;
mktime(&when);
times_in.push_back(when);
}
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
sql << "insert into test18 (d1) values (:d1)", use(times_in, inds_in, "d1");
sql.commit();
std::vector<std::tm> times_out(2);
std::vector<eIndicator> inds_out(2);
sql << "select d1 from test18 where d1 > sysdate or d1 is null", into(times_out, inds_out);
assert(times_out.size() == 2);
assert(memcmp(&times_out[0], &times_in[0], sizeof(std::tm)) == 0);
assert(inds_out[0] == eOK);
assert(inds_out[1] == eNull);
}
// test resizing
{
std::vector<std::tm> out(2);
Statement st = (sql.prepare << "select d1 from test18 where d1 is not null", into(out) );
assert(st.execute(1));
assert(out.size() == 2);
assert(mktime(&out[0]) == mktime(&times[0]));
assert(mktime(&out[1]) == mktime(&times[1]));
assert(st.fetch());
assert(out.size() == 2);
assert(mktime(&out[0]) == mktime(&times[2]));
assert(mktime(&out[1]) == mktime(&times[3]));
assert(st.fetch());
assert(out.size() == 1);
assert(mktime(&out[0]) == mktime(&times_in[0]));
assert(!st.fetch());
}
std::cout << "test 18 passed"<< std::endl;
}
// test bulk operations for std::time_t
void test19()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test19"; } catch(SOCIError&) {} // ignore
sql << "create table test19 (d1 date)";
time_t t1 = std::time(0) + 60*60*24;
time_t t2 = t1 + 60*60*24*2;
time_t t3 = t1 + 60*60*24*4;
std::vector<time_t> times_in1;
{
times_in1.push_back(t1);
times_in1.push_back(t2);
times_in1.push_back(t3);
sql << "insert into test19(d1) values(:d1)", use(times_in1);
int count(0);
sql << "select count(*) from test19", into(count);
assert(count == 3);
}
// test resizing from exec
{
std::vector<time_t> times_out(3); // one too many
sql << "select d1 from test19", into(times_out);
assert(times_out.size() == 3);
assert(times_out[1] == t2);
}
//test indicators
std::vector<std::time_t> times_in2;
{
time_t today = time(0);
time_t yesterday = today - 60*60*24;
time_t dayBefore = yesterday - 60*60*24;
times_in2.push_back(yesterday);
times_in2.push_back(dayBefore);
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
sql << "insert into test19 (d1) values(:d1)", use(times_in2, inds_in, "d1");
assert(times_in2.size() == 2);
assert(times_in2[0] == yesterday);
assert(times_in2[1] == dayBefore);
std::vector<std::time_t> times_out(2);
std::vector<eIndicator> inds_out(2);
sql << "select d1 from test19 where d1 < sysdate or d1 is null",
into(times_out, inds_out);
assert(times_out.size() == 2);
assert(times_out[0] == times_in2[0]);
assert(inds_out[0] == eOK);
assert(inds_out[1] == eNull);
}
//test resizing
{
std::vector<std::time_t> times_in3;
time_t now = time(0);
times_in3.push_back(now);
sql << "insert into test19 (d1) values(:d1)", use(times_in3);
sql.commit();
std::vector<std::time_t> times_out(2);
Statement st = (sql.prepare <<"select d1 from test19 where d1 is not null", into(times_out));
assert(st.execute(1));
assert(times_out.size() == 2 && times_out[0] == times_in1[0] && times_out[1] == times_in1[1]);
assert(st.fetch());
assert(times_out.size() == 2);
assert(times_out[0] == times_in1[2]);
assert(times_out[1] == times_in2[0]);
assert(st.fetch());
assert(times_out.size() == 1 && times_out[0] == times_in3[0]);
assert(!st.fetch());
}
std::cout << "test 19 passed" << std::endl;
}
// test bulk operations for char
void test20()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test20"; } catch(SOCIError&) {} // ignore
sql << "create table test20(code char(1))";
std::vector<char> in;
in.push_back('A');
in.push_back('B');
sql << "insert into test20 (code) values(:code)", use(in);
int count(0);
sql << "select count(*) from test20", into(count);
assert(count == 2);
std::vector<char> out(2);
sql << "select code from test20", into(out);
assert(out.size() == 2 && out[0] == 'A' && out[1] == 'B');
// test indicators
{
std::vector<char> in2;
in2.push_back('C');
in2.push_back('D');
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
sql << "insert into test20 (code) values (:code)", use(in2, inds_in, "code");
std::vector<char> out2(4);
std::vector<eIndicator> inds_out(4);
sql << "select code from test20", into(out2, inds_out);
assert(out2[2] == 'C' && inds_out[2] == eOK);
assert(inds_out[3] == eNull);
}
std::cout << "test 20 passed" << std::endl;
}
// test bulk operations for short
void test21()
{
Session sql(serviceName, userName, password);
try { sql << "drop table test21"; } catch(SOCIError&) {} // ignore
sql << "create table test21(id number(2))";
std::vector<short> in;
in.push_back(1);
in.push_back(2);
sql << "insert into test21 (id) values(:id)", use(in);
int count(0);
sql << "select count(*) from test21", into(count);
assert(count == 2);
std::vector<short> out(2);
sql << "select id from test21", into(out);
assert(out.size() == 2 && out[0] == 1 && out[1] == 2);
// test indicators
{
std::vector<short> in2;
in2.push_back(3);
in2.push_back(4);
assert(in2[0] == 3);
assert(in2[1] == 4);
std::vector<eIndicator> inds_in;
inds_in.push_back(eOK);
inds_in.push_back(eNull);
sql << "insert into test21 (id) values (:id)", use(in2, inds_in, "id");
std::vector<short> out2(4);
std::vector<eIndicator> inds_out(4);
sql << "select id from test21", into(out2, inds_out);
assert(in2[0] == 3);
assert(out2[2] == 3);
assert(inds_out[2] == eOK);
assert(inds_out[3] == eNull);
}
std::cout << "test 21 passed" << std::endl;
}
int main(int argc, char** argv)
{
if (argc == 4)
@@ -759,7 +1445,7 @@ int main(int argc, char** argv)
test4();
test5();
test6();
test7();
test7();
test8();
test9();
test10();
@@ -767,7 +1453,14 @@ int main(int argc, char** argv)
test12();
test13();
test14();
test15();
test16();
test17();
test18();
test19();
test20();
test21();
std::cout << "\nOK, all tests passed.\n\n";
}
catch (std::exception const & e)