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 "tablecontainer.hxx"
21 #include "dbastrings.hrc"
23 #include <comphelper/property.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <tools/debug.hxx>
26 #include <comphelper/enumhelper.hxx>
27 #include "core_resource.hxx"
28 #include "core_resource.hrc"
29 #include <com/sun/star/sdb/CommandType.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/PropertyState.hpp>
32 #include <com/sun/star/beans/XPropertyState.hpp>
33 #include <com/sun/star/sdb/TableDefinition.hpp>
34 #include <com/sun/star/sdbc/XConnection.hpp>
35 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
36 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 #include <com/sun/star/sdbc/KeyRule.hpp>
39 #include <com/sun/star/sdbcx/KeyType.hpp>
40 #include <com/sun/star/sdbc/ColumnValue.hpp>
41 #include <com/sun/star/sdbc/XRow.hpp>
42 #include <comphelper/types.hxx>
43 #include <connectivity/dbtools.hxx>
44 #include <comphelper/extract.hxx>
45 #include <connectivity/dbexception.hxx>
46 #include "TableDeco.hxx"
47 #include "sdbcoretools.hxx"
48 #include "ContainerMediator.hxx"
49 #include "definitioncolumn.hxx"
50 #include "objectnameapproval.hxx"
51 #include <tools/diagnose_ex.h>
53 using namespace dbaccess
;
54 using namespace dbtools
;
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::lang
;
57 using namespace ::com::sun::star::beans
;
58 using namespace ::com::sun::star::sdbc
;
59 using namespace ::com::sun::star::sdb
;
60 using namespace ::com::sun::star::sdbcx
;
61 using namespace ::com::sun::star::container
;
62 using namespace ::com::sun::star::util
;
63 using namespace ::osl
;
64 using namespace ::comphelper
;
65 using namespace ::cppu
;
66 using namespace ::connectivity::sdbcx
;
70 bool lcl_isPropertySetDefaulted(const Sequence
< OUString
>& _aNames
,const Reference
<XPropertySet
>& _xProp
)
72 Reference
<XPropertyState
> xState(_xProp
,UNO_QUERY
);
75 const OUString
* pIter
= _aNames
.getConstArray();
76 const OUString
* pEnd
= pIter
+ _aNames
.getLength();
77 for(;pIter
!= pEnd
;++pIter
)
81 PropertyState aState
= xState
->getPropertyState(*pIter
);
82 if ( aState
!= PropertyState_DEFAULT_VALUE
)
85 catch(const Exception
&)
87 SAL_WARN("dbaccess", "lcl_isPropertySetDefaulted: Exception caught!" );
90 return ( pIter
== pEnd
);
98 OTableContainer::OTableContainer(::cppu::OWeakObject
& _rParent
,
99 ::osl::Mutex
& _rMutex
,
100 const Reference
< XConnection
>& _xCon
,
102 const Reference
< XNameContainer
>& _xTableDefinitions
,
103 IRefreshListener
* _pRefreshListener
,
104 ::dbtools::IWarningsContainer
* _pWarningsContainer
105 ,oslInterlockedCount
& _nInAppend
)
106 :OFilteredContainer(_rParent
,_rMutex
,_xCon
,_bCase
,_pRefreshListener
,_pWarningsContainer
,_nInAppend
)
107 ,m_xTableDefinitions(_xTableDefinitions
)
108 ,m_pTableMediator( NULL
)
113 OTableContainer::~OTableContainer()
117 void OTableContainer::removeMasterContainerListener()
121 Reference
<XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
122 xCont
->removeContainerListener( this );
124 catch( const Exception
& )
126 DBG_UNHANDLED_EXCEPTION();
130 OUString
OTableContainer::getTableTypeRestriction() const
132 // no restriction at all (other than the ones provided externally)
137 IMPLEMENT_SERVICE_INFO2(OTableContainer
, "com.sun.star.sdb.dbaccess.OTableContainer", SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
)
141 void lcl_createDefintionObject(const OUString
& _rName
142 ,const Reference
< XNameContainer
>& _xTableDefinitions
143 ,Reference
<XPropertySet
>& _xTableDefinition
144 ,Reference
<XNameAccess
>& _xColumnDefinitions
)
146 if ( _xTableDefinitions
.is() )
148 if ( _xTableDefinitions
->hasByName(_rName
) )
149 _xTableDefinition
.set(_xTableDefinitions
->getByName(_rName
),UNO_QUERY
);
153 _xTableDefinition
.set( TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName
), UNO_QUERY
);
154 _xTableDefinitions
->insertByName(_rName
,makeAny(_xTableDefinition
));
156 Reference
<XColumnsSupplier
> xColumnsSupplier(_xTableDefinition
,UNO_QUERY
);
157 if ( xColumnsSupplier
.is() )
158 _xColumnDefinitions
= xColumnsSupplier
->getColumns();
164 connectivity::sdbcx::ObjectType
OTableContainer::createObject(const OUString
& _rName
)
166 Reference
<XColumnsSupplier
> xSup
;
167 if(m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
))
168 xSup
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
170 connectivity::sdbcx::ObjectType xRet
;
171 if ( m_xMetaData
.is() )
173 Reference
<XPropertySet
> xTableDefinition
;
174 Reference
<XNameAccess
> xColumnDefinitions
;
175 lcl_createDefintionObject(_rName
,m_xTableDefinitions
,xTableDefinition
,xColumnDefinitions
);
179 ODBTableDecorator
* pTable
= new ODBTableDecorator( m_xConnection
, xSup
, ::dbtools::getNumberFormats( m_xConnection
) ,xColumnDefinitions
);
185 OUString sCatalog
,sSchema
,sTable
;
186 ::dbtools::qualifiedNameComponents(m_xMetaData
,
191 ::dbtools::eInDataManipulation
);
193 if(!sCatalog
.isEmpty())
194 aCatalog
<<= sCatalog
;
195 OUString sType
,sDescription
;
196 Sequence
< OUString
> aTypeFilter
;
197 getAllTableTypeFilter( aTypeFilter
);
199 Reference
< XResultSet
> xRes
= m_xMetaData
.is() ? m_xMetaData
->getTables(aCatalog
,sSchema
,sTable
,aTypeFilter
) : Reference
< XResultSet
>();
200 if(xRes
.is() && xRes
->next())
202 Reference
< XRow
> xRow(xRes
,UNO_QUERY
);
205 sType
= xRow
->getString(4);
206 sDescription
= xRow
->getString(5);
209 ::comphelper::disposeComponent(xRes
);
210 ODBTable
* pTable
= new ODBTable(this
217 ,xColumnDefinitions
);
221 Reference
<XPropertySet
> xDest(xRet
,UNO_QUERY
);
222 if ( xTableDefinition
.is() )
223 ::comphelper::copyProperties(xTableDefinition
,xDest
);
225 if ( !m_pTableMediator
.is() )
226 m_pTableMediator
= new OContainerMediator(
227 this, m_xTableDefinitions
.get(), m_xConnection
);
228 if ( m_pTableMediator
.is() )
229 m_pTableMediator
->notifyElementCreated(_rName
,xDest
);
235 Reference
< XPropertySet
> OTableContainer::createDescriptor()
237 Reference
< XPropertySet
> xRet
;
239 // first we have to look if the master tables support this
240 // and if so then create a table object as well with the master tables
241 Reference
<XColumnsSupplier
> xMasterColumnsSup
;
242 Reference
<XDataDescriptorFactory
> xDataFactory(m_xMasterContainer
,UNO_QUERY
);
243 if ( xDataFactory
.is() && m_xMetaData
.is() )
245 xMasterColumnsSup
= Reference
< XColumnsSupplier
>( xDataFactory
->createDataDescriptor(), UNO_QUERY
);
246 ODBTableDecorator
* pTable
= new ODBTableDecorator( m_xConnection
, xMasterColumnsSup
, ::dbtools::getNumberFormats( m_xConnection
) ,NULL
);
252 ODBTable
* pTable
= new ODBTable(this, m_xConnection
);
260 ObjectType
OTableContainer::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
262 // append the new table with a create stmt
263 OUString aName
= getString(descriptor
->getPropertyValue(PROPERTY_NAME
));
264 if(m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(aName
))
266 OUString
sMessage(DBACORE_RESSTRING(RID_STR_TABLE_IS_FILTERED
));
267 throw SQLException(sMessage
.replaceAll("$name$", aName
),static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)),SQLSTATE_GENERAL
,1000,Any());
270 Reference
< XConnection
> xConnection( m_xConnection
.get(), UNO_QUERY
);
271 PContainerApprove
pApprove( new ObjectNameApproval( xConnection
, ObjectNameApproval::TypeTable
) );
272 pApprove
->approveElement( aName
, descriptor
);
276 EnsureReset
aReset(m_nInAppend
);
277 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
280 xAppend
->appendByDescriptor(descriptor
);
284 OUString aSql
= ::dbtools::createSqlCreateTableStatement(descriptor
,m_xConnection
);
286 Reference
<XConnection
> xCon
= m_xConnection
;
287 OSL_ENSURE(xCon
.is(),"Connection is null!");
290 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
292 xStmt
->execute(aSql
);
293 ::comphelper::disposeComponent(xStmt
);
297 catch(const Exception
&)
302 Reference
<XPropertySet
> xTableDefinition
;
303 Reference
<XNameAccess
> xColumnDefinitions
;
304 lcl_createDefintionObject(getNameForObject(descriptor
),m_xTableDefinitions
,xTableDefinition
,xColumnDefinitions
);
305 Reference
<XColumnsSupplier
> xSup(descriptor
,UNO_QUERY
);
306 Reference
<XDataDescriptorFactory
> xFac(xColumnDefinitions
,UNO_QUERY
);
307 Reference
<XAppend
> xAppend(xColumnDefinitions
,UNO_QUERY
);
308 bool bModified
= false;
309 if ( xSup
.is() && xColumnDefinitions
.is() && xFac
.is() && xAppend
.is() )
311 Reference
<XNameAccess
> xNames
= xSup
->getColumns();
314 Reference
<XPropertySet
> xProp
= xFac
->createDataDescriptor();
315 Sequence
< OUString
> aSeq
= xNames
->getElementNames();
316 const OUString
* pIter
= aSeq
.getConstArray();
317 const OUString
* pEnd
= pIter
+ aSeq
.getLength();
318 for(;pIter
!= pEnd
;++pIter
)
320 if ( !xColumnDefinitions
->hasByName(*pIter
) )
322 Reference
<XPropertySet
> xColumn(xNames
->getByName(*pIter
),UNO_QUERY
);
323 if ( !OColumnSettings::hasDefaultSettings( xColumn
) )
325 ::comphelper::copyProperties( xColumn
, xProp
);
326 xAppend
->appendByDescriptor( xProp
);
333 const static OUString s_pTableProps
[] = { OUString(PROPERTY_FILTER
), OUString(PROPERTY_ORDER
)
334 , OUString(PROPERTY_APPLYFILTER
), OUString(PROPERTY_FONT
)
335 , OUString(PROPERTY_ROW_HEIGHT
), OUString(PROPERTY_TEXTCOLOR
)
336 , OUString(PROPERTY_TEXTLINECOLOR
), OUString(PROPERTY_TEXTEMPHASIS
)
337 , OUString(PROPERTY_TEXTRELIEF
) };
338 Sequence
< OUString
> aNames(s_pTableProps
,sizeof(s_pTableProps
)/sizeof(s_pTableProps
[0]));
339 if ( bModified
|| !lcl_isPropertySetDefaulted(aNames
,xTableDefinition
) )
340 ::dbaccess::notifyDataSourceModified(m_xTableDefinitions
,true);
342 return createObject( _rForName
);
346 void OTableContainer::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
351 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
353 xDrop
->dropByName(_sElementName
);
356 OUString sCatalog
,sSchema
,sTable
,sComposedName
;
358 bool bIsView
= false;
359 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
360 if ( xTable
.is() && m_xMetaData
.is() )
362 if( m_xMetaData
.is() && m_xMetaData
->supportsCatalogsInTableDefinitions() )
363 xTable
->getPropertyValue(PROPERTY_CATALOGNAME
) >>= sCatalog
;
364 if( m_xMetaData
.is() && m_xMetaData
->supportsSchemasInTableDefinitions() )
365 xTable
->getPropertyValue(PROPERTY_SCHEMANAME
) >>= sSchema
;
366 xTable
->getPropertyValue(PROPERTY_NAME
) >>= sTable
;
368 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::eInTableDefinitions
);
371 xTable
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
372 bIsView
= sType
.equalsIgnoreAsciiCase("VIEW");
375 if(sComposedName
.isEmpty())
376 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
378 OUString
aSql("DROP ");
380 if ( bIsView
) // here we have a view
384 aSql
+= sComposedName
;
385 Reference
<XConnection
> xCon
= m_xConnection
;
386 OSL_ENSURE(xCon
.is(),"Connection is null!");
389 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
391 xStmt
->execute(aSql
);
392 ::comphelper::disposeComponent(xStmt
);
396 if ( m_xTableDefinitions
.is() && m_xTableDefinitions
->hasByName(_sElementName
) )
398 m_xTableDefinitions
->removeByName(_sElementName
);
401 catch(const Exception
&)
409 void SAL_CALL
OTableContainer::elementInserted( const ContainerEvent
& Event
) throw (RuntimeException
, std::exception
)
411 ::osl::MutexGuard
aGuard(m_rMutex
);
413 Event
.Accessor
>>= sName
;
414 if ( !m_nInAppend
&& !hasByName(sName
) )
416 if(!m_xMasterContainer
.is() || m_xMasterContainer
->hasByName(sName
))
418 ObjectType xName
= createObject(sName
);
419 insertElement(sName
,xName
);
420 // and notify our listeners
421 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(sName
), makeAny(xName
), Any());
422 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvent
);
427 void SAL_CALL
OTableContainer::elementRemoved( const ContainerEvent
& /*Event*/ ) throw (RuntimeException
, std::exception
)
431 void SAL_CALL
OTableContainer::elementReplaced( const ContainerEvent
& Event
) throw (RuntimeException
, std::exception
)
433 // create a new config entry
435 OUString sOldComposedName
,sNewComposedName
;
436 Event
.ReplacedElement
>>= sOldComposedName
;
437 Event
.Accessor
>>= sNewComposedName
;
439 renameObject(sOldComposedName
,sNewComposedName
);
443 void SAL_CALL
OTableContainer::disposing()
445 OFilteredContainer::disposing();
446 // say goodbye to our listeners
447 m_xTableDefinitions
= NULL
;
448 m_pTableMediator
= NULL
;
451 void SAL_CALL
OTableContainer::disposing( const ::com::sun::star::lang::EventObject
& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
455 void OTableContainer::addMasterContainerListener()
459 Reference
< XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
460 xCont
->addContainerListener( this );
462 catch( const Exception
& )
464 DBG_UNHANDLED_EXCEPTION();
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */