Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "connectivity/dbconversion.hxx"
21 : #include <connectivity/dbtools.hxx>
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/XNumberFormatTypes.hpp>
26 : #include <com/sun/star/sdb/XColumnUpdate.hpp>
27 : #include <com/sun/star/sdb/XColumn.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <comphelper/extract.hxx>
30 : #include "TConnection.hxx"
31 : #include "diagnose_ex.h"
32 : #include <comphelper/numbers.hxx>
33 : #include <rtl/ustrbuf.hxx>
34 : #include <tools/diagnose_ex.h>
35 :
36 :
37 : using namespace ::connectivity;
38 : using namespace ::comphelper;
39 : using namespace ::com::sun::star::script;
40 : using namespace ::com::sun::star::sdb;
41 : using namespace ::com::sun::star::sdbc;
42 : using namespace ::dbtools;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::com::sun::star::beans;
45 : using namespace ::com::sun::star::util;
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::util;
48 : using namespace ::com::sun::star::beans;
49 : // -----------------------------------------------------------------------------
50 0 : ::rtl::OUString DBTypeConversion::toSQLString(sal_Int32 eType, const Any& _rVal, sal_Bool bQuote,
51 : const Reference< XTypeConverter >& _rxTypeConverter)
52 : {
53 0 : ::rtl::OUStringBuffer aRet;
54 0 : if (_rVal.hasValue())
55 : {
56 : try
57 : {
58 0 : switch (eType)
59 : {
60 : case DataType::INTEGER:
61 : case DataType::BIT:
62 : case DataType::BOOLEAN:
63 : case DataType::TINYINT:
64 : case DataType::SMALLINT:
65 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
66 : {
67 0 : if (::cppu::any2bool(_rVal))
68 0 : aRet.appendAscii("1");
69 : else
70 0 : aRet.appendAscii("0");
71 : }
72 : else
73 : {
74 0 : ::rtl::OUString sTemp;
75 0 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
76 0 : aRet.append(sTemp);
77 : }
78 0 : break;
79 : case DataType::CHAR:
80 : case DataType::VARCHAR:
81 : case DataType::LONGVARCHAR:
82 0 : if (bQuote)
83 0 : aRet.appendAscii("'");
84 : {
85 0 : ::rtl::OUString aTemp;
86 0 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
87 0 : sal_Int32 nIndex = (sal_Int32)-1;
88 0 : const ::rtl::OUString sQuot(RTL_CONSTASCII_USTRINGPARAM("\'"));
89 0 : const ::rtl::OUString sQuotToReplace(RTL_CONSTASCII_USTRINGPARAM("\'\'"));
90 0 : do
91 : {
92 0 : nIndex += 2;
93 0 : nIndex = aTemp.indexOf(sQuot,nIndex);
94 0 : if(nIndex != -1)
95 0 : aTemp = aTemp.replaceAt(nIndex,sQuot.getLength(),sQuotToReplace);
96 : } while (nIndex != -1);
97 :
98 0 : aRet.append(aTemp);
99 : }
100 0 : if (bQuote)
101 0 : aRet.appendAscii("'");
102 0 : break;
103 : case DataType::REAL:
104 : case DataType::DOUBLE:
105 : case DataType::DECIMAL:
106 : case DataType::NUMERIC:
107 : case DataType::BIGINT:
108 : default:
109 : {
110 0 : ::rtl::OUString sTemp;
111 0 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
112 0 : aRet.append(sTemp);
113 : }
114 0 : break;
115 : case DataType::TIMESTAMP:
116 : {
117 0 : DateTime aDateTime;
118 0 : bool bOk = false;
119 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
120 : {
121 0 : double nValue = 0.0;
122 0 : _rVal >>= nValue;
123 0 : aDateTime = DBTypeConversion::toDateTime(nValue);
124 0 : bOk = true;
125 : }
126 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
127 : {
128 0 : ::rtl::OUString sValue;
129 0 : _rVal >>= sValue;
130 0 : aDateTime = DBTypeConversion::toDateTime(sValue);
131 0 : bOk = true;
132 : }
133 : else
134 0 : bOk = _rVal >>= aDateTime;
135 :
136 : OSL_VERIFY_RES( bOk, "DBTypeConversion::toSQLString: _rVal is not datetime!");
137 : // check if this is really a timestamp or only a date
138 0 : if ( bOk )
139 : {
140 0 : if (bQuote)
141 0 : aRet.appendAscii("{TS '");
142 0 : aRet.append(DBTypeConversion::toDateTimeString(aDateTime));
143 0 : if (bQuote)
144 0 : aRet.appendAscii("'}");
145 : break;
146 : }
147 : break;
148 : }
149 : case DataType::DATE:
150 : {
151 0 : Date aDate;
152 0 : bool bOk = false;
153 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
154 : {
155 0 : double nValue = 0.0;
156 0 : _rVal >>= nValue;
157 0 : aDate = DBTypeConversion::toDate(nValue);
158 0 : bOk = true;
159 : }
160 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
161 : {
162 0 : ::rtl::OUString sValue;
163 0 : _rVal >>= sValue;
164 0 : aDate = DBTypeConversion::toDate(sValue);
165 0 : bOk = true;
166 : }
167 : else
168 0 : bOk = _rVal >>= aDate;
169 : OSL_VERIFY_RES( bOk, "DBTypeConversion::toSQLString: _rVal is not date!");
170 0 : if (bQuote)
171 0 : aRet.appendAscii("{D '");
172 0 : aRet.append(DBTypeConversion::toDateString(aDate));
173 0 : if (bQuote)
174 0 : aRet.appendAscii("'}");
175 0 : } break;
176 : case DataType::TIME:
177 : {
178 0 : Time aTime;
179 0 : bool bOk = false;
180 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
181 : {
182 0 : double nValue = 0.0;
183 0 : _rVal >>= nValue;
184 0 : aTime = DBTypeConversion::toTime(nValue);
185 0 : bOk = true;
186 : }
187 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
188 : {
189 0 : ::rtl::OUString sValue;
190 0 : _rVal >>= sValue;
191 0 : aTime = DBTypeConversion::toTime(sValue);
192 0 : bOk = true;
193 : }
194 : else
195 0 : bOk = _rVal >>= aTime;
196 : OSL_VERIFY_RES( bOk,"DBTypeConversion::toSQLString: _rVal is not time!");
197 0 : if (bQuote)
198 0 : aRet.appendAscii("{T '");
199 0 : aRet.append(DBTypeConversion::toTimeString(aTime));
200 0 : if (bQuote)
201 0 : aRet.appendAscii("'}");
202 0 : } break;
203 : }
204 : }
205 0 : catch ( const Exception& )
206 : {
207 : OSL_FAIL("TypeConversion Error");
208 : }
209 : }
210 : else
211 0 : aRet.appendAscii(" NULL ");
212 0 : return aRet.makeStringAndClear();
213 : }
214 : // -----------------------------------------------------------------------------
215 0 : Date DBTypeConversion::getNULLDate(const Reference< XNumberFormatsSupplier > &xSupplier)
216 : {
217 : OSL_ENSURE(xSupplier.is(), "getNULLDate : the formatter doesn't implement a supplier !");
218 0 : if (xSupplier.is())
219 : {
220 : try
221 : {
222 : // get the null date
223 0 : Date aDate;
224 0 : xSupplier->getNumberFormatSettings()->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("NullDate"))) >>= aDate;
225 0 : return aDate;
226 : }
227 0 : catch ( const Exception& )
228 : {
229 : }
230 : }
231 :
232 0 : return getStandardDate();
233 : }
234 : // -----------------------------------------------------------------------------
235 0 : void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
236 : const Reference<XNumberFormatter>& xFormatter,
237 : const Date& rNullDate,
238 : const ::rtl::OUString& rString,
239 : sal_Int32 nKey,
240 : sal_Int16 nFieldType,
241 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
242 : {
243 0 : if (!rString.isEmpty())
244 : {
245 : // Does the String need to be formatted?
246 0 : sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED;
247 0 : sal_Bool bTextFormat = nTypeClass == NumberFormat::TEXT;
248 0 : sal_Int32 nKeyToUse = bTextFormat ? 0 : nKey;
249 0 : sal_Int16 nRealUsedTypeClass = nTypeClass;
250 : // for a Text-Format the formatter needs some more freedom, otherwise
251 : // convertStringToNumber will throw a NotNumericException
252 : try
253 : {
254 0 : double fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
255 0 : sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(0, rString);
256 0 : if (nRealUsedKey != nKeyToUse)
257 0 : nRealUsedTypeClass = getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;
258 :
259 : // and again a special treatment, this time for percent formats
260 0 : if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
261 : { // formatting should be "percent", but the String provides just a simple number -> adjust
262 0 : ::rtl::OUString sExpanded(rString);
263 0 : static ::rtl::OUString s_sPercentSymbol( RTL_CONSTASCII_USTRINGPARAM( "%" ));
264 : // need a method to add a sal_Unicode to a string, 'til then we use a static string
265 0 : sExpanded += s_sPercentSymbol;
266 0 : fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
267 : }
268 :
269 0 : switch (nRealUsedTypeClass)
270 : {
271 : case NumberFormat::DATE:
272 : case NumberFormat::DATETIME:
273 : case NumberFormat::TIME:
274 0 : DBTypeConversion::setValue(xVariant,rNullDate,fValue,nRealUsedTypeClass);
275 : // xVariant->updateDouble(toStandardDbDate(rNullDate, fValue));
276 0 : break;
277 : case NumberFormat::CURRENCY:
278 : case NumberFormat::NUMBER:
279 : case NumberFormat::SCIENTIFIC:
280 : case NumberFormat::FRACTION:
281 : case NumberFormat::PERCENT:
282 0 : xVariant->updateDouble(fValue);
283 0 : break;
284 : default:
285 0 : xVariant->updateString(rString);
286 : }
287 : }
288 0 : catch(const Exception& )
289 : {
290 0 : xVariant->updateString(rString);
291 : }
292 : }
293 : else
294 : {
295 0 : switch (nFieldType)
296 : {
297 : case ::com::sun::star::sdbc::DataType::CHAR:
298 : case ::com::sun::star::sdbc::DataType::VARCHAR:
299 : case ::com::sun::star::sdbc::DataType::LONGVARCHAR:
300 0 : xVariant->updateString(rString);
301 0 : break;
302 : default:
303 0 : xVariant->updateNull();
304 : }
305 : }
306 0 : }
307 :
308 : //------------------------------------------------------------------------------
309 0 : void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
310 : const Date& rNullDate,
311 : const double& rValue,
312 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
313 : {
314 0 : switch (nKeyType & ~NumberFormat::DEFINED)
315 : {
316 : case NumberFormat::DATE:
317 0 : xVariant->updateDate(toDate( rValue, rNullDate));
318 0 : break;
319 : case NumberFormat::DATETIME:
320 0 : xVariant->updateTimestamp(toDateTime(rValue,rNullDate));
321 0 : break;
322 : case NumberFormat::TIME:
323 0 : xVariant->updateTime(toTime(rValue));
324 0 : break;
325 : default:
326 : {
327 0 : double nValue = rValue;
328 : // Reference<XPropertySet> xProp(xVariant,UNO_QUERY);
329 : // if ( xProp.is()
330 : // && xProp->getPropertySetInfo()->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))
331 : // && !::comphelper::getBOOL(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))) )
332 : // {
333 : // switch (::comphelper::getINT32(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
334 : // {
335 : // case DataType::TINYINT:
336 : // nValue = static_cast<sal_uInt8>(rValue);
337 : // break;
338 : // case DataType::SMALLINT:
339 : // nValue = static_cast<sal_uInt16>(rValue);
340 : // break;
341 : // case DataType::INTEGER:
342 : // nValue = static_cast<sal_uInt32>(rValue);
343 : // break;
344 : // case DataType::BIGINT:
345 : // nValue = static_cast<sal_uInt64>(rValue);
346 : // break;
347 : // }
348 : // }
349 0 : xVariant->updateDouble(nValue);
350 : }
351 : }
352 0 : }
353 :
354 : //------------------------------------------------------------------------------
355 0 : double DBTypeConversion::getValue( const Reference< XColumn >& i_column, const Date& i_relativeToNullDate )
356 : {
357 : try
358 : {
359 0 : const Reference< XPropertySet > xProp( i_column, UNO_QUERY_THROW );
360 :
361 0 : const sal_Int32 nColumnType = ::comphelper::getINT32( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_TYPE ) ) );
362 0 : switch ( nColumnType )
363 : {
364 : case DataType::DATE:
365 0 : return toDouble( i_column->getDate(), i_relativeToNullDate );
366 :
367 : case DataType::TIME:
368 0 : return toDouble( i_column->getTime() );
369 :
370 : case DataType::TIMESTAMP:
371 0 : return toDouble( i_column->getTimestamp(), i_relativeToNullDate );
372 :
373 : default:
374 : {
375 0 : sal_Bool bIsSigned = sal_True;
376 0 : OSL_VERIFY( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ISSIGNED ) ) >>= bIsSigned );
377 0 : if ( !bIsSigned )
378 : {
379 0 : switch ( nColumnType)
380 : {
381 : case DataType::TINYINT:
382 0 : return static_cast<double>(static_cast<sal_uInt8>(i_column->getByte()));
383 : case DataType::SMALLINT:
384 0 : return static_cast<double>(static_cast<sal_uInt16>(i_column->getShort()));
385 : case DataType::INTEGER:
386 0 : return static_cast<double>(static_cast<sal_uInt32>(i_column->getInt()));
387 : case DataType::BIGINT:
388 0 : return static_cast<double>(static_cast<sal_uInt64>(i_column->getLong()));
389 : }
390 : }
391 : }
392 0 : return i_column->getDouble();
393 0 : }
394 : }
395 0 : catch( const Exception& )
396 : {
397 : DBG_UNHANDLED_EXCEPTION();
398 0 : return 0.0;
399 : }
400 : }
401 : //------------------------------------------------------------------------------
402 0 : ::rtl::OUString DBTypeConversion::getFormattedValue(const Reference< XPropertySet>& _xColumn,
403 : const Reference<XNumberFormatter>& _xFormatter,
404 : const ::com::sun::star::lang::Locale& _rLocale,
405 : const Date& _rNullDate)
406 : {
407 : OSL_ENSURE(_xColumn.is() && _xFormatter.is(), "DBTypeConversion::getFormattedValue: invalid arg !");
408 0 : if (!_xColumn.is() || !_xFormatter.is())
409 0 : return ::rtl::OUString();
410 :
411 0 : sal_Int32 nKey(0);
412 : try
413 : {
414 0 : _xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= nKey;
415 : }
416 0 : catch (const Exception& )
417 : {
418 : OSL_FAIL("DBTypeConversion::getValue: caught an exception while asking for the format key!");
419 : }
420 :
421 0 : if (!nKey)
422 : {
423 0 : Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
424 0 : Reference<XNumberFormatTypes> xTypeList(_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), UNO_QUERY);
425 :
426 : nKey = ::dbtools::getDefaultNumberFormat(_xColumn,
427 : Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
428 0 : _rLocale);
429 :
430 : }
431 :
432 0 : sal_Int16 nKeyType = getNumberFormatType(_xFormatter, nKey) & ~NumberFormat::DEFINED;
433 :
434 0 : return DBTypeConversion::getFormattedValue(Reference< XColumn > (_xColumn, UNO_QUERY), _xFormatter, _rNullDate, nKey, nKeyType);
435 : }
436 :
437 : //------------------------------------------------------------------------------
438 0 : ::rtl::OUString DBTypeConversion::getFormattedValue(const Reference<XColumn>& xVariant,
439 : const Reference<XNumberFormatter>& xFormatter,
440 : const Date& rNullDate,
441 : sal_Int32 nKey,
442 : sal_Int16 nKeyType)
443 : {
444 0 : ::rtl::OUString aString;
445 0 : if (xVariant.is())
446 : {
447 : try
448 : {
449 0 : switch (nKeyType & ~NumberFormat::DEFINED)
450 : {
451 : case NumberFormat::DATE:
452 : case NumberFormat::DATETIME:
453 : {
454 : // get a value which represents the given date, relative to the given null date
455 0 : double fValue = getValue( xVariant, rNullDate );
456 0 : if ( !xVariant->wasNull() )
457 : {
458 : // get the null date of the formatter
459 0 : Date aFormatterNullDate( rNullDate );
460 : try
461 : {
462 0 : Reference< XNumberFormatsSupplier > xSupplier( xFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
463 0 : Reference< XPropertySet > xFormatterSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
464 0 : OSL_VERIFY( xFormatterSettings->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NullDate" ) ) ) >>= aFormatterNullDate );
465 : }
466 0 : catch( const Exception& )
467 : {
468 : DBG_UNHANDLED_EXCEPTION();
469 : }
470 : // get a value which represents the given date, relative to the null date of the formatter
471 0 : fValue -= toDays( rNullDate, aFormatterNullDate );
472 : // format this value
473 0 : aString = xFormatter->convertNumberToString( nKey, fValue );
474 : }
475 : }
476 0 : break;
477 : case NumberFormat::TIME:
478 : case NumberFormat::NUMBER:
479 : case NumberFormat::SCIENTIFIC:
480 : case NumberFormat::FRACTION:
481 : case NumberFormat::PERCENT:
482 : {
483 0 : double fValue = xVariant->getDouble();
484 0 : if (!xVariant->wasNull())
485 0 : aString = xFormatter->convertNumberToString(nKey, fValue);
486 0 : } break;
487 : case NumberFormat::CURRENCY:
488 : {
489 0 : double fValue = xVariant->getDouble();
490 0 : if (!xVariant->wasNull())
491 0 : aString = xFormatter->getInputString(nKey, fValue);
492 0 : } break;
493 : case NumberFormat::TEXT:
494 0 : aString = xFormatter->formatString(nKey, xVariant->getString());
495 0 : break;
496 : default:
497 0 : aString = xVariant->getString();
498 : }
499 : }
500 0 : catch(const Exception& )
501 : {
502 0 : aString = xVariant->getString();
503 : }
504 : }
505 0 : return aString;
506 : }
507 : //------------------------------------------------------------------
508 :
509 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|