merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / commontools / paramwrapper.cxx
blob36b20b7f5047c8bc662bac4b3fc9ea505480ddb1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: paramwrapper.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_connectivity.hxx"
32 #include <connectivity/paramwrapper.hxx>
34 /** === begin UNO includes === **/
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 #include <com/sun/star/sdbc/DataType.hpp>
37 #include <com/sun/star/lang/WrappedTargetException.hpp>
38 #include <com/sun/star/sdb/XParametersSupplier.hpp>
39 #include <com/sun/star/lang/DisposedException.hpp>
40 /** === end UNO includes === **/
42 #include <tools/diagnose_ex.h>
43 #include <comphelper/enumhelper.hxx>
45 #define PROPERTY_ID_VALUE 1000
47 //........................................................................
48 namespace dbtools
50 namespace param
52 //........................................................................
54 /** === begin UNO using === **/
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::beans::XPropertySet;
57 using ::com::sun::star::sdbc::XParameters;
58 using ::com::sun::star::uno::Sequence;
59 using ::com::sun::star::uno::Type;
60 using ::com::sun::star::uno::RuntimeException;
61 using ::com::sun::star::uno::XWeak;
62 using ::com::sun::star::beans::XPropertySet;
63 using ::com::sun::star::beans::XFastPropertySet;
64 using ::com::sun::star::beans::XMultiPropertySet;
65 using ::com::sun::star::beans::XPropertySetInfo;
66 using ::com::sun::star::beans::Property;
67 using ::com::sun::star::uno::Exception;
68 using ::com::sun::star::uno::UNO_QUERY_THROW;
69 using ::com::sun::star::uno::Any;
70 using ::com::sun::star::lang::IllegalArgumentException;
71 using ::com::sun::star::sdbc::SQLException;
72 using ::com::sun::star::lang::WrappedTargetException;
73 using ::com::sun::star::lang::IndexOutOfBoundsException;
74 using ::com::sun::star::container::XEnumeration;
75 using ::com::sun::star::sdb::XSingleSelectQueryAnalyzer;
76 using ::com::sun::star::sdb::XParametersSupplier;
77 using ::com::sun::star::lang::DisposedException;
78 /** === end UNO using === **/
79 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
80 namespace DataType = ::com::sun::star::sdbc::DataType;
82 //====================================================================
83 //= ParameterWrapper
84 //====================================================================
85 //--------------------------------------------------------------------
86 ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn )
87 :PropertyBase( m_aBHelper )
88 ,m_xDelegator( _rxColumn )
90 if ( m_xDelegator.is() )
91 m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
92 if ( !m_xDelegatorPSI.is() )
93 throw RuntimeException();
96 //--------------------------------------------------------------------
97 ParameterWrapper::ParameterWrapper( const Reference< XPropertySet >& _rxColumn,
98 const Reference< XParameters >& _rxAllParameters, const ::std::vector< sal_Int32 >& _rIndexes )
99 :PropertyBase( m_aBHelper )
100 ,m_aIndexes( _rIndexes )
101 ,m_xDelegator( _rxColumn )
102 ,m_xValueDestination( _rxAllParameters )
104 if ( m_xDelegator.is() )
105 m_xDelegatorPSI = m_xDelegator->getPropertySetInfo();
106 if ( !m_xDelegatorPSI.is() )
107 throw RuntimeException();
109 OSL_ENSURE( !m_aIndexes.empty(), "ParameterWrapper::ParameterWrapper: sure about the indexes?" );
112 //--------------------------------------------------------------------
113 ParameterWrapper::~ParameterWrapper()
117 //--------------------------------------------------------------------
118 IMPLEMENT_FORWARD_XINTERFACE2( ParameterWrapper, UnoBase, PropertyBase )
120 //--------------------------------------------------------------------
121 Sequence< Type > SAL_CALL ParameterWrapper::getTypes( ) throw(RuntimeException)
123 Sequence< Type > aTypes( 4 );
124 aTypes[ 1 ] = ::getCppuType( static_cast< Reference< XWeak >* >( NULL ) );
125 aTypes[ 1 ] = ::getCppuType( static_cast< Reference< XPropertySet >* >( NULL ) );
126 aTypes[ 2 ] = ::getCppuType( static_cast< Reference< XFastPropertySet >* >( NULL ) );
127 aTypes[ 3 ] = ::getCppuType( static_cast< Reference< XMultiPropertySet >* >( NULL ) );
128 return aTypes;
131 //--------------------------------------------------------------------
132 IMPLEMENT_GET_IMPLEMENTATION_ID( ParameterWrapper )
134 //--------------------------------------------------------------------
135 ::rtl::OUString ParameterWrapper::impl_getPseudoAggregatePropertyName( sal_Int32 _nHandle ) const
137 Reference< XPropertySetInfo > xInfo = const_cast<ParameterWrapper*>( this )->getPropertySetInfo();
138 Sequence< Property > aProperties = xInfo->getProperties();
139 const Property* pProperties = aProperties.getConstArray();
140 for ( sal_Int32 i = 0; i < aProperties.getLength(); ++i, ++pProperties )
142 if ( pProperties->Handle == _nHandle )
143 return pProperties->Name;
146 OSL_ENSURE( sal_False, "ParameterWrapper::impl_getPseudoAggregatePropertyName: invalid argument!" );
147 return ::rtl::OUString();
150 //--------------------------------------------------------------------
151 Reference< XPropertySetInfo > ParameterWrapper::getPropertySetInfo() throw( RuntimeException )
153 return createPropertySetInfo( getInfoHelper() );
156 //--------------------------------------------------------------------
157 ::cppu::IPropertyArrayHelper& ParameterWrapper::getInfoHelper()
159 if ( !m_pInfoHelper.get() )
161 Sequence< Property > aProperties;
164 aProperties = m_xDelegatorPSI->getProperties();
165 sal_Int32 nProperties( aProperties.getLength() );
166 aProperties.realloc( nProperties + 1 );
167 aProperties[ nProperties ] = Property(
168 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Value" ) ),
169 PROPERTY_ID_VALUE,
170 ::cppu::UnoType< Any >::get(),
171 PropertyAttribute::TRANSIENT | PropertyAttribute::MAYBEVOID
174 catch( const Exception& )
176 DBG_UNHANDLED_EXCEPTION();
179 m_pInfoHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties, false ) );
181 return *m_pInfoHelper;
184 //--------------------------------------------------------------------
185 sal_Bool ParameterWrapper::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw( IllegalArgumentException )
187 OSL_ENSURE( PROPERTY_ID_VALUE == nHandle, "ParameterWrapper::convertFastPropertyValue: the only non-readonly prop should be our PROPERTY_VALUE!" );
188 (void)nHandle;
190 // we're lazy here ...
191 rOldValue = m_aValue.makeAny();
192 rConvertedValue = rValue;
193 return sal_True; // assume "modified" ...
196 //--------------------------------------------------------------------
197 void ParameterWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw( Exception )
199 if ( nHandle == PROPERTY_ID_VALUE )
203 // TODO : aParamType & nScale can be obtained within the constructor ....
204 sal_Int32 nParamType = DataType::VARCHAR;
205 OSL_VERIFY( m_xDelegator->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= nParamType );
207 sal_Int32 nScale = 0;
208 if ( m_xDelegatorPSI->hasPropertyByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Scale" ) ) ) )
209 OSL_VERIFY( m_xDelegator->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Scale" ) ) ) >>= nScale );
211 if ( m_xValueDestination.is() )
213 for ( ::std::vector< sal_Int32 >::iterator aIter = m_aIndexes.begin(); aIter != m_aIndexes.end(); ++aIter )
215 m_xValueDestination->setObjectWithInfo( *aIter + 1, rValue, nParamType, nScale );
216 // (the index of the parameters is one-based)
220 m_aValue = rValue;
222 catch( SQLException& e )
224 WrappedTargetException aExceptionWrapper;
225 aExceptionWrapper.Context = e.Context;
226 aExceptionWrapper.Message = e.Message;
227 aExceptionWrapper.TargetException <<= e;
228 throw WrappedTargetException( aExceptionWrapper );
231 else
233 ::rtl::OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
234 m_xDelegator->setPropertyValue( aName, rValue );
238 //--------------------------------------------------------------------
239 void ParameterWrapper::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
241 if ( nHandle == PROPERTY_ID_VALUE )
243 rValue = m_aValue.makeAny();
245 else
247 ::rtl::OUString aName = impl_getPseudoAggregatePropertyName( nHandle );
248 rValue = m_xDelegator->getPropertyValue( aName );
252 //--------------------------------------------------------------------
253 void SAL_CALL ParameterWrapper::dispose()
255 ::osl::MutexGuard aGuard( m_aMutex );
257 m_aValue.setNull();
258 m_aIndexes.resize(0);
259 m_xDelegator.clear();
260 m_xDelegatorPSI.clear();
261 m_xValueDestination.clear();
263 m_aBHelper.bDisposed = sal_True;
266 //====================================================================
267 //= ParameterWrapperContainer
268 //====================================================================
269 //--------------------------------------------------------------------
270 ParameterWrapperContainer::ParameterWrapperContainer()
271 :ParameterWrapperContainer_Base( m_aMutex )
275 //--------------------------------------------------------------------
276 ParameterWrapperContainer::ParameterWrapperContainer( const Reference< XSingleSelectQueryAnalyzer >& _rxComposer )
277 :ParameterWrapperContainer_Base( m_aMutex )
279 Reference< XParametersSupplier > xSuppParams( _rxComposer, UNO_QUERY_THROW );
280 Reference< XIndexAccess > xParameters( xSuppParams->getParameters(), UNO_QUERY_THROW );
281 sal_Int32 nParamCount( xParameters->getCount() );
282 m_aParameters.reserve( nParamCount );
283 for ( sal_Int32 i=0; i<nParamCount; ++i )
285 m_aParameters.push_back( new ParameterWrapper( Reference< XPropertySet >( xParameters->getByIndex( i ), UNO_QUERY_THROW ) ) );
289 //--------------------------------------------------------------------
290 ParameterWrapperContainer::~ParameterWrapperContainer()
294 //--------------------------------------------------------------------
295 Type SAL_CALL ParameterWrapperContainer::getElementType() throw( RuntimeException )
297 ::osl::MutexGuard aGuard( m_aMutex );
298 impl_checkDisposed_throw();
299 return ::getCppuType( static_cast< Reference< XPropertySet >* >( NULL ) );
302 //--------------------------------------------------------------------
303 sal_Bool SAL_CALL ParameterWrapperContainer::hasElements() throw( RuntimeException )
305 ::osl::MutexGuard aGuard( m_aMutex );
306 impl_checkDisposed_throw();
307 return !m_aParameters.empty();
310 //--------------------------------------------------------------------
311 sal_Int32 SAL_CALL ParameterWrapperContainer::getCount() throw( RuntimeException )
313 ::osl::MutexGuard aGuard( m_aMutex );
314 impl_checkDisposed_throw();
315 return m_aParameters.size();
318 //--------------------------------------------------------------------
319 Any SAL_CALL ParameterWrapperContainer::getByIndex( sal_Int32 _nIndex ) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
321 ::osl::MutexGuard aGuard( m_aMutex );
322 impl_checkDisposed_throw();
324 if ( ( _nIndex < 0 ) || ( _nIndex >= (sal_Int32)m_aParameters.size() ) )
325 throw IndexOutOfBoundsException();
327 return makeAny( Reference< XPropertySet >( m_aParameters[ _nIndex ].get() ) );
330 //--------------------------------------------------------------------
331 Reference< XEnumeration > ParameterWrapperContainer::createEnumeration() throw( RuntimeException )
333 ::osl::MutexGuard aGuard( m_aMutex );
334 impl_checkDisposed_throw();
336 return new ::comphelper::OEnumerationByIndex( static_cast< XIndexAccess* >( this ) );
339 //--------------------------------------------------------------------
340 void ParameterWrapperContainer::impl_checkDisposed_throw()
342 if ( rBHelper.bDisposed )
343 throw DisposedException( ::rtl::OUString(), *this );
346 //--------------------------------------------------------------------
347 void SAL_CALL ParameterWrapperContainer::disposing()
349 ::osl::MutexGuard aGuard( m_aMutex );
350 impl_checkDisposed_throw();
352 for ( Parameters::const_iterator param = m_aParameters.begin();
353 param != m_aParameters.end();
354 ++param
357 (*param)->dispose();
360 Parameters aEmpty;
361 m_aParameters.swap( aEmpty );
364 //........................................................................
365 } } // namespace dbtools::param
366 //........................................................................