25#include <com/sun/star/sdbc/DataType.hpp>
26#include <com/sun/star/io/WrongFormatException.hpp>
27#include <com/sun/star/util/Time.hpp>
28#include <com/sun/star/util/Date.hpp>
29#include <com/sun/star/util/DateTime.hpp>
33#include <rtl/ustrbuf.hxx>
35#include <boost/date_time/posix_time/posix_time.hpp>
45OUString lcl_double_dabble(
const std::vector<sal_uInt8>&
bytes)
47 size_t nbits = 8 *
bytes.size();
48 size_t nscratch = nbits / 2;
49 std::vector<char> scratch(nscratch, 0);
51 for (
size_t i = 0;
i <
bytes.size(); ++
i)
53 for (
size_t j = 0; j < 8; ++j)
56 int shifted_in = (
bytes[
i] & (1 << (7 - j))) ? 1 : 0;
59 for (
size_t k = 0; k < nscratch; ++k)
60 scratch[k] += (scratch[k] >= 5) ? 3 : 0;
63 for (
size_t k = 0; k < nscratch - 1; ++k)
67 scratch[k] |= (scratch[k + 1] >= 8) ? 1 : 0;
71 scratch[nscratch - 1] <<= 1;
72 scratch[nscratch - 1] &= 0xF;
73 scratch[nscratch - 1] |= shifted_in;
77 auto it = scratch.begin();
79 while (*it == 0 && scratch.size() > 1)
81 it = scratch.erase(it);
85 for (
auto& digit : scratch)
89 return OStringToOUString(std::string_view(scratch.data(), scratch.size()),
90 RTL_TEXTENCODING_UTF8);
93OUString lcl_makeStringFromBigint(std::vector<sal_uInt8>&& aBytes)
98 if ((aBytes[0] & 0x80) != 0)
101 for (
auto&
byte : aBytes)
105 for (
size_t i = aBytes.size() - 1; i != 0; --i)
113 OUString sNum = lcl_double_dabble(aBytes);
115 return sRet.makeStringAndClear();
118OUString lcl_putDot(std::u16string_view sNum, sal_Int32 nScale)
121 OUStringBuffer sBuf{ sNum };
122 sal_Int32 nNullsToAppend = nScale - sNum.size() + 1;
123 for (sal_Int32 i = 0;
i < nNullsToAppend; ++
i)
127 sBuf.insert(sBuf.getLength() - 1 - nScale,
".");
128 return sBuf.makeStringAndClear();
134using namespace css::uno;
135using namespace css::sdbc;
136using namespace css::io;
137using namespace boost::posix_time;
138using namespace boost::gregorian;
145 m_pStream->SetEndian(SvStreamEndian::BIG);
161 Sequence<sal_Unicode>
aBuffer(nUTFLen);
165 sal_Int32 nStrLen = 0;
192 throw WrongFormatException();
196 if ((char2 & 0xC0) != 0x80)
198 throw WrongFormatException();
201 pStr[nStrLen++] = (
sal_Unicode(c & 0x1F) << 6) | (char2 & 0x3F);
209 throw WrongFormatException();
215 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
217 throw WrongFormatException();
225 throw WrongFormatException();
228 return OUString(pStr, nStrLen);
240 auto nLen = nColTypes.size();
241 std::vector<Any>
aData;
243 for (
size_t i = 0;
i < nLen; ++
i)
251 sal_Int32
nType = nColTypes[
i].getDataType();
258 case DataType::VARCHAR:
259 case DataType::LONGVARCHAR:
262 case DataType::TINYINT:
263 case DataType::SMALLINT:
270 case DataType::INTEGER:
277 case DataType::BIGINT:
285 case DataType::FLOAT:
286 case DataType::DOUBLE:
294 case DataType::NUMERIC:
295 case DataType::DECIMAL:
300 std::vector<sal_uInt8> aBytes(nSize);
301 m_pStream->ReadBytes(aBytes.data(), nSize);
302 assert(aBytes.size() > 0);
304 sal_Int32 nScale = 0;
307 OUString sNum = lcl_makeStringFromBigint(std::move(aBytes));
308 Sequence<Any>
result{
Any(lcl_putDot(sNum, nScale)),
Any(nScale) };
316 ptime epoch = time_from_string(
"1970-01-01 00:00:00.000");
317 ptime time = epoch + milliseconds(
value);
318 date asDate = time.date();
320 css::util::Date loDate(asDate.day(), asDate.month(),
329 auto valueInSecs =
value / 1000;
333 const int secPerDay = 24 * 60 * 60;
334 valueInSecs = (valueInSecs + secPerDay) % secPerDay;
336 auto nHours = valueInSecs / (60 * 60);
337 valueInSecs = valueInSecs % 3600;
338 const sal_uInt16 nMins = valueInSecs / 60;
339 const sal_uInt16 nSecs = valueInSecs % 60;
340 css::util::Time time((
value % 1000) * 1000000, nSecs, nMins, nHours,
true);
344 case DataType::TIMESTAMP:
346 sal_Int64 nEpochMillis = 0;
348 ptime epoch = time_from_string(
"1970-01-01 00:00:00.000");
349 ptime time = epoch + milliseconds(nEpochMillis);
350 date asDate = time.date();
352 sal_Int32 nNanos = 0;
356 css::util::DateTime dateTime;
357 dateTime.NanoSeconds = nNanos;
358 dateTime.Seconds = time.time_of_day().seconds();
359 dateTime.Minutes = time.time_of_day().minutes();
360 dateTime.Hours = time.time_of_day().hours();
361 dateTime.Day = asDate.day();
362 dateTime.Month = asDate.month();
363 dateTime.Year = asDate.year();
367 case DataType::BOOLEAN:
371 aData.push_back(
Any(
static_cast<bool>(nBool)));
374 case DataType::OTHER:
377 case DataType::BINARY:
378 case DataType::VARBINARY:
379 case DataType::LONGVARBINARY:
384 Sequence<sal_Int8> aBytes(nSize);
385 m_pStream->ReadBytes(aBytes.getArray(), nSize);
391 throw WrongFormatException();
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
constexpr OUStringLiteral aData
std::vector< sal_uInt8 > bytes
std::unique_ptr< char[]> aBuffer