OUString DBTypeConversion::toSQLString(sal_Int32 eType, const Any& _rVal, const Reference< XTypeConverter >& _rxTypeConverter)
{
OUStringBuffer aRet; if (_rVal.hasValue())
{ try
{ switch (eType)
{ case DataType::INTEGER: case DataType::BIT: case DataType::BOOLEAN: case DataType::TINYINT: case DataType::SMALLINT: if (_rVal.getValueTypeClass() == css::uno::TypeClass_BOOLEAN)
{ if (::cppu::any2bool(_rVal))
aRet.append("1"); else
aRet.append("0");
} else
{
OUString sTemp;
_rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
aRet.append(sTemp);
} break; case DataType::CHAR: case DataType::VARCHAR: case DataType::LONGVARCHAR:
aRet.append("'");
{
OUString aTemp;
_rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
aTemp = aTemp.replaceAll(u"\'", u"\'\'");
aRet.append(aTemp);
}
aRet.append("'"); break; case DataType::REAL: case DataType::DOUBLE: case DataType::DECIMAL: case DataType::NUMERIC: case DataType::BIGINT: default:
{
OUString sTemp;
_rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
aRet.append(sTemp);
} break; case DataType::TIMESTAMP:
{
DateTime aDateTime; bool bOk = false; if (_rVal.getValueTypeClass() == css::uno::TypeClass_DOUBLE)
{ double nValue = 0.0;
_rVal >>= nValue;
aDateTime = DBTypeConversion::toDateTime(nValue);
bOk = true;
} elseif (_rVal.getValueTypeClass() == css::uno::TypeClass_STRING)
{
OUString sValue;
_rVal >>= sValue;
aDateTime = DBTypeConversion::toDateTime(sValue);
bOk = true;
} else
bOk = _rVal >>= aDateTime;
OSL_ENSURE( bOk, "DBTypeConversion::toSQLString: _rVal is not datetime!"); // check if this is really a timestamp or only a date if ( bOk )
{
aRet.append("{ts '"
+ DBTypeConversion::toDateTimeString(aDateTime)
+ "'}"); break;
} break;
} case DataType::DATE:
{
Date aDate; bool bOk = false; if (_rVal.getValueTypeClass() == css::uno::TypeClass_DOUBLE)
{ double nValue = 0.0;
_rVal >>= nValue;
aDate = DBTypeConversion::toDate(nValue);
bOk = true;
} elseif (_rVal.getValueTypeClass() == css::uno::TypeClass_STRING)
{
OUString sValue;
_rVal >>= sValue;
aDate = DBTypeConversion::toDate(sValue);
bOk = true;
} else
bOk = _rVal >>= aDate;
OSL_ENSURE( bOk, "DBTypeConversion::toSQLString: _rVal is not date!");
aRet.append("{d '"
+ DBTypeConversion::toDateString(aDate)
+ "'}");
} break; case DataType::TIME:
{
css::util::Time aTime; bool bOk = false; if (_rVal.getValueTypeClass() == css::uno::TypeClass_DOUBLE)
{ double nValue = 0.0;
_rVal >>= nValue;
aTime = DBTypeConversion::toTime(nValue);
bOk = true;
} elseif (_rVal.getValueTypeClass() == css::uno::TypeClass_STRING)
{
OUString sValue;
_rVal >>= sValue;
aTime = DBTypeConversion::toTime(sValue);
bOk = true;
} else
bOk = _rVal >>= aTime;
OSL_ENSURE( bOk,"DBTypeConversion::toSQLString: _rVal is not time!");
aRet.append("{t '"
+ DBTypeConversion::toTimeString(aTime)
+ "'}");
} break;
}
} catch ( const Exception& )
{
OSL_FAIL("TypeConversion Error");
}
} else
aRet.append(" NULL "); return aRet.makeStringAndClear();
}
Date DBTypeConversion::getNULLDate(const Reference< XNumberFormatsSupplier > &xSupplier)
{
OSL_ENSURE(xSupplier.is(), "getNULLDate : the formatter doesn't implement a supplier !"); if (xSupplier.is())
{ try
{ // get the null date
Date aDate;
xSupplier->getNumberFormatSettings()->getPropertyValue(u"NullDate"_ustr) >>= aDate; return aDate;
} catch ( const Exception& )
{
}
}
return getStandardDate();
}
void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant, const Reference<XNumberFormatter>& xFormatter, const Date& rNullDate, const OUString& rString,
sal_Int32 nKey,
sal_Int16 nFieldType,
sal_Int16 nKeyType)
{ if (!rString.isEmpty())
{ // Does the String need to be formatted?
sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED; bool bTextFormat = nTypeClass == NumberFormat::TEXT;
sal_Int32 nKeyToUse = bTextFormat ? 0 : nKey;
sal_Int16 nRealUsedTypeClass = nTypeClass; // for a Text-Format the formatter needs some more freedom, otherwise // convertStringToNumber will throw a NotNumericException try
{ double fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
Reference< XNumberFormats > xFormats(xFormatter->getNumberFormatsSupplier()->getNumberFormats());
Reference< XNumberFormatTypes > xFormatTypes(xFormats, UNO_QUERY);
sal_Int32 nStandardKey(0); if(xFormatTypes.is())
{ const Reference< XPropertySet > xFormatProps(xFormats->getByKey(nKeyToUse)); if (xFormatProps.is())
{
css::lang::Locale loc; if (xFormatProps->getPropertyValue(u"Locale"_ustr) >>= loc)
nStandardKey = xFormatTypes->getStandardIndex(loc); else
{
assert(false);
}
} else
{
SAL_WARN("connectivity.commontools", "no format by key " << nKeyToUse);
}
} else
{
assert(false);
} // Why use nStandardKey rather than nKeyToUse here? I'm not sure, but "it was always like that". // Previously had hardcoded 0 instead of nStandardKey, which led to problems with dates // because of differences M/D/Y vs D/M/Y. This at least fixes those problems, but possibly // nKeyToUse is an even better choice than nStandardKey. // OTOH, using nKeyToUse nullifies the special treatment for percent formats, // leading to "5" (in a percent format) to be understood as "500%" instead of "5%".
sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(nStandardKey, rString); if (nRealUsedKey != nKeyToUse)
nRealUsedTypeClass = getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;
// and again a special treatment, this time for percent formats if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
{ // formatting should be "percent", but the String provides just a simple number -> adjust
OUString sExpanded = rString + "%";
fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
}
switch (nRealUsedTypeClass)
{ case NumberFormat::DATE: case NumberFormat::DATETIME: case NumberFormat::TIME:
DBTypeConversion::setValue(xVariant,rNullDate,fValue,nRealUsedTypeClass); break; case NumberFormat::CURRENCY: case NumberFormat::NUMBER: case NumberFormat::SCIENTIFIC: case NumberFormat::FRACTION: case NumberFormat::PERCENT:
xVariant->updateDouble(fValue); break; default:
xVariant->updateString(rString);
}
} catch(const Exception& )
{
xVariant->updateString(rString);
}
} else
{ switch (nFieldType)
{ case css::sdbc::DataType::CHAR: case css::sdbc::DataType::VARCHAR: case css::sdbc::DataType::LONGVARCHAR:
xVariant->updateString(rString); break; default:
xVariant->updateNull();
}
}
}
OUString DBTypeConversion::getFormattedValue(const Reference<XColumn>& xVariant, const Reference<XNumberFormatter>& xFormatter, const Date& rNullDate,
sal_Int32 nKey,
sal_Int16 nKeyType)
{
OUString aString; if (xVariant.is())
{ try
{ switch (nKeyType & ~NumberFormat::DEFINED)
{ case NumberFormat::DATE: case NumberFormat::DATETIME:
{ // get a value which represents the given date, relative to the given null date double fValue = getValue( xVariant, rNullDate ); if ( !xVariant->wasNull() )
{ // get the null date of the formatter
Date aFormatterNullDate( rNullDate ); try
{
Reference< XNumberFormatsSupplier > xSupplier( xFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
Reference< XPropertySet > xFormatterSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
OSL_VERIFY( xFormatterSettings->getPropertyValue(u"NullDate"_ustr) >>= aFormatterNullDate );
} catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
} // get a value which represents the given date, relative to the null date of the formatter
fValue -= toDays( rNullDate, aFormatterNullDate ); // format this value
aString = xFormatter->convertNumberToString( nKey, fValue );
}
} break; case NumberFormat::TIME: case NumberFormat::NUMBER: case NumberFormat::SCIENTIFIC: case NumberFormat::FRACTION: case NumberFormat::PERCENT:
{ double fValue = xVariant->getDouble(); if (!xVariant->wasNull())
aString = xFormatter->convertNumberToString(nKey, fValue);
} break; case NumberFormat::CURRENCY:
{ double fValue = xVariant->getDouble(); if (!xVariant->wasNull())
aString = xFormatter->getInputString(nKey, fValue);
} break; case NumberFormat::TEXT:
aString = xFormatter->formatString(nKey, xVariant->getString()); break; default:
aString = xVariant->getString();
}
} catch(const Exception& )
{
aString = xVariant->getString();
}
} return aString;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.