bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / commontools / paramwrapper.cxx
blobfc575550b57029b611b08db07bd04904b5e27826
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #include <connectivity/paramwrapper.hxx>
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>
27 #include <tools/diagnose_ex.h>
28 #include <comphelper/enumhelper.hxx>
30 #define PROPERTY_ID_VALUE 1000
33 namespace dbtools
35 namespace param
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;
62 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
63 namespace DataType = ::com::sun::star::sdbc::DataType;
65 ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn )
66 :PropertyBase( m_aBHelper )
67 ,m_xDelegator( _rxColumn )
69 if ( m_xDelegator.is() )
70 m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
71 if ( !m_xDelegatorPSI.is() )
72 throw RuntimeException();
76 ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn,
77 const Reference< XParameters >& _rxAllParameters, const ::std::vector< sal_Int32 >& _rIndexes )
78 :PropertyBase( m_aBHelper )
79 ,m_aIndexes( _rIndexes )
80 ,m_xDelegator( _rxColumn )
81 ,m_xValueDestination( _rxAllParameters )
83 if ( m_xDelegator.is() )
84 m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
85 if ( !m_xDelegatorPSI.is() )
86 throw RuntimeException();
88 OSL_ENSURE( !m_aIndexes.empty(), "ParameterWrapper::ParameterWrapper: sure about the indexes?" );
92 ParameterWrapper::~ParameterWrapper()
97 IMPLEMENT_FORWARD_REFCOUNT( ParameterWrapper, UnoBase )
99 css::uno::Any ParameterWrapper::queryInterface(css::uno::Type const & aType)
100 throw (css::uno::RuntimeException, std::exception)
102 css::uno::Any a(UnoBase::queryInterface(aType));
103 if (!a.hasValue()) {
104 a = PropertyBase::queryInterface(aType);
105 if (!a.hasValue()
106 && aType == cppu::UnoType<css::lang::XTypeProvider>::get())
108 a <<= css::uno::Reference<css::lang::XTypeProvider>(this);
111 return a;
115 Sequence< Type > SAL_CALL ParameterWrapper::getTypes( ) throw(RuntimeException, std::exception)
117 Sequence< Type > aTypes( 5 );
118 aTypes[ 0 ] = cppu::UnoType<XWeak>::get();
119 aTypes[ 1 ] = cppu::UnoType<XTypeProvider>::get();
120 aTypes[ 2 ] = cppu::UnoType<XPropertySet>::get();
121 aTypes[ 3 ] = cppu::UnoType<XFastPropertySet>::get();
122 aTypes[ 4 ] = cppu::UnoType<XMultiPropertySet>::get();
123 return aTypes;
127 IMPLEMENT_GET_IMPLEMENTATION_ID( ParameterWrapper )
130 OUString ParameterWrapper::impl_getPseudoAggregatePropertyName( sal_Int32 _nHandle ) const
132 Reference< XPropertySetInfo > xInfo = const_cast<ParameterWrapper*>( this )->getPropertySetInfo();
133 Sequence< Property > aProperties = xInfo->getProperties();
134 const Property* pProperties = aProperties.getConstArray();
135 for ( sal_Int32 i = 0; i < aProperties.getLength(); ++i, ++pProperties )
137 if ( pProperties->Handle == _nHandle )
138 return pProperties->Name;
141 OSL_FAIL( "ParameterWrapper::impl_getPseudoAggregatePropertyName: invalid argument!" );
142 return OUString();
146 Reference< XPropertySetInfo > ParameterWrapper::getPropertySetInfo() throw( RuntimeException, std::exception )
148 return createPropertySetInfo( getInfoHelper() );
152 ::cppu::IPropertyArrayHelper& ParameterWrapper::getInfoHelper()
154 if ( !m_pInfoHelper.get() )
156 Sequence< Property > aProperties;
159 aProperties = m_xDelegatorPSI->getProperties();
160 sal_Int32 nProperties( aProperties.getLength() );
161 aProperties.realloc( nProperties + 1 );
162 aProperties[ nProperties ] = Property(
163 OUString( "Value" ),
164 PROPERTY_ID_VALUE,
165 ::cppu::UnoType< Any >::get(),
166 PropertyAttribute::TRANSIENT | PropertyAttribute::MAYBEVOID
169 catch( const Exception& )
171 DBG_UNHANDLED_EXCEPTION();
174 m_pInfoHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties, false ) );
176 return *m_pInfoHelper;
180 sal_Bool ParameterWrapper::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw( IllegalArgumentException )
182 OSL_ENSURE( PROPERTY_ID_VALUE == nHandle, "ParameterWrapper::convertFastPropertyValue: the only non-readonly prop should be our PROPERTY_VALUE!" );
183 (void)nHandle;
185 // we're lazy here ...
186 rOldValue = m_aValue.makeAny();
187 rConvertedValue = rValue;
188 return sal_True; // assume "modified" ...
192 void ParameterWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw( Exception, std::exception )
194 if ( nHandle == PROPERTY_ID_VALUE )
198 // TODO : aParamType & nScale can be obtained within the constructor ....
199 sal_Int32 nParamType = DataType::VARCHAR;
200 OSL_VERIFY( m_xDelegator->getPropertyValue("Type") >>= nParamType );
202 sal_Int32 nScale = 0;
203 if ( m_xDelegatorPSI->hasPropertyByName("Scale") )
204 OSL_VERIFY( m_xDelegator->getPropertyValue("Scale") >>= nScale );
206 if ( m_xValueDestination.is() )
208 for ( ::std::vector< sal_Int32 >::iterator aIter = m_aIndexes.begin(); aIter != m_aIndexes.end(); ++aIter )
210 m_xValueDestination->setObjectWithInfo( *aIter + 1, rValue, nParamType, nScale );
211 // (the index of the parameters is one-based)
215 m_aValue = rValue;
217 catch( SQLException& e )
219 WrappedTargetException aExceptionWrapper;
220 aExceptionWrapper.Context = e.Context;
221 aExceptionWrapper.Message = e.Message;
222 aExceptionWrapper.TargetException <<= e;
223 throw WrappedTargetException( aExceptionWrapper );
226 else
228 OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
229 m_xDelegator->setPropertyValue( aName, rValue );
234 void ParameterWrapper::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
236 if ( nHandle == PROPERTY_ID_VALUE )
238 rValue = m_aValue.makeAny();
240 else
242 OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
243 rValue = m_xDelegator->getPropertyValue( aName );
248 void SAL_CALL ParameterWrapper::dispose()
250 ::osl::MutexGuard aGuard( m_aMutex );
252 m_aValue.setNull();
253 m_aIndexes.resize(0);
254 m_xDelegator.clear();
255 m_xDelegatorPSI.clear();
256 m_xValueDestination.clear();
258 m_aBHelper.bDisposed = sal_True;
261 ParameterWrapperContainer::ParameterWrapperContainer()
262 :ParameterWrapperContainer_Base( m_aMutex )
267 ParameterWrapperContainer::ParameterWrapperContainer( const Reference< XSingleSelectQueryAnalyzer >& _rxComposer )
268 :ParameterWrapperContainer_Base( m_aMutex )
270 Reference< XParametersSupplier > xSuppParams( _rxComposer, UNO_QUERY_THROW );
271 Reference< XIndexAccess > xParameters( xSuppParams->getParameters(), UNO_QUERY_THROW );
272 sal_Int32 nParamCount( xParameters->getCount() );
273 m_aParameters.reserve( nParamCount );
274 for ( sal_Int32 i=0; i<nParamCount; ++i )
276 m_aParameters.push_back( new ParameterWrapper( Reference< XPropertySet >( xParameters->getByIndex( i ), UNO_QUERY_THROW ) ) );
281 ParameterWrapperContainer::~ParameterWrapperContainer()
286 Type SAL_CALL ParameterWrapperContainer::getElementType() throw( RuntimeException, std::exception )
288 ::osl::MutexGuard aGuard( m_aMutex );
289 impl_checkDisposed_throw();
290 return cppu::UnoType<XPropertySet>::get();
294 sal_Bool SAL_CALL ParameterWrapperContainer::hasElements() throw( RuntimeException, std::exception )
296 ::osl::MutexGuard aGuard( m_aMutex );
297 impl_checkDisposed_throw();
298 return !m_aParameters.empty();
302 sal_Int32 SAL_CALL ParameterWrapperContainer::getCount() throw( RuntimeException, std::exception )
304 ::osl::MutexGuard aGuard( m_aMutex );
305 impl_checkDisposed_throw();
306 return m_aParameters.size();
310 Any SAL_CALL ParameterWrapperContainer::getByIndex( sal_Int32 _nIndex ) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
312 ::osl::MutexGuard aGuard( m_aMutex );
313 impl_checkDisposed_throw();
315 if ( ( _nIndex < 0 ) || ( _nIndex >= (sal_Int32)m_aParameters.size() ) )
316 throw IndexOutOfBoundsException();
318 return makeAny( Reference< XPropertySet >( m_aParameters[ _nIndex ].get() ) );
322 Reference< XEnumeration > ParameterWrapperContainer::createEnumeration() throw( RuntimeException, std::exception )
324 ::osl::MutexGuard aGuard( m_aMutex );
325 impl_checkDisposed_throw();
327 return new ::comphelper::OEnumerationByIndex( static_cast< XIndexAccess* >( this ) );
331 void ParameterWrapperContainer::impl_checkDisposed_throw()
333 if ( rBHelper.bDisposed )
334 throw DisposedException( OUString(), *this );
338 void SAL_CALL ParameterWrapperContainer::disposing()
340 ::osl::MutexGuard aGuard( m_aMutex );
341 impl_checkDisposed_throw();
343 for ( Parameters::const_iterator param = m_aParameters.begin();
344 param != m_aParameters.end();
345 ++param
348 (*param)->dispose();
351 Parameters aEmpty;
352 m_aParameters.swap( aEmpty );
356 } } // namespace dbtools::param
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */