22#include <com/sun/star/script/XTypeConverter.hpp>
23#include <com/sun/star/sdbc/DataType.hpp>
24#include <com/sun/star/util/NumberFormat.hpp>
25#include <com/sun/star/util/XNumberFormatter.hpp>
26#include <com/sun/star/util/XNumberFormatTypes.hpp>
27#include <com/sun/star/sdb/XColumnUpdate.hpp>
28#include <com/sun/star/sdb/XColumn.hpp>
29#include <com/sun/star/beans/XPropertySet.hpp>
34#include <rtl/ustrbuf.hxx>
51 const Reference< XTypeConverter >& _rxTypeConverter)
60 case DataType::INTEGER:
62 case DataType::BOOLEAN:
63 case DataType::TINYINT:
64 case DataType::SMALLINT:
65 if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_BOOLEAN)
67 if (::cppu::any2bool(_rVal))
75 _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
80 case DataType::VARCHAR:
81 case DataType::LONGVARCHAR:
85 _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
86 aTemp = aTemp.replaceAll(u
"\'", u
"\'\'");
92 case DataType::DOUBLE:
93 case DataType::DECIMAL:
94 case DataType::NUMERIC:
95 case DataType::BIGINT:
99 _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
103 case DataType::TIMESTAMP:
107 if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_DOUBLE)
114 else if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_STRING)
122 bOk = _rVal >>= aDateTime;
124 OSL_ENSURE( bOk,
"DBTypeConversion::toSQLString: _rVal is not datetime!");
139 if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_DOUBLE)
146 else if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_STRING)
154 bOk = _rVal >>= aDate;
155 OSL_ENSURE( bOk,
"DBTypeConversion::toSQLString: _rVal is not date!");
162 css::util::Time aTime;
164 if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_DOUBLE)
171 else if (_rVal.getValueType().getTypeClass() == css::uno::TypeClass_STRING)
179 bOk = _rVal >>= aTime;
180 OSL_ENSURE( bOk,
"DBTypeConversion::toSQLString: _rVal is not time!");
187 catch (
const Exception& )
189 OSL_FAIL(
"TypeConversion Error");
193 aRet.append(
" NULL ");
194 return aRet.makeStringAndClear();
199 OSL_ENSURE(xSupplier.is(),
"getNULLDate : the formatter doesn't implement a supplier !");
206 xSupplier->getNumberFormatSettings()->getPropertyValue(
"NullDate") >>= aDate;
209 catch (
const Exception& )
218 const Reference<XNumberFormatter>& xFormatter,
219 const Date& rNullDate,
220 const OUString& rString,
222 sal_Int16 nFieldType,
225 if (!rString.isEmpty())
228 sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED;
229 bool bTextFormat = nTypeClass == NumberFormat::TEXT;
230 sal_Int32 nKeyToUse = bTextFormat ? 0 : nKey;
231 sal_Int16 nRealUsedTypeClass = nTypeClass;
236 double fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
237 Reference< XNumberFormats > xFormats(xFormatter->getNumberFormatsSupplier()->getNumberFormats());
238 Reference< XNumberFormatTypes > xFormatTypes(xFormats, UNO_QUERY);
239 sal_Int32 nStandardKey(0);
240 if(xFormatTypes.is())
242 const Reference< XPropertySet > xFormatProps(xFormats->getByKey(nKeyToUse));
243 if (xFormatProps.is())
245 css::lang::Locale loc;
246 if (xFormatProps->getPropertyValue(
"Locale") >>= loc)
247 nStandardKey = xFormatTypes->getStandardIndex(loc);
255 SAL_WARN(
"connectivity.commontools",
"no format by key " << nKeyToUse);
268 sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(nStandardKey, rString);
269 if (nRealUsedKey != nKeyToUse)
270 nRealUsedTypeClass =
getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;
273 if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
275 OUString sExpanded = rString +
"%";
276 fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
279 switch (nRealUsedTypeClass)
281 case NumberFormat::DATE:
282 case NumberFormat::DATETIME:
283 case NumberFormat::TIME:
286 case NumberFormat::CURRENCY:
287 case NumberFormat::NUMBER:
288 case NumberFormat::SCIENTIFIC:
289 case NumberFormat::FRACTION:
290 case NumberFormat::PERCENT:
291 xVariant->updateDouble(fValue);
294 xVariant->updateString(rString);
297 catch(
const Exception& )
299 xVariant->updateString(rString);
306 case css::sdbc::DataType::CHAR:
307 case css::sdbc::DataType::VARCHAR:
308 case css::sdbc::DataType::LONGVARCHAR:
309 xVariant->updateString(rString);
312 xVariant->updateNull();
319 const Date& rNullDate,
320 const double& rValue,
323 switch (nKeyType & ~NumberFormat::DEFINED)
325 case NumberFormat::DATE:
326 xVariant->updateDate(
toDate( rValue, rNullDate));
328 case NumberFormat::DATETIME:
329 xVariant->updateTimestamp(
toDateTime(rValue,rNullDate));
331 case NumberFormat::TIME:
332 xVariant->updateTime(
toTime(rValue));
358 xVariant->updateDouble(nValue);
368 const Reference< XPropertySet > xProp( i_column, UNO_QUERY_THROW );
370 const sal_Int32 nColumnType = ::comphelper::getINT32( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(
PROPERTY_ID_TYPE ) ) );
371 switch ( nColumnType )
374 return toDouble( i_column->getDate(), i_relativeToNullDate );
377 return toDouble( i_column->getTime() );
379 case DataType::TIMESTAMP:
380 return toDouble( i_column->getTimestamp(), i_relativeToNullDate );
384 bool bIsSigned =
true;
385 OSL_VERIFY( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(
PROPERTY_ID_ISSIGNED ) ) >>= bIsSigned );
388 switch ( nColumnType)
390 case DataType::TINYINT:
391 return static_cast<double>(
static_cast<sal_uInt8>(i_column->getByte()));
392 case DataType::SMALLINT:
393 return static_cast<double>(
static_cast<sal_uInt16
>(i_column->getShort()));
394 case DataType::INTEGER:
395 return static_cast<double>(
static_cast<sal_uInt32
>(i_column->getInt()));
396 case DataType::BIGINT:
397 return static_cast<double>(
static_cast<sal_uInt64
>(i_column->getLong()));
401 return i_column->getDouble();
404 catch(
const Exception& )
412 const Reference<XNumberFormatter>& _xFormatter,
413 const css::lang::Locale& _rLocale,
414 const Date& _rNullDate)
416 OSL_ENSURE(_xColumn.is() && _xFormatter.is(),
"DBTypeConversion::getFormattedValue: invalid arg !");
417 if (!_xColumn.is() || !_xFormatter.is())
423 _xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(
PROPERTY_ID_FORMATKEY)) >>= nKey;
425 catch (
const Exception& )
427 TOOLS_WARN_EXCEPTION(
"connectivity.commontools",
"DBTypeConversion::getValue: caught an exception while asking for the format key!");
432 Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
435 Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
447 const Reference<XNumberFormatter>& xFormatter,
448 const Date& rNullDate,
457 switch (nKeyType & ~NumberFormat::DEFINED)
459 case NumberFormat::DATE:
460 case NumberFormat::DATETIME:
463 double fValue =
getValue( xVariant, rNullDate );
464 if ( !xVariant->wasNull() )
467 Date aFormatterNullDate( rNullDate );
470 Reference< XNumberFormatsSupplier > xSupplier( xFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
471 Reference< XPropertySet > xFormatterSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
472 OSL_VERIFY( xFormatterSettings->getPropertyValue(
"NullDate") >>= aFormatterNullDate );
474 catch(
const Exception& )
479 fValue -=
toDays( rNullDate, aFormatterNullDate );
481 aString = xFormatter->convertNumberToString( nKey, fValue );
485 case NumberFormat::TIME:
486 case NumberFormat::NUMBER:
487 case NumberFormat::SCIENTIFIC:
488 case NumberFormat::FRACTION:
489 case NumberFormat::PERCENT:
491 double fValue = xVariant->getDouble();
492 if (!xVariant->wasNull())
493 aString = xFormatter->convertNumberToString(nKey, fValue);
495 case NumberFormat::CURRENCY:
497 double fValue = xVariant->getDouble();
498 if (!xVariant->wasNull())
499 aString = xFormatter->getInputString(nKey, fValue);
501 case NumberFormat::TEXT:
502 aString = xFormatter->formatString(nKey, xVariant->getString());
505 aString = xVariant->getString();
508 catch(
const Exception& )
510 aString = xVariant->getString();
static double toDouble(std::string_view rString)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
sal_Int16 getNumberFormatType(const css::uno::Reference< css::util::XNumberFormats > &xFormats, sal_Int32 nKey)
static bool getValue(EContact *pContact, sal_Int32 nColumnNum, GType nType, GValue *pStackValue, bool &_out_rWasNull)
#define PROPERTY_ID_ISSIGNED
#define PROPERTY_ID_FORMATKEY