Update ooo320-m1
[ooovba.git] / dbaccess / source / core / api / viewcontainer.cxx
blob0b7158ef5f9e405dc3cd2712da9e1b12aec51743
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewcontainer.cxx,v $
10 * $Revision: 1.28 $
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"
36 #endif
37 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
38 #include "dbastrings.hrc"
39 #endif
40 #ifndef _TOOLS_DEBUG_HXX
41 #include <tools/debug.hxx>
42 #endif
43 #ifndef _WLDCRD_HXX
44 #include <tools/wldcrd.hxx>
45 #endif
46 #ifndef _COMPHELPER_ENUMHELPER_HXX_
47 #include <comphelper/enumhelper.hxx>
48 #endif
49 #ifndef _DBA_CORE_RESOURCE_HXX_
50 #include "core_resource.hxx"
51 #endif
52 #ifndef _DBA_CORE_RESOURCE_HRC_
53 #include "core_resource.hrc"
54 #endif
56 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
57 #include <com/sun/star/beans/XPropertySet.hpp>
58 #endif
60 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
61 #include <com/sun/star/sdbc/XConnection.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
64 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
67 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
68 #endif
69 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
70 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
71 #endif
72 #ifndef _COM_SUN_STAR_SDBC_KEYRULE_HPP_
73 #include <com/sun/star/sdbc/KeyRule.hpp>
74 #endif
75 #ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
76 #include <com/sun/star/sdbcx/KeyType.hpp>
77 #endif
78 #ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
79 #include <com/sun/star/sdbc/ColumnValue.hpp>
80 #endif
81 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
82 #include <com/sun/star/sdbc/XRow.hpp>
83 #endif
84 #ifndef _COMPHELPER_TYPES_HXX_
85 #include <comphelper/types.hxx>
86 #endif
87 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
88 #include <connectivity/dbtools.hxx>
89 #endif
90 #ifndef _COMPHELPER_EXTRACT_HXX_
91 #include <comphelper/extract.hxx>
92 #endif
93 #ifndef _DBHELPER_DBEXCEPTION_HXX_
94 #include <connectivity/dbexception.hxx>
95 #endif
96 #ifndef _CONNECTIVITY_SDBCX_VIEW_HXX_
97 #include <connectivity/sdbcx/VView.hxx>
98 #endif
99 #ifndef _RTL_USTRBUF_HXX_
100 #include <rtl/ustrbuf.hxx>
101 #endif
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 //==========================================================================
119 //= OViewContainer
120 //==========================================================================
121 DBG_NAME(OViewContainer)
122 //------------------------------------------------------------------------------
123 OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
124 ,::osl::Mutex& _rMutex
125 ,const Reference< XConnection >& _xCon
126 ,sal_Bool _bCase
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()
139 // dispose();
140 DBG_DTOR(OViewContainer, NULL);
142 //------------------------------------------------------------------------------
143 // XServiceInfo
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)
149 ObjectType xProp;
150 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
151 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
153 if ( !xProp.is() )
155 ::rtl::OUString sCatalog,sSchema,sTable;
156 ::dbtools::qualifiedNameComponents(m_xMetaData,
157 _rName,
158 sCatalog,
159 sSchema,
160 sTable,
161 ::dbtools::eInDataManipulation);
162 return new ::connectivity::sdbcx::OView(isCaseSensitive(),
163 sTable,
164 m_xMetaData,
166 ::rtl::OUString(),
167 sSchema,
168 sCatalog
172 return xProp;
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();
184 else
185 xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
187 return xRet;
189 // -----------------------------------------------------------------------------
190 // XAppend
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;
198 if(xAppend.is())
200 EnsureReset aReset(m_nInAppend);
202 xAppend->appendByDescriptor(descriptor);
203 if(m_xMasterContainer->hasByName(aName))
204 xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
206 else
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!");
223 if ( xCon.is() )
225 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
226 if ( xStmt.is() )
227 xStmt->execute( aSQL.makeStringAndClear() );
231 return createObject( _rForName );
233 // -------------------------------------------------------------------------
234 // XDrop
235 void OViewContainer::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
237 if ( !m_bInElementRemoved )
239 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
240 if(xDrop.is())
241 xDrop->dropByName(_sElementName);
242 else
244 ::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
246 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
247 if ( xTable.is() )
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!");
263 if ( xCon.is() )
265 Reference< XStatement > xStmt = xCon->createStatement( );
266 if(xStmt.is())
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 )
279 && ( !m_nInAppend )
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;
300 dropByName(sName);
302 catch(Exception&)
304 m_bInElementRemoved = sal_False;
305 throw;
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" ) );