calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / dbaccess / source / core / api / column.cxx
blob661624c2e5c046717ce841328e23da006db1e876
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <column.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 <utility>
38 #include <osl/diagnose.h>
40 using namespace dbaccess;
41 using namespace connectivity;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::sdbcx;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::awt;
48 using namespace ::com::sun::star::io;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::util;
51 using namespace ::osl;
52 using namespace ::comphelper;
53 using namespace ::cppu;
56 // OColumn
57 OColumn::OColumn( const bool _bNameIsReadOnly )
58 :OColumnBase( m_aMutex )
59 ,::comphelper::OPropertyContainer( OColumnBase::rBHelper )
62 registerProperty( PROPERTY_NAME, PROPERTY_ID_NAME, _bNameIsReadOnly ? PropertyAttribute::READONLY : 0,
63 &m_sName, cppu::UnoType<decltype(m_sName)>::get() );
66 OColumn::~OColumn()
70 // css::lang::XTypeProvider
71 Sequence< Type > OColumn::getTypes()
73 return ::comphelper::concatSequences(
74 OColumnBase::getTypes(),
75 getBaseTypes()
79 // css::uno::XInterface
80 IMPLEMENT_FORWARD_XINTERFACE2( OColumn, OColumnBase, ::comphelper::OPropertyContainer )
82 // css::lang::XServiceInfo
83 OUString OColumn::getImplementationName( )
85 return "com.sun.star.sdb.OColumn";
88 sal_Bool OColumn::supportsService( const OUString& _rServiceName )
90 return cppu::supportsService(this, _rServiceName);
93 Sequence< OUString > OColumn::getSupportedServiceNames( )
95 return { SERVICE_SDBCX_COLUMN };
98 // OComponentHelper
99 void OColumn::disposing()
101 OPropertyContainer::disposing();
104 // css::beans::XPropertySet
105 Reference< XPropertySetInfo > OColumn::getPropertySetInfo()
107 return createPropertySetInfo( getInfoHelper() ) ;
110 OUString SAL_CALL OColumn::getName( )
112 return m_sName;
115 void SAL_CALL OColumn::setName( const OUString& _rName )
117 m_sName = _rName;
120 void OColumn::registerProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void* _pPointerToMember, const Type& _rMemberType )
122 ::comphelper::OPropertyContainer::registerProperty( _rName, _nHandle, _nAttributes, _pPointerToMember, _rMemberType );
125 void OColumn::registerMayBeVoidProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, Any* _pPointerToMember, const Type& _rExpectedType )
127 ::comphelper::OPropertyContainer::registerMayBeVoidProperty( _rName, _nHandle, _nAttributes, _pPointerToMember, _rExpectedType );
130 // OColumns
132 OColumns::OColumns(::cppu::OWeakObject& _rParent,
133 ::osl::Mutex& _rMutex,
134 bool _bCaseSensitive,const std::vector< OUString> &_rVector,
135 IColumnFactory* _pColFactory,
136 ::connectivity::sdbcx::IRefreshableColumns* _pRefresh,
137 bool _bAddColumn,
138 bool _bDropColumn,
139 bool _bUseHardRef)
140 : OColumns_BASE(_rParent,_bCaseSensitive,_rMutex,_rVector,_bUseHardRef)
141 ,m_pMediator(nullptr)
142 ,m_pColFactoryImpl(_pColFactory)
143 ,m_pRefreshColumns(_pRefresh)
144 ,m_bInitialized(false)
145 ,m_bAddColumn(_bAddColumn)
146 ,m_bDropColumn(_bDropColumn)
150 OColumns::OColumns(::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,
151 css::uno::Reference< css::container::XNameAccess > _xDrvColumns,
152 bool _bCaseSensitive,const std::vector< OUString> &_rVector,
153 IColumnFactory* _pColFactory,
154 ::connectivity::sdbcx::IRefreshableColumns* _pRefresh,
155 bool _bAddColumn,
156 bool _bDropColumn,
157 bool _bUseHardRef)
158 : OColumns_BASE(_rParent,_bCaseSensitive,_rMutex,_rVector,_bUseHardRef)
159 ,m_pMediator(nullptr)
160 ,m_xDrvColumns(std::move(_xDrvColumns))
161 ,m_pColFactoryImpl(_pColFactory)
162 ,m_pRefreshColumns(_pRefresh)
163 ,m_bInitialized(false)
164 ,m_bAddColumn(_bAddColumn)
165 ,m_bDropColumn(_bDropColumn)
169 OColumns::~OColumns()
173 // XServiceInfo
174 OUString OColumns::getImplementationName( )
176 return "com.sun.star.sdb.OColumns";
179 sal_Bool OColumns::supportsService( const OUString& _rServiceName )
181 return cppu::supportsService(this, _rServiceName);
184 Sequence< OUString > OColumns::getSupportedServiceNames( )
186 return { SERVICE_SDBCX_CONTAINER };
189 void OColumns::append( const OUString& _rName, OColumn* _pColumn )
191 MutexGuard aGuard(m_rMutex);
193 OSL_ENSURE( _pColumn, "OColumns::append: invalid column!" );
194 OSL_ENSURE( !m_pElements->exists( _rName ),"OColumns::append: Column already exists");
196 _pColumn->m_sName = _rName;
198 // now really insert the column
199 insertElement( _rName, _pColumn );
202 void OColumns::clearColumns()
204 MutexGuard aGuard(m_rMutex);
205 disposing();
208 void OColumns::disposing()
210 MutexGuard aGuard(m_rMutex);
211 m_xDrvColumns = nullptr;
212 m_pMediator = nullptr;
213 m_pColFactoryImpl = nullptr;
214 OColumns_BASE::disposing();
217 void OColumns::impl_refresh()
219 if (m_pRefreshColumns)
220 m_pRefreshColumns->refreshColumns();
223 connectivity::sdbcx::ObjectType OColumns::createObject(const OUString& _rName)
225 OSL_ENSURE(m_pColFactoryImpl, "OColumns::createObject: no column factory!");
227 connectivity::sdbcx::ObjectType xRet;
228 if ( m_pColFactoryImpl )
230 xRet = m_pColFactoryImpl->createColumn(_rName);
231 Reference<XChild> xChild(xRet,UNO_QUERY);
232 if ( xChild.is() )
233 xChild->setParent(static_cast<XChild*>(static_cast<TXChild*>(this)));
236 Reference<XPropertySet> xDest(xRet,UNO_QUERY);
237 if ( m_pMediator && xDest.is() )
238 m_pMediator->notifyElementCreated(_rName,xDest);
240 return xRet;
243 Reference< XPropertySet > OColumns::createDescriptor()
245 if ( m_pColFactoryImpl )
247 Reference<XPropertySet> xRet = m_pColFactoryImpl->createColumnDescriptor();
248 Reference<XChild> xChild(xRet,UNO_QUERY);
249 if ( xChild.is() )
250 xChild->setParent(static_cast<XChild*>(static_cast<TXChild*>(this)));
251 return xRet;
253 else
254 return Reference< XPropertySet >();
257 Any SAL_CALL OColumns::queryInterface( const Type & rType )
259 Any aRet;
260 if(m_xDrvColumns.is())
262 aRet = m_xDrvColumns->queryInterface(rType);
263 if ( aRet.hasValue() )
264 aRet = OColumns_BASE::queryInterface( rType);
265 if ( !aRet.hasValue() )
266 aRet = TXChild::queryInterface( rType);
267 return aRet;
269 else if(!m_pTable || !m_pTable->isNew())
271 if(!m_bAddColumn && rType == cppu::UnoType<XAppend>::get())
272 return Any();
273 if(!m_bDropColumn && rType == cppu::UnoType<XDrop>::get())
274 return Any();
277 aRet = OColumns_BASE::queryInterface( rType);
278 if ( !aRet.hasValue() )
279 aRet = TXChild::queryInterface( rType);
280 return aRet;
283 Sequence< Type > SAL_CALL OColumns::getTypes( )
285 bool bAppendFound = false,bDropFound = false;
287 sal_Int32 nSize = 0;
288 Type aAppendType = cppu::UnoType<XAppend>::get();
289 Type aDropType = cppu::UnoType<XDrop>::get();
290 if(m_xDrvColumns.is())
292 Reference<XTypeProvider> xTypes(m_xDrvColumns,UNO_QUERY);
293 Sequence< Type > aTypes(xTypes->getTypes());
295 const Type* pBegin = aTypes.getConstArray();
296 const Type* pEnd = pBegin + aTypes.getLength();
297 for (;pBegin != pEnd ; ++pBegin)
299 if(aAppendType == *pBegin)
300 bAppendFound = true;
301 else if(aDropType == *pBegin)
302 bDropFound = true;
304 nSize = (bDropFound ? (bAppendFound ? 0 : 1) : (bAppendFound ? 1 : 2));
306 else
308 if (m_pTable && m_pTable->isNew())
309 nSize = 0;
310 else if (m_bDropColumn)
311 nSize = m_bAddColumn ? 0 : 1;
312 else
313 nSize = m_bAddColumn ? 1 : 2;
314 bDropFound = (m_pTable && m_pTable->isNew()) || m_bDropColumn;
315 bAppendFound = (m_pTable && m_pTable->isNew()) || m_bAddColumn;
317 Sequence< Type > aTypes(::comphelper::concatSequences(OColumns_BASE::getTypes(),TXChild::getTypes()));
318 Sequence< Type > aRet(aTypes.getLength() - nSize);
320 const Type* pBegin = aTypes.getConstArray();
321 const Type* pEnd = pBegin + aTypes.getLength();
322 for(sal_Int32 i=0;pBegin != pEnd ;++pBegin)
324 if(*pBegin != aAppendType && *pBegin != aDropType)
325 aRet.getArray()[i++] = *pBegin;
326 else if(bDropFound && *pBegin == aDropType)
327 aRet.getArray()[i++] = *pBegin;
328 else if(bAppendFound && *pBegin == aAppendType)
329 aRet.getArray()[i++] = *pBegin;
331 return aRet;
334 // XAppend
335 sdbcx::ObjectType OColumns::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
337 sdbcx::ObjectType xReturn;
339 Reference< XAppend > xAppend( m_xDrvColumns, UNO_QUERY );
340 if ( xAppend.is() )
342 xAppend->appendByDescriptor(descriptor);
343 xReturn = createObject( _rForName );
345 else if ( m_pTable && !m_pTable->isNew() )
347 if ( m_bAddColumn )
349 Reference< css::sdb::tools::XTableAlteration> xAlterService = m_pTable->getAlterService();
350 if ( xAlterService.is() )
352 xAlterService->addColumn(m_pTable,descriptor);
353 xReturn = createObject( _rForName );
355 else
356 xReturn = OColumns_BASE::appendObject( _rForName, descriptor );
358 else
359 ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_ADD ), static_cast<XChild*>(static_cast<TXChild*>(this)) );
361 else
362 xReturn = cloneDescriptor( descriptor );
364 if ( m_pColFactoryImpl )
365 m_pColFactoryImpl->columnAppended( descriptor );
367 ::dbaccess::notifyDataSourceModified(m_xParent);
369 return xReturn;
372 // XDrop
373 void OColumns::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
375 Reference< XDrop > xDrop( m_xDrvColumns, UNO_QUERY );
376 if ( xDrop.is() )
378 xDrop->dropByName( _sElementName );
380 else if ( m_pTable && !m_pTable->isNew() )
382 if ( m_bDropColumn )
384 Reference< css::sdb::tools::XTableAlteration> xAlterService = m_pTable->getAlterService();
385 if ( xAlterService.is() )
386 xAlterService->dropColumn(m_pTable,_sElementName);
387 else
388 OColumns_BASE::dropObject(_nPos,_sElementName);
390 else
391 ::dbtools::throwGenericSQLException( DBA_RES( RID_STR_NO_COLUMN_DROP ), static_cast<XChild*>(static_cast<TXChild*>(this)) );
394 if ( m_pColFactoryImpl )
395 m_pColFactoryImpl->columnDropped(_sElementName);
397 ::dbaccess::notifyDataSourceModified(m_xParent);
400 Reference< XInterface > SAL_CALL OColumns::getParent( )
402 ::osl::MutexGuard aGuard(m_rMutex);
403 return m_xParent;
406 void SAL_CALL OColumns::setParent( const Reference< XInterface >& _xParent )
408 ::osl::MutexGuard aGuard(m_rMutex);
409 m_xParent = _xParent;
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */