added std::pair support

This commit is contained in:
Peter Schojer 2008-09-02 07:02:58 +00:00
parent 64933507ac
commit b74e41b626
5 changed files with 245 additions and 0 deletions

View File

@ -2046,6 +2046,42 @@ void SQLiteTest::testDynamicAny()
}
void SQLiteTest::testPair()
{
Session tmp (Poco::Data::SQLite::Connector::KEY, "dummy.db");
assert (tmp.isConnected());
std::string tableName("Simpsons");
std::pair<std::string, int> junior = std::make_pair("Junior", 12);
std::pair<std::string, int> senior = std::make_pair("Senior", 99);
int count = 0;
std::string result;
tmp << "DROP TABLE IF EXISTS Simpsons", now;
tmp << "CREATE TABLE IF NOT EXISTS Simpsons (LastName VARCHAR(30), Age INTEGER(3))", now;
tmp << "SELECT name FROM sqlite_master WHERE tbl_name=?", use(tableName), into(result), now;
assert (result == tableName);
// these are fine
tmp << "INSERT INTO Simpsons VALUES(?, ?)", use(junior), now;
tmp << "INSERT INTO Simpsons VALUES(?, ?)", useRef(senior), now;
tmp << "SELECT COUNT(*) FROM Simpsons", into(count), now;
assert (2 == count);
std::vector<std::pair<std::string, int> > ret;
tmp << "SELECT * FROM Simpsons", into(ret), range(2,2), now;
assert (ret[0].second == 12 || ret[1].second == 12);
assert (ret[0].second == 99 || ret[1].second == 99);
assert (ret[0].first == "Junior" || ret[1].first == "Junior");
assert (ret[0].first == "Senior" || ret[1].first == "Senior");
}
void SQLiteTest::testSQLChannel()
{
Session tmp (Poco::Data::SQLite::Connector::KEY, "dummy.db");
@ -2344,6 +2380,7 @@ CppUnit::Test* SQLiteTest::suite()
CppUnit_addTest(pSuite, SQLiteTest, testExternalBindingAndExtraction);
CppUnit_addTest(pSuite, SQLiteTest, testBindingCount);
CppUnit_addTest(pSuite, SQLiteTest, testMultipleResults);
CppUnit_addTest(pSuite, SQLiteTest, testPair);
return pSuite;
}

View File

@ -119,6 +119,7 @@ public:
void testAny();
void testDynamicAny();
void testPair();
void testSQLChannel();
void testSQLLogger();

View File

@ -1350,6 +1350,120 @@ private:
};
template <class K, class V>
class Binding<std::pair<K, V> >: public AbstractBinding
/// Specialization for std::multimap.
{
public:
explicit Binding(std::pair<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_val(val),
_bound(false)
/// Creates the Binding.
{
reset();
}
~Binding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<K>::size() + TypeHandler<V>::size();
}
std::size_t numOfRowsHandled() const
{
return 1;
}
bool canBind() const
{
return !_bound;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<K>::bind(pos, _val.first, getBinder(), getDirection());
TypeHandler<V>::bind(pos+1, _val.second, getBinder(), getDirection());
_bound = true;
}
void reset()
{
_bound = false;
}
private:
std::pair<K, V>& _val;
bool _bound;
};
template <class K, class V>
class CopyBinding<std::pair<K, V> >: public AbstractBinding
/// Specialization for std::multimap.
{
public:
explicit CopyBinding(std::pair<K, V>& val,
const std::string& name = "",
Direction direction = PD_IN):
AbstractBinding(name, direction),
_pVal(new std::pair<K, V>(val)),
_bound(false)
/// Creates the Binding.
{
reset();
}
~CopyBinding()
/// Destroys the Binding.
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<K>::size() + TypeHandler<V>::size();
}
std::size_t numOfRowsHandled() const
{
return 1;
}
bool canBind() const
{
return !_bound;
}
void bind(std::size_t pos)
{
poco_assert_dbg(getBinder() != 0);
poco_assert_dbg(canBind());
TypeHandler<K>::bind(pos, _pVal->first, getBinder(), getDirection());
TypeHandler<V>::bind(pos+1, _pVal->second, getBinder(), getDirection());
_bound = true;;
}
void reset()
{
_bound = false;
}
private:
typedef typename TypeWrapper<std::pair<K, V> >::TYPE ValueType;
SharedPtr<ValueType> _pVal;
bool _bound;
};
namespace Keywords {

View File

@ -764,6 +764,61 @@ private:
};
template <class K, class V>
class Extraction<std::pair<K, V> >: public AbstractExtraction
/// Map Data Type specialization for extraction of values from a query result set.
{
public:
Extraction(std::pair<K, V>& result, const Position& pos = Position(0)):
AbstractExtraction(Limit::LIMIT_UNLIMITED, pos.value()),
_rResult(result),
_default()
{
}
Extraction(std::pair<K, V>& result, const std::pair<K, V>& def, const Position& pos = Position(0)):
AbstractExtraction(Limit::LIMIT_UNLIMITED, pos.value()),
_rResult(result),
_default(def)
{
}
~Extraction()
{
}
std::size_t numOfColumnsHandled() const
{
return TypeHandler<K>::size() + TypeHandler<V>::size();
}
std::size_t numOfRowsHandled() const
{
return 1;
}
std::size_t numOfRowsAllowed() const
{
return getLimit();
}
std::size_t extract(std::size_t pos)
{
TypeHandler<std::pair<K, V> >::extract(pos, _rResult, _default, getExtractor());
return 1u;
}
AbstractPrepare* createPrepareObject(AbstractPreparation* pPrep, std::size_t pos)
{
return new Prepare<V>(pPrep, pos, _default);
}
private:
std::pair<K, V>& _rResult;
std::pair<K, V> _default;
};
namespace Keywords {

View File

@ -1763,6 +1763,44 @@ private:
TypeHandler& operator=(const TypeHandler&);
};
template <class K, class V>
class TypeHandler<std::pair<K, V> >: public AbstractTypeHandler
{
public:
static void bind(std::size_t pos, const std::pair<K, V>& obj, AbstractBinder* pBinder, AbstractBinder::Direction dir)
{
TypeHandler<K>::bind(pos, obj.first, pBinder, dir);
pos += TypeHandler<K>::size();
TypeHandler<V>::bind(pos, obj.second, pBinder, dir);
}
static std::size_t size()
{
return TypeHandler<K>::size() + TypeHandler<V>::size();
}
static void extract(std::size_t pos, std::pair<K, V>& obj, const std::pair<K, V>& defVal, AbstractExtractor* pExt)
{
TypeHandler<K>::extract(pos, obj.first, defVal.first, pExt);
pos += TypeHandler<K>::size();
TypeHandler<V>::extract(pos, obj.second, defVal.second, pExt);
}
static void prepare(std::size_t pos, std::pair<K, V>& obj, AbstractPreparation* pPrepare)
{
TypeHandler<K>::prepare(pos, obj.first, pPrepare);
pos += TypeHandler<K>::size();
TypeHandler<V>::prepare(pos, obj.second, pPrepare);
}
private:
TypeHandler(const TypeHandler&);
TypeHandler& operator = (const TypeHandler&);
};
} } // namespace Poco::Data