summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTamas Bunth <tamas.bunth@collabora.co.uk>2019-01-29 13:00:21 +0100
committerAndras Timar <andras.timar@collabora.com>2019-01-30 15:41:33 +0100
commit49f0bb2eba1e52bf3654bcec9682c8f0331c63a3 (patch)
treeb11e11b4007f55daefea9eaf6c7acc0aa6a9383a
parentUpdate git submodules (diff)
downloadcore-49f0bb2eba1e52bf3654bcec9682c8f0331c63a3.tar.gz
core-49f0bb2eba1e52bf3654bcec9682c8f0331c63a3.zip
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> Reviewed-on: https://gerrit.libreoffice.org/67091 Reviewed-by: Andras Timar <andras.timar@collabora.com> Tested-by: Andras Timar <andras.timar@collabora.com>
-rw-r--r--connectivity/qa/connectivity/mysql/mysql.cxx36
-rw-r--r--connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx11
-rw-r--r--connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx2
3 files changed, 44 insertions, 5 deletions
diff --git a/connectivity/qa/connectivity/mysql/mysql.cxx b/connectivity/qa/connectivity/mysql/mysql.cxx
index 6c9fb45b7c30..115347e64f42 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>
@@ -49,6 +51,7 @@ public:
void testDBPositionChange();
void testMultipleResultsets();
void testDBMetaData();
+ void testTimestampField();
CPPUNIT_TEST_SUITE(MysqlTestDriver);
CPPUNIT_TEST(testDBConnection);
@@ -56,6 +59,7 @@ public:
CPPUNIT_TEST(testIntegerInsertAndQuery);
CPPUNIT_TEST(testMultipleResultsets);
CPPUNIT_TEST(testDBMetaData);
+ CPPUNIT_TEST(testTimestampField);
CPPUNIT_TEST_SUITE_END();
};
@@ -322,6 +326,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 4633d064ce9a..4c6c6e018c59 100644
--- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx
+++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx
@@ -238,7 +238,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)
@@ -286,7 +286,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)
@@ -478,7 +478,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;
@@ -517,11 +517,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 91202f5ff716..daee0ad3b9d7 100644
--- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx
+++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx
@@ -77,7 +77,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);