LCOV - code coverage report
Current view: top level - connectivity/source/commontools - paramwrapper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 42 145 29.0 %
Date: 2014-11-03 Functions: 11 27 40.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : #include <connectivity/paramwrapper.hxx>
      20             : 
      21             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      22             : #include <com/sun/star/sdbc/DataType.hpp>
      23             : #include <com/sun/star/lang/WrappedTargetException.hpp>
      24             : #include <com/sun/star/sdb/XParametersSupplier.hpp>
      25             : #include <com/sun/star/lang/DisposedException.hpp>
      26             : 
      27             : #include <tools/diagnose_ex.h>
      28             : #include <comphelper/enumhelper.hxx>
      29             : 
      30             : #define PROPERTY_ID_VALUE   1000
      31             : 
      32             : 
      33             : namespace dbtools
      34             : {
      35             : namespace param
      36             : {
      37             : 
      38             : 
      39             :     using ::com::sun::star::uno::Reference;
      40             :     using ::com::sun::star::sdbc::XParameters;
      41             :     using ::com::sun::star::uno::Sequence;
      42             :     using ::com::sun::star::uno::Type;
      43             :     using ::com::sun::star::uno::RuntimeException;
      44             :     using ::com::sun::star::uno::XWeak;
      45             :     using ::com::sun::star::beans::XPropertySet;
      46             :     using ::com::sun::star::beans::XFastPropertySet;
      47             :     using ::com::sun::star::beans::XMultiPropertySet;
      48             :     using ::com::sun::star::beans::XPropertySetInfo;
      49             :     using ::com::sun::star::beans::Property;
      50             :     using ::com::sun::star::uno::Exception;
      51             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      52             :     using ::com::sun::star::uno::Any;
      53             :     using ::com::sun::star::lang::IllegalArgumentException;
      54             :     using ::com::sun::star::sdbc::SQLException;
      55             :     using ::com::sun::star::lang::WrappedTargetException;
      56             :     using ::com::sun::star::lang::IndexOutOfBoundsException;
      57             :     using ::com::sun::star::container::XEnumeration;
      58             :     using ::com::sun::star::sdb::XSingleSelectQueryAnalyzer;
      59             :     using ::com::sun::star::sdb::XParametersSupplier;
      60             :     using ::com::sun::star::lang::DisposedException;
      61             : 
      62             :     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
      63             :     namespace DataType = ::com::sun::star::sdbc::DataType;
      64             : 
      65             : 
      66             :     //= ParameterWrapper
      67             : 
      68             : 
      69           6 :     ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn )
      70             :         :PropertyBase( m_aBHelper )
      71           6 :         ,m_xDelegator( _rxColumn )
      72             :     {
      73           6 :         if ( m_xDelegator.is() )
      74           6 :             m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
      75           6 :         if ( !m_xDelegatorPSI.is() )
      76           0 :             throw RuntimeException();
      77           6 :     }
      78             : 
      79             : 
      80           0 :     ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn,
      81             :             const Reference< XParameters >& _rxAllParameters, const ::std::vector< sal_Int32 >& _rIndexes )
      82             :         :PropertyBase( m_aBHelper )
      83             :         ,m_aIndexes( _rIndexes )
      84             :         ,m_xDelegator( _rxColumn )
      85           0 :         ,m_xValueDestination( _rxAllParameters )
      86             :     {
      87           0 :         if ( m_xDelegator.is() )
      88           0 :             m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
      89           0 :         if ( !m_xDelegatorPSI.is() )
      90           0 :             throw RuntimeException();
      91             : 
      92             :         OSL_ENSURE( !m_aIndexes.empty(), "ParameterWrapper::ParameterWrapper: sure about the indexes?" );
      93           0 :     }
      94             : 
      95             : 
      96          12 :     ParameterWrapper::~ParameterWrapper()
      97             :     {
      98          12 :     }
      99             : 
     100             : 
     101          24 :     IMPLEMENT_FORWARD_REFCOUNT( ParameterWrapper, UnoBase )
     102             : 
     103           0 :     css::uno::Any ParameterWrapper::queryInterface(css::uno::Type const & aType)
     104             :         throw (css::uno::RuntimeException, std::exception)
     105             :     {
     106           0 :         css::uno::Any a(UnoBase::queryInterface(aType));
     107           0 :         if (!a.hasValue()) {
     108           0 :             a = PropertyBase::queryInterface(aType);
     109           0 :             if (!a.hasValue()
     110           0 :                 && aType == cppu::UnoType<css::lang::XTypeProvider>::get())
     111             :             {
     112           0 :                 a <<= css::uno::Reference<css::lang::XTypeProvider>(this);
     113             :             }
     114             :         }
     115           0 :         return a;
     116             :     }
     117             : 
     118             : 
     119           0 :     Sequence< Type > SAL_CALL ParameterWrapper::getTypes(   ) throw(RuntimeException, std::exception)
     120             :     {
     121           0 :         Sequence< Type > aTypes( 5 );
     122           0 :         aTypes[ 0 ] = cppu::UnoType<XWeak>::get();
     123           0 :         aTypes[ 1 ] = cppu::UnoType<XTypeProvider>::get();
     124           0 :         aTypes[ 2 ] = cppu::UnoType<XPropertySet>::get();
     125           0 :         aTypes[ 3 ] = cppu::UnoType<XFastPropertySet>::get();
     126           0 :         aTypes[ 4 ] = cppu::UnoType<XMultiPropertySet>::get();
     127           0 :         return aTypes;
     128             :     }
     129             : 
     130             : 
     131           0 :     IMPLEMENT_GET_IMPLEMENTATION_ID( ParameterWrapper )
     132             : 
     133             : 
     134           0 :     OUString ParameterWrapper::impl_getPseudoAggregatePropertyName( sal_Int32 _nHandle ) const
     135             :     {
     136           0 :         Reference< XPropertySetInfo >  xInfo = const_cast<ParameterWrapper*>( this )->getPropertySetInfo();
     137           0 :         Sequence< Property > aProperties = xInfo->getProperties();
     138           0 :         const Property* pProperties = aProperties.getConstArray();
     139           0 :         for ( sal_Int32 i = 0; i < aProperties.getLength(); ++i, ++pProperties )
     140             :         {
     141           0 :             if ( pProperties->Handle == _nHandle )
     142           0 :                 return pProperties->Name;
     143             :         }
     144             : 
     145             :         OSL_FAIL( "ParameterWrapper::impl_getPseudoAggregatePropertyName: invalid argument!" );
     146           0 :         return OUString();
     147             :     }
     148             : 
     149             : 
     150           0 :     Reference< XPropertySetInfo > ParameterWrapper::getPropertySetInfo() throw( RuntimeException, std::exception )
     151             :     {
     152           0 :         return createPropertySetInfo( getInfoHelper() );
     153             :     }
     154             : 
     155             : 
     156           0 :     ::cppu::IPropertyArrayHelper& ParameterWrapper::getInfoHelper()
     157             :     {
     158           0 :         if ( !m_pInfoHelper.get() )
     159             :         {
     160           0 :             Sequence< Property > aProperties;
     161             :             try
     162             :             {
     163           0 :                 aProperties = m_xDelegatorPSI->getProperties();
     164           0 :                 sal_Int32 nProperties( aProperties.getLength() );
     165           0 :                 aProperties.realloc( nProperties + 1 );
     166           0 :                 aProperties[ nProperties ] = Property(
     167             :                     OUString( "Value" ),
     168             :                     PROPERTY_ID_VALUE,
     169           0 :                     ::cppu::UnoType< Any >::get(),
     170             :                     PropertyAttribute::TRANSIENT | PropertyAttribute::MAYBEVOID
     171           0 :                 );
     172             :             }
     173           0 :             catch( const Exception& )
     174             :             {
     175             :                 DBG_UNHANDLED_EXCEPTION();
     176             :             }
     177             : 
     178           0 :             m_pInfoHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties, false ) );
     179             :         }
     180           0 :         return *m_pInfoHelper;
     181             :     }
     182             : 
     183             : 
     184           0 :     sal_Bool ParameterWrapper::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw( IllegalArgumentException )
     185             :     {
     186             :         OSL_ENSURE( PROPERTY_ID_VALUE == nHandle, "ParameterWrapper::convertFastPropertyValue: the only non-readonly prop should be our PROPERTY_VALUE!" );
     187             :         (void)nHandle;
     188             : 
     189             :         // we're lazy here ...
     190           0 :         rOldValue = m_aValue.makeAny();
     191           0 :         rConvertedValue = rValue;
     192           0 :         return sal_True;    // assume "modified" ...
     193             :     }
     194             : 
     195             : 
     196           0 :     void ParameterWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw( Exception, std::exception )
     197             :     {
     198           0 :         if ( nHandle == PROPERTY_ID_VALUE )
     199             :         {
     200             :             try
     201             :             {
     202             :                 // TODO : aParamType & nScale can be obtained within the constructor ....
     203           0 :                 sal_Int32 nParamType = DataType::VARCHAR;
     204           0 :                 OSL_VERIFY( m_xDelegator->getPropertyValue("Type") >>= nParamType );
     205             : 
     206           0 :                 sal_Int32 nScale = 0;
     207           0 :                 if ( m_xDelegatorPSI->hasPropertyByName("Scale") )
     208           0 :                     OSL_VERIFY( m_xDelegator->getPropertyValue("Scale") >>= nScale );
     209             : 
     210           0 :                 if ( m_xValueDestination.is() )
     211             :                 {
     212           0 :                     for ( ::std::vector< sal_Int32 >::iterator aIter = m_aIndexes.begin(); aIter != m_aIndexes.end(); ++aIter )
     213             :                     {
     214           0 :                         m_xValueDestination->setObjectWithInfo( *aIter + 1, rValue, nParamType, nScale );
     215             :                             // (the index of the parameters is one-based)
     216             :                     }
     217             :                 }
     218             : 
     219           0 :                 m_aValue = rValue;
     220             :             }
     221           0 :             catch( SQLException& e )
     222             :             {
     223           0 :                 WrappedTargetException aExceptionWrapper;
     224           0 :                 aExceptionWrapper.Context = e.Context;
     225           0 :                 aExceptionWrapper.Message = e.Message;
     226           0 :                 aExceptionWrapper.TargetException <<= e;
     227           0 :                 throw WrappedTargetException( aExceptionWrapper );
     228             :             }
     229             :         }
     230             :         else
     231             :         {
     232           0 :             OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
     233           0 :             m_xDelegator->setPropertyValue( aName, rValue );
     234             :         }
     235           0 :     }
     236             : 
     237             : 
     238           0 :     void ParameterWrapper::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
     239             :     {
     240           0 :         if ( nHandle == PROPERTY_ID_VALUE )
     241             :         {
     242           0 :             rValue = m_aValue.makeAny();
     243             :         }
     244             :         else
     245             :         {
     246           0 :             OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
     247           0 :             rValue = m_xDelegator->getPropertyValue( aName );
     248             :         }
     249           0 :     }
     250             : 
     251             : 
     252           6 :     void SAL_CALL ParameterWrapper::dispose()
     253             :     {
     254           6 :         ::osl::MutexGuard aGuard( m_aMutex );
     255             : 
     256           6 :         m_aValue.setNull();
     257           6 :         m_aIndexes.resize(0);
     258           6 :         m_xDelegator.clear();
     259           6 :         m_xDelegatorPSI.clear();
     260           6 :         m_xValueDestination.clear();
     261             : 
     262           6 :         m_aBHelper.bDisposed = sal_True;
     263           6 :     }
     264             : 
     265             : 
     266             :     //= ParameterWrapperContainer
     267             : 
     268             : 
     269           0 :     ParameterWrapperContainer::ParameterWrapperContainer()
     270           0 :         :ParameterWrapperContainer_Base( m_aMutex )
     271             :     {
     272           0 :     }
     273             : 
     274             : 
     275          40 :     ParameterWrapperContainer::ParameterWrapperContainer( const Reference< XSingleSelectQueryAnalyzer >& _rxComposer )
     276          40 :         :ParameterWrapperContainer_Base( m_aMutex )
     277             :     {
     278          40 :         Reference< XParametersSupplier > xSuppParams( _rxComposer, UNO_QUERY_THROW );
     279          80 :         Reference< XIndexAccess > xParameters( xSuppParams->getParameters(), UNO_QUERY_THROW );
     280          40 :         sal_Int32 nParamCount( xParameters->getCount() );
     281          40 :         m_aParameters.reserve( nParamCount );
     282          46 :         for ( sal_Int32 i=0; i<nParamCount; ++i )
     283             :         {
     284           6 :             m_aParameters.push_back( new ParameterWrapper( Reference< XPropertySet >( xParameters->getByIndex( i ), UNO_QUERY_THROW ) ) );
     285          40 :         }
     286          40 :     }
     287             : 
     288             : 
     289          80 :     ParameterWrapperContainer::~ParameterWrapperContainer()
     290             :     {
     291          80 :     }
     292             : 
     293             : 
     294           0 :     Type SAL_CALL ParameterWrapperContainer::getElementType() throw( RuntimeException, std::exception )
     295             :     {
     296           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     297           0 :         impl_checkDisposed_throw();
     298           0 :         return cppu::UnoType<XPropertySet>::get();
     299             :     }
     300             : 
     301             : 
     302           0 :     sal_Bool SAL_CALL ParameterWrapperContainer::hasElements() throw( RuntimeException, std::exception )
     303             :     {
     304           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     305           0 :         impl_checkDisposed_throw();
     306           0 :         return !m_aParameters.empty();
     307             :     }
     308             : 
     309             : 
     310           0 :     sal_Int32 SAL_CALL ParameterWrapperContainer::getCount() throw( RuntimeException, std::exception )
     311             :     {
     312           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     313           0 :         impl_checkDisposed_throw();
     314           0 :         return m_aParameters.size();
     315             :     }
     316             : 
     317             : 
     318           0 :     Any SAL_CALL ParameterWrapperContainer::getByIndex( sal_Int32 _nIndex ) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
     319             :     {
     320           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     321           0 :         impl_checkDisposed_throw();
     322             : 
     323           0 :         if ( ( _nIndex < 0 ) || ( _nIndex >= (sal_Int32)m_aParameters.size() ) )
     324           0 :             throw IndexOutOfBoundsException();
     325             : 
     326           0 :         return makeAny( Reference< XPropertySet >( m_aParameters[ _nIndex ].get() ) );
     327             :     }
     328             : 
     329             : 
     330           0 :     Reference< XEnumeration > ParameterWrapperContainer::createEnumeration() throw( RuntimeException, std::exception )
     331             :     {
     332           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     333           0 :         impl_checkDisposed_throw();
     334             : 
     335           0 :         return new ::comphelper::OEnumerationByIndex( static_cast< XIndexAccess* >( this ) );
     336             :     }
     337             : 
     338             : 
     339          40 :     void ParameterWrapperContainer::impl_checkDisposed_throw()
     340             :     {
     341          40 :         if ( rBHelper.bDisposed )
     342           0 :             throw DisposedException( OUString(), *this );
     343          40 :     }
     344             : 
     345             : 
     346          40 :     void SAL_CALL ParameterWrapperContainer::disposing()
     347             :     {
     348          40 :         ::osl::MutexGuard aGuard( m_aMutex );
     349          40 :         impl_checkDisposed_throw();
     350             : 
     351         138 :         for (   Parameters::const_iterator param = m_aParameters.begin();
     352          92 :                 param != m_aParameters.end();
     353             :                 ++param
     354             :             )
     355             :         {
     356           6 :             (*param)->dispose();
     357             :         }
     358             : 
     359          80 :         Parameters aEmpty;
     360          80 :         m_aParameters.swap( aEmpty );
     361          40 :     }
     362             : 
     363             : 
     364             : } } // namespace dbtools::param
     365             : 
     366             : 
     367             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10