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 :
20 : #include "ContainerMediator.hxx"
21 : #include "apitools.hxx"
22 : #include "column.hxx"
23 : #include "core_resource.hrc"
24 : #include "core_resource.hxx"
25 : #include "dbastrings.hrc"
26 : #include "sdbcoretools.hxx"
27 :
28 : #include <com/sun/star/lang/DisposedException.hpp>
29 : #include <com/sun/star/sdbc/ColumnValue.hpp>
30 : #include <com/sun/star/sdbc/DataType.hpp>
31 :
32 : #include <comphelper/enumhelper.hxx>
33 : #include <comphelper/extract.hxx>
34 : #include <comphelper/property.hxx>
35 : #include <comphelper/seqstream.hxx>
36 : #include <comphelper/sequence.hxx>
37 : #include <comphelper/types.hxx>
38 : #include <connectivity/TTableHelper.hxx>
39 : #include <connectivity/dbexception.hxx>
40 : #include <connectivity/dbtools.hxx>
41 : #include <cppuhelper/supportsservice.hxx>
42 : #include <cppuhelper/typeprovider.hxx>
43 : #include <osl/diagnose.h>
44 : #include <tools/debug.hxx>
45 :
46 : #include <algorithm>
47 :
48 : using namespace dbaccess;
49 : using namespace connectivity;
50 : using namespace ::com::sun::star::sdbc;
51 : using namespace ::com::sun::star::sdbcx;
52 : using namespace ::com::sun::star::beans;
53 : using namespace ::com::sun::star::uno;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::awt;
56 : using namespace ::com::sun::star::io;
57 : using namespace ::com::sun::star::container;
58 : using namespace ::com::sun::star::util;
59 : using namespace ::osl;
60 : using namespace ::comphelper;
61 : using namespace ::cppu;
62 :
63 :
64 : // OColumn
65 0 : OColumn::OColumn( const bool _bNameIsReadOnly )
66 : :OColumnBase( m_aMutex )
67 0 : ,::comphelper::OPropertyContainer( OColumnBase::rBHelper )
68 : {
69 :
70 : registerProperty( PROPERTY_NAME, PROPERTY_ID_NAME, _bNameIsReadOnly ? PropertyAttribute::READONLY : 0,
71 0 : &m_sName, ::getCppuType( &m_sName ) );
72 0 : }
73 :
74 0 : OColumn::~OColumn()
75 : {
76 0 : }
77 :
78 : // com::sun::star::lang::XTypeProvider
79 0 : Sequence< Type > OColumn::getTypes() throw (RuntimeException, std::exception)
80 : {
81 : return ::comphelper::concatSequences(
82 : OColumnBase::getTypes(),
83 : getBaseTypes()
84 0 : );
85 : }
86 :
87 : // com::sun::star::uno::XInterface
88 0 : IMPLEMENT_FORWARD_XINTERFACE2( OColumn, OColumnBase, ::comphelper::OPropertyContainer )
89 :
90 : // ::com::sun::star::lang::XServiceInfo
91 0 : OUString OColumn::getImplementationName( ) throw(RuntimeException, std::exception)
92 : {
93 0 : return OUString("com.sun.star.sdb.OColumn");
94 : }
95 :
96 0 : sal_Bool OColumn::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
97 : {
98 0 : return cppu::supportsService(this, _rServiceName);
99 : }
100 :
101 0 : Sequence< OUString > OColumn::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
102 : {
103 0 : Sequence< OUString > aSNS( 1 );
104 0 : aSNS[0] = SERVICE_SDBCX_COLUMN;
105 0 : return aSNS;
106 : }
107 :
108 : // OComponentHelper
109 0 : void OColumn::disposing()
110 : {
111 0 : OPropertyContainer::disposing();
112 0 : }
113 :
114 : // com::sun::star::beans::XPropertySet
115 0 : Reference< XPropertySetInfo > OColumn::getPropertySetInfo() throw (RuntimeException, std::exception)
116 : {
117 0 : return createPropertySetInfo( getInfoHelper() ) ;
118 : }
119 :
120 0 : OUString SAL_CALL OColumn::getName( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
121 : {
122 0 : return m_sName;
123 : }
124 :
125 0 : void SAL_CALL OColumn::setName( const OUString& _rName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
126 : {
127 0 : m_sName = _rName;
128 0 : }
129 :
130 0 : void OColumn::fireValueChange(const ::connectivity::ORowSetValue& /*_rOldValue*/)
131 : {
132 : OSL_FAIL( "OColumn::fireValueChange: not implemented!" );
133 0 : }
134 :
135 0 : void OColumn::registerProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void* _pPointerToMember, const Type& _rMemberType )
136 : {
137 0 : ::comphelper::OPropertyContainer::registerProperty( _rName, _nHandle, _nAttributes, _pPointerToMember, _rMemberType );
138 0 : }
139 :
140 0 : void OColumn::registerMayBeVoidProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, Any* _pPointerToMember, const Type& _rExpectedType )
141 : {
142 0 : ::comphelper::OPropertyContainer::registerMayBeVoidProperty( _rName, _nHandle, _nAttributes, _pPointerToMember, _rExpectedType );
143 0 : }
144 :
145 0 : void OColumn::registerPropertyNoMember( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Type& _rType, const void* _pInitialValue )
146 : {
147 0 : ::comphelper::OPropertyContainer::registerPropertyNoMember( _rName, _nHandle, _nAttributes, _rType, _pInitialValue );
148 0 : }
149 :
150 : // OColumns
151 :
152 0 : OColumns::OColumns(::cppu::OWeakObject& _rParent,
153 : ::osl::Mutex& _rMutex,
154 : sal_Bool _bCaseSensitive,const ::std::vector< OUString> &_rVector,
155 : IColumnFactory* _pColFactory,
156 : ::connectivity::sdbcx::IRefreshableColumns* _pRefresh,
157 : sal_Bool _bAddColumn,
158 : sal_Bool _bDropColumn,
159 : sal_Bool _bUseHardRef)
160 : : OColumns_BASE(_rParent,_bCaseSensitive,_rMutex,_rVector,_bUseHardRef)
161 : ,m_pMediator(NULL)
162 : ,m_xDrvColumns(NULL)
163 : ,m_pColFactoryImpl(_pColFactory)
164 : ,m_pRefreshColumns(_pRefresh)
165 : ,m_bInitialized(sal_False)
166 : ,m_bAddColumn(_bAddColumn)
167 0 : ,m_bDropColumn(_bDropColumn)
168 : {
169 0 : }
170 :
171 0 : OColumns::OColumns(::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,
172 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _rxDrvColumns,
173 : sal_Bool _bCaseSensitive,const ::std::vector< OUString> &_rVector,
174 : IColumnFactory* _pColFactory,
175 : ::connectivity::sdbcx::IRefreshableColumns* _pRefresh,
176 : sal_Bool _bAddColumn,
177 : sal_Bool _bDropColumn,
178 : sal_Bool _bUseHardRef)
179 : : OColumns_BASE(_rParent,_bCaseSensitive,_rMutex,_rVector,_bUseHardRef)
180 : ,m_pMediator(NULL)
181 : ,m_xDrvColumns(_rxDrvColumns)
182 : ,m_pColFactoryImpl(_pColFactory)
183 : ,m_pRefreshColumns(_pRefresh)
184 : ,m_bInitialized(sal_False)
185 : ,m_bAddColumn(_bAddColumn)
186 0 : ,m_bDropColumn(_bDropColumn)
187 : {
188 0 : }
189 :
190 0 : OColumns::~OColumns()
191 : {
192 0 : }
193 :
194 : // XServiceInfo
195 0 : OUString OColumns::getImplementationName( ) throw(RuntimeException, std::exception)
196 : {
197 0 : return OUString("com.sun.star.sdb.OColumns");
198 : }
199 :
200 0 : sal_Bool OColumns::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
201 : {
202 0 : return cppu::supportsService(this, _rServiceName);
203 : }
204 :
205 0 : Sequence< OUString > OColumns::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
206 : {
207 0 : Sequence< OUString > aSNS( 1 );
208 0 : aSNS[0] = SERVICE_SDBCX_CONTAINER;
209 0 : return aSNS;
210 : }
211 :
212 0 : void OColumns::append( const OUString& _rName, OColumn* _pColumn )
213 : {
214 0 : MutexGuard aGuard(m_rMutex);
215 :
216 : OSL_ENSURE( _pColumn, "OColumns::append: invalid column!" );
217 : OSL_ENSURE( !m_pElements->exists( _rName ),"OColumns::append: Column already exists");
218 :
219 0 : _pColumn->m_sName = _rName;
220 :
221 : // now really insert the column
222 0 : insertElement( _rName, _pColumn );
223 0 : }
224 :
225 0 : void OColumns::clearColumns()
226 : {
227 0 : MutexGuard aGuard(m_rMutex);
228 0 : disposing();
229 0 : }
230 :
231 0 : void SAL_CALL OColumns::disposing(void)
232 : {
233 0 : MutexGuard aGuard(m_rMutex);
234 0 : m_xDrvColumns = NULL;
235 0 : m_pMediator = NULL;
236 0 : m_pColFactoryImpl = NULL;
237 0 : OColumns_BASE::disposing();
238 0 : }
239 :
240 0 : void OColumns::impl_refresh() throw(::com::sun::star::uno::RuntimeException)
241 : {
242 0 : if (m_pRefreshColumns)
243 0 : m_pRefreshColumns->refreshColumns();
244 0 : }
245 :
246 0 : connectivity::sdbcx::ObjectType OColumns::createObject(const OUString& _rName)
247 : {
248 : OSL_ENSURE(m_pColFactoryImpl, "OColumns::createObject: no column factory!");
249 :
250 0 : connectivity::sdbcx::ObjectType xRet;
251 0 : if ( m_pColFactoryImpl )
252 : {
253 0 : xRet = m_pColFactoryImpl->createColumn(_rName);
254 0 : Reference<XChild> xChild(xRet,UNO_QUERY);
255 0 : if ( xChild.is() )
256 0 : xChild->setParent(static_cast<XChild*>(static_cast<TXChild*>(this)));
257 : }
258 :
259 0 : Reference<XPropertySet> xDest(xRet,UNO_QUERY);
260 0 : if ( m_pMediator && xDest.is() )
261 0 : m_pMediator->notifyElementCreated(_rName,xDest);
262 :
263 0 : return xRet;
264 : }
265 :
266 0 : Reference< XPropertySet > OColumns::createDescriptor()
267 : {
268 0 : if ( m_pColFactoryImpl )
269 : {
270 0 : Reference<XPropertySet> xRet = m_pColFactoryImpl->createColumnDescriptor();
271 0 : Reference<XChild> xChild(xRet,UNO_QUERY);
272 0 : if ( xChild.is() )
273 0 : xChild->setParent(static_cast<XChild*>(static_cast<TXChild*>(this)));
274 0 : return xRet;
275 : }
276 : else
277 0 : return Reference< XPropertySet >();
278 : }
279 :
280 0 : Any SAL_CALL OColumns::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
281 : {
282 0 : Any aRet;
283 0 : if(m_xDrvColumns.is())
284 : {
285 0 : aRet = m_xDrvColumns->queryInterface(rType);
286 0 : if ( aRet.hasValue() )
287 0 : aRet = OColumns_BASE::queryInterface( rType);
288 0 : if ( !aRet.hasValue() )
289 0 : aRet = TXChild::queryInterface( rType);
290 0 : return aRet;
291 : }
292 0 : else if(!m_pTable || (m_pTable && !m_pTable->isNew()))
293 : {
294 0 : if(!m_bAddColumn && rType == getCppuType( (Reference<XAppend>*)0))
295 0 : return Any();
296 0 : if(!m_bDropColumn && rType == getCppuType( (Reference<XDrop>*)0))
297 0 : return Any();
298 : }
299 :
300 0 : aRet = OColumns_BASE::queryInterface( rType);
301 0 : if ( !aRet.hasValue() )
302 0 : aRet = TXChild::queryInterface( rType);
303 0 : return aRet;
304 : }
305 :
306 0 : Sequence< Type > SAL_CALL OColumns::getTypes( ) throw(RuntimeException, std::exception)
307 : {
308 0 : sal_Bool bAppendFound = sal_False,bDropFound = sal_False;
309 :
310 0 : sal_Int32 nSize = 0;
311 0 : Type aAppendType = getCppuType( (Reference<XAppend>*)0);
312 0 : Type aDropType = getCppuType( (Reference<XDrop>*)0);
313 0 : if(m_xDrvColumns.is())
314 : {
315 0 : Reference<XTypeProvider> xTypes(m_xDrvColumns,UNO_QUERY);
316 0 : Sequence< Type > aTypes(xTypes->getTypes());
317 :
318 0 : const Type* pBegin = aTypes.getConstArray();
319 0 : const Type* pEnd = pBegin + aTypes.getLength();
320 0 : for (;pBegin != pEnd ; ++pBegin)
321 : {
322 0 : if(aAppendType == *pBegin)
323 0 : bAppendFound = sal_True;
324 0 : else if(aDropType == *pBegin)
325 0 : bDropFound = sal_True;
326 : }
327 0 : nSize = (bDropFound ? (bAppendFound ? 0 : 1) : (bAppendFound ? 1 : 2));
328 : }
329 : else
330 : {
331 0 : nSize = ((m_pTable && m_pTable->isNew()) ? 0 :
332 : ((m_bDropColumn ?
333 0 : (m_bAddColumn ? 0 : 1) : (m_bAddColumn ? 1 : 2))));
334 0 : bDropFound = (m_pTable && m_pTable->isNew()) || m_bDropColumn;
335 0 : bAppendFound = (m_pTable && m_pTable->isNew()) || m_bAddColumn;
336 : }
337 0 : Sequence< Type > aTypes(::comphelper::concatSequences(OColumns_BASE::getTypes(),TXChild::getTypes()));
338 0 : Sequence< Type > aRet(aTypes.getLength() - nSize);
339 :
340 0 : const Type* pBegin = aTypes.getConstArray();
341 0 : const Type* pEnd = pBegin + aTypes.getLength();
342 0 : for(sal_Int32 i=0;pBegin != pEnd ;++pBegin)
343 : {
344 0 : if(*pBegin != aAppendType && *pBegin != aDropType)
345 0 : aRet.getArray()[i++] = *pBegin;
346 0 : else if(bDropFound && *pBegin == aDropType)
347 0 : aRet.getArray()[i++] = *pBegin;
348 0 : else if(bAppendFound && *pBegin == aAppendType)
349 0 : aRet.getArray()[i++] = *pBegin;
350 : }
351 0 : return aRet;
352 : }
353 :
354 : // XAppend
355 0 : sdbcx::ObjectType OColumns::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
356 : {
357 0 : sdbcx::ObjectType xReturn;
358 :
359 0 : Reference< XAppend > xAppend( m_xDrvColumns, UNO_QUERY );
360 0 : if ( xAppend.is() )
361 : {
362 0 : xAppend->appendByDescriptor(descriptor);
363 0 : xReturn = createObject( _rForName );
364 : }
365 0 : else if ( m_pTable && !m_pTable->isNew() )
366 : {
367 0 : if ( m_bAddColumn )
368 : {
369 0 : Reference< ::com::sun::star::sdb::tools::XTableAlteration> xAlterService = m_pTable->getAlterService();
370 0 : if ( xAlterService.is() )
371 : {
372 0 : xAlterService->addColumn(m_pTable,descriptor);
373 0 : xReturn = createObject( _rForName );
374 : }
375 : else
376 0 : xReturn = OColumns_BASE::appendObject( _rForName, descriptor );
377 : }
378 : else
379 0 : ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_ADD ), static_cast<XChild*>(static_cast<TXChild*>(this)) );
380 : }
381 : else
382 0 : xReturn = cloneDescriptor( descriptor );
383 :
384 0 : if ( m_pColFactoryImpl )
385 0 : m_pColFactoryImpl->columnAppended( descriptor );
386 :
387 0 : ::dbaccess::notifyDataSourceModified(m_xParent,sal_True);
388 :
389 0 : return xReturn;
390 : }
391 :
392 : // XDrop
393 0 : void OColumns::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
394 : {
395 0 : Reference< XDrop > xDrop( m_xDrvColumns, UNO_QUERY );
396 0 : if ( xDrop.is() )
397 : {
398 0 : xDrop->dropByName( _sElementName );
399 : }
400 0 : else if ( m_pTable && !m_pTable->isNew() )
401 : {
402 0 : if ( m_bDropColumn )
403 : {
404 0 : Reference< ::com::sun::star::sdb::tools::XTableAlteration> xAlterService = m_pTable->getAlterService();
405 0 : if ( xAlterService.is() )
406 0 : xAlterService->dropColumn(m_pTable,_sElementName);
407 : else
408 0 : OColumns_BASE::dropObject(_nPos,_sElementName);
409 : }
410 : else
411 0 : ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_DROP ), static_cast<XChild*>(static_cast<TXChild*>(this)) );
412 : }
413 :
414 0 : if ( m_pColFactoryImpl )
415 0 : m_pColFactoryImpl->columnDropped(_sElementName);
416 :
417 0 : ::dbaccess::notifyDataSourceModified(m_xParent,sal_True);
418 0 : }
419 :
420 0 : Reference< XInterface > SAL_CALL OColumns::getParent( ) throw (RuntimeException, std::exception)
421 : {
422 0 : ::osl::MutexGuard aGuard(m_rMutex);
423 0 : return m_xParent;
424 : }
425 :
426 0 : void SAL_CALL OColumns::setParent( const Reference< XInterface >& _xParent ) throw (NoSupportException, RuntimeException, std::exception)
427 : {
428 0 : ::osl::MutexGuard aGuard(m_rMutex);
429 0 : m_xParent = _xParent;
430 0 : }
431 :
432 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|