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 3 : OUString DBTypeConversion::toSQLString(sal_Int32 eType, const Any& _rVal, bool bQuote,
49 : const Reference< XTypeConverter >& _rxTypeConverter)
50 : {
51 3 : OUStringBuffer aRet;
52 3 : if (_rVal.hasValue())
53 : {
54 : try
55 : {
56 3 : 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 3 : if (bQuote)
81 3 : aRet.appendAscii("'");
82 : {
83 3 : OUString aTemp;
84 3 : _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
85 3 : sal_Int32 nIndex = (sal_Int32)-1;
86 6 : const OUString sQuot("\'");
87 6 : const OUString sQuotToReplace("\'\'");
88 3 : do
89 : {
90 3 : nIndex += 2;
91 3 : nIndex = aTemp.indexOf(sQuot,nIndex);
92 3 : if(nIndex != -1)
93 0 : aTemp = aTemp.replaceAt(nIndex,sQuot.getLength(),sQuotToReplace);
94 : } while (nIndex != -1);
95 :
96 6 : aRet.append(aTemp);
97 : }
98 3 : if (bQuote)
99 3 : aRet.appendAscii("'");
100 3 : 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 3 : 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 : fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
293 : }
294 :
295 0 : switch (nRealUsedTypeClass)
296 : {
297 : case NumberFormat::DATE:
298 : case NumberFormat::DATETIME:
299 : case NumberFormat::TIME:
300 0 : DBTypeConversion::setValue(xVariant,rNullDate,fValue,nRealUsedTypeClass);
301 : // xVariant->updateDouble(toStandardDbDate(rNullDate, fValue));
302 0 : break;
303 : case NumberFormat::CURRENCY:
304 : case NumberFormat::NUMBER:
305 : case NumberFormat::SCIENTIFIC:
306 : case NumberFormat::FRACTION:
307 : case NumberFormat::PERCENT:
308 0 : xVariant->updateDouble(fValue);
309 0 : break;
310 : default:
311 0 : xVariant->updateString(rString);
312 0 : }
313 : }
314 0 : catch(const Exception& )
315 : {
316 0 : xVariant->updateString(rString);
317 : }
318 : }
319 : else
320 : {
321 0 : switch (nFieldType)
322 : {
323 : case ::com::sun::star::sdbc::DataType::CHAR:
324 : case ::com::sun::star::sdbc::DataType::VARCHAR:
325 : case ::com::sun::star::sdbc::DataType::LONGVARCHAR:
326 0 : xVariant->updateString(rString);
327 0 : break;
328 : default:
329 0 : xVariant->updateNull();
330 : }
331 : }
332 0 : }
333 :
334 :
335 0 : void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
336 : const Date& rNullDate,
337 : const double& rValue,
338 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
339 : {
340 0 : switch (nKeyType & ~NumberFormat::DEFINED)
341 : {
342 : case NumberFormat::DATE:
343 0 : xVariant->updateDate(toDate( rValue, rNullDate));
344 0 : break;
345 : case NumberFormat::DATETIME:
346 0 : xVariant->updateTimestamp(toDateTime(rValue,rNullDate));
347 0 : break;
348 : case NumberFormat::TIME:
349 0 : xVariant->updateTime(toTime(rValue));
350 0 : break;
351 : default:
352 : {
353 0 : double nValue = rValue;
354 : // Reference<XPropertySet> xProp(xVariant,UNO_QUERY);
355 : // if ( xProp.is()
356 : // && xProp->getPropertySetInfo()->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))
357 : // && !::comphelper::getBOOL(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSIGNED))) )
358 : // {
359 : // switch (::comphelper::getINT32(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
360 : // {
361 : // case DataType::TINYINT:
362 : // nValue = static_cast<sal_uInt8>(rValue);
363 : // break;
364 : // case DataType::SMALLINT:
365 : // nValue = static_cast<sal_uInt16>(rValue);
366 : // break;
367 : // case DataType::INTEGER:
368 : // nValue = static_cast<sal_uInt32>(rValue);
369 : // break;
370 : // case DataType::BIGINT:
371 : // nValue = static_cast<sal_uInt64>(rValue);
372 : // break;
373 : // }
374 : // }
375 0 : xVariant->updateDouble(nValue);
376 : }
377 : }
378 0 : }
379 :
380 :
381 0 : double DBTypeConversion::getValue( const Reference< XColumn >& i_column, const Date& i_relativeToNullDate )
382 : {
383 : try
384 : {
385 0 : const Reference< XPropertySet > xProp( i_column, UNO_QUERY_THROW );
386 :
387 0 : const sal_Int32 nColumnType = ::comphelper::getINT32( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_TYPE ) ) );
388 0 : switch ( nColumnType )
389 : {
390 : case DataType::DATE:
391 0 : return toDouble( i_column->getDate(), i_relativeToNullDate );
392 :
393 : case DataType::TIME:
394 0 : return toDouble( i_column->getTime() );
395 :
396 : case DataType::TIMESTAMP:
397 0 : return toDouble( i_column->getTimestamp(), i_relativeToNullDate );
398 :
399 : default:
400 : {
401 0 : bool bIsSigned = true;
402 0 : OSL_VERIFY( xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ISSIGNED ) ) >>= bIsSigned );
403 0 : if ( !bIsSigned )
404 : {
405 0 : switch ( nColumnType)
406 : {
407 : case DataType::TINYINT:
408 0 : return static_cast<double>(static_cast<sal_uInt8>(i_column->getByte()));
409 : case DataType::SMALLINT:
410 0 : return static_cast<double>(static_cast<sal_uInt16>(i_column->getShort()));
411 : case DataType::INTEGER:
412 0 : return static_cast<double>(static_cast<sal_uInt32>(i_column->getInt()));
413 : case DataType::BIGINT:
414 0 : return static_cast<double>(static_cast<sal_uInt64>(i_column->getLong()));
415 : }
416 : }
417 : }
418 0 : return i_column->getDouble();
419 0 : }
420 : }
421 0 : catch( const Exception& )
422 : {
423 : DBG_UNHANDLED_EXCEPTION();
424 0 : return 0.0;
425 : }
426 : }
427 :
428 0 : OUString DBTypeConversion::getFormattedValue(const Reference< XPropertySet>& _xColumn,
429 : const Reference<XNumberFormatter>& _xFormatter,
430 : const ::com::sun::star::lang::Locale& _rLocale,
431 : const Date& _rNullDate)
432 : {
433 : OSL_ENSURE(_xColumn.is() && _xFormatter.is(), "DBTypeConversion::getFormattedValue: invalid arg !");
434 0 : if (!_xColumn.is() || !_xFormatter.is())
435 0 : return OUString();
436 :
437 0 : sal_Int32 nKey(0);
438 : try
439 : {
440 0 : _xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= nKey;
441 : }
442 0 : catch (const Exception& )
443 : {
444 : OSL_FAIL("DBTypeConversion::getValue: caught an exception while asking for the format key!");
445 : }
446 :
447 0 : if (!nKey)
448 : {
449 0 : Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
450 0 : Reference<XNumberFormatTypes> xTypeList(_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), UNO_QUERY);
451 :
452 : nKey = ::dbtools::getDefaultNumberFormat(_xColumn,
453 : Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
454 0 : _rLocale);
455 :
456 : }
457 :
458 0 : sal_Int16 nKeyType = getNumberFormatType(_xFormatter, nKey) & ~NumberFormat::DEFINED;
459 :
460 0 : return DBTypeConversion::getFormattedValue(Reference< XColumn > (_xColumn, UNO_QUERY), _xFormatter, _rNullDate, nKey, nKeyType);
461 : }
462 :
463 :
464 0 : OUString DBTypeConversion::getFormattedValue(const Reference<XColumn>& xVariant,
465 : const Reference<XNumberFormatter>& xFormatter,
466 : const Date& rNullDate,
467 : sal_Int32 nKey,
468 : sal_Int16 nKeyType)
469 : {
470 0 : OUString aString;
471 0 : if (xVariant.is())
472 : {
473 : try
474 : {
475 0 : switch (nKeyType & ~NumberFormat::DEFINED)
476 : {
477 : case NumberFormat::DATE:
478 : case NumberFormat::DATETIME:
479 : {
480 : // get a value which represents the given date, relative to the given null date
481 0 : double fValue = getValue( xVariant, rNullDate );
482 0 : if ( !xVariant->wasNull() )
483 : {
484 : // get the null date of the formatter
485 0 : Date aFormatterNullDate( rNullDate );
486 : try
487 : {
488 0 : Reference< XNumberFormatsSupplier > xSupplier( xFormatter->getNumberFormatsSupplier(), UNO_SET_THROW );
489 0 : Reference< XPropertySet > xFormatterSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW );
490 0 : OSL_VERIFY( xFormatterSettings->getPropertyValue("NullDate") >>= aFormatterNullDate );
491 : }
492 0 : catch( const Exception& )
493 : {
494 : DBG_UNHANDLED_EXCEPTION();
495 : }
496 : // get a value which represents the given date, relative to the null date of the formatter
497 0 : fValue -= toDays( rNullDate, aFormatterNullDate );
498 : // format this value
499 0 : aString = xFormatter->convertNumberToString( nKey, fValue );
500 : }
501 : }
502 0 : break;
503 : case NumberFormat::TIME:
504 : case NumberFormat::NUMBER:
505 : case NumberFormat::SCIENTIFIC:
506 : case NumberFormat::FRACTION:
507 : case NumberFormat::PERCENT:
508 : {
509 0 : double fValue = xVariant->getDouble();
510 0 : if (!xVariant->wasNull())
511 0 : aString = xFormatter->convertNumberToString(nKey, fValue);
512 0 : } break;
513 : case NumberFormat::CURRENCY:
514 : {
515 0 : double fValue = xVariant->getDouble();
516 0 : if (!xVariant->wasNull())
517 0 : aString = xFormatter->getInputString(nKey, fValue);
518 0 : } break;
519 : case NumberFormat::TEXT:
520 0 : aString = xFormatter->formatString(nKey, xVariant->getString());
521 0 : break;
522 : default:
523 0 : aString = xVariant->getString();
524 : }
525 : }
526 0 : catch(const Exception& )
527 : {
528 0 : aString = xVariant->getString();
529 : }
530 : }
531 0 : return aString;
532 : }
533 :
534 :
535 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|