Throw when NULL is selected but no indicator was defined

This commit is contained in:
stephenhutton
2005-09-20 08:48:53 +00:00
parent 69d9b55c3b
commit 6f9e5f1bea
3 changed files with 95 additions and 41 deletions

View File

@@ -886,6 +886,12 @@ void StandardIntoType::postFetch(bool gotData, bool calledFromFetch)
}
else
{
if (indOCIHolder_ == -1)
{
// fetched null and no indicator - programming error!
throw SOCIError("Null value fetched and no indicator defined.");
}
if (gotData == false)
{
// no data fetched and no indicator - programming error!
@@ -959,6 +965,15 @@ void VectorIntoType::postFetch(bool gotData, bool calledFromFetch)
}
else
{
for(size_t i=0; i<indOCIHolderVec_.size(); ++i)
{
if (indOCIHolderVec_[i] == -1)
{
// fetched null and no indicator - programming error!
throw SOCIError("Null value fetched and no indicator defined.");
}
}
if (gotData == false)
{
// no data fetched and no indicator - programming error!
@@ -1048,6 +1063,8 @@ 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)
{
VectorIntoType::define(st, position);
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
@@ -1137,6 +1154,8 @@ void UseType<unsigned long>::bind(Statement &st, int &position)
void IntoType<std::vector<unsigned long> >::define(Statement &st, int &position)
{
VectorIntoType::define(st, position);
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
@@ -1224,6 +1243,8 @@ void UseType<double>::bind(Statement &st, int &position)
void IntoType<std::vector<double> >::define(Statement &st, int &position)
{
VectorIntoType::define(st, position);
st_ = &st;
sword res = OCIDefineByPos(st.stmtp_, &defnp_, st.session_.errhp_,
@@ -1438,6 +1459,8 @@ size_t columnSize(Statement &st, int position)
void IntoType<std::vector<std::string> >::define(Statement &st, int &position)
{
VectorIntoType::define(st, position);
st_ = &st;
strLen_ = columnSize(st, position) + 1;
@@ -1625,6 +1648,8 @@ void UseType<std::tm>::postUse()
// into and use types for std::vector<std::tm>
void IntoType<std::vector<std::tm> >::define(Statement &st, int &position)
{
VectorIntoType::define(st, position);
st_ = &st;
const sb4 dlen = 7;

81
soci.h
View File

@@ -692,13 +692,18 @@ private:
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();
}
VectorIntoType(std::size_t sz)
: st_(NULL), defnp_(NULL), ind_(NULL), indOCIHolder_(NULL), sz_(sz) {}
VectorIntoType(std::vector<eIndicator> &ind)
: st_(NULL), defnp_(NULL), ind_(&ind.at(0)), sz_(ind.size()) {}
virtual void define(Statement &st, int &position)
{
//TODO can we add a preDefine() function to the interface, and do this there?
indOCIHolderVec_.resize(sz_);
indOCIHolder_ = &indOCIHolderVec_.front();
}
virtual void preFetch() {}
virtual void postFetch(bool gotData, bool calledFromFetch);
@@ -713,6 +718,7 @@ protected:
private:
virtual void convertFrom() {}
std::vector<sb2> indOCIHolderVec_;
const std::size_t sz_;
};
class VectorUseType : public UseTypeBase
@@ -754,12 +760,9 @@ class StandardIntoType : public IntoTypeBase
{
public:
StandardIntoType()
: st_(NULL), defnp_(NULL), ind_(NULL) {}
: st_(NULL), defnp_(NULL), ind_(NULL), indOCIHolder_(0) {}
StandardIntoType(eIndicator& ind)
: st_(NULL), defnp_(NULL), ind_(&ind)
{}
StandardIntoType(std::vector<eIndicator>& ind)
: st_(NULL), defnp_(NULL), ind_(&ind.at(0)){}
: st_(NULL), defnp_(NULL), ind_(&ind), indOCIHolder_(0) {}
virtual void preFetch() {}
virtual void postFetch(bool gotData, bool calledFromFetch);
@@ -877,15 +880,18 @@ template <typename T>
class IntegerVectorIntoType : public VectorIntoType
{
public:
IntegerVectorIntoType(std::vector<T> &v) : v_(v) {}
IntegerVectorIntoType(std::vector<T> &v, std::size_t sz)
: VectorIntoType(sz), 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)
{
virtual void define(Statement &st, int &position)
{
VectorIntoType::define(st, position);
st_ = &st;
sword res;
@@ -897,7 +903,7 @@ public:
{
throwSOCIError(res, st.session_.errhp_);
}
}
}
private:
std::vector<T>& v_;
@@ -997,7 +1003,8 @@ template <>
class IntoType<std::vector<int> > : public IntegerVectorIntoType<int>
{
public:
IntoType(std::vector<int> &v) : IntegerVectorIntoType<int>(v) {}
IntoType(std::vector<int> &v)
: IntegerVectorIntoType<int>(v, v.size()) {}
IntoType(std::vector<int> &v, std::vector<eIndicator> &ind)
: IntegerVectorIntoType<int>(v, ind) {}
};
@@ -1019,7 +1026,8 @@ template <>
class IntoType<std::vector<short> > : public IntegerVectorIntoType<short>
{
public:
IntoType(std::vector<short> &v) : IntegerVectorIntoType<short>(v) {}
IntoType(std::vector<short> &v)
: IntegerVectorIntoType<short>(v, v.size()) {}
IntoType(std::vector<short> &v, std::vector<eIndicator> &ind)
: IntegerVectorIntoType<short>(v, ind) {}
};
@@ -1074,7 +1082,8 @@ template <>
class IntoType<std::vector<char> >: public VectorIntoType
{
public:
IntoType(std::vector<char> &v) : v_(v) {}
IntoType(std::vector<char> &v)
: VectorIntoType(v.size()), v_(v) {}
IntoType(std::vector<char> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v){}
@@ -1138,7 +1147,8 @@ template <>
class IntoType<std::vector<unsigned long> > : public VectorIntoType
{
public:
IntoType(std::vector<unsigned long> &v) : v_(v) {}
IntoType(std::vector<unsigned long> &v)
: VectorIntoType(v.size()), v_(v) {}
IntoType(std::vector<unsigned long> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v) {}
@@ -1201,7 +1211,8 @@ template <>
class IntoType<std::vector<double> > : public VectorIntoType
{
public:
IntoType(std::vector<double> &v) : v_(v) {}
IntoType(std::vector<double> &v)
: VectorIntoType(v.size()), v_(v) {}
IntoType(std::vector<double> &v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), v_(v) {}
@@ -1269,7 +1280,7 @@ 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()) {}
: VectorIntoType(v.size()), 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()) {}
@@ -1421,7 +1432,7 @@ class IntoType<std::vector<std::tm> > : public VectorIntoType
{
public:
IntoType(std::vector<std::tm>& v)
: vec_(v), buf_(NULL) {}
: VectorIntoType(v.size()), vec_(v), buf_(NULL) {}
IntoType(std::vector<std::tm>& v, std::vector<eIndicator> &vind)
: VectorIntoType(vind), vec_(v), buf_(NULL) {}
@@ -1562,16 +1573,10 @@ 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<BASE_TYPE>(base_value_), value_(value), base_value_(value.size()) {}
IntoType(std::vector<T> &value, std::vector<eIndicator> &ind)
: IntoType<BASE_TYPE>(base_value_, ind), value_(value)
{
base_value_.resize(value_.size());
}
: IntoType<BASE_TYPE>(base_value_, ind), value_(value), base_value_(value.size()) {}
int size(){return base_value_.size();}
void resize(int sz){value_.resize(sz); base_value_.resize(sz);}
@@ -1598,17 +1603,13 @@ class UseType<std::vector<T> > : public UseType<std::vector<typename TypeConvers
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)
: UseType<BASE_TYPE>(base_value_), value_(value), base_value_(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());
}
: UseType<BASE_TYPE>(base_value_, ind, name), value_(value), base_value_(value_.size()) {}
private:
void convertFrom()
{

View File

@@ -17,6 +17,19 @@ void test1()
int x = -5;
try
{
session.once << "select null from dual", into(x);
// exception expected (null value and no indicator)
assert(false);
}
catch(std::exception const &e)
{
std::string msg(e.what());
assert(msg == "Null value fetched and no indicator defined.");
}
try
{
session.once << "select 7 from dual where 0 = 1", into(x);
@@ -884,6 +897,21 @@ void test15()
assert(inds_out[0] == eOK && inds_out[1] == eNull && inds_out[2] == eOK);
}
// verify an exception is thrown if null is selected and no indicator was provided
{
std::string msg;
std::vector<int> intos(3);
try
{
sql << "select code from test15", into(intos);
}
catch(SOCIError& e)
{
msg = e.what();
}
assert(msg == "Null value fetched and no indicator defined." );
}
// test basic select
{
const size_t sz = 3;
@@ -1267,6 +1295,7 @@ void test19()
{
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);
}
@@ -1306,7 +1335,6 @@ void test19()
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));