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