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: viewcontainer.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 #ifndef _DBA_CORE_VIEWCONTAINER_HXX_
35 #include "viewcontainer.hxx"
37 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
38 #include "dbastrings.hrc"
40 #ifndef _TOOLS_DEBUG_HXX
41 #include <tools/debug.hxx>
44 #include <tools/wldcrd.hxx>
46 #ifndef _COMPHELPER_ENUMHELPER_HXX_
47 #include <comphelper/enumhelper.hxx>
49 #ifndef _DBA_CORE_RESOURCE_HXX_
50 #include "core_resource.hxx"
52 #ifndef _DBA_CORE_RESOURCE_HRC_
53 #include "core_resource.hrc"
56 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
57 #include <com/sun/star/beans/XPropertySet.hpp>
60 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
61 #include <com/sun/star/sdbc/XConnection.hpp>
63 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
64 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
66 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
67 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
69 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
70 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
72 #ifndef _COM_SUN_STAR_SDBC_KEYRULE_HPP_
73 #include <com/sun/star/sdbc/KeyRule.hpp>
75 #ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
76 #include <com/sun/star/sdbcx/KeyType.hpp>
78 #ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
79 #include <com/sun/star/sdbc/ColumnValue.hpp>
81 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
82 #include <com/sun/star/sdbc/XRow.hpp>
84 #ifndef _COMPHELPER_TYPES_HXX_
85 #include <comphelper/types.hxx>
87 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
88 #include <connectivity/dbtools.hxx>
90 #ifndef _COMPHELPER_EXTRACT_HXX_
91 #include <comphelper/extract.hxx>
93 #ifndef _DBHELPER_DBEXCEPTION_HXX_
94 #include <connectivity/dbexception.hxx>
96 #ifndef _CONNECTIVITY_SDBCX_VIEW_HXX_
97 #include <connectivity/sdbcx/VView.hxx>
99 #ifndef _RTL_USTRBUF_HXX_
100 #include <rtl/ustrbuf.hxx>
103 using namespace dbaccess
;
104 using namespace dbtools
;
105 using namespace ::com::sun::star::uno
;
106 using namespace ::com::sun::star::lang
;
107 using namespace ::com::sun::star::beans
;
108 using namespace ::com::sun::star::sdbc
;
109 using namespace ::com::sun::star::sdb
;
110 using namespace ::com::sun::star::sdbcx
;
111 using namespace ::com::sun::star::util
;
112 using namespace ::com::sun::star::container
;
113 using namespace ::osl
;
114 using namespace ::comphelper
;
115 using namespace ::cppu
;
116 using namespace ::connectivity::sdbcx
;
118 //==========================================================================
120 //==========================================================================
121 DBG_NAME(OViewContainer
)
122 //------------------------------------------------------------------------------
123 OViewContainer::OViewContainer(::cppu::OWeakObject
& _rParent
124 ,::osl::Mutex
& _rMutex
125 ,const Reference
< XConnection
>& _xCon
127 ,IRefreshListener
* _pRefreshListener
128 ,::dbtools::IWarningsContainer
* _pWarningsContainer
129 ,oslInterlockedCount
& _nInAppend
)
130 :OFilteredContainer(_rParent
,_rMutex
,_xCon
,_bCase
,_pRefreshListener
,_pWarningsContainer
,_nInAppend
)
131 ,m_bInElementRemoved(false)
133 DBG_CTOR(OViewContainer
, NULL
);
136 //------------------------------------------------------------------------------
137 OViewContainer::~OViewContainer()
140 DBG_DTOR(OViewContainer
, NULL
);
142 //------------------------------------------------------------------------------
144 //------------------------------------------------------------------------------
145 IMPLEMENT_SERVICE_INFO2(OViewContainer
, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
)
146 // -----------------------------------------------------------------------------
147 ObjectType
OViewContainer::createObject(const ::rtl::OUString
& _rName
)
150 if ( m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
) )
151 xProp
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
155 ::rtl::OUString sCatalog
,sSchema
,sTable
;
156 ::dbtools::qualifiedNameComponents(m_xMetaData
,
161 ::dbtools::eInDataManipulation
);
162 return new ::connectivity::sdbcx::OView(isCaseSensitive(),
174 // -----------------------------------------------------------------------------
175 Reference
< XPropertySet
> OViewContainer::createDescriptor()
177 Reference
< XPropertySet
> xRet
;
178 // frist we have to look if the master tables does support this
179 // and if then create a table object as well with the master tables
180 Reference
<XColumnsSupplier
> xMasterColumnsSup
;
181 Reference
<XDataDescriptorFactory
> xDataFactory(m_xMasterContainer
,UNO_QUERY
);
182 if(xDataFactory
.is())
183 xRet
= xDataFactory
->createDataDescriptor();
185 xRet
= new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData
);
189 // -----------------------------------------------------------------------------
191 ObjectType
OViewContainer::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
193 // append the new table with a create stmt
194 ::rtl::OUString aName
= getString(descriptor
->getPropertyValue(PROPERTY_NAME
));
196 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
197 Reference
< XPropertySet
> xProp
= descriptor
;
200 EnsureReset
aReset(m_nInAppend
);
202 xAppend
->appendByDescriptor(descriptor
);
203 if(m_xMasterContainer
->hasByName(aName
))
204 xProp
.set(m_xMasterContainer
->getByName(aName
),UNO_QUERY
);
208 ::rtl::OUString sComposedName
= ::dbtools::composeTableName( m_xMetaData
, descriptor
, ::dbtools::eInTableDefinitions
, false, false, true );
209 if(!sComposedName
.getLength())
210 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
212 ::rtl::OUString sCommand
;
213 descriptor
->getPropertyValue(PROPERTY_COMMAND
) >>= sCommand
;
215 ::rtl::OUStringBuffer aSQL
;
216 aSQL
.appendAscii( "CREATE VIEW " );
217 aSQL
.append ( sComposedName
);
218 aSQL
.appendAscii( " AS " );
219 aSQL
.append ( sCommand
);
221 Reference
<XConnection
> xCon
= m_xConnection
;
222 OSL_ENSURE(xCon
.is(),"Connection is null!");
225 ::utl::SharedUNOComponent
< XStatement
> xStmt( xCon
->createStatement() );
227 xStmt
->execute( aSQL
.makeStringAndClear() );
231 return createObject( _rForName
);
233 // -------------------------------------------------------------------------
235 void OViewContainer::dropObject(sal_Int32 _nPos
,const ::rtl::OUString _sElementName
)
237 if ( !m_bInElementRemoved
)
239 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
241 xDrop
->dropByName(_sElementName
);
244 ::rtl::OUString sCatalog
,sSchema
,sTable
,sComposedName
;
246 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
249 xTable
->getPropertyValue(PROPERTY_CATALOGNAME
) >>= sCatalog
;
250 xTable
->getPropertyValue(PROPERTY_SCHEMANAME
) >>= sSchema
;
251 xTable
->getPropertyValue(PROPERTY_NAME
) >>= sTable
;
253 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, sal_True
, ::dbtools::eInTableDefinitions
);
256 if(!sComposedName
.getLength())
257 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
259 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("DROP VIEW ");
260 aSql
+= sComposedName
;
261 Reference
<XConnection
> xCon
= m_xConnection
;
262 OSL_ENSURE(xCon
.is(),"Connection is null!");
265 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
267 xStmt
->execute(aSql
);
268 ::comphelper::disposeComponent(xStmt
);
273 // -----------------------------------------------------------------------------
274 void SAL_CALL
OViewContainer::elementInserted( const ContainerEvent
& Event
) throw (RuntimeException
)
276 ::osl::MutexGuard
aGuard(m_rMutex
);
277 ::rtl::OUString sName
;
278 if ( ( Event
.Accessor
>>= sName
)
280 && ( !hasByName( sName
) )
283 Reference
<XPropertySet
> xProp(Event
.Element
,UNO_QUERY
);
284 ::rtl::OUString sType
;
285 xProp
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
286 if ( sType
== ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VIEW")) )
287 insertElement(sName
,createObject(sName
));
290 // -----------------------------------------------------------------------------
291 void SAL_CALL
OViewContainer::elementRemoved( const ContainerEvent
& Event
) throw (RuntimeException
)
293 ::osl::MutexGuard
aGuard(m_rMutex
);
294 ::rtl::OUString sName
;
295 if ( (Event
.Accessor
>>= sName
) && hasByName(sName
) )
297 m_bInElementRemoved
= true;
304 m_bInElementRemoved
= sal_False
;
307 m_bInElementRemoved
= false;
310 // -----------------------------------------------------------------------------
311 void SAL_CALL
OViewContainer::disposing( const ::com::sun::star::lang::EventObject
& /*Source*/ ) throw (RuntimeException
)
314 // -----------------------------------------------------------------------------
315 void SAL_CALL
OViewContainer::elementReplaced( const ContainerEvent
& /*Event*/ ) throw (RuntimeException
)
318 // -----------------------------------------------------------------------------
319 ::rtl::OUString
OViewContainer::getTableTypeRestriction() const
321 // no restriction at all (other than the ones provided externally)
322 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VIEW" ) );