Kaydet (Commit) 76491860 authored tarafından Tamas Bunth's avatar Tamas Bunth Kaydeden (comit) Tamás Bunth

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: 's avatarTamás Bunth <btomi96@gmail.com>
üst 4faafea4
......@@ -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();
......
......@@ -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();
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment