Kaydet (Commit) 6391e823 authored tarafından Lionel Elie Mamane's avatar Lionel Elie Mamane

Replace buggy pgsql-sdbc datetime functions with dbtools equivalents

In particular, the string2time function segfaults when called on an empty string
(unconditionally tries to access the n-th character without checking whether the string is that long)
this happens in particular when reading a column of type TIME with a NULL value

Change-Id: I302044f67a92fe20685ce677ba3affdb9b44cb53
üst df0ac379
......@@ -20,6 +20,7 @@ $(eval $(call gb_Library_use_sdk_api,postgresql-sdbc-impl))
$(eval $(call gb_Library_use_libraries,postgresql-sdbc-impl,\
cppu \
cppuhelper \
dbtools \
sal \
salhelper \
$(gb_UWINAPI) \
......
......@@ -49,6 +49,8 @@
#include <com/sun/star/lang/DisposedException.hpp>
#include <connectivity/dbconversion.hxx>
using osl::Mutex;
using osl::MutexGuard;
......@@ -83,6 +85,8 @@ using com::sun::star::sdbc::XResultSetMetaDataSupplier;
using com::sun::star::beans::Property;
using namespace dbtools;
namespace pq_sdbc_driver
{
static ::cppu::IPropertyArrayHelper & getResultSetPropertyArrayHelper()
......@@ -540,19 +544,19 @@ Sequence< sal_Int8 > BaseResultSet::getBytes( sal_Int32 columnIndex )
::com::sun::star::util::Date BaseResultSet::getDate( sal_Int32 columnIndex )
throw (SQLException, RuntimeException)
{
return string2Date( getString( columnIndex ) );
return DBTypeConversion::toDate( getString( columnIndex ) );
}
::com::sun::star::util::Time BaseResultSet::getTime( sal_Int32 columnIndex )
throw (SQLException, RuntimeException)
{
return string2Time( getString( columnIndex ) );
return DBTypeConversion::toTime( getString( columnIndex ) );
}
::com::sun::star::util::DateTime BaseResultSet::getTimestamp( sal_Int32 columnIndex )
throw (SQLException, RuntimeException)
{
return string2DateTime( getString( columnIndex ) );
return DBTypeConversion::toDateTime( getString( columnIndex ) );
}
// LEM TODO: these look like they are missing an actual implementation
......
......@@ -54,6 +54,8 @@
#include <string.h>
#include <connectivity/dbconversion.hxx>
using osl::Mutex;
using osl::MutexGuard;
......@@ -89,6 +91,8 @@ using com::sun::star::beans::XPropertySet;
using com::sun::star::beans::XMultiPropertySet;
using com::sun::star::beans::XFastPropertySet;
using namespace dbtools;
namespace pq_sdbc_driver
{
static ::cppu::IPropertyArrayHelper & getPreparedStatementPropertyArrayHelper()
......@@ -597,20 +601,20 @@ void PreparedStatement::setBytes(
void PreparedStatement::setDate( sal_Int32 parameterIndex, const ::com::sun::star::util::Date& x )
throw (SQLException, RuntimeException)
{
setString( parameterIndex, date2String( x ) );
setString( parameterIndex, DBTypeConversion::toDateString( x ) );
}
void PreparedStatement::setTime( sal_Int32 parameterIndex, const ::com::sun::star::util::Time& x )
throw (SQLException, RuntimeException)
{
setString( parameterIndex, time2String( x ) );
setString( parameterIndex, DBTypeConversion::toTimeString( x ) );
}
void PreparedStatement::setTimestamp(
sal_Int32 parameterIndex, const ::com::sun::star::util::DateTime& x )
throw (SQLException, RuntimeException)
{
setString( parameterIndex, dateTime2String( x ) );
setString( parameterIndex, DBTypeConversion::toDateTimeString( x ) );
}
void PreparedStatement::setBinaryStream(
......
......@@ -54,7 +54,6 @@
#include <libpq-fe.h>
#include <string.h>
using com::sun::star::beans::XPropertySet;
using com::sun::star::lang::XComponent;
......@@ -84,110 +83,6 @@ using com::sun::star::container::XEnumerationAccess;
namespace pq_sdbc_driver
{
OUString date2String( const com::sun::star::util::Date & x )
{
// TODO FIXME: replace by DBTypeConversion::toDateString
char buffer[64];
sprintf( buffer, "%d-%02d-%02d", x.Year, x.Month, x.Day );
return OUString::createFromAscii( buffer );
}
com::sun::star::util::Date string2Date( const OUString &date )
{
// TODO FIXME: replace by DBTypeConversion::toDate (if it parses the same format)
// Format: Year-Month-Day
com::sun::star::util::Date ret;
ret.Year = (sal_Int32) rtl_ustr_toInt32( date.pData->buffer, 10 );
int index = date.indexOf( '-' );
if( index >= 0 )
{
ret.Month = (sal_Int32)rtl_ustr_toInt32( &(date.pData->buffer[ index+1]), 10 );
int start = index;
index = date.indexOf( '-', start+1 );
if( index >= 0 )
{
ret.Day = (sal_Int32)rtl_ustr_toInt32( &date.pData->buffer[index+1], 10 );
}
}
return ret;
}
OUString time2String( const com::sun::star::util::Time & x )
{
// TODO FIXME: replace by DBTypeConversion::toTimeString
const size_t buflen = 19;
char buffer[buflen];
snprintf( buffer, buflen, "%02d:%02d:%02d.%09" SAL_PRIuUINT32, x.Hours, x.Minutes, x.Seconds, x.NanoSeconds );
return OUString::createFromAscii( buffer );
}
com::sun::star::util::Time string2Time( const OUString & time )
{
// TODO FIXME: replace by DBTypeConversion::toTime
com::sun::star::util::Time ret;
sal_Unicode temp[4];
temp[0] = time[0];
temp[1] = time[1];
temp[2] = 0;
ret.Hours = (sal_Int32)rtl_ustr_toInt32( temp , 10 );
temp[0] = time[3];
temp[1] = time[4];
ret.Minutes = (sal_Int32)rtl_ustr_toInt32( temp , 10 );
temp[0] = time[6];
temp[1] = time[7];
ret.Seconds = (sal_Int32)rtl_ustr_toInt32( temp , 10 );
if( time.getLength() >9 )
{
// FIXME does not take into account shorter precision
ret.NanoSeconds = (sal_Int32)rtl_ustr_toInt32( &time.getStr()[9] , 10 );
}
return ret;
}
OUString dateTime2String( const com::sun::star::util::DateTime & x )
{
// TODO FIXME: replace by DBTypeConversion::toDateTimeString
char buffer[128];
sprintf( buffer, "%d-%02d-%02d %02d:%02d:%02d.%09" SAL_PRIuUINT32,
x.Year, x.Month, x.Day,
x.Hours, x.Minutes, x.Seconds, x.NanoSeconds );
return OUString::createFromAscii( buffer );
}
com::sun::star::util::DateTime string2DateTime( const OUString & dateTime )
{
// TODO FIXME: replace by DBTypeConversion::toDateTime (if same format)
int space = dateTime.indexOf( ' ' );
com::sun::star::util::DateTime ret;
if( space >= 0 )
{
com::sun::star::util::Date date ( string2Date( OUString( dateTime.getStr(), space ) ) );
com::sun::star::util::Time time( string2Time( OUString( dateTime.getStr() + space +1 ) ) );
ret.Day = date.Day;
ret.Month = date.Month;
ret.Year = date.Year;
ret.Hours = time.Hours;
ret.Minutes = time.Minutes;
ret.Seconds = time.Seconds;
ret.NanoSeconds = time.NanoSeconds;
}
return ret;
}
OUString concatQualified( const OUString & a, const OUString &b)
{
OUStringBuffer buf( a.getLength() + 2 + b.getLength() );
......
......@@ -51,15 +51,6 @@ namespace pq_sdbc_driver
{
bool isWhitespace( sal_Unicode c );
OUString date2String( const com::sun::star::util::Date & date );
com::sun::star::util::Date string2Date( const OUString & str );
OUString time2String( const com::sun::star::util::Time & time );
com::sun::star::util::Time string2Time( const OUString & str );
OUString dateTime2String( const com::sun::star::util::DateTime & dateTime );
com::sun::star::util::DateTime string2DateTime( const OUString & dateTime );
OUString concatQualified( const OUString & a, const OUString &b);
OString OUStringToOString( OUString str, ConnectionSettings *settings);
......
......@@ -49,6 +49,8 @@
#include <string.h>
#include <connectivity/dbconversion.hxx>
using osl::MutexGuard;
......@@ -75,6 +77,8 @@ using com::sun::star::beans::XFastPropertySet;
using com::sun::star::beans::XPropertySet;
using com::sun::star::beans::XMultiPropertySet;
using namespace dbtools;
namespace pq_sdbc_driver
{
......@@ -513,17 +517,17 @@ void UpdateableResultSet::updateBytes( sal_Int32 columnIndex, const ::com::sun::
void UpdateableResultSet::updateDate( sal_Int32 columnIndex, const ::com::sun::star::util::Date& x ) throw (SQLException, RuntimeException)
{
updateString( columnIndex, date2String( x ) );
updateString( columnIndex, DBTypeConversion::toDateString( x ) );
}
void UpdateableResultSet::updateTime( sal_Int32 columnIndex, const ::com::sun::star::util::Time& x ) throw (SQLException, RuntimeException)
{
updateString( columnIndex, time2String( x ) );
updateString( columnIndex, DBTypeConversion::toTimeString( x ) );
}
void UpdateableResultSet::updateTimestamp( sal_Int32 columnIndex, const ::com::sun::star::util::DateTime& x ) throw (SQLException, RuntimeException)
{
updateString( columnIndex, dateTime2String( x ) );
updateString( columnIndex, DBTypeConversion::toDateTimeString( x ) );
}
void UpdateableResultSet::updateBinaryStream( sal_Int32 /* columnIndex */, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& /* x */, sal_Int32 /* length */ ) throw (SQLException, RuntimeException)
......
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