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