1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablecontainer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "tablecontainer.hxx"
35 #include "dbastrings.hrc"
37 #include <comphelper/property.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <tools/debug.hxx>
40 #include <comphelper/enumhelper.hxx>
41 #include "core_resource.hxx"
42 #include "core_resource.hrc"
43 #include <com/sun/star/sdb/CommandType.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/beans/PropertyState.hpp>
46 #include <com/sun/star/beans/XPropertyState.hpp>
47 #include <com/sun/star/sdbc/XConnection.hpp>
48 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
49 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
50 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
51 #include <com/sun/star/sdbc/KeyRule.hpp>
52 #include <com/sun/star/sdbcx/KeyType.hpp>
53 #include <com/sun/star/sdbc/ColumnValue.hpp>
54 #include <com/sun/star/sdbc/XRow.hpp>
55 #include <comphelper/types.hxx>
56 #include <connectivity/dbtools.hxx>
57 #include <comphelper/extract.hxx>
58 #include <connectivity/dbexception.hxx>
59 #include "TableDeco.hxx"
60 #include "sdbcoretools.hxx"
61 #include "ContainerMediator.hxx"
62 #include "definitioncolumn.hxx"
63 #include "objectnameapproval.hxx"
64 #include <tools/string.hxx>
65 #include <rtl/logfile.hxx>
66 #ifndef TOOLS_DIAGNOSE_EX_H
67 #include <tools/diagnose_ex.h>
69 #ifndef TOOLS_DIAGNOSE_EX_H
70 #include <tools/diagnose_ex.h>
73 using namespace dbaccess
;
74 using namespace dbtools
;
75 using namespace ::com::sun::star::uno
;
76 using namespace ::com::sun::star::lang
;
77 using namespace ::com::sun::star::beans
;
78 using namespace ::com::sun::star::sdbc
;
79 using namespace ::com::sun::star::sdb
;
80 using namespace ::com::sun::star::sdbcx
;
81 using namespace ::com::sun::star::container
;
82 using namespace ::com::sun::star::util
;
83 using namespace ::osl
;
84 using namespace ::comphelper
;
85 using namespace ::cppu
;
86 using namespace ::connectivity::sdbcx
;
90 sal_Bool
lcl_isPropertySetDefaulted(const Sequence
< ::rtl::OUString
>& _aNames
,const Reference
<XPropertySet
>& _xProp
)
92 Reference
<XPropertyState
> xState(_xProp
,UNO_QUERY
);
95 const ::rtl::OUString
* pIter
= _aNames
.getConstArray();
96 const ::rtl::OUString
* pEnd
= pIter
+ _aNames
.getLength();
97 for(;pIter
!= pEnd
;++pIter
)
101 PropertyState aState
= xState
->getPropertyState(*pIter
);
102 if ( aState
!= PropertyState_DEFAULT_VALUE
)
107 OSL_ENSURE( 0, "lcl_isPropertySetDefaulted: Exception caught!" );
110 return ( pIter
== pEnd
);
115 //==========================================================================
117 //==========================================================================
118 DBG_NAME(OTableContainer
)
119 //------------------------------------------------------------------------------
120 OTableContainer::OTableContainer(::cppu::OWeakObject
& _rParent
,
121 ::osl::Mutex
& _rMutex
,
122 const Reference
< XConnection
>& _xCon
,
124 const Reference
< XNameContainer
>& _xTableDefinitions
,
125 IRefreshListener
* _pRefreshListener
,
126 ::dbtools::IWarningsContainer
* _pWarningsContainer
127 ,oslInterlockedCount
& _nInAppend
)
128 :OFilteredContainer(_rParent
,_rMutex
,_xCon
,_bCase
,_pRefreshListener
,_pWarningsContainer
,_nInAppend
)
129 ,m_xTableDefinitions(_xTableDefinitions
)
130 ,m_pTableMediator( NULL
)
131 ,m_bInDrop(sal_False
)
133 DBG_CTOR(OTableContainer
, NULL
);
136 //------------------------------------------------------------------------------
137 OTableContainer::~OTableContainer()
139 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::OTableContainer" );
141 DBG_DTOR(OTableContainer
, NULL
);
144 // -----------------------------------------------------------------------------
145 void OTableContainer::removeMasterContainerListener()
147 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::removeMasterContainerListener" );
150 Reference
<XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
151 xCont
->removeContainerListener( this );
153 catch( const Exception
& )
155 DBG_UNHANDLED_EXCEPTION();
159 // -----------------------------------------------------------------------------
160 ::rtl::OUString
OTableContainer::getTableTypeRestriction() const
162 // no restriction at all (other than the ones provided externally)
163 return ::rtl::OUString();
166 // -----------------------------------------------------------------------------
168 //------------------------------------------------------------------------------
169 IMPLEMENT_SERVICE_INFO2(OTableContainer
, "com.sun.star.sdb.dbaccess.OTableContainer", SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
)
171 // -----------------------------------------------------------------------------
174 void lcl_createDefintionObject(const ::rtl::OUString
& _rName
175 ,const Reference
< XNameContainer
>& _xTableDefinitions
176 ,Reference
<XPropertySet
>& _xTableDefinition
177 ,Reference
<XNameAccess
>& _xColumnDefinitions
178 ,sal_Bool _bModified
)
180 if ( _xTableDefinitions
.is() )
182 if ( _xTableDefinitions
->hasByName(_rName
) )
183 _xTableDefinition
.set(_xTableDefinitions
->getByName(_rName
),UNO_QUERY
);
186 Sequence
< Any
> aArguments(1);
187 PropertyValue aValue
;
189 aValue
.Name
= PROPERTY_NAME
;
190 aValue
.Value
<<= _rName
;
191 aArguments
[0] <<= aValue
;
192 _xTableDefinition
.set(::comphelper::getProcessServiceFactory()->createInstanceWithArguments(SERVICE_SDB_TABLEDEFINITION
,aArguments
),UNO_QUERY
);
193 _xTableDefinitions
->insertByName(_rName
,makeAny(_xTableDefinition
));
194 ::dbaccess::notifyDataSourceModified(_xTableDefinitions
,_bModified
);
196 Reference
<XColumnsSupplier
> xColumnsSupplier(_xTableDefinition
,UNO_QUERY
);
197 if ( xColumnsSupplier
.is() )
198 _xColumnDefinitions
= xColumnsSupplier
->getColumns();
201 // -------------------------------------------------------------------------
203 // -------------------------------------------------------------------------
204 connectivity::sdbcx::ObjectType
OTableContainer::createObject(const ::rtl::OUString
& _rName
)
206 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::createObject" );
207 Reference
<XColumnsSupplier
> xSup
;
208 if(m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
))
209 xSup
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
211 connectivity::sdbcx::ObjectType xRet
;
212 if ( m_xMetaData
.is() )
214 Reference
<XPropertySet
> xTableDefinition
;
215 Reference
<XNameAccess
> xColumnDefinitions
;
216 lcl_createDefintionObject(_rName
,m_xTableDefinitions
,xTableDefinition
,xColumnDefinitions
,sal_False
);
220 ODBTableDecorator
* pTable
= new ODBTableDecorator( m_xConnection
, xSup
, ::dbtools::getNumberFormats( m_xConnection
) ,xColumnDefinitions
);
226 ::rtl::OUString sCatalog
,sSchema
,sTable
;
227 ::dbtools::qualifiedNameComponents(m_xMetaData
,
232 ::dbtools::eInDataManipulation
);
234 if(sCatalog
.getLength())
235 aCatalog
<<= sCatalog
;
236 ::rtl::OUString sType
,sDescription
;
237 Sequence
< ::rtl::OUString
> aTypeFilter
;
238 getAllTableTypeFilter( aTypeFilter
);
240 Reference
< XResultSet
> xRes
= m_xMetaData
.is() ? m_xMetaData
->getTables(aCatalog
,sSchema
,sTable
,aTypeFilter
) : Reference
< XResultSet
>();
241 if(xRes
.is() && xRes
->next())
243 Reference
< XRow
> xRow(xRes
,UNO_QUERY
);
246 sType
= xRow
->getString(4);
247 sDescription
= xRow
->getString(5);
250 ::comphelper::disposeComponent(xRes
);
251 ODBTable
* pTable
= new ODBTable(this
258 ,xColumnDefinitions
);
262 Reference
<XPropertySet
> xDest(xRet
,UNO_QUERY
);
263 if ( xTableDefinition
.is() )
264 ::comphelper::copyProperties(xTableDefinition
,xDest
);
266 if ( !m_pTableMediator
.is() )
267 m_pTableMediator
= new OContainerMediator(
268 this, m_xTableDefinitions
.get(), m_xConnection
, OContainerMediator::eTables
);
269 if ( m_pTableMediator
.is() )
270 m_pTableMediator
->notifyElementCreated(_rName
,xDest
);
275 // -----------------------------------------------------------------------------
276 Reference
< XPropertySet
> OTableContainer::createDescriptor()
278 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::createDescriptor" );
279 Reference
< XPropertySet
> xRet
;
281 // frist we have to look if the master tables does support this
282 // and if then create a table object as well with the master tables
283 Reference
<XColumnsSupplier
> xMasterColumnsSup
;
284 Reference
<XDataDescriptorFactory
> xDataFactory(m_xMasterContainer
,UNO_QUERY
);
285 if ( xDataFactory
.is() && m_xMetaData
.is() )
287 xMasterColumnsSup
= Reference
< XColumnsSupplier
>( xDataFactory
->createDataDescriptor(), UNO_QUERY
);
288 ODBTableDecorator
* pTable
= new ODBTableDecorator( m_xConnection
, xMasterColumnsSup
, ::dbtools::getNumberFormats( m_xConnection
) ,NULL
);
294 ODBTable
* pTable
= new ODBTable(this, m_xConnection
);
300 // -----------------------------------------------------------------------------
302 ObjectType
OTableContainer::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
304 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::appendObject" );
305 // append the new table with a create stmt
306 ::rtl::OUString aName
= getString(descriptor
->getPropertyValue(PROPERTY_NAME
));
307 if(m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(aName
))
309 String
sMessage(DBACORE_RESSTRING(RID_STR_TABLE_IS_FILTERED
));
310 sMessage
.SearchAndReplaceAscii("$name$", aName
);
311 throw SQLException(sMessage
,static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)),SQLSTATE_GENERAL
,1000,Any());
314 Reference
< XConnection
> xConnection( m_xConnection
.get(), UNO_QUERY
);
315 PContainerApprove
pApprove( new ObjectNameApproval( xConnection
, ObjectNameApproval::TypeTable
) );
316 pApprove
->approveElement( aName
, descriptor
);
320 EnsureReset
aReset(m_nInAppend
);
321 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
324 xAppend
->appendByDescriptor(descriptor
);
328 ::rtl::OUString aSql
= ::dbtools::createSqlCreateTableStatement(descriptor
,m_xConnection
);
330 Reference
<XConnection
> xCon
= m_xConnection
;
331 OSL_ENSURE(xCon
.is(),"Connection is null!");
334 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
336 xStmt
->execute(aSql
);
337 ::comphelper::disposeComponent(xStmt
);
346 Reference
<XPropertySet
> xTableDefinition
;
347 Reference
<XNameAccess
> xColumnDefinitions
;
348 lcl_createDefintionObject(getNameForObject(descriptor
),m_xTableDefinitions
,xTableDefinition
,xColumnDefinitions
,sal_False
);
349 Reference
<XColumnsSupplier
> xSup(descriptor
,UNO_QUERY
);
350 Reference
<XDataDescriptorFactory
> xFac(xColumnDefinitions
,UNO_QUERY
);
351 Reference
<XAppend
> xAppend(xColumnDefinitions
,UNO_QUERY
);
352 sal_Bool bModified
= sal_False
;
353 if ( xSup
.is() && xColumnDefinitions
.is() && xFac
.is() && xAppend
.is() )
355 Reference
<XNameAccess
> xNames
= xSup
->getColumns();
358 Reference
<XPropertySet
> xProp
= xFac
->createDataDescriptor();
359 Sequence
< ::rtl::OUString
> aSeq
= xNames
->getElementNames();
360 const ::rtl::OUString
* pIter
= aSeq
.getConstArray();
361 const ::rtl::OUString
* pEnd
= pIter
+ aSeq
.getLength();
362 for(;pIter
!= pEnd
;++pIter
)
364 if ( !xColumnDefinitions
->hasByName(*pIter
) )
366 Reference
<XPropertySet
> xColumn(xNames
->getByName(*pIter
),UNO_QUERY
);
367 OColumnSettings
* pColumnSettings
= NULL
;
368 if ( ::comphelper::getImplementation( pColumnSettings
, xColumn
) )
370 if ( ( pColumnSettings
&& !pColumnSettings
->isDefaulted() ) )
372 ::comphelper::copyProperties(xColumn
,xProp
);
373 xAppend
->appendByDescriptor(xProp
);
374 bModified
= sal_True
;
381 const static ::rtl::OUString s_pTableProps
[] = { ::rtl::OUString(PROPERTY_FILTER
), ::rtl::OUString(PROPERTY_ORDER
)
382 , ::rtl::OUString(PROPERTY_APPLYFILTER
), ::rtl::OUString(PROPERTY_FONT
)
383 , ::rtl::OUString(PROPERTY_ROW_HEIGHT
), ::rtl::OUString(PROPERTY_TEXTCOLOR
)
384 , ::rtl::OUString(PROPERTY_TEXTLINECOLOR
), ::rtl::OUString(PROPERTY_TEXTEMPHASIS
)
385 , ::rtl::OUString(PROPERTY_TEXTRELIEF
) };
386 Sequence
< ::rtl::OUString
> aNames(s_pTableProps
,sizeof(s_pTableProps
)/sizeof(s_pTableProps
[0]));
387 if ( bModified
|| !lcl_isPropertySetDefaulted(aNames
,xTableDefinition
) )
388 ::dbaccess::notifyDataSourceModified(m_xTableDefinitions
,sal_True
);
390 return createObject( _rForName
);
392 // -------------------------------------------------------------------------
394 void OTableContainer::dropObject(sal_Int32 _nPos
,const ::rtl::OUString _sElementName
)
396 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::dropObject" );
397 m_bInDrop
= sal_True
;
400 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
402 xDrop
->dropByName(_sElementName
);
405 ::rtl::OUString sCatalog
,sSchema
,sTable
,sComposedName
;
407 sal_Bool bIsView
= sal_False
;
408 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
409 if ( xTable
.is() && m_xMetaData
.is() )
411 if( m_xMetaData
.is() && m_xMetaData
->supportsCatalogsInTableDefinitions() )
412 xTable
->getPropertyValue(PROPERTY_CATALOGNAME
) >>= sCatalog
;
413 if( m_xMetaData
.is() && m_xMetaData
->supportsSchemasInTableDefinitions() )
414 xTable
->getPropertyValue(PROPERTY_SCHEMANAME
) >>= sSchema
;
415 xTable
->getPropertyValue(PROPERTY_NAME
) >>= sTable
;
417 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, sal_True
, ::dbtools::eInTableDefinitions
);
419 ::rtl::OUString sType
;
420 xTable
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
421 bIsView
= sType
.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW")));
424 if(!sComposedName
.getLength())
425 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
427 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("DROP ");
430 if ( bIsView
) // here we have a view
431 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW "));
433 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TABLE "));
434 aSql
+= sComposedName
;
435 Reference
<XConnection
> xCon
= m_xConnection
;
436 OSL_ENSURE(xCon
.is(),"Connection is null!");
439 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
441 xStmt
->execute(aSql
);
442 ::comphelper::disposeComponent(xStmt
);
446 if ( m_xTableDefinitions
.is() && m_xTableDefinitions
->hasByName(_sElementName
) )
448 m_xTableDefinitions
->removeByName(_sElementName
);
453 m_bInDrop
= sal_False
;
456 m_bInDrop
= sal_False
;
458 // -----------------------------------------------------------------------------
459 void SAL_CALL
OTableContainer::elementInserted( const ContainerEvent
& Event
) throw (RuntimeException
)
461 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::elementInserted" );
462 ::osl::MutexGuard
aGuard(m_rMutex
);
463 ::rtl::OUString sName
;
464 Event
.Accessor
>>= sName
;
465 if ( !m_nInAppend
&& !hasByName(sName
) )
467 if(!m_xMasterContainer
.is() || m_xMasterContainer
->hasByName(sName
))
469 ObjectType xName
= createObject(sName
);
470 insertElement(sName
,xName
);
471 // and notify our listeners
472 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(sName
), makeAny(xName
), Any());
473 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvent
);
477 // -----------------------------------------------------------------------------
478 void SAL_CALL
OTableContainer::elementRemoved( const ContainerEvent
& /*Event*/ ) throw (RuntimeException
)
480 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::elementRemoved" );
482 // -----------------------------------------------------------------------------
483 void SAL_CALL
OTableContainer::elementReplaced( const ContainerEvent
& Event
) throw (RuntimeException
)
485 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::elementReplaced" );
486 // create a new config entry
488 ::rtl::OUString sOldComposedName
,sNewComposedName
;
489 Event
.ReplacedElement
>>= sOldComposedName
;
490 Event
.Accessor
>>= sNewComposedName
;
492 renameObject(sOldComposedName
,sNewComposedName
);
495 // -----------------------------------------------------------------------------
496 void SAL_CALL
OTableContainer::disposing()
498 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::disposing" );
499 OFilteredContainer::disposing();
500 // say our listeners goobye
501 m_xTableDefinitions
= NULL
;
502 m_pTableMediator
= NULL
;
504 // -----------------------------------------------------------------------------
505 void SAL_CALL
OTableContainer::disposing( const ::com::sun::star::lang::EventObject
& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException
)
507 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "api", "Ocke.Janssen@sun.com", "OTableContainer::disposing" );
510 // -----------------------------------------------------------------------------
511 void OTableContainer::addMasterContainerListener()
515 Reference
< XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
516 xCont
->addContainerListener( this );
518 catch( const Exception
& )
520 DBG_UNHANDLED_EXCEPTION();