Skip to content

feat(data): add implementation of bulkbinding uuid containers for odbc binder #4979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions Data/ODBC/include/Poco/Data/ODBC/Binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ class ODBC_API Binder: public Poco::Data::AbstractBinder
void bind(std::size_t pos, const UUID& val, Direction dir);
/// Binds a UUID.

void bind(std::size_t pos, const std::vector<Poco::UUID>& val, Direction dir);
/// Binds a UUID vector.

void bind(std::size_t pos, const std::deque<Poco::UUID>& val, Direction dir);
/// Binds a UUID deque.

void bind(std::size_t pos, const std::list<Poco::UUID>& val, Direction dir);
/// Binds a UUID list.

void bind(std::size_t pos, const NullData& val, Direction dir);
/// Binds a null. In-bound only.

Expand Down Expand Up @@ -930,6 +939,63 @@ class ODBC_API Binder: public Poco::Data::AbstractBinder
}
}

template<typename C>
void bindImplContainerUUID(std::size_t pos, const C& val, Direction dir)
{
if (isOutBound(dir) || !isInBound(dir))
throw NotImplementedException("ODBC::Binder::bindImplContainerUUID():UUID container parameter type can only be inbound.");

if (PB_IMMEDIATE != _paramBinding)
throw InvalidAccessException("ODBC::Binder::bindImplContainerUUID():Containers can only be bound immediately.");

std::size_t length = val.size();

if (0 == length)
throw InvalidArgumentException("ODBC::Binder::bindImplContainerUUID():Empty Containers not allowed.");

setParamSetSize(length);

SQLINTEGER size = 16; // UUID is fixed 16 bytes
if (_vecLengthIndicator.size() <= pos)
{
_vecLengthIndicator.resize(pos + 1, 0);
_vecLengthIndicator[pos] = new LengthVec(length ? length : 1, size);
}

if (_charPtrs.size() <= pos)
_charPtrs.resize(pos + 1, 0);

_charPtrs[pos] = (char*)std::calloc(val.size() * size, sizeof(char));
std::size_t offset = 0;
for (typename C::const_iterator it = val.begin(); it != val.end(); ++it)
{
std::vector<char> bytes(16);
it->copyTo(bytes.data()); // Extract 16-byte binary data
if (bytes.size() != static_cast<std::size_t>(size))
throw LengthExceededException("Invalid UUID size");
std::memcpy(_charPtrs[pos] + offset, bytes.data(), size);
offset += size;
}

SQLINTEGER colSize = 0;
SQLSMALLINT decDigits = 0;
getColSizeAndPrecision(pos, SQL_C_BINARY, colSize, decDigits);

if (Utility::isError(SQLBindParameter(_rStmt,
(SQLUSMALLINT)pos + 1,
toODBCDirection(dir),
SQL_C_BINARY,
SQL_GUID,
colSize,
decDigits,
_charPtrs[pos],
(SQLINTEGER)size,
&(*_vecLengthIndicator[pos])[0])))
{
throw StatementException(_rStmt, "ODBC::Binder::bindImplContainerUUID():SQLBindParameter(UUID[])");
}
}

template<typename C>
void bindImplNullContainer(std::size_t pos, const C& val, Direction dir)
{
Expand Down Expand Up @@ -1579,6 +1645,24 @@ inline void Binder::bind(std::size_t pos, const std::list<DateTime>& val, Direct
}


inline void Binder::bind(std::size_t pos, const std::vector<Poco::UUID>& val, Direction dir)
{
bindImplContainerUUID(pos, val, dir);
}


inline void Binder::bind(std::size_t pos, const std::deque<Poco::UUID>& val, Direction dir)
{
bindImplContainerUUID(pos, val, dir);
}


inline void Binder::bind(std::size_t pos, const std::list<Poco::UUID>& val, Direction dir)
{
bindImplContainerUUID(pos, val, dir);
}


inline void Binder::bind(std::size_t pos, const std::vector<NullData>& val, Direction dir)
{
bindImplNullContainer(pos, val, dir);
Expand Down
25 changes: 25 additions & 0 deletions Data/ODBC/testsuite/src/ODBCSQLServerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ catch(Poco::Exception& ex)
}
}

void ODBCSQLServerTest::testUUIDsBulk()
{
try
{
if (!_pSession) fail("Test not available.");

_pSession->setFeature("autoBind", true);
_pSession->setFeature("autoExtract", true);

recreateUUIDsTable();
int rows = 1000;
std::vector<Poco::UUID> uuids(rows);
for (int i = 0; i < rows; ++i) {
uuids[i]= Poco::UUIDGenerator::defaultGenerator().createRandom();
}

*_pSession << "INSERT INTO Strings VALUES (?)"s, use(uuids, bulk), Poco::Data::Keywords::now;
}
catch (Poco::Exception& ex)
{
std::cout << ex.displayText() << std::endl;
}
}


void ODBCSQLServerTest::testBulk()
{
Expand Down Expand Up @@ -1119,6 +1143,7 @@ CppUnit::Test* ODBCSQLServerTest::suite()
CppUnit_addTest(pSuite, ODBCSQLServerTest, testPrepare);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testBulk);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testNullBulk);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testUUIDsBulk);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testBulkPerformance);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testSetSimple);
CppUnit_addTest(pSuite, ODBCSQLServerTest, testSetComplex);
Expand Down
1 change: 1 addition & 0 deletions Data/ODBC/testsuite/src/ODBCSQLServerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ODBCSQLServerTest: public ODBCTest
void testBigBatch();
void testNull() override;
void testNullBulk() override;
void testUUIDsBulk();
void testBulk() override;

void testStoredProcedure() override;
Expand Down
2 changes: 2 additions & 0 deletions Data/ODBC/testsuite/src/ODBCTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "Poco/Data/ODBC/Utility.h"
#include "Poco/SharedPtr.h"
#include "Poco/Exception.h"
#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
#include "SQLExecutor.h"


Expand Down