1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <connectivity/formattedcolumnvalue.hxx>
22 #include <connectivity/dbtools.hxx>
23 #include <connectivity/dbconversion.hxx>
25 #include <com/sun/star/util/NumberFormatter.hpp>
26 #include <com/sun/star/util/Date.hpp>
27 #include <com/sun/star/sdbc/XConnection.hpp>
28 #include <com/sun/star/util/XNumberFormatTypes.hpp>
29 #include <com/sun/star/util/NumberFormat.hpp>
30 #include <com/sun/star/sdbc/DataType.hpp>
32 #include <tools/diagnose_ex.h>
33 #include <i18nlangtag/mslangid.hxx>
34 #include <i18nlangtag/languagetag.hxx>
35 #include <comphelper/numbers.hxx>
36 #include <unotools/sharedunocomponent.hxx>
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::UNO_QUERY
;
45 using ::com::sun::star::uno::UNO_QUERY_THROW
;
46 using ::com::sun::star::uno::UNO_SET_THROW
;
47 using ::com::sun::star::uno::Exception
;
48 using ::com::sun::star::uno::RuntimeException
;
49 using ::com::sun::star::uno::Any
;
50 using ::com::sun::star::uno::makeAny
;
51 using ::com::sun::star::uno::XComponentContext
;
52 using ::com::sun::star::sdbc::XRowSet
;
53 using ::com::sun::star::beans::XPropertySet
;
54 using ::com::sun::star::util::NumberFormatter
;
55 using ::com::sun::star::util::XNumberFormatter
;
56 using ::com::sun::star::util::Date
;
57 using ::com::sun::star::sdbc::XConnection
;
58 using ::com::sun::star::util::XNumberFormatsSupplier
;
59 using ::com::sun::star::beans::XPropertySetInfo
;
60 using ::com::sun::star::lang::Locale
;
61 using ::com::sun::star::util::XNumberFormatTypes
;
62 using ::com::sun::star::sdb::XColumn
;
63 using ::com::sun::star::sdb::XColumnUpdate
;
64 using ::com::sun::star::lang::XComponent
;
66 namespace DataType
= ::com::sun::star::sdbc::DataType
;
67 namespace NumberFormat
= ::com::sun::star::util::NumberFormat
;
69 struct FormattedColumnValue_Data
71 Reference
< XNumberFormatter
> m_xFormatter
;
73 sal_Int32 m_nFormatKey
;
74 sal_Int32 m_nFieldType
;
78 Reference
< XColumn
> m_xColumn
;
79 Reference
< XColumnUpdate
> m_xColumnUpdate
;
81 FormattedColumnValue_Data()
83 ,m_aNullDate( DBTypeConversion::getStandardDate() )
85 ,m_nFieldType( DataType::OTHER
)
86 ,m_nKeyType( NumberFormat::UNDEFINED
)
87 ,m_bNumericField( false )
98 void lcl_clear_nothrow( FormattedColumnValue_Data
& _rData
)
100 _rData
.m_xFormatter
.clear();
101 _rData
.m_nFormatKey
= 0;
102 _rData
.m_nFieldType
= DataType::OTHER
;
103 _rData
.m_nKeyType
= NumberFormat::UNDEFINED
;
104 _rData
.m_bNumericField
= false;
106 _rData
.m_xColumn
.clear();
107 _rData
.m_xColumnUpdate
.clear();
111 void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data
& _rData
,
112 const Reference
< XNumberFormatter
>& i_rNumberFormatter
, const Reference
< XPropertySet
>& _rxColumn
)
114 lcl_clear_nothrow( _rData
);
116 OSL_PRECOND( i_rNumberFormatter
.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" );
117 if ( !i_rNumberFormatter
.is() )
122 Reference
< XNumberFormatsSupplier
> xNumberFormatsSupp( i_rNumberFormatter
->getNumberFormatsSupplier(), UNO_SET_THROW
);
124 // remember the column
125 _rData
.m_xColumn
.set( _rxColumn
, UNO_QUERY_THROW
);
126 _rData
.m_xColumnUpdate
.set( _rxColumn
, UNO_QUERY
);
128 // determine the field type, and whether it's a numeric field
129 OSL_VERIFY( _rxColumn
->getPropertyValue("Type") >>= _rData
.m_nFieldType
);
131 switch ( _rData
.m_nFieldType
)
135 case DataType::TIMESTAMP
:
137 case DataType::BOOLEAN
:
138 case DataType::TINYINT
:
139 case DataType::SMALLINT
:
140 case DataType::INTEGER
:
142 case DataType::BIGINT
:
143 case DataType::DOUBLE
:
144 case DataType::NUMERIC
:
145 case DataType::DECIMAL
:
146 _rData
.m_bNumericField
= true;
149 _rData
.m_bNumericField
= false;
153 // get the format key of our bound field
154 Reference
< XPropertySetInfo
> xPSI( _rxColumn
->getPropertySetInfo(), UNO_QUERY_THROW
);
155 bool bHaveFieldFormat
= false;
156 const OUString
sFormatKeyProperty( "FormatKey" );
157 if ( xPSI
->hasPropertyByName( sFormatKeyProperty
) )
159 bHaveFieldFormat
= ( _rxColumn
->getPropertyValue( sFormatKeyProperty
) >>= _rData
.m_nFormatKey
);
161 if ( !bHaveFieldFormat
)
163 // fall back to a format key as indicated by the field type
164 Locale
aSystemLocale( LanguageTag( MsLangId::getSystemLanguage() ).getLocale() );
165 Reference
< XNumberFormatTypes
> xNumTypes( xNumberFormatsSupp
->getNumberFormats(), UNO_QUERY_THROW
);
166 _rData
.m_nFormatKey
= getDefaultNumberFormat( _rxColumn
, xNumTypes
, aSystemLocale
);
169 // some more formatter settings
170 _rData
.m_nKeyType
= ::comphelper::getNumberFormatType( xNumberFormatsSupp
->getNumberFormats(), _rData
.m_nFormatKey
);
171 Reference
< XPropertySet
> xFormatSettings( xNumberFormatsSupp
->getNumberFormatSettings(), UNO_QUERY_THROW
);
172 OSL_VERIFY( xFormatSettings
->getPropertyValue("NullDate") >>= _rData
.m_aNullDate
);
174 // remember the formatter
175 _rData
.m_xFormatter
= i_rNumberFormatter
;
177 catch( const Exception
& )
179 DBG_UNHANDLED_EXCEPTION();
184 void lcl_initColumnDataValue_nothrow( const Reference
<XComponentContext
>& i_rContext
, FormattedColumnValue_Data
& i_rData
,
185 const Reference
< XRowSet
>& i_rRowSet
, const Reference
< XPropertySet
>& i_rColumn
)
187 OSL_PRECOND( i_rRowSet
.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
188 if ( !i_rRowSet
.is() )
191 Reference
< XNumberFormatter
> xNumberFormatter
;
194 // get the number formats supplier of the connection of the form
195 Reference
< XConnection
> xConnection( getConnection( i_rRowSet
), UNO_QUERY_THROW
);
196 Reference
< XNumberFormatsSupplier
> xSupplier( getNumberFormats( xConnection
, true, i_rContext
), UNO_SET_THROW
);
198 // create a number formatter for it
199 xNumberFormatter
.set( NumberFormatter::create( i_rContext
), UNO_QUERY_THROW
);
200 xNumberFormatter
->attachNumberFormatsSupplier( xSupplier
);
202 catch( const Exception
& )
204 DBG_UNHANDLED_EXCEPTION();
207 lcl_initColumnDataValue_nothrow( i_rData
, xNumberFormatter
, i_rColumn
);
211 FormattedColumnValue::FormattedColumnValue( const Reference
< XComponentContext
>& _rxContext
,
212 const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& i_rColumn
)
213 :m_pData( new FormattedColumnValue_Data
)
215 lcl_initColumnDataValue_nothrow( _rxContext
, *m_pData
, _rxRowSet
, i_rColumn
);
219 FormattedColumnValue::FormattedColumnValue( const Reference
< XNumberFormatter
>& i_rNumberFormatter
,
220 const Reference
< XPropertySet
>& _rxColumn
)
221 :m_pData( new FormattedColumnValue_Data
)
223 lcl_initColumnDataValue_nothrow( *m_pData
, i_rNumberFormatter
, _rxColumn
);
227 void FormattedColumnValue::clear()
229 lcl_clear_nothrow( *m_pData
);
233 FormattedColumnValue::~FormattedColumnValue()
238 sal_Int16
FormattedColumnValue::getKeyType() const
240 return m_pData
->m_nKeyType
;
244 const Reference
< XColumn
>& FormattedColumnValue::getColumn() const
246 return m_pData
->m_xColumn
;
249 bool FormattedColumnValue::setFormattedValue( const OUString
& _rFormattedStringValue
) const
251 OSL_PRECOND( m_pData
->m_xColumnUpdate
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
252 if ( !m_pData
->m_xColumnUpdate
.is() )
257 if ( m_pData
->m_bNumericField
)
259 ::dbtools::DBTypeConversion::setValue( m_pData
->m_xColumnUpdate
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
,
260 _rFormattedStringValue
, m_pData
->m_nFormatKey
, ::sal::static_int_cast
< sal_Int16
>( m_pData
->m_nFieldType
),
261 m_pData
->m_nKeyType
);
265 m_pData
->m_xColumnUpdate
->updateString( _rFormattedStringValue
);
268 catch( const Exception
& )
276 OUString
FormattedColumnValue::getFormattedValue() const
278 OSL_PRECOND( m_pData
->m_xColumn
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
280 OUString sStringValue
;
281 if ( m_pData
->m_xColumn
.is() )
283 if ( m_pData
->m_bNumericField
)
285 sStringValue
= DBTypeConversion::getFormattedValue(
286 m_pData
->m_xColumn
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
, m_pData
->m_nFormatKey
, m_pData
->m_nKeyType
291 sStringValue
= m_pData
->m_xColumn
->getString();
298 } // namespace dbtools
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */