mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 19:25:53 +02:00
[SF 2272430] BLOB and CLOB
Renamed: (Abstract)Preparation => (Abstract)Preparator (Abstract)Prepare => (Abstract)Preparation
This commit is contained in:
@@ -42,11 +42,10 @@
|
||||
|
||||
#include "Poco/Data/SQLite/SQLite.h"
|
||||
#include "Poco/Data/AbstractBinder.h"
|
||||
#include "Poco/Data/LOB.h"
|
||||
#include "Poco/Any.h"
|
||||
#include "Poco/DynamicAny.h"
|
||||
|
||||
|
||||
struct sqlite3_stmt;
|
||||
#include "sqlite3.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -114,6 +113,9 @@ public:
|
||||
void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir);
|
||||
/// Binds a BLOB.
|
||||
|
||||
void bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir);
|
||||
/// Binds a CLOB.
|
||||
|
||||
void bind(std::size_t pos, const Date& val, Direction dir);
|
||||
/// Binds a Date.
|
||||
|
||||
@@ -131,6 +133,17 @@ private:
|
||||
/// Checks the SQLite return code and throws an appropriate exception
|
||||
/// if error has occurred.
|
||||
|
||||
template <typename T>
|
||||
void bindLOB(std::size_t pos, const Poco::Data::LOB<T>& val, Direction dir)
|
||||
{
|
||||
// convert a blob to a an unsigned char* array
|
||||
const T* pData = reinterpret_cast<const T*>(val.rawContent());
|
||||
int valSize = static_cast<int>(val.size());
|
||||
|
||||
int rc = sqlite3_bind_blob(_pStmt, static_cast<int>(pos), pData, valSize, SQLITE_STATIC); // no deep copy, do not free memory
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
sqlite3_stmt* _pStmt;
|
||||
};
|
||||
|
||||
@@ -208,6 +221,18 @@ inline void Binder::bind(std::size_t pos, const char* const &pVal, Direction dir
|
||||
}
|
||||
|
||||
|
||||
inline void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
|
||||
{
|
||||
bindLOB<Poco::Data::BLOB::ValueType>(pos, val, dir);
|
||||
}
|
||||
|
||||
|
||||
inline void Binder::bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir)
|
||||
{
|
||||
bindLOB<Poco::Data::CLOB::ValueType>(pos, val, dir);
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
||||
|
||||
|
||||
|
@@ -48,13 +48,11 @@
|
||||
#include "Poco/Data/Constants.h"
|
||||
#include "Poco/Any.h"
|
||||
#include "Poco/DynamicAny.h"
|
||||
#include "sqlite3.h"
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
|
||||
struct sqlite3_stmt;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
@@ -121,6 +119,9 @@ public:
|
||||
bool extract(std::size_t pos, Poco::Data::BLOB& val);
|
||||
/// Extracts a BLOB.
|
||||
|
||||
bool extract(std::size_t pos, Poco::Data::CLOB& val);
|
||||
/// Extracts a CLOB.
|
||||
|
||||
bool extract(std::size_t pos, Poco::Data::Date& val);
|
||||
/// Extracts a Date.
|
||||
|
||||
@@ -285,6 +286,16 @@ private:
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool extractLOB(std::size_t pos, Poco::Data::LOB<T>& val)
|
||||
{
|
||||
if (isNull(pos)) return false;
|
||||
int size = sqlite3_column_bytes(_pStmt, (int) pos);
|
||||
const T* pTmp = reinterpret_cast<const T*>(sqlite3_column_blob(_pStmt, (int) pos));
|
||||
val = Poco::Data::LOB<T>(pTmp, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
sqlite3_stmt* _pStmt;
|
||||
NullIndVec _nulls;
|
||||
};
|
||||
@@ -299,6 +310,18 @@ inline void Extractor::reset()
|
||||
}
|
||||
|
||||
|
||||
inline bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
|
||||
{
|
||||
return extractLOB<Poco::Data::BLOB::ValueType>(pos, val);
|
||||
}
|
||||
|
||||
|
||||
inline bool Extractor::extract(std::size_t pos, Poco::Data::CLOB& val)
|
||||
{
|
||||
return extractLOB<Poco::Data::CLOB::ValueType>(pos, val);
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
||||
|
||||
|
||||
|
@@ -38,11 +38,9 @@
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/DateTimeFormat.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
@@ -104,17 +102,6 @@ void Binder::bind(std::size_t pos, const std::string& val, Direction dir)
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
|
||||
{
|
||||
// convert a blob to a an unsigned char* array
|
||||
const unsigned char* pData = reinterpret_cast<const unsigned char*>(val.rawContent());
|
||||
int valSize = static_cast<int>(val.size());
|
||||
|
||||
int rc = sqlite3_bind_blob(_pStmt, static_cast<int>(pos), pData, valSize, SQLITE_STATIC); // no deep copy, do not free memory
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Date& val, Direction dir)
|
||||
{
|
||||
DateTime dt(val.year(), val.month(), val.day());
|
||||
|
@@ -38,11 +38,10 @@
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Data/LOB.h"
|
||||
#include "Poco/Data/DataException.h"
|
||||
#include "Poco/DateTimeParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
@@ -116,17 +115,6 @@ bool Extractor::extract(std::size_t pos, std::string& val)
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
int size = sqlite3_column_bytes(_pStmt, (int) pos);
|
||||
const char* pTmp = reinterpret_cast<const char*>(sqlite3_column_blob(_pStmt, (int) pos));
|
||||
val = Poco::Data::BLOB(pTmp, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Int8& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
|
@@ -35,7 +35,7 @@
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Data/LOB.h"
|
||||
#include "Poco/Data/Statement.h"
|
||||
#include "Poco/Data/RecordSet.h"
|
||||
#include "Poco/Data/SQLChannel.h"
|
||||
@@ -65,7 +65,7 @@ using Poco::Data::Column;
|
||||
using Poco::Data::Row;
|
||||
using Poco::Data::SQLChannel;
|
||||
using Poco::Data::LimitException;
|
||||
using Poco::Data::BLOB;
|
||||
using Poco::Data::CLOB;
|
||||
using Poco::Data::Date;
|
||||
using Poco::Data::Time;
|
||||
using Poco::Data::AbstractExtractionVec;
|
||||
@@ -143,7 +143,7 @@ public:
|
||||
pBinder->bind(pos++, obj.age, dir);
|
||||
}
|
||||
|
||||
static void prepare(std::size_t pos, Person& obj, AbstractPreparation* pPrepare)
|
||||
static void prepare(std::size_t pos, Person& obj, AbstractPreparator* pPrepare)
|
||||
{
|
||||
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Address VARCHAR, Age INTEGER(3))
|
||||
poco_assert_dbg (pPrepare != 0);
|
||||
@@ -1215,7 +1215,7 @@ void SQLiteTest::testEmptyDB()
|
||||
}
|
||||
|
||||
|
||||
void SQLiteTest::testBLOB()
|
||||
void SQLiteTest::testCLOB()
|
||||
{
|
||||
std::string lastName("lastname");
|
||||
std::string firstName("firstname");
|
||||
@@ -1223,12 +1223,12 @@ void SQLiteTest::testBLOB()
|
||||
Session tmp (Poco::Data::SQLite::Connector::KEY, "dummy.db");
|
||||
tmp << "DROP TABLE IF EXISTS Person", now;
|
||||
tmp << "CREATE TABLE IF NOT EXISTS Person (LastName VARCHAR(30), FirstName VARCHAR, Address VARCHAR, Image BLOB)", now;
|
||||
BLOB img("0123456789", 10);
|
||||
CLOB img("0123456789", 10);
|
||||
int count = 0;
|
||||
tmp << "INSERT INTO PERSON VALUES(:ln, :fn, :ad, :img)", use(lastName), use(firstName), use(address), use(img), now;
|
||||
tmp << "SELECT COUNT(*) FROM PERSON", into(count), now;
|
||||
assert (count == 1);
|
||||
BLOB res;
|
||||
CLOB res;
|
||||
poco_assert (res.size() == 0);
|
||||
|
||||
tmp << "SELECT Image FROM Person WHERE LastName == :ln", bind("lastname"), into(res), now;
|
||||
@@ -2369,7 +2369,7 @@ CppUnit::Test* SQLiteTest::suite()
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testIllegalRange);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testSingleSelect);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testEmptyDB);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testBLOB);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testCLOB);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTuple10);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTupleVector10);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTuple9);
|
||||
|
@@ -87,7 +87,7 @@ public:
|
||||
void testSingleSelect();
|
||||
void testEmptyDB();
|
||||
|
||||
void testBLOB();
|
||||
void testCLOB();
|
||||
|
||||
void testTuple1();
|
||||
void testTupleVector1();
|
||||
|
Reference in New Issue
Block a user