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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* Effective License of whole file:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
*
* The Contents of this file are made available subject to the terms of
* the GNU Lesser General Public License Version 2.1
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* Contributor(s): Joerg Budischewski
*
* All parts contributed on or after August 2011:
*
* 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/.
*
************************************************************************/
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbc/XParameters.hpp>
#include <cppuhelper/implbase1.hxx>
#include "pq_xcontainer.hxx"
#include "pq_statics.hxx"
#include "pq_tools.hxx"
using osl::MutexGuard;
using com::sun::star::beans::XPropertySet;
using com::sun::star::uno::Any;
using com::sun::star::uno::makeAny;
using com::sun::star::uno::UNO_QUERY;
using com::sun::star::uno::Type;
using com::sun::star::uno::XInterface;
using com::sun::star::uno::Reference;
using com::sun::star::uno::Sequence;
using com::sun::star::uno::RuntimeException;
using com::sun::star::container::NoSuchElementException;
using com::sun::star::container::XEnumeration;
using com::sun::star::container::XContainerListener;
using com::sun::star::container::ContainerEvent;
using com::sun::star::lang::IndexOutOfBoundsException;
using com::sun::star::lang::XEventListener;
using com::sun::star::lang::WrappedTargetException;
using com::sun::star::sdbc::XRow;
using com::sun::star::sdbc::XCloseable;
using com::sun::star::sdbc::XStatement;
using com::sun::star::sdbc::XResultSet;
using com::sun::star::sdbc::XParameters;
using com::sun::star::sdbc::XPreparedStatement;
using com::sun::star::sdbcx::XDataDescriptorFactory;
namespace pq_sdbc_driver
{
class ReplacedBroadcaster : public EventBroadcastHelper
{
ContainerEvent m_event;
public:
ReplacedBroadcaster(
const Reference< XInterface > & source,
const OUString & name,
const Any & newElement,
const OUString & oldElement ) :
m_event( source, makeAny( name ), newElement, makeAny(oldElement) )
{}
virtual void fire( XEventListener * listener ) const SAL_OVERRIDE
{
((XContainerListener*)listener)->elementReplaced( m_event );
}
virtual Type getType() const SAL_OVERRIDE
{
return getCppuType( (Reference< XContainerListener > *)0 );
}
};
class InsertedBroadcaster : public EventBroadcastHelper
{
public:
ContainerEvent m_event;
InsertedBroadcaster(
const Reference< XInterface > & source,
const OUString & name,
const Any & newElement ) :
m_event( source, makeAny( name ), newElement, Any() )
{}
virtual void fire( XEventListener * listener ) const SAL_OVERRIDE
{
((XContainerListener*)listener)->elementInserted( m_event );
}
virtual Type getType() const SAL_OVERRIDE
{
return getCppuType( (Reference< XContainerListener > *)0 );
}
};
class RemovedBroadcaster : public EventBroadcastHelper
{
public:
ContainerEvent m_event;
RemovedBroadcaster(
const Reference< XInterface > & source,
const OUString & name) :
m_event( source, makeAny( name ), Any(), Any() )
{}
virtual void fire( XEventListener * listener ) const SAL_OVERRIDE
{
((XContainerListener*)listener)->elementRemoved( m_event );
}
virtual Type getType() const SAL_OVERRIDE
{
return getCppuType( (Reference< XContainerListener > *)0 );
}
};
Container::Container(
const ::rtl::Reference< RefCountedMutex > & refMutex,
const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
ConnectionSettings *pSettings,
const OUString &type)
: ContainerBase( refMutex->mutex ),
m_refMutex( refMutex ),
m_pSettings( pSettings ),
m_origin( origin ),
m_type( type )
{
}
Any Container::getByName( const OUString& aName )
throw (NoSuchElementException,WrappedTargetException,RuntimeException, std::exception)
{
String2IntMap::const_iterator ii = m_name2index.find( aName );
if( ii == m_name2index.end() )
{
OUStringBuffer buf(128);
buf.appendAscii( "Element " );
buf.append( aName );
buf.appendAscii( " unknown in " );
buf.append( m_type );
buf.appendAscii( "-Container" );
throw NoSuchElementException( buf.makeStringAndClear() , *this );
}
OSL_ASSERT( ii->second >= 0 && ii->second < m_values.getLength() );
return m_values[ ii->second ];
}
Sequence< OUString > Container::getElementNames( )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
Sequence< OUString > ret( m_values.getLength() );
for( String2IntMap::const_iterator ii = m_name2index.begin();
ii != m_name2index.end() ;
++ ii )
{
// give element names in index order !
ret[ii->second] = ii->first;
// ret[i] = ii->first;
}
return ret;
}
sal_Bool Container::hasByName( const OUString& aName )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return m_name2index.find( aName ) != m_name2index.end();
}
// Methods
Type Container::getElementType( )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return Type();
}
sal_Bool Container::hasElements( )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return ! m_name2index.empty();
}
Any Container::getByIndex( sal_Int32 Index )
throw (::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException, std::exception)
{
if( Index < 0 || Index >= m_values.getLength() )
{
OUStringBuffer buf(128);
buf.appendAscii( "Index " );
buf.append( Index );
buf.appendAscii(" out of range for " );
buf.append( m_type );
buf.appendAscii("-Container, expected 0 <= x <= " );
buf.append( (sal_Int32 ) (m_values.getLength() -1));
throw IndexOutOfBoundsException( buf.makeStringAndClear(), *this );
}
return m_values[Index];
}
sal_Int32 Container::getCount()
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return m_values.getLength();
}
class ContainerEnumeration : public ::cppu::WeakImplHelper1< XEnumeration >
{
com::sun::star::uno::Sequence< com::sun::star::uno::Any > m_vec;
sal_Int32 m_index;
public:
ContainerEnumeration( const com::sun::star::uno::Sequence< com::sun::star::uno::Any > &vec )
: m_vec( vec ),
m_index( -1 )
{}
public:
// XEnumeration
virtual sal_Bool SAL_CALL hasMoreElements( )
throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual ::com::sun::star::uno::Any SAL_CALL nextElement( )
throw (::com::sun::star::container::NoSuchElementException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
};
sal_Bool ContainerEnumeration::hasMoreElements()
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return m_vec.getLength() > m_index +1;
}
com::sun::star::uno::Any ContainerEnumeration::nextElement()
throw (::com::sun::star::container::NoSuchElementException,
::com::sun::star::lang::WrappedTargetException,
::com::sun::star::uno::RuntimeException, std::exception)
{
if( ! hasMoreElements() )
{
throw NoSuchElementException(
"NoSuchElementException during enumeration", *this );
}
m_index ++;
return m_vec[m_index];
}
Reference< XEnumeration > Container::createEnumeration( )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
return new ContainerEnumeration( m_values );
}
void Container::addRefreshListener(
const ::com::sun::star::uno::Reference< ::com::sun::star::util::XRefreshListener >& l )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
rBHelper.addListener( getCppuType(&l) , l );
}
void Container::removeRefreshListener(
const ::com::sun::star::uno::Reference< ::com::sun::star::util::XRefreshListener >& l )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
rBHelper.removeListener( getCppuType(&l) , l );
}
void Container::disposing()
{
m_origin.clear();
}
void Container::rename( const OUString &oldName, const OUString &newName )
{
Any newValue;
{
osl::MutexGuard guard ( m_refMutex->mutex );
String2IntMap::iterator ii = m_name2index.find( oldName );
if( ii != m_name2index.end() )
{
sal_Int32 nIndex = ii->second;
newValue = m_values[nIndex];
m_name2index.erase( ii );
m_name2index[ newName ] = nIndex;
}
}
fire( ReplacedBroadcaster( *this, newName, newValue, oldName ) );
fire( RefreshedBroadcaster( *this ) );
}
void Container::dropByName( const OUString& elementName )
throw (::com::sun::star::sdbc::SQLException,
::com::sun::star::container::NoSuchElementException,
::com::sun::star::uno::RuntimeException, std::exception)
{
osl::MutexGuard guard( m_refMutex->mutex );
String2IntMap::const_iterator ii = m_name2index.find( elementName );
if( ii == m_name2index.end() )
{
OUStringBuffer buf( 128 );
buf.appendAscii( "Column " );
buf.append( elementName );
buf.appendAscii( " is unknown in " );
buf.append( m_type );
// buf.appendAscii( " " );
// buf.append( m_schemaName );
// buf.appendAscii( "." );
// buf.append( m_tableName );
buf.appendAscii( " container, so it can't be dropped" );
throw com::sun::star::container::NoSuchElementException(
buf.makeStringAndClear(), *this );
}
dropByIndex( ii->second );
}
void Container::dropByIndex( sal_Int32 index )
throw (::com::sun::star::sdbc::SQLException,
::com::sun::star::lang::IndexOutOfBoundsException,
::com::sun::star::uno::RuntimeException, std::exception)
{
osl::MutexGuard guard( m_refMutex->mutex );
if( index < 0 || index >= m_values.getLength() )
{
OUStringBuffer buf( 128 );
buf.appendAscii( "Index out of range (allowed 0 to " );
buf.append((sal_Int32)(m_values.getLength() -1) );
buf.appendAscii( ", got " );
buf.append( index );
buf.appendAscii( ") in " );
buf.append( m_type );
throw com::sun::star::lang::IndexOutOfBoundsException(
buf.makeStringAndClear(), *this );
}
OUString name;
for( String2IntMap::iterator ii = m_name2index.begin() ;
ii != m_name2index.end() ;
++ ii )
{
if( ii->second == index )
{
name = ii->first;
m_name2index.erase( ii );
break;
}
}
for( int i = index +1 ; i < m_values.getLength() ; i ++ )
{
m_values[i-1] = m_values[i];
// I know, this is expensive, but don't want to maintain another map ...
for( String2IntMap::iterator ii = m_name2index.begin() ;
ii != m_name2index.end() ;
++ ii )
{
if( ii->second == i )
{
ii->second = i-1;
break;
}
}
}
m_values.realloc( m_values.getLength() - 1 );
fire( RemovedBroadcaster( *this, name ) );
}
void Container::append(
const OUString & name,
const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
throw ( ::com::sun::star::container::ElementExistException )
{
osl::MutexGuard guard( m_refMutex->mutex );
if( hasByName( name ) )
{
OUStringBuffer buf( 128 );
buf.appendAscii( "a ");
buf.append( m_type );
buf.appendAscii( " with name " );
buf.append( name );
buf.appendAscii( " already exists in this container" );
throw com::sun::star::container::ElementExistException(
buf.makeStringAndClear() , *this );
}
int index = m_values.getLength();
m_values.realloc( m_values.getLength() + 1 );
m_values[index] = makeAny( descriptor );
m_name2index[name] = index;
fire( InsertedBroadcaster( *this, name, makeAny( descriptor ) ) );
}
void Container::appendByDescriptor(
const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor)
throw (::com::sun::star::sdbc::SQLException,
::com::sun::star::container::ElementExistException,
::com::sun::star::uno::RuntimeException, std::exception)
{
append( extractStringProperty( descriptor, getStatics().NAME ), descriptor );
}
void Container::addContainerListener(
const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
rBHelper.addListener( getCppuType(&l) , l );
}
void Container::removeContainerListener(
const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l )
throw (::com::sun::star::uno::RuntimeException, std::exception)
{
rBHelper.removeListener( getCppuType(&l) , l );
}
void Container::fire( const EventBroadcastHelper &helper )
{
Reference< ::com::sun::star::util::XRefreshListener > l;
cppu::OInterfaceContainerHelper *container = rBHelper.getContainer( helper.getType() );
if( container )
{
cppu::OInterfaceIteratorHelper iterator( * container );
while( iterator.hasMoreElements() )
{
try
{
helper.fire( (XEventListener * ) iterator.next() );
}
catch ( com::sun::star::uno::RuntimeException & )
{
OSL_ENSURE( false, "exception catched" );
// loose coupling, a runtime exception shall not break anything
// TODO: log away as warning !
}
catch( com::sun::star::uno::Exception & )
{
OSL_ENSURE( false, "exception from listener flying through" );
throw;
}
}
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|