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