diff --git a/soci.cpp b/soci.cpp index b03e8fb1..0c6c1790 100644 --- a/soci.cpp +++ b/soci.cpp @@ -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(errbuf); - errNum = static_cast(errcode); - break; + msg = reinterpret_cast(errbuf); + errNum = static_cast(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(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["<size() << ", intos_[0] has size "<< intosSize <size(); + } + else if (usesSize != uses_[i]->size()) + { + std::ostringstream msg; + msg << "Bind variable size mismatch (use["<size() << ", use[0] has size "<< usesSize <(stmtp_), static_cast(OCI_HTYPE_STMT), + static_cast(&rows), static_cast(&sizep), + static_cast(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::define(Statement &st, int &position) @@ -860,6 +1044,50 @@ void UseType::bind(Statement &st, int &position) } } +// into and use types for std::vector +void IntoType >::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 >::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(const_cast(name_.c_str())), + static_cast(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::bind(Statement &st, int &position) throwSOCIError(res, st.session_.errhp_); } } +// into and use types for std::vector + +void IntoType >::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 >::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(const_cast(name_.c_str())), + static_cast(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::bind(Statement &st, int &position) } } +// into and use types for std::vector + +void IntoType >::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 >::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(const_cast(name_.c_str())), + static_cast(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::define(Statement &st, int &position) @@ -1006,7 +1321,7 @@ void IntoType::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::cleanUp() { delete [] buf_; buf_ = NULL; + + StandardIntoType::cleanUp(); } void UseType::bind(Statement &st, int &position) @@ -1082,6 +1399,136 @@ void UseType::cleanUp() buf_ = NULL; } +// into and use types for std::vector +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(st.stmtp_), + static_cast(OCI_HTYPE_STMT), + reinterpret_cast(st.session_.errhp_), + reinterpret_cast(&colhd), + static_cast(position)); + if (res != OCI_SUCCESS) throwSOCIError(res, st.session_.errhp_); + + // Get The Data Size + res = OCIAttrGet( reinterpret_cast(colhd), + static_cast(OCI_DTYPE_PARAM), + reinterpret_cast(&colSize), + 0, + static_cast(OCI_ATTR_DATA_SIZE), + reinterpret_cast(st.session_.errhp_)); + if (res != OCI_SUCCESS) throwSOCIError(res, st.session_.errhp_); + + return colSize; +} + + +void IntoType >::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 >::postFetch(bool gotData, bool calledFromFetch) +{ + char* pos = buf_; + if (gotData) + { + for (size_t i=0; i >::cleanUp() +{ + delete [] buf_; + buf_ = NULL; + VectorIntoType::cleanUp(); +} + +void UseType >::bind(Statement &st, int &position) +{ + st_ = &st; + sword res; + + size_t maxSize = 0; + for (size_t i=0; i maxSize ? sz : maxSize; + } + + + buf_ = new char[maxSize * v_.size()]; + char* pos = buf_; + for (size_t i=0; i(const_cast(name_.c_str())), + static_cast(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 >::cleanUp() +{ + delete []buf_; + buf_ = NULL; + + VectorUseType::cleanUp(); +} + + // into and use types for date and time (struct tm) void IntoType::define(Statement &st, int &position) @@ -1114,6 +1561,7 @@ void IntoType::postFetch(bool gotData, bool calledFromFetch) std::mktime(&tm_); } StandardIntoType::postFetch(gotData, calledFromFetch); + } void UseType::bind(Statement &st, int &position) @@ -1173,6 +1621,133 @@ void UseType::postUse() StandardUseType::postUse(); } +// into and use types for std::vector +void IntoType >::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 >::cleanUp() +{ + delete []buf_; + buf_ = NULL; + VectorIntoType::cleanUp(); +} + +void IntoType >::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 >::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(const_cast(name_.c_str())), + static_cast(name_.size()), buf_, dlen, SQLT_DAT, + indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT); + } + + if (res != OCI_SUCCESS) + { + throwSOCIError(res, st.session_.errhp_); + } + +} + +void UseType >::cleanUp() +{ + delete []buf_; + buf_ = NULL; + VectorUseType::cleanUp(); +} + +void UseType >::preUse() +{ + VectorUseType::preUse(); + + ub1* pos = buf_; + for (size_t i=0; i < v_.size(); ++i) + { + *pos++ = static_cast(100 + (1900 + v_[i].tm_year) / 100); + *pos++ = static_cast(100 + v_[i].tm_year % 100); + *pos++ = static_cast(v_[i].tm_mon + 1); + *pos++ = static_cast(v_[i].tm_mday); + *pos++ = static_cast(v_[i].tm_hour + 1); + *pos++ = static_cast(v_[i].tm_min + 1); + *pos++ = static_cast(v_[i].tm_sec + 1); + } +} + +void UseType >::postUse() +{ + ub1* pos = buf_; + for (size_t i=0; i::define(Statement &st, int &position) diff --git a/soci.h b/soci.h index 1bf957ae..6dfe3706 100644 --- a/soci.h +++ b/soci.h @@ -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, p1)); } +template +details::IntoTypePtr into(T &t, std::vector &indicator) +{ + return details::IntoTypePtr(new IntoType(t, indicator)); +} + template details::IntoTypePtr into(T &t, eIndicator &indicator) { @@ -183,6 +194,12 @@ details::UseTypePtr use(T &t, T1 p1) return details::UseTypePtr(new UseType(t, p1)); } +template +details::UseTypePtr use(T &t, const std::vector &indicator) +{ + return details::UseTypePtr(new UseType(t, indicator)); +} + template 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, ind, p1)); } +template +details::UseTypePtr use(T &t, const std::vector &ind, T1 p1) +{ + return details::UseTypePtr(new UseType(t, ind, p1)); +} + template 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 class TypeConversion @@ -405,6 +428,37 @@ private: std::map 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 + 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 intos_; std::vector uses_; - + private: + Row* row_; - + int fetchSize_; + template void intoRow() { @@ -446,6 +502,14 @@ private: } template 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 - 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& 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 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 &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 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& 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 where T is an integer + +template +class IntegerVectorIntoType : public VectorIntoType +{ +public: + IntegerVectorIntoType(std::vector &v) : v_(v) {} + IntegerVectorIntoType(std::vector &v, std::vector &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(sizeof(T)), SQLT_INT, + indOCIHolder_, 0, 0, OCI_DEFAULT); + if (res != OCI_SUCCESS) + { + throwSOCIError(res, st.session_.errhp_); + } + } + +private: + std::vector& v_; +}; + +template +class IntegerVectorUseType : public VectorUseType +{ +public: + IntegerVectorUseType(std::vector &v, std::string const &name = std::string()) + : VectorUseType(name), v_(v) {} + IntegerVectorUseType(std::vector &v, const std::vector &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(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(const_cast(name_.c_str())), + static_cast(name_.size()), + &v_.at(0), static_cast(sizeof(T)), SQLT_INT, + indOCIHolder_, 0, 0, 0, 0, OCI_DEFAULT); + } + + if (res != OCI_SUCCESS) + { + throwSOCIError(res, st.session_.errhp_); + } + } + +private: + std::vector& v_; +}; + // into and use types for short @@ -805,6 +987,51 @@ public: : IntegerUseType(i, ind, name) {} }; +// into and use types for std::vector + +template <> +class IntoType > : public IntegerVectorIntoType +{ +public: + IntoType(std::vector &v) : IntegerVectorIntoType(v) {} + IntoType(std::vector &v, std::vector &ind) + : IntegerVectorIntoType(v, ind) {} +}; + +template <> +class UseType > : public IntegerVectorUseType +{ +public: + UseType(std::vector &i, std::string const &name = std::string()) + : IntegerVectorUseType(i, name) {} + UseType(std::vector &i, const std::vector &ind, + std::string const &name = std::string()) + : IntegerVectorUseType(i, ind, name) {} +}; + +// into and use types for std::vector + +template <> +class IntoType > : public IntegerVectorIntoType +{ +public: + IntoType(std::vector &v) : IntegerVectorIntoType(v) {} + IntoType(std::vector &v, std::vector &ind) + : IntegerVectorIntoType(v, ind) {} +}; + +template <> +class UseType > : public IntegerVectorUseType +{ +public: + UseType(std::vector &v, std::string const &name = std::string()) + : IntegerVectorUseType(v, name) {} + UseType(std::vector &v, const std::vector &ind, + std::string const &name = std::string()) + : IntegerVectorUseType(v, ind, name) {} +}; + + // into and use types for char template <> @@ -837,6 +1064,39 @@ public: char& c_; }; +// into and use types for std::vector + +template <> +class IntoType >: public VectorIntoType +{ +public: + IntoType(std::vector &v) : v_(v) {} + IntoType(std::vector &v, std::vector &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 &v_; +}; + +template <> +class UseType >: public VectorUseType +{ +public: + UseType(std::vector &v, std::string const &name = std::string()) + : VectorUseType(name), v_(v) {} + UseType(std::vector &v, const std::vector &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 &v_; +}; // into and use types for unsigned long @@ -870,6 +1130,37 @@ private: unsigned long &ul_; }; +template <> +class IntoType > : public VectorIntoType +{ +public: + IntoType(std::vector &v) : v_(v) {} + IntoType(std::vector &v, std::vector &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 &v_; +}; + +template <> +class UseType > : public VectorUseType +{ +public: + UseType(std::vector &v, std::string const &name = std::string()) + : VectorUseType(name), v_(v) {} + UseType(std::vector &v, const std::vector &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 &v_; +}; + // into and use types for double template <> @@ -885,7 +1176,6 @@ private: double &d_; }; -template <> class UseType : public StandardUseType { public: @@ -901,6 +1191,38 @@ private: double &d_; }; +// into and use types for std::vector + +template <> +class IntoType > : public VectorIntoType +{ +public: + IntoType(std::vector &v) : v_(v) {} + IntoType(std::vector &v, std::vector &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 &v_; +}; + +template <> +class UseType > : public VectorUseType +{ +public: + UseType(std::vector &v, std::string const &name = std::string()) + : VectorUseType(name), v_(v) {} + UseType(std::vector &v, const std::vector &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 &v_; +}; + // into and use types for char* template <> @@ -936,6 +1258,58 @@ private: size_t bufSize_; }; +// into and use types for std::vector + +template <> +class IntoType > : public VectorIntoType +{ +public: + IntoType(std::vector& v) + : VectorIntoType(), v_(v), buf_(NULL), strLen_(0), sizes_(v_.size()) {} + IntoType(std::vector& v, std::vector &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& v_; + char* buf_; + size_t strLen_; + sb4 maxSize_; + std::vector sizes_; +}; + +template <> +class UseType > : public VectorUseType +{ +public: + UseType(std::vector& v, std::string const &name = std::string()) + : VectorUseType(name), v_(v) {} + UseType(std::vector& v, const std::vector &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 sizes_; + std::vector& v_; + char* buf_; + sb4 maxSize_; +}; + + // into and use types for char arrays (with size known at compile-time) template @@ -1022,7 +1396,7 @@ class UseType : 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 + +template <> +class IntoType > : public VectorIntoType +{ +public: + IntoType(std::vector& v) + : vec_(v), buf_(NULL) {} + IntoType(std::vector& v, std::vector &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& vec_; + char* buf_; +}; + + +template <> +class UseType > : public VectorUseType +{ +public: + UseType(std::vector& v, std::string const &name = std::string()) + : VectorUseType(name), v_(v), buf_(NULL) {} + UseType(std::vector& v, const std::vector &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& 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 +class IntoType >: public IntoType::base_type> > +{ +public: + typedef typename std::vector::base_type> BASE_TYPE; + + IntoType(std::vector &value) + : IntoType(base_value_), value_(value) + { + base_value_.resize(value_.size()); + } + + IntoType(std::vector &value, std::vector &ind) + : IntoType(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::from(base_value_[i]); + } + } + + std::vector &value_; + BASE_TYPE base_value_; +}; + +// Automatically create a std::vector based UseType from a TypeConversion +template +class UseType > : public UseType::base_type> > +{ +public: + typedef typename std::vector::base_type> BASE_TYPE; + + UseType(std::vector &value) : UseType(base_value_), value_(value) + { + base_value_.resize(value_.size()); + } + UseType(std::vector &value, const std::vector &ind, + const std::string &name=std::string()) + : UseType(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::from(base_value_[i]); + } + } + void convertTo() + { + size_t sz = value_.size(); + for(size_t i=0; i::to(value_[i]); + } + } + + std::vector &value_; + BASE_TYPE base_value_; +}; + // basic BLOB operations class BLOB diff --git a/test.cpp b/test.cpp index b2d2c5d4..f3c5efe0 100644 --- a/test.cpp +++ b/test.cpp @@ -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" < ids; + ids.push_back(1); + ids.push_back(2); + std::vector 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 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 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 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 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 inds(3); + std::vector 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 ids(3); + sql << "select id from test15", into(ids); + + std::vector inds_in; + inds_in.push_back(eOK); + inds_in.push_back(eNull); + inds_in.push_back(eOK); + + std::vector 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 inds_out(3); + std::vector 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 inds(sz); + std::vector 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 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 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 more; + more.push_back(13); + more.push_back(14); + sql << "insert into test15(id) values(:id)", use(more); + + std::vector 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 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 names; + std::vector 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 names_out(2); + std::vector 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 simple; + simple.push_back("simple1"); + simple.push_back("simple2"); + sql << "insert into test16(name) values(:names)", use(simple, "names"); + + std::vector 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 inds_in; + inds_in.push_back(eOK); + inds_in.push_back(eNull); + inds_in.push_back(eOK); + + std::vector names_in; + names_in.push_back("indtest1"); + names_in.push_back("indtest2"); + names_in.push_back("indtest3"); + + std::vector 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 inds_out(3); + inds_out[0] = eNoData; + inds_out[1] = eNoData; + inds_out[2] = eNoData; + std::vector 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 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 nums; + nums.push_back(100 * 1000); + nums.push_back(200 * 1000); + nums.push_back(300 * 1000); + + std::vector 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 nums_out(2); + std::vector 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 indsd_in; + indsd_in.push_back(eOK); + indsd_in.push_back(eNull); + + std::vector vd; + vd.push_back(99.99); + vd.push_back(100.1); + + std::vector indsl_in; + indsl_in.push_back(eNull); + indsl_in.push_back(eOK); + + std::vector 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 d_out(2); + std::vector l_out(2); + std::vector indd_out(2); + std::vector 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"< 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 times_out(3); + sql << "select d1 from test18", into(times_out); + + assert(times_out.size() == 3); + for (size_t i=0; i(i+10)); + } + + std::vector 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 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 times_out(2); + std::vector 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(×_out[0], ×_in[0], sizeof(std::tm)) == 0); + assert(inds_out[0] == eOK); + assert(inds_out[1] == eNull); + } + + // test resizing + { + std::vector 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(×[0])); + assert(mktime(&out[1]) == mktime(×[1])); + + assert(st.fetch()); + assert(out.size() == 2); + assert(mktime(&out[0]) == mktime(×[2])); + assert(mktime(&out[1]) == mktime(×[3])); + + assert(st.fetch()); + assert(out.size() == 1); + assert(mktime(&out[0]) == mktime(×_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 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 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 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 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 times_out(2); + std::vector 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 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 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 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 out(2); + sql << "select code from test20", into(out); + assert(out.size() == 2 && out[0] == 'A' && out[1] == 'B'); + + // test indicators + { + std::vector in2; + in2.push_back('C'); + in2.push_back('D'); + + std::vector 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 out2(4); + std::vector 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 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 out(2); + sql << "select id from test21", into(out); + assert(out.size() == 2 && out[0] == 1 && out[1] == 2); + + // test indicators + { + std::vector in2; + in2.push_back(3); + in2.push_back(4); + + assert(in2[0] == 3); + assert(in2[1] == 4); + + std::vector 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 out2(4); + std::vector 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)