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 #include "connectivity/formattedcolumnvalue.hxx" 32*cdf0e10cSrcweir #include "connectivity/dbtools.hxx" 33*cdf0e10cSrcweir #include "connectivity/dbconversion.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir /** === begin UNO includes === **/ 36*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatter.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp> 42*cdf0e10cSrcweir /** === end UNO includes === **/ 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir //#include <unotools/syslocale.hxx> 45*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 46*cdf0e10cSrcweir #include <i18npool/mslangid.hxx> 47*cdf0e10cSrcweir #include <comphelper/numbers.hxx> 48*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 49*cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx> 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir //........................................................................ 52*cdf0e10cSrcweir namespace dbtools 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir //........................................................................ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** === begin UNO using === **/ 57*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 58*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 59*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 60*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 61*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 62*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 63*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 64*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 65*cdf0e10cSrcweir using ::com::sun::star::sdbc::XRowSet; 66*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 67*cdf0e10cSrcweir using ::com::sun::star::util::XNumberFormatter; 68*cdf0e10cSrcweir using ::com::sun::star::util::Date; 69*cdf0e10cSrcweir using ::com::sun::star::sdbc::XConnection; 70*cdf0e10cSrcweir using ::com::sun::star::util::XNumberFormatsSupplier; 71*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySetInfo; 72*cdf0e10cSrcweir using ::com::sun::star::lang::Locale; 73*cdf0e10cSrcweir using ::com::sun::star::util::XNumberFormatTypes; 74*cdf0e10cSrcweir using ::com::sun::star::sdb::XColumn; 75*cdf0e10cSrcweir using ::com::sun::star::sdb::XColumnUpdate; 76*cdf0e10cSrcweir using ::com::sun::star::lang::XComponent; 77*cdf0e10cSrcweir /** === end UNO using === **/ 78*cdf0e10cSrcweir namespace DataType = ::com::sun::star::sdbc::DataType; 79*cdf0e10cSrcweir namespace NumberFormat = ::com::sun::star::util::NumberFormat; 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir //==================================================================== 82*cdf0e10cSrcweir //= FormattedColumnValue_Data 83*cdf0e10cSrcweir //==================================================================== 84*cdf0e10cSrcweir struct FormattedColumnValue_Data 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir Reference< XNumberFormatter > m_xFormatter; 87*cdf0e10cSrcweir Date m_aNullDate; 88*cdf0e10cSrcweir sal_Int32 m_nFormatKey; 89*cdf0e10cSrcweir sal_Int32 m_nFieldType; 90*cdf0e10cSrcweir sal_Int16 m_nKeyType; 91*cdf0e10cSrcweir bool m_bNumericField; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir Reference< XColumn > m_xColumn; 94*cdf0e10cSrcweir Reference< XColumnUpdate > m_xColumnUpdate; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir FormattedColumnValue_Data() 97*cdf0e10cSrcweir :m_xFormatter() 98*cdf0e10cSrcweir ,m_aNullDate( DBTypeConversion::getStandardDate() ) 99*cdf0e10cSrcweir ,m_nFormatKey( 0 ) 100*cdf0e10cSrcweir ,m_nFieldType( DataType::OTHER ) 101*cdf0e10cSrcweir ,m_nKeyType( NumberFormat::UNDEFINED ) 102*cdf0e10cSrcweir ,m_bNumericField( false ) 103*cdf0e10cSrcweir ,m_xColumn() 104*cdf0e10cSrcweir ,m_xColumnUpdate() 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir }; 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir //-------------------------------------------------------------------- 110*cdf0e10cSrcweir namespace 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir //................................................................ 113*cdf0e10cSrcweir void lcl_clear_nothrow( FormattedColumnValue_Data& _rData ) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir _rData.m_xFormatter.clear(); 116*cdf0e10cSrcweir _rData.m_nFormatKey = 0; 117*cdf0e10cSrcweir _rData.m_nFieldType = DataType::OTHER; 118*cdf0e10cSrcweir _rData.m_nKeyType = NumberFormat::UNDEFINED; 119*cdf0e10cSrcweir _rData.m_bNumericField = false; 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir _rData.m_xColumn.clear(); 122*cdf0e10cSrcweir _rData.m_xColumnUpdate.clear(); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir //................................................................ 126*cdf0e10cSrcweir void lcl_initColumnDataValue_nothrow( FormattedColumnValue_Data& _rData, 127*cdf0e10cSrcweir const Reference< XNumberFormatter >& i_rNumberFormatter, const Reference< XPropertySet >& _rxColumn ) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir lcl_clear_nothrow( _rData ); 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir OSL_PRECOND( i_rNumberFormatter.is(), "lcl_initColumnDataValue_nothrow: no number formats -> no formatted values!" ); 132*cdf0e10cSrcweir if ( !i_rNumberFormatter.is() ) 133*cdf0e10cSrcweir return; 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir try 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir Reference< XNumberFormatsSupplier > xNumberFormatsSupp( i_rNumberFormatter->getNumberFormatsSupplier(), UNO_SET_THROW ); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir // remember the column 140*cdf0e10cSrcweir _rData.m_xColumn.set( _rxColumn, UNO_QUERY_THROW ); 141*cdf0e10cSrcweir _rData.m_xColumnUpdate.set( _rxColumn, UNO_QUERY ); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // determine the field type, and whether it's a numeric field 144*cdf0e10cSrcweir OSL_VERIFY( _rxColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= _rData.m_nFieldType ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir switch ( _rData.m_nFieldType ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir case DataType::DATE: 149*cdf0e10cSrcweir case DataType::TIME: 150*cdf0e10cSrcweir case DataType::TIMESTAMP: 151*cdf0e10cSrcweir case DataType::BIT: 152*cdf0e10cSrcweir case DataType::BOOLEAN: 153*cdf0e10cSrcweir case DataType::TINYINT: 154*cdf0e10cSrcweir case DataType::SMALLINT: 155*cdf0e10cSrcweir case DataType::INTEGER: 156*cdf0e10cSrcweir case DataType::REAL: 157*cdf0e10cSrcweir case DataType::BIGINT: 158*cdf0e10cSrcweir case DataType::DOUBLE: 159*cdf0e10cSrcweir case DataType::NUMERIC: 160*cdf0e10cSrcweir case DataType::DECIMAL: 161*cdf0e10cSrcweir _rData.m_bNumericField = true; 162*cdf0e10cSrcweir break; 163*cdf0e10cSrcweir default: 164*cdf0e10cSrcweir _rData.m_bNumericField = false; 165*cdf0e10cSrcweir break; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir // get the format key of our bound field 169*cdf0e10cSrcweir Reference< XPropertySetInfo > xPSI( _rxColumn->getPropertySetInfo(), UNO_QUERY_THROW ); 170*cdf0e10cSrcweir bool bHaveFieldFormat = false; 171*cdf0e10cSrcweir const ::rtl::OUString sFormatKeyProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FormatKey" ) ) ); 172*cdf0e10cSrcweir if ( xPSI->hasPropertyByName( sFormatKeyProperty ) ) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir bHaveFieldFormat = ( _rxColumn->getPropertyValue( sFormatKeyProperty ) >>= _rData.m_nFormatKey ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir if ( !bHaveFieldFormat ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir // fall back to a format key as indicated by the field type 179*cdf0e10cSrcweir Locale aSystemLocale; 180*cdf0e10cSrcweir MsLangId::convertLanguageToLocale( MsLangId::getSystemLanguage(), aSystemLocale ); 181*cdf0e10cSrcweir Reference< XNumberFormatTypes > xNumTypes( xNumberFormatsSupp->getNumberFormats(), UNO_QUERY_THROW ); 182*cdf0e10cSrcweir _rData.m_nFormatKey = getDefaultNumberFormat( _rxColumn, xNumTypes, aSystemLocale ); 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir // some more formatter settings 186*cdf0e10cSrcweir _rData.m_nKeyType = ::comphelper::getNumberFormatType( xNumberFormatsSupp->getNumberFormats(), _rData.m_nFormatKey ); 187*cdf0e10cSrcweir Reference< XPropertySet > xFormatSettings( xNumberFormatsSupp->getNumberFormatSettings(), UNO_QUERY_THROW ); 188*cdf0e10cSrcweir OSL_VERIFY( xFormatSettings->getPropertyValue( ::rtl::OUString::createFromAscii( "NullDate" ) ) >>= _rData.m_aNullDate ); 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir // remember the formatter 191*cdf0e10cSrcweir _rData.m_xFormatter = i_rNumberFormatter; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir catch( const Exception& ) 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir //................................................................ 200*cdf0e10cSrcweir void lcl_initColumnDataValue_nothrow( const ::comphelper::ComponentContext& i_rContext, FormattedColumnValue_Data& i_rData, 201*cdf0e10cSrcweir const Reference< XRowSet >& i_rRowSet, const Reference< XPropertySet >& i_rColumn ) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir OSL_PRECOND( i_rRowSet.is(), "lcl_initColumnDataValue_nothrow: no row set!" ); 204*cdf0e10cSrcweir if ( !i_rRowSet.is() ) 205*cdf0e10cSrcweir return; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir Reference< XNumberFormatter > xNumberFormatter; 208*cdf0e10cSrcweir try 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir // get the number formats supplier of the connection of the form 211*cdf0e10cSrcweir Reference< XConnection > xConnection( getConnection( i_rRowSet ), UNO_QUERY_THROW ); 212*cdf0e10cSrcweir Reference< XNumberFormatsSupplier > xSupplier( getNumberFormats( xConnection, sal_True, i_rContext.getLegacyServiceFactory() ), UNO_SET_THROW ); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir // create a number formatter for it 215*cdf0e10cSrcweir xNumberFormatter.set( i_rContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW ); 216*cdf0e10cSrcweir xNumberFormatter->attachNumberFormatsSupplier( xSupplier ); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir catch( const Exception& ) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir lcl_initColumnDataValue_nothrow( i_rData, xNumberFormatter, i_rColumn ); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir //==================================================================== 228*cdf0e10cSrcweir //= FormattedColumnValue 229*cdf0e10cSrcweir //==================================================================== 230*cdf0e10cSrcweir //-------------------------------------------------------------------- 231*cdf0e10cSrcweir FormattedColumnValue::FormattedColumnValue( const ::comphelper::ComponentContext& i_rContext, 232*cdf0e10cSrcweir const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& i_rColumn ) 233*cdf0e10cSrcweir :m_pData( new FormattedColumnValue_Data ) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir lcl_initColumnDataValue_nothrow( i_rContext, *m_pData, _rxRowSet, i_rColumn ); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir //-------------------------------------------------------------------- 239*cdf0e10cSrcweir FormattedColumnValue::FormattedColumnValue( const Reference< XNumberFormatter >& i_rNumberFormatter, 240*cdf0e10cSrcweir const Reference< XPropertySet >& _rxColumn ) 241*cdf0e10cSrcweir :m_pData( new FormattedColumnValue_Data ) 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir lcl_initColumnDataValue_nothrow( *m_pData, i_rNumberFormatter, _rxColumn ); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir //-------------------------------------------------------------------- 247*cdf0e10cSrcweir void FormattedColumnValue::clear() 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir lcl_clear_nothrow( *m_pData ); 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir //-------------------------------------------------------------------- 253*cdf0e10cSrcweir FormattedColumnValue::~FormattedColumnValue() 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir clear(); 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir //-------------------------------------------------------------------- 259*cdf0e10cSrcweir sal_Int32 FormattedColumnValue::getFormatKey() const 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir return m_pData->m_nFormatKey; 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir //-------------------------------------------------------------------- 265*cdf0e10cSrcweir sal_Int32 FormattedColumnValue::getFieldType() const 266*cdf0e10cSrcweir { 267*cdf0e10cSrcweir return m_pData->m_nFieldType; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir //-------------------------------------------------------------------- 271*cdf0e10cSrcweir sal_Int16 FormattedColumnValue::getKeyType() const 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir return m_pData->m_nKeyType; 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir //-------------------------------------------------------------------- 277*cdf0e10cSrcweir bool FormattedColumnValue::isNumericField() const 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir return m_pData->m_bNumericField; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir //-------------------------------------------------------------------- 283*cdf0e10cSrcweir const Reference< XColumn >& FormattedColumnValue::getColumn() const 284*cdf0e10cSrcweir { 285*cdf0e10cSrcweir return m_pData->m_xColumn; 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir //-------------------------------------------------------------------- 289*cdf0e10cSrcweir const Reference< XColumnUpdate >& FormattedColumnValue::getColumnUpdate() const 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir return m_pData->m_xColumnUpdate; 292*cdf0e10cSrcweir } 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir //-------------------------------------------------------------------- 295*cdf0e10cSrcweir bool FormattedColumnValue::setFormattedValue( const ::rtl::OUString& _rFormattedStringValue ) const 296*cdf0e10cSrcweir { 297*cdf0e10cSrcweir OSL_PRECOND( m_pData->m_xColumnUpdate.is(), "FormattedColumnValue::setFormattedValue: no column!" ); 298*cdf0e10cSrcweir if ( !m_pData->m_xColumnUpdate.is() ) 299*cdf0e10cSrcweir return false; 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir try 302*cdf0e10cSrcweir { 303*cdf0e10cSrcweir if ( m_pData->m_bNumericField ) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir ::dbtools::DBTypeConversion::setValue( m_pData->m_xColumnUpdate, m_pData->m_xFormatter, m_pData->m_aNullDate, 306*cdf0e10cSrcweir _rFormattedStringValue, m_pData->m_nFormatKey, ::sal::static_int_cast< sal_Int16 >( m_pData->m_nFieldType ), 307*cdf0e10cSrcweir m_pData->m_nKeyType ); 308*cdf0e10cSrcweir } 309*cdf0e10cSrcweir else 310*cdf0e10cSrcweir { 311*cdf0e10cSrcweir m_pData->m_xColumnUpdate->updateString( _rFormattedStringValue ); 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir } 314*cdf0e10cSrcweir catch( const Exception& ) 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir return false; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir return true; 319*cdf0e10cSrcweir } 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir //-------------------------------------------------------------------- 322*cdf0e10cSrcweir ::rtl::OUString FormattedColumnValue::getFormattedValue() const 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir OSL_PRECOND( m_pData->m_xColumn.is(), "FormattedColumnValue::setFormattedValue: no column!" ); 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir ::rtl::OUString sStringValue; 327*cdf0e10cSrcweir if ( m_pData->m_xColumn.is() ) 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir if ( m_pData->m_bNumericField ) 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir sStringValue = DBTypeConversion::getFormattedValue( 332*cdf0e10cSrcweir m_pData->m_xColumn, m_pData->m_xFormatter, m_pData->m_aNullDate, m_pData->m_nFormatKey, m_pData->m_nKeyType 333*cdf0e10cSrcweir ); 334*cdf0e10cSrcweir } 335*cdf0e10cSrcweir else 336*cdf0e10cSrcweir { 337*cdf0e10cSrcweir sStringValue = m_pData->m_xColumn->getString(); 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir } 340*cdf0e10cSrcweir return sStringValue; 341*cdf0e10cSrcweir } 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir //........................................................................ 344*cdf0e10cSrcweir } // namespace dbtools 345*cdf0e10cSrcweir //........................................................................ 346