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