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>
31 #include <com/sun/star/sdb/XColumn.hpp>
32 #include <com/sun/star/sdb/XColumnUpdate.hpp>
34 #include <tools/diagnose_ex.h>
35 #include <i18nlangtag/mslangid.hxx>
36 #include <i18nlangtag/languagetag.hxx>
37 #include <comphelper/numbers.hxx>
38 #include <unotools/sharedunocomponent.hxx>
45 using ::com::sun::star::uno::Reference
;
46 using ::com::sun::star::uno::UNO_QUERY
;
47 using ::com::sun::star::uno::UNO_QUERY_THROW
;
48 using ::com::sun::star::uno::UNO_SET_THROW
;
49 using ::com::sun::star::uno::Exception
;
50 using ::com::sun::star::uno::XComponentContext
;
51 using ::com::sun::star::sdbc::XRowSet
;
52 using ::com::sun::star::beans::XPropertySet
;
53 using ::com::sun::star::util::NumberFormatter
;
54 using ::com::sun::star::util::XNumberFormatter
;
55 using ::com::sun::star::util::Date
;
56 using ::com::sun::star::sdbc::XConnection
;
57 using ::com::sun::star::util::XNumberFormatsSupplier
;
58 using ::com::sun::star::beans::XPropertySetInfo
;
59 using ::com::sun::star::lang::Locale
;
60 using ::com::sun::star::util::XNumberFormatTypes
;
61 using ::com::sun::star::sdb::XColumn
;
62 using ::com::sun::star::sdb::XColumnUpdate
;
64 namespace DataType
= ::com::sun::star::sdbc::DataType
;
65 namespace NumberFormat
= ::com::sun::star::util::NumberFormat
;
67 struct FormattedColumnValue_Data
69 Reference
< XNumberFormatter
> m_xFormatter
;
71 sal_Int32 m_nFormatKey
;
72 sal_Int32 m_nFieldType
;
76 Reference
< XColumn
> m_xColumn
;
77 Reference
< XColumnUpdate
> m_xColumnUpdate
;
79 FormattedColumnValue_Data()
81 ,m_aNullDate( DBTypeConversion::getStandardDate() )
83 ,m_nFieldType( DataType::OTHER
)
84 ,m_nKeyType( NumberFormat::UNDEFINED
)
85 ,m_bNumericField( false )
96 void lcl_clear_nothrow( FormattedColumnValue_Data
& _rData
)
98 _rData
.m_xFormatter
.clear();
99 _rData
.m_nFormatKey
= 0;
100 _rData
.m_nFieldType
= DataType::OTHER
;
101 _rData
.m_nKeyType
= NumberFormat::UNDEFINED
;
102 _rData
.m_bNumericField
= false;
104 _rData
.m_xColumn
.clear();
105 _rData
.m_xColumnUpdate
.clear();
109 void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data
& _rData
,
110 const Reference
< XNumberFormatter
>& i_rNumberFormatter
, const Reference
< XPropertySet
>& _rxColumn
)
112 lcl_clear_nothrow( _rData
);
114 OSL_PRECOND( i_rNumberFormatter
.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" );
115 if ( !i_rNumberFormatter
.is() )
120 Reference
< XNumberFormatsSupplier
> xNumberFormatsSupp( i_rNumberFormatter
->getNumberFormatsSupplier(), UNO_SET_THROW
);
122 // remember the column
123 _rData
.m_xColumn
.set( _rxColumn
, UNO_QUERY_THROW
);
124 _rData
.m_xColumnUpdate
.set( _rxColumn
, UNO_QUERY
);
126 // determine the field type, and whether it's a numeric field
127 OSL_VERIFY( _rxColumn
->getPropertyValue("Type") >>= _rData
.m_nFieldType
);
129 switch ( _rData
.m_nFieldType
)
133 case DataType::TIMESTAMP
:
135 case DataType::BOOLEAN
:
136 case DataType::TINYINT
:
137 case DataType::SMALLINT
:
138 case DataType::INTEGER
:
140 case DataType::BIGINT
:
141 case DataType::DOUBLE
:
142 case DataType::NUMERIC
:
143 case DataType::DECIMAL
:
144 _rData
.m_bNumericField
= true;
147 _rData
.m_bNumericField
= false;
151 // get the format key of our bound field
152 Reference
< XPropertySetInfo
> xPSI( _rxColumn
->getPropertySetInfo(), UNO_SET_THROW
);
153 bool bHaveFieldFormat
= false;
154 const OUString
sFormatKeyProperty( "FormatKey" );
155 if ( xPSI
->hasPropertyByName( sFormatKeyProperty
) )
157 bHaveFieldFormat
= ( _rxColumn
->getPropertyValue( sFormatKeyProperty
) >>= _rData
.m_nFormatKey
);
159 if ( !bHaveFieldFormat
)
161 // fall back to a format key as indicated by the field type
162 Locale
aSystemLocale( LanguageTag( MsLangId::getSystemLanguage() ).getLocale() );
163 Reference
< XNumberFormatTypes
> xNumTypes( xNumberFormatsSupp
->getNumberFormats(), UNO_QUERY_THROW
);
164 _rData
.m_nFormatKey
= getDefaultNumberFormat( _rxColumn
, xNumTypes
, aSystemLocale
);
167 // some more formatter settings
168 _rData
.m_nKeyType
= ::comphelper::getNumberFormatType( xNumberFormatsSupp
->getNumberFormats(), _rData
.m_nFormatKey
);
169 Reference
< XPropertySet
> xFormatSettings( xNumberFormatsSupp
->getNumberFormatSettings(), UNO_SET_THROW
);
170 OSL_VERIFY( xFormatSettings
->getPropertyValue("NullDate") >>= _rData
.m_aNullDate
);
172 // remember the formatter
173 _rData
.m_xFormatter
= i_rNumberFormatter
;
175 catch( const Exception
& )
177 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
182 void lcl_initColumnDataValue_nothrow( const Reference
<XComponentContext
>& i_rContext
, FormattedColumnValue_Data
& i_rData
,
183 const Reference
< XRowSet
>& i_rRowSet
, const Reference
< XPropertySet
>& i_rColumn
)
185 OSL_PRECOND( i_rRowSet
.is(), "lcl_initColumnDataValue_nothrow: no row set!" );
186 if ( !i_rRowSet
.is() )
189 Reference
< XNumberFormatter
> xNumberFormatter
;
192 // get the number formats supplier of the connection of the form
193 Reference
< XConnection
> xConnection( getConnection( i_rRowSet
), UNO_SET_THROW
);
194 Reference
< XNumberFormatsSupplier
> xSupplier( getNumberFormats( xConnection
, true, i_rContext
), UNO_SET_THROW
);
196 // create a number formatter for it
197 xNumberFormatter
.set( NumberFormatter::create( i_rContext
), UNO_QUERY_THROW
);
198 xNumberFormatter
->attachNumberFormatsSupplier( xSupplier
);
200 catch( const Exception
& )
202 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
205 lcl_initColumnDataValue_nothrow( i_rData
, xNumberFormatter
, i_rColumn
);
209 FormattedColumnValue::FormattedColumnValue( const Reference
< XComponentContext
>& _rxContext
,
210 const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& i_rColumn
)
211 :m_pData( new FormattedColumnValue_Data
)
213 lcl_initColumnDataValue_nothrow( _rxContext
, *m_pData
, _rxRowSet
, i_rColumn
);
217 FormattedColumnValue::FormattedColumnValue( const Reference
< XNumberFormatter
>& i_rNumberFormatter
,
218 const Reference
< XPropertySet
>& _rxColumn
)
219 :m_pData( new FormattedColumnValue_Data
)
221 lcl_initColumnDataValue_nothrow( *m_pData
, i_rNumberFormatter
, _rxColumn
);
225 FormattedColumnValue::~FormattedColumnValue()
227 lcl_clear_nothrow( *m_pData
);
230 sal_Int16
FormattedColumnValue::getKeyType() const
232 return m_pData
->m_nKeyType
;
236 const Reference
< XColumn
>& FormattedColumnValue::getColumn() const
238 return m_pData
->m_xColumn
;
241 bool FormattedColumnValue::setFormattedValue( const OUString
& _rFormattedStringValue
) const
243 OSL_PRECOND( m_pData
->m_xColumnUpdate
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
244 if ( !m_pData
->m_xColumnUpdate
.is() )
249 if ( m_pData
->m_bNumericField
)
251 ::dbtools::DBTypeConversion::setValue( m_pData
->m_xColumnUpdate
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
,
252 _rFormattedStringValue
, m_pData
->m_nFormatKey
, ::sal::static_int_cast
< sal_Int16
>( m_pData
->m_nFieldType
),
253 m_pData
->m_nKeyType
);
257 m_pData
->m_xColumnUpdate
->updateString( _rFormattedStringValue
);
260 catch( const Exception
& )
268 OUString
FormattedColumnValue::getFormattedValue() const
270 OSL_PRECOND( m_pData
->m_xColumn
.is(), "FormattedColumnValue::setFormattedValue: no column!" );
272 OUString sStringValue
;
273 if ( m_pData
->m_xColumn
.is() )
275 if ( m_pData
->m_bNumericField
)
277 sStringValue
= DBTypeConversion::getFormattedValue(
278 m_pData
->m_xColumn
, m_pData
->m_xFormatter
, m_pData
->m_aNullDate
, m_pData
->m_nFormatKey
, m_pData
->m_nKeyType
283 sStringValue
= m_pData
->m_xColumn
->getString();
290 } // namespace dbtools
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */