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