diff options
author | Tamas Bunth <tamas.bunth@collabora.co.uk> | 2019-01-29 13:00:21 +0100 |
---|---|---|
committer | Tamás Bunth <btomi96@gmail.com> | 2019-01-29 18:07:08 +0100 |
commit | 76491860113d60965cd234770afcef747fe4bd65 (patch) | |
tree | 3c374ff615384741ee918a38cfb62e2083d3ba21 /connectivity | |
parent | 4faafea4f24316e75b80e6ef97c1a4d39551a0b2 (diff) |
mysqlc: fix timestamp query of result set
Also add test for inserting and reading timestamp values.
Change-Id: I2ba997c438f4e33965b0fe0602e58eddeff38b01
Reviewed-on: https://gerrit.libreoffice.org/67066
Tested-by: Jenkins
Reviewed-by: Tamás Bunth <btomi96@gmail.com>
Diffstat (limited to 'connectivity')
-rw-r--r-- | connectivity/qa/connectivity/mysql/mysql.cxx | 36 | ||||
-rw-r--r-- | connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx | 11 | ||||
-rw-r--r-- | connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx | 2 |
3 files changed, 44 insertions, 5 deletions
diff --git a/connectivity/qa/connectivity/mysql/mysql.cxx b/connectivity/qa/connectivity/mysql/mysql.cxx index bfefeec132c1..1cbe34790421 100644 --- a/connectivity/qa/connectivity/mysql/mysql.cxx +++ b/connectivity/qa/connectivity/mysql/mysql.cxx @@ -20,6 +20,8 @@ #include <com/sun/star/sdbc/XParameters.hpp> #include <com/sun/star/sdbc/XStatement.hpp> #include <com/sun/star/sdbc/XDriver.hpp> + +#include <com/sun/star/util/DateTime.hpp> #include <svtools/miscopt.hxx> #include <osl/process.h> @@ -50,6 +52,7 @@ public: void testDBPositionChange(); void testMultipleResultsets(); void testDBMetaData(); + void testTimestampField(); CPPUNIT_TEST_SUITE(MysqlTestDriver); CPPUNIT_TEST(testDBConnection); @@ -57,6 +60,7 @@ public: CPPUNIT_TEST(testIntegerInsertAndQuery); CPPUNIT_TEST(testMultipleResultsets); CPPUNIT_TEST(testDBMetaData); + CPPUNIT_TEST(testTimestampField); CPPUNIT_TEST_SUITE_END(); }; @@ -337,6 +341,38 @@ void MysqlTestDriver::testDBMetaData() nUpdateCount = xStatement->executeUpdate("DROP TABLE myTestTable"); } +void MysqlTestDriver::testTimestampField() +{ + Reference<XConnection> xConnection = m_xDriver->connect(m_sUrl, m_infos); + if (!xConnection.is()) + CPPUNIT_ASSERT_MESSAGE("cannot connect to data source!", xConnection.is()); + uno::Reference<XStatement> xStatement = xConnection->createStatement(); + CPPUNIT_ASSERT(xStatement.is()); + xStatement->executeUpdate("DROP TABLE IF EXISTS myTestTable"); + + xStatement->executeUpdate( + "CREATE TABLE myTestTable (id INTEGER PRIMARY KEY, mytimestamp timestamp)"); + xStatement->executeUpdate("INSERT INTO myTestTable VALUES (1, '2008-02-16 20:15:03')"); + + // now let's query + Reference<XResultSet> xResultSet + = xStatement->executeQuery("SELECT mytimestamp from myTestTable"); + + xResultSet->next(); // use it + Reference<XRow> xRow(xResultSet, UNO_QUERY); + CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xRow.is()); + util::DateTime dt = xRow->getTimestamp(1); + CPPUNIT_ASSERT_EQUAL(static_cast<short>(2008), dt.Year); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), dt.Month); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(16), dt.Day); + + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(20), dt.Hours); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(15), dt.Minutes); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(3), dt.Seconds); + + xStatement->executeUpdate("DROP TABLE myTestTable"); +} + CPPUNIT_TEST_SUITE_REGISTRATION(MysqlTestDriver); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx index c8a5f387bc95..05d5c1321604 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx @@ -236,7 +236,7 @@ uno::Reference<XInputStream> SAL_CALL OResultSet::getBinaryStream(sal_Int32 colu OString sVal = m_aRows[m_nRowPosition][column - 1]; return new SequenceInputStream{ uno::Sequence<sal_Int8>( - reinterpret_cast<sal_Int8 const*>(sVal.getStr()), getDataLength(column - 1)) }; + reinterpret_cast<sal_Int8 const*>(sVal.getStr()), getDataLength(column)) }; } uno::Reference<XInputStream> SAL_CALL OResultSet::getCharacterStream(sal_Int32 column) @@ -284,7 +284,7 @@ uno::Sequence<sal_Int8> SAL_CALL OResultSet::getBytes(sal_Int32 column) return uno::Sequence<sal_Int8>(); return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sVal.getStr()), - getDataLength(column - 1)); + getDataLength(column)); } Date SAL_CALL OResultSet::getDate(sal_Int32 column) @@ -476,7 +476,7 @@ Time SAL_CALL OResultSet::getTime(sal_Int32 column) return t; OString sVal = m_aRows[m_nRowPosition][column - 1]; - OString timeString{ sVal.getStr(), getDataLength(column - 1) }; + OString timeString{ sVal.getStr(), getDataLength(column) }; OString token; sal_Int32 nIndex, i = 0; @@ -515,11 +515,14 @@ DateTime SAL_CALL OResultSet::getTimestamp(sal_Int32 column) // YY-MM-DD HH:MM:SS std::vector<OString> dateAndTime - = lcl_split(OString{ sVal.getStr(), getDataLength(column - 1) }, ' '); + = lcl_split(OString{ sVal.getStr(), getDataLength(column) }, ' '); auto dateParts = lcl_split(dateAndTime.at(0), '-'); auto timeParts = lcl_split(dateAndTime.at(1), ':'); + if (dateParts.size() < 2 || timeParts.size() < 2) + throw SQLException("Timestamp has a wrong format", *this, OUString(), 1, Any()); + DateTime dt; dt.Year = dateParts.at(0).toUInt32(); diff --git a/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx b/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx index 181410b7f3f3..e87a4efef73e 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx @@ -75,7 +75,7 @@ class OResultSet final : public OBase_Mutex, sal_Int32 getDataLength(sal_Int32 column) { - return m_aRows[m_nRowCount][column - 1].getLength(); + return m_aRows[m_nRowPosition][column - 1].getLength(); } bool checkNull(sal_Int32 column); |