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 :
48 6 : OUString DBTypeConversion::toSQLString(sal_Int32 eType, const Any& _rVal, bool bQuote,
49 : const Reference< XTypeConverter >& _rxTypeConverter)
50 : {
51 6 : OUStringBuffer aRet;
52 6 : if (_rVal.hasValue())
53 : {
54 : try
55 : {
56 6 : switch (eType)
57 : {
58 : case DataType::INTEGER:
59 : case DataType::BIT:
60 : case DataType::BOOLEAN:
61 : case DataType::TINYINT:
62 : case DataType::SMALLINT:
63 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
64 : {
65 0 : if (::cppu::any2bool(_rVal))
66 0 : aRet.appendAscii("1");
67 : else
68 0 : aRet.appendAscii("0");
69 : }
70 : else
71 : {
72 0 : OUString sTemp;
73 0 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
74 0 : aRet.append(sTemp);
75 : }
76 0 : break;
77 : case DataType::CHAR:
78 : case DataType::VARCHAR:
79 : case DataType::LONGVARCHAR:
80 6 : if (bQuote)
81 6 : aRet.appendAscii("'");
82 : {
83 6 : OUString aTemp;
84 6 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
85 6 : sal_Int32 nIndex = (sal_Int32)-1;
86 12 : const OUString sQuot("\'");
87 12 : const OUString sQuotToReplace("\'\'");
88 6 : do
89 : {
90 6 : nIndex += 2;
91 6 : nIndex = aTemp.indexOf(sQuot,nIndex);
92 6 : if(nIndex != -1)
93 0 : aTemp = aTemp.replaceAt(nIndex,sQuot.getLength(),sQuotToReplace);
94 : } while (nIndex != -1);
95 :
96 12 : aRet.append(aTemp);
97 : }
98 6 : if (bQuote)
99 6 : aRet.appendAscii("'");
100 6 : break;
101 : case DataType::REAL:
102 : case DataType::DOUBLE:
103 : case DataType::DECIMAL:
104 : case DataType::NUMERIC:
105 : case DataType::BIGINT:
106 : default:
107 : {
108 0 : OUString sTemp;
109 0 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= sTemp;
110 0 : aRet.append(sTemp);
111 : }
112 0 : break;
113 : case DataType::TIMESTAMP:
114 : {
115 0 : DateTime aDateTime;
116 0 : bool bOk = false;
117 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
118 : {
119 0 : double nValue = 0.0;
120 0 : _rVal >>= nValue;
121 0 : aDateTime = DBTypeConversion::toDateTime(nValue);
122 0 : bOk = true;
123 : }
124 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
125 : {
126 0 : OUString sValue;
127 0 : _rVal >>= sValue;
128 0 : aDateTime = DBTypeConversion::toDateTime(sValue);
129 0 : bOk = true;
130 : }
131 : else
132 0 : bOk = _rVal >>= aDateTime;
133 :
134 : OSL_VERIFY_RES( bOk, "DBTypeConversion::toSQLString: _rVal is not datetime!");
135 : // check if this is really a timestamp or only a date
136 0 : if ( bOk )
137 : {
138 0 : if (bQuote)
139 0 : aRet.appendAscii("{ts '");
140 0 : aRet.append(DBTypeConversion::toDateTimeString(aDateTime));
141 0 : if (bQuote)
142 0 : aRet.appendAscii("'}");
143 0 : break;
144 : }
145 0 : break;
146 : }
147 : case DataType::DATE:
148 : {
149 0 : Date aDate;
150 0 : bool bOk = false;
151 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
152 : {
153 0 : double nValue = 0.0;
154 0 : _rVal >>= nValue;
155 0 : aDate = DBTypeConversion::toDate(nValue);
156 0 : bOk = true;
157 : }
158 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
159 : {
160 0 : OUString sValue;
161 0 : _rVal >>= sValue;
162 0 : aDate = DBTypeConversion::toDate(sValue);
163 0 : bOk = true;
164 : }
165 : else
166 0 : bOk = _rVal >>= aDate;
167 : OSL_VERIFY_RES( bOk, "DBTypeConversion::toSQLString: _rVal is not date!");
168 0 : if (bQuote)
169 0 : aRet.appendAscii("{d '");
170 0 : aRet.append(DBTypeConversion::toDateString(aDate));
171 0 : if (bQuote)
172 0 : aRet.appendAscii("'}");
173 0 : } break;
174 : case DataType::TIME:
175 : {
176 0 : css::util::Time aTime;
177 0 : bool bOk = false;
178 0 : if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE)
179 : {
180 0 : double nValue = 0.0;
181 0 : _rVal >>= nValue;
182 0 : aTime = DBTypeConversion::toTime(nValue);
183 0 : bOk = true;
184 : }
185 0 : else if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_STRING)
186 : {
187 0 : OUString sValue;
188 0 : _rVal >>= sValue;
189 0 : aTime = DBTypeConversion::toTime(sValue);
190 0 : bOk = true;
191 : }
192 : else
193 0 : bOk = _rVal >>= aTime;
194 : OSL_VERIFY_RES( bOk,"DBTypeConversion::toSQLString: _rVal is not time!");
195 0 : if (bQuote)
196 0 : aRet.appendAscii("{t '");
197 0 : aRet.append(DBTypeConversion::toTimeString(aTime));
198 0 : if (bQuote)
199 0 : aRet.appendAscii("'}");
200 0 : } break;
201 : }
202 : }
203 0 : catch ( const Exception& )
204 : {
205 : OSL_FAIL("TypeConversion Error");
206 : }
207 : }
208 : else
209 0 : aRet.appendAscii(" NULL ");
210 6 : return aRet.makeStringAndClear();
211 : }
212 :
213 0 : Date DBTypeConversion::getNULLDate(const Reference< XNumberFormatsSupplier > &xSupplier)
214 : {
215 : OSL_ENSURE(xSupplier.is(), "getNULLDate : the formatter doesn't implement a supplier !");
216 0 : if (xSupplier.is())
217 : {
218 : try
219 : {
220 : // get the null date
221 0 : Date aDate;
222 0 : xSupplier->getNumberFormatSettings()->getPropertyValue("NullDate") >>= aDate;
223 0 : return aDate;
224 : }
225 0 : catch ( const Exception& )
226 : {
227 : }
228 : }
229 :
230 0 : return getStandardDate();
231 : }
232 :
233 0 : void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
234 : const Reference<XNumberFormatter>& xFormatter,
235 : const Date& rNullDate,
236 : const OUString& rString,
237 : sal_Int32 nKey,
238 : sal_Int16 nFieldType,
239 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
240 : {
241 0 : if (!rString.isEmpty())
242 : {
243 : // Does the String need to be formatted?
244 0 : sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED;
245 0 : bool bTextFormat = nTypeClass == NumberFormat::TEXT;
246 0 : sal_Int32 nKeyToUse = bTextFormat ? 0 : nKey;
247 0 : sal_Int16 nRealUsedTypeClass = nTypeClass;
248 : // for a Text-Format the formatter needs some more freedom, otherwise
249 : // convertStringToNumber will throw a NotNumericException
250 : try
251 : {
252 0 : double fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
253 0 : Reference< XNumberFormats > xFormats(xFormatter->getNumberFormatsSupplier()->getNumberFormats());
254 0 : Reference< XNumberFormatTypes > xFormatTypes(xFormats, UNO_QUERY);
255 0 : sal_Int32 nStandardKey(0);
256 0 : if(xFormatTypes.is())
257 : {
258 0 : const Reference< XPropertySet > xFormatProps(xFormats->getByKey(nKeyToUse));
259 0 : if (xFormatProps.is())
260 : {
261 0 : css::lang::Locale loc;
262 0 : if (xFormatProps->getPropertyValue("Locale") >>= loc)
263 0 : nStandardKey = xFormatTypes->getStandardIndex(loc);
264 : else
265 : {
266 : assert(false);
267 0 : }
268 : }
269 : else
270 : {
271 : SAL_WARN("connectivity.commontools", "no format by key " << nKeyToUse);
272 0 : }
273 : }
274 : else
275 : {
276 : assert(false);
277 : }
278 : // Why use nStandardKey rather than nKeyToUse here? I'm not sure, but "it was always like that".
279 : // Previously had hardcoded 0 instead of nStandardKey, which led to problems with dates
280 : // because of differences M/D/Y vs D/M/Y. This at least fixes those problems, but possibly
281 : // nKeyToUse is an even better choice than nStandardKey.
282 : // OTOH, using nKeyToUse nullifies the special treatment for percent formats,
283 : // leading to "5" (in a percent format) to be understood as "500%" instead of "5%".
284 0 : sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(nStandardKey, rString);
285 0 : if (nRealUsedKey != nKeyToUse)
286 0 : nRealUsedTypeClass = getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;
287 :
288 : // and again a special treatment, this time for percent formats
289 0 : if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
290 : { // formatting should be "percent", but the String provides just a simple number -> adjust
291 0 : OUString sExpanded(rString);
292 0 : static OUString s_sPercentSymbol( "%" );
293 : // need a method to add a sal_Unicode to a string, 'til then we use a static string
294 0 : sExpanded += s_sPercentSymbol;
295 0 : fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
296 : }
297 :
298 0 : switch (nRealUsedTypeClass)
299 : {
300 : case NumberFormat::DATE:
301 : case NumberFormat::DATETIME:
302 : case NumberFormat::TIME:
303 0 : DBTypeConversion::setValue(xVariant,rNullDate,fValue,nRealUsedTypeClass);
304 : // xVariant->updateDouble(toStandardDbDate(rNullDate, fValue));
305 0 : break;
306 : case NumberFormat::CURRENCY:
307 : case NumberFormat::NUMBER:
308 : case NumberFormat::SCIENTIFIC:
309 : case NumberFormat::FRACTION:
310 : case NumberFormat::PERCENT:
311 0 : xVariant->updateDouble(fValue);
312 0 : break;
313 : default:
314 0 : xVariant->updateString(rString);
315 0 : }
316 : }
317 0 : catch(const Exception& )
318 : {
319 0 : xVariant->updateString(rString);
320 : }
321 : }
322 : else
323 : {
324 0 : switch (nFieldType)
325 : {
326 : case ::com::sun::star::sdbc::DataType::CHAR:
327 : case ::com::sun::star::sdbc::DataType::VARCHAR:
328 : case ::com::sun::star::sdbc::DataType::LONGVARCHAR:
329 0 : xVariant->updateString(rString);
330 0 : break;
331 : default:
332 0 : xVariant->updateNull();
333 : }
334 : }
335 0 : }
336 :
337 :
338 0 : void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
339 : const Date& rNullDate,
340 : const double& rValue,
341 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
342 : {
343 0 : switch (nKeyType & ~NumberFormat::DEFINED)
344 : {
345 : case NumberFormat::DATE:
346 0 : xVariant->updateDate(toDate( rValue, rNullDate));
347 0 : break;
348 : case NumberFormat::DATETIME:
349 0 : xVariant->updateTimestamp(toDateTime(rValue,rNullDate));
350 0 : break;
351 : case NumberFormat::TIME:
352 0 : xVariant->updateTime(toTime(rValue));
353 0 : break;
354 : default:
355 : {
356 0 : double nValue = rValue;
357 : // Reference<XPropertySet> xProp(xVariant,UNO_QUERY);
358 : // if ( xProp.is()
359 : // && xProp->getPropertySetInfo()->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))
360 : // && !::comphelper::getBOOL(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))) )
361 : // {
362 : // switch (::comphelper::getINT32(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
363 : // {
364 : // case DataType::TINYINT:
365 : // nValue = static_cast<sal_uInt8>(rValue);
366 : // break;
367 : // case DataType::SMALLINT:
368 : // nValue = static_cast<sal_uInt16>(rValue);
369 : // break;
370 : // case DataType::INTEGER:
371 : // nValue = static_cast<sal_uInt32>(rValue);
372 : // break;
373 : // case DataType::BIGINT:
374 : // nValue = static_cast<sal_uInt64>(rValue);
375 : // break;
376 : // }
377 : // }
378 0 : xVariant->updateDouble(nValue);
379 : }
380 : }
381 0 : }
382 :
383 :
384 0 : double DBTypeConversion::getValue( const Reference< XColumn >& i_column, const Date& i_relativeToNullDate )
385 : {
386 : try
387 : {
388 0 : const Reference< XPropertySet > xProp( i_column, UNO_QUERY_THROW );
389 :
390 0 : const sal_Int32 nColumnType = ::comphelper::getINT32( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_TYPE ) ) );
391 0 : switch ( nColumnType )
392 : {
393 : case DataType::DATE:
394 0 : return toDouble( i_column->getDate(), i_relativeToNullDate );
395 :
396 : case DataType::TIME:
397 0 : return toDouble( i_column->getTime() );
398 :
399 : case DataType::TIMESTAMP:
400 0 : return toDouble( i_column->getTimestamp(), i_relativeToNullDate );
401 :
402 : default:
403 : {
404 0 : bool bIsSigned = true;
405 0 : OSL_VERIFY( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ISSIGNED ) ) >>= bIsSigned );
406 0 : if ( !bIsSigned )
407 : {
408 0 : switch ( nColumnType)
409 : {
410 : case DataType::TINYINT:
411 0 : return static_cast<double>(static_cast<sal_uInt8>(i_column->getByte()));
412 : case DataType::SMALLINT:
413 0 : return static_cast<double>(static_cast<sal_uInt16>(i_column->getShort()));
414 : case DataType::INTEGER:
415 0 : return static_cast<double>(static_cast<sal_uInt32>(i_column->getInt()));
416 : case DataType::BIGINT:
417 0 : return static_cast<double>(static_cast<sal_uInt64>(i_column->getLong()));
418 : }
419 : }
420 : }
421 0 : return i_column->getDouble();
422 0 : }
423 : }
424 0 : catch( const Exception& )
425 : {
426 : DBG_UNHANDLED_EXCEPTION();
427 0 : return 0.0;
428 : }
429 : }
430 :
431 0 : OUString DBTypeConversion::getFormattedValue(const Reference< XPropertySet>& _xColumn,
432 : const Reference<XNumberFormatter>& _xFormatter,
433 : const ::com::sun::star::lang::Locale& _rLocale,
434 : const Date& _rNullDate)
435 : {
436 : OSL_ENSURE(_xColumn.is() && _xFormatter.is(), "DBTypeConversion::getFormattedValue: invalid arg !");
437 0 : if (!_xColumn.is() || !_xFormatter.is())
438 0 : return OUString();
439 :
440 0 : sal_Int32 nKey(0);
441 : try
442 : {
443 0 : _xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= nKey;
444 : }
445 0 : catch (const Exception& )
446 : {
447 : OSL_FAIL("DBTypeConversion::getValue: caught an exception while asking for the format key!");
448 : }
449 :
450 0 : if (!nKey)
451 : {
452 0 : Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
453 0 : Reference<XNumberFormatTypes> xTypeList(_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), UNO_QUERY);
454 :
455 : nKey = ::dbtools::getDefaultNumberFormat(_xColumn,
456 : Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
457 0 : _rLocale);
458 :
459 : }
460 :
461 0 : sal_Int16 nKeyType = getNumberFormatType(_xFormatter, nKey) & ~NumberFormat::DEFINED;
462 :
463 0 : return DBTypeConversion::getFormattedValue(Reference< XColumn > (_xColumn, UNO_QUERY), _xFormatter, _rNullDate, nKey, nKeyType);
464 : }
465 :
466 :
467 0 : OUString DBTypeConversion::getFormattedValue(const Reference<XColumn>& xVariant,
468 : const Reference<XNumberFormatter>& xFormatter,
469 : const Date& rNullDate,
470 : sal_Int32 nKey,
471 : sal_Int16 nKeyType)
472 : {
473 0 : OUString aString;
474 0 : if (xVariant.is())
475 : {
476 : try
477 : {
478 0 : switch (nKeyType & ~NumberFormat::DEFINED)
479 : {
480 : case NumberFormat::DATE:
481 : case NumberFormat::DATETIME:
482 : {
483 : // get a value which represents the given date, relative to the given null date
484 0 : double fValue = getValue( xVariant, rNullDate );
485 0 : if ( !xVariant->wasNull() )
486 : {
487 : // get the null date of the formatter
488 0 : Date aFormatterNullDate( rNullDate );
489 : try
490 : {
491 0 : Reference< XNumberFormatsSupplier > xSupplier( xFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
492 0 : Reference< XPropertySet > xFormatterSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
493 0 : OSL_VERIFY( xFormatterSettings->getPropertyValue("NullDate") >>= aFormatterNullDate );
494 : }
495 0 : catch( const Exception& )
496 : {
497 : DBG_UNHANDLED_EXCEPTION();
498 : }
499 : // get a value which represents the given date, relative to the null date of the formatter
500 0 : fValue -= toDays( rNullDate, aFormatterNullDate );
501 : // format this value
502 0 : aString = xFormatter->convertNumberToString( nKey, fValue );
503 : }
504 : }
505 0 : break;
506 : case NumberFormat::TIME:
507 : case NumberFormat::NUMBER:
508 : case NumberFormat::SCIENTIFIC:
509 : case NumberFormat::FRACTION:
510 : case NumberFormat::PERCENT:
511 : {
512 0 : double fValue = xVariant->getDouble();
513 0 : if (!xVariant->wasNull())
514 0 : aString = xFormatter->convertNumberToString(nKey, fValue);
515 0 : } break;
516 : case NumberFormat::CURRENCY:
517 : {
518 0 : double fValue = xVariant->getDouble();
519 0 : if (!xVariant->wasNull())
520 0 : aString = xFormatter->getInputString(nKey, fValue);
521 0 : } break;
522 : case NumberFormat::TEXT:
523 0 : aString = xFormatter->formatString(nKey, xVariant->getString());
524 0 : break;
525 : default:
526 0 : aString = xVariant->getString();
527 : }
528 : }
529 0 : catch(const Exception& )
530 : {
531 0 : aString = xVariant->getString();
532 : }
533 : }
534 0 : return aString;
535 : }
536 :
537 :
538 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|