diff options
Diffstat (limited to 'connectivity/source/drivers/firebird/PreparedStatement.cxx')
-rw-r--r-- | connectivity/source/drivers/firebird/PreparedStatement.cxx | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/connectivity/source/drivers/firebird/PreparedStatement.cxx b/connectivity/source/drivers/firebird/PreparedStatement.cxx index 04e65b9c5e95..3209f9d02aea 100644 --- a/connectivity/source/drivers/firebird/PreparedStatement.cxx +++ b/connectivity/source/drivers/firebird/PreparedStatement.cxx @@ -52,6 +52,8 @@ using namespace com::sun::star::util; IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbcx.firebird.PreparedStatement","com.sun.star.sdbc.PreparedStatement"); +constexpr size_t MAX_SIZE_SEGMENT = 65535; // max value of a segment of CLOB, if we want more than 65535 bytes, we need more segments + OPreparedStatement::OPreparedStatement( Connection* _pConnection, const OUString& sql) @@ -209,7 +211,8 @@ void SAL_CALL OPreparedStatement::setString(sal_Int32 nParameterIndex, { str = str.copy(0, max_varchar_len); } - const auto nLength = str.getLength(); + const sal_uInt16 nLength = str.getLength(); + static_assert(sizeof(nLength) == 2, "must match dest memcpy len"); memcpy(pVar->sqldata, &nLength, 2); // Actual data memcpy(pVar->sqldata + 2, str.getStr(), str.getLength()); @@ -611,9 +614,9 @@ void SAL_CALL OPreparedStatement::setClob(sal_Int32 nParameterIndex, const Refer sal_Int64 nCharWritten = 1; // XClob is indexed from 1 ISC_STATUS aErr = 0; sal_Int64 nLen = xClob->length(); - while ( nLen > nCharWritten ) + while ( nLen >= nCharWritten ) { - sal_Int64 nCharRemain = nLen - nCharWritten; + sal_Int64 nCharRemain = nLen - nCharWritten + 1; constexpr sal_uInt16 MAX_SIZE = SAL_MAX_UINT16 / 4; sal_uInt16 nWriteSize = std::min<sal_Int64>(nCharRemain, MAX_SIZE); OString sData = OUStringToOString( @@ -662,10 +665,41 @@ void OPreparedStatement::setClob( sal_Int32 nParameterIndex, const OUString& rSt OString sData = OUStringToOString( rStr, RTL_TEXTENCODING_UTF8); - ISC_STATUS aErr = isc_put_segment( m_statusVector, + size_t nDataSize = sData.getLength(); + ISC_STATUS aErr = 0; + // we can't store more than MAX_SIZE_SEGMENT in a segment + if (nDataSize <= MAX_SIZE_SEGMENT) + { + aErr = isc_put_segment( m_statusVector, &aBlobHandle, sData.getLength(), sData.getStr() ); + } + else + { + // if we need more, let's split the input and first let's calculate the nb of entire chunks needed + size_t nNbEntireChunks = nDataSize / MAX_SIZE_SEGMENT; + for (size_t i = 0; i < nNbEntireChunks; ++i) + { + OString strCurrentChunk = sData.copy(i * MAX_SIZE_SEGMENT, MAX_SIZE_SEGMENT); + aErr = isc_put_segment( m_statusVector, + &aBlobHandle, + strCurrentChunk.getLength(), + strCurrentChunk.getStr() ); + if (aErr) + break; + } + size_t nRemainingBytes = nDataSize - (nNbEntireChunks * MAX_SIZE_SEGMENT); + if (nRemainingBytes && !aErr) + { + // then copy the remaining + OString strCurrentChunk = sData.copy(nNbEntireChunks * MAX_SIZE_SEGMENT, nRemainingBytes); + aErr = isc_put_segment( m_statusVector, + &aBlobHandle, + strCurrentChunk.getLength(), + strCurrentChunk.getStr() ); + } + } // We need to make sure we close the Blob even if there are errors, hence evaluate // errors after closing. @@ -910,7 +944,7 @@ void SAL_CALL OPreparedStatement::setBytes(sal_Int32 nParameterIndex, { xBytesCopy.realloc( nMaxSize ); } - const auto nSize = xBytesCopy.getLength(); + const sal_uInt16 nSize = xBytesCopy.getLength(); // 8000 corresponds to value from lcl_addDefaultParameters // in dbaccess/source/filter/hsqldb/createparser.cxx if (nSize > 8000) @@ -918,6 +952,7 @@ void SAL_CALL OPreparedStatement::setBytes(sal_Int32 nParameterIndex, free(pVar->sqldata); pVar->sqldata = static_cast<char *>(malloc(sizeof(char) * nSize + 2)); } + static_assert(sizeof(nSize) == 2, "must match dest memcpy len"); // First 2 bytes indicate string size memcpy(pVar->sqldata, &nSize, 2); // Actual data |