1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <ContainerMediator.hxx>
22 #include <strings.hrc>
23 #include <strings.hxx>
24 #include <core_resource.hxx>
25 #include <stringconstants.hxx>
26 #include <sdbcoretools.hxx>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/sdb/tools/XTableAlteration.hpp>
31 #include <comphelper/sequence.hxx>
32 #include <comphelper/types.hxx>
33 #include <comphelper/uno3.hxx>
34 #include <connectivity/TTableHelper.hxx>
35 #include <connectivity/dbexception.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <osl/diagnose.h>
39 using namespace dbaccess
;
40 using namespace connectivity
;
41 using namespace ::com::sun::star::sdbc
;
42 using namespace ::com::sun::star::sdbcx
;
43 using namespace ::com::sun::star::beans
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::awt
;
47 using namespace ::com::sun::star::io
;
48 using namespace ::com::sun::star::container
;
49 using namespace ::com::sun::star::util
;
50 using namespace ::osl
;
51 using namespace ::comphelper
;
52 using namespace ::cppu
;
56 OColumn::OColumn( const bool _bNameIsReadOnly
)
57 :OColumnBase( m_aMutex
)
58 ,::comphelper::OPropertyContainer( OColumnBase::rBHelper
)
61 registerProperty( PROPERTY_NAME
, PROPERTY_ID_NAME
, _bNameIsReadOnly
? PropertyAttribute::READONLY
: 0,
62 &m_sName
, cppu::UnoType
<decltype(m_sName
)>::get() );
69 // css::lang::XTypeProvider
70 Sequence
< Type
> OColumn::getTypes()
72 return ::comphelper::concatSequences(
73 OColumnBase::getTypes(),
78 // css::uno::XInterface
79 IMPLEMENT_FORWARD_XINTERFACE2( OColumn
, OColumnBase
, ::comphelper::OPropertyContainer
)
81 // css::lang::XServiceInfo
82 OUString
OColumn::getImplementationName( )
84 return "com.sun.star.sdb.OColumn";
87 sal_Bool
OColumn::supportsService( const OUString
& _rServiceName
)
89 return cppu::supportsService(this, _rServiceName
);
92 Sequence
< OUString
> OColumn::getSupportedServiceNames( )
94 return { SERVICE_SDBCX_COLUMN
};
98 void OColumn::disposing()
100 OPropertyContainer::disposing();
103 // css::beans::XPropertySet
104 Reference
< XPropertySetInfo
> OColumn::getPropertySetInfo()
106 return createPropertySetInfo( getInfoHelper() ) ;
109 OUString SAL_CALL
OColumn::getName( )
114 void SAL_CALL
OColumn::setName( const OUString
& _rName
)
119 void OColumn::registerProperty( const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
, void* _pPointerToMember
, const Type
& _rMemberType
)
121 ::comphelper::OPropertyContainer::registerProperty( _rName
, _nHandle
, _nAttributes
, _pPointerToMember
, _rMemberType
);
124 void OColumn::registerMayBeVoidProperty( const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
, Any
* _pPointerToMember
, const Type
& _rExpectedType
)
126 ::comphelper::OPropertyContainer::registerMayBeVoidProperty( _rName
, _nHandle
, _nAttributes
, _pPointerToMember
, _rExpectedType
);
131 OColumns::OColumns(::cppu::OWeakObject
& _rParent
,
132 ::osl::Mutex
& _rMutex
,
133 bool _bCaseSensitive
,const std::vector
< OUString
> &_rVector
,
134 IColumnFactory
* _pColFactory
,
135 ::connectivity::sdbcx::IRefreshableColumns
* _pRefresh
,
139 : OColumns_BASE(_rParent
,_bCaseSensitive
,_rMutex
,_rVector
,_bUseHardRef
)
140 ,m_pMediator(nullptr)
141 ,m_pColFactoryImpl(_pColFactory
)
142 ,m_pRefreshColumns(_pRefresh
)
143 ,m_bInitialized(false)
144 ,m_bAddColumn(_bAddColumn
)
145 ,m_bDropColumn(_bDropColumn
)
149 OColumns::OColumns(::cppu::OWeakObject
& _rParent
, ::osl::Mutex
& _rMutex
,
150 const css::uno::Reference
< css::container::XNameAccess
>& _rxDrvColumns
,
151 bool _bCaseSensitive
,const std::vector
< OUString
> &_rVector
,
152 IColumnFactory
* _pColFactory
,
153 ::connectivity::sdbcx::IRefreshableColumns
* _pRefresh
,
157 : OColumns_BASE(_rParent
,_bCaseSensitive
,_rMutex
,_rVector
,_bUseHardRef
)
158 ,m_pMediator(nullptr)
159 ,m_xDrvColumns(_rxDrvColumns
)
160 ,m_pColFactoryImpl(_pColFactory
)
161 ,m_pRefreshColumns(_pRefresh
)
162 ,m_bInitialized(false)
163 ,m_bAddColumn(_bAddColumn
)
164 ,m_bDropColumn(_bDropColumn
)
168 OColumns::~OColumns()
173 OUString
OColumns::getImplementationName( )
175 return "com.sun.star.sdb.OColumns";
178 sal_Bool
OColumns::supportsService( const OUString
& _rServiceName
)
180 return cppu::supportsService(this, _rServiceName
);
183 Sequence
< OUString
> OColumns::getSupportedServiceNames( )
185 return { SERVICE_SDBCX_CONTAINER
};
188 void OColumns::append( const OUString
& _rName
, OColumn
* _pColumn
)
190 MutexGuard
aGuard(m_rMutex
);
192 OSL_ENSURE( _pColumn
, "OColumns::append: invalid column!" );
193 OSL_ENSURE( !m_pElements
->exists( _rName
),"OColumns::append: Column already exists");
195 _pColumn
->m_sName
= _rName
;
197 // now really insert the column
198 insertElement( _rName
, _pColumn
);
201 void OColumns::clearColumns()
203 MutexGuard
aGuard(m_rMutex
);
207 void OColumns::disposing()
209 MutexGuard
aGuard(m_rMutex
);
210 m_xDrvColumns
= nullptr;
211 m_pMediator
= nullptr;
212 m_pColFactoryImpl
= nullptr;
213 OColumns_BASE::disposing();
216 void OColumns::impl_refresh()
218 if (m_pRefreshColumns
)
219 m_pRefreshColumns
->refreshColumns();
222 connectivity::sdbcx::ObjectType
OColumns::createObject(const OUString
& _rName
)
224 OSL_ENSURE(m_pColFactoryImpl
, "OColumns::createObject: no column factory!");
226 connectivity::sdbcx::ObjectType xRet
;
227 if ( m_pColFactoryImpl
)
229 xRet
= m_pColFactoryImpl
->createColumn(_rName
);
230 Reference
<XChild
> xChild(xRet
,UNO_QUERY
);
232 xChild
->setParent(static_cast<XChild
*>(static_cast<TXChild
*>(this)));
235 Reference
<XPropertySet
> xDest(xRet
,UNO_QUERY
);
236 if ( m_pMediator
&& xDest
.is() )
237 m_pMediator
->notifyElementCreated(_rName
,xDest
);
242 Reference
< XPropertySet
> OColumns::createDescriptor()
244 if ( m_pColFactoryImpl
)
246 Reference
<XPropertySet
> xRet
= m_pColFactoryImpl
->createColumnDescriptor();
247 Reference
<XChild
> xChild(xRet
,UNO_QUERY
);
249 xChild
->setParent(static_cast<XChild
*>(static_cast<TXChild
*>(this)));
253 return Reference
< XPropertySet
>();
256 Any SAL_CALL
OColumns::queryInterface( const Type
& rType
)
259 if(m_xDrvColumns
.is())
261 aRet
= m_xDrvColumns
->queryInterface(rType
);
262 if ( aRet
.hasValue() )
263 aRet
= OColumns_BASE::queryInterface( rType
);
264 if ( !aRet
.hasValue() )
265 aRet
= TXChild::queryInterface( rType
);
268 else if(!m_pTable
|| !m_pTable
->isNew())
270 if(!m_bAddColumn
&& rType
== cppu::UnoType
<XAppend
>::get())
272 if(!m_bDropColumn
&& rType
== cppu::UnoType
<XDrop
>::get())
276 aRet
= OColumns_BASE::queryInterface( rType
);
277 if ( !aRet
.hasValue() )
278 aRet
= TXChild::queryInterface( rType
);
282 Sequence
< Type
> SAL_CALL
OColumns::getTypes( )
284 bool bAppendFound
= false,bDropFound
= false;
287 Type aAppendType
= cppu::UnoType
<XAppend
>::get();
288 Type aDropType
= cppu::UnoType
<XDrop
>::get();
289 if(m_xDrvColumns
.is())
291 Reference
<XTypeProvider
> xTypes(m_xDrvColumns
,UNO_QUERY
);
292 Sequence
< Type
> aTypes(xTypes
->getTypes());
294 const Type
* pBegin
= aTypes
.getConstArray();
295 const Type
* pEnd
= pBegin
+ aTypes
.getLength();
296 for (;pBegin
!= pEnd
; ++pBegin
)
298 if(aAppendType
== *pBegin
)
300 else if(aDropType
== *pBegin
)
303 nSize
= (bDropFound
? (bAppendFound
? 0 : 1) : (bAppendFound
? 1 : 2));
307 if (m_pTable
&& m_pTable
->isNew())
309 else if (m_bDropColumn
)
310 nSize
= m_bAddColumn
? 0 : 1;
312 nSize
= m_bAddColumn
? 1 : 2;
313 bDropFound
= (m_pTable
&& m_pTable
->isNew()) || m_bDropColumn
;
314 bAppendFound
= (m_pTable
&& m_pTable
->isNew()) || m_bAddColumn
;
316 Sequence
< Type
> aTypes(::comphelper::concatSequences(OColumns_BASE::getTypes(),TXChild::getTypes()));
317 Sequence
< Type
> aRet(aTypes
.getLength() - nSize
);
319 const Type
* pBegin
= aTypes
.getConstArray();
320 const Type
* pEnd
= pBegin
+ aTypes
.getLength();
321 for(sal_Int32 i
=0;pBegin
!= pEnd
;++pBegin
)
323 if(*pBegin
!= aAppendType
&& *pBegin
!= aDropType
)
324 aRet
.getArray()[i
++] = *pBegin
;
325 else if(bDropFound
&& *pBegin
== aDropType
)
326 aRet
.getArray()[i
++] = *pBegin
;
327 else if(bAppendFound
&& *pBegin
== aAppendType
)
328 aRet
.getArray()[i
++] = *pBegin
;
334 sdbcx::ObjectType
OColumns::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
336 sdbcx::ObjectType xReturn
;
338 Reference
< XAppend
> xAppend( m_xDrvColumns
, UNO_QUERY
);
341 xAppend
->appendByDescriptor(descriptor
);
342 xReturn
= createObject( _rForName
);
344 else if ( m_pTable
&& !m_pTable
->isNew() )
348 Reference
< css::sdb::tools::XTableAlteration
> xAlterService
= m_pTable
->getAlterService();
349 if ( xAlterService
.is() )
351 xAlterService
->addColumn(m_pTable
,descriptor
);
352 xReturn
= createObject( _rForName
);
355 xReturn
= OColumns_BASE::appendObject( _rForName
, descriptor
);
358 ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_ADD
), static_cast<XChild
*>(static_cast<TXChild
*>(this)) );
361 xReturn
= cloneDescriptor( descriptor
);
363 if ( m_pColFactoryImpl
)
364 m_pColFactoryImpl
->columnAppended( descriptor
);
366 ::dbaccess::notifyDataSourceModified(m_xParent
);
372 void OColumns::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
374 Reference
< XDrop
> xDrop( m_xDrvColumns
, UNO_QUERY
);
377 xDrop
->dropByName( _sElementName
);
379 else if ( m_pTable
&& !m_pTable
->isNew() )
383 Reference
< css::sdb::tools::XTableAlteration
> xAlterService
= m_pTable
->getAlterService();
384 if ( xAlterService
.is() )
385 xAlterService
->dropColumn(m_pTable
,_sElementName
);
387 OColumns_BASE::dropObject(_nPos
,_sElementName
);
390 ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_DROP
), static_cast<XChild
*>(static_cast<TXChild
*>(this)) );
393 if ( m_pColFactoryImpl
)
394 m_pColFactoryImpl
->columnDropped(_sElementName
);
396 ::dbaccess::notifyDataSourceModified(m_xParent
);
399 Reference
< XInterface
> SAL_CALL
OColumns::getParent( )
401 ::osl::MutexGuard
aGuard(m_rMutex
);
405 void SAL_CALL
OColumns::setParent( const Reference
< XInterface
>& _xParent
)
407 ::osl::MutexGuard
aGuard(m_rMutex
);
408 m_xParent
= _xParent
;
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */