Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / api / viewcontainer.cxx
blob2bdfb70e9c581351f51307289782bbf92970ef16
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 .
21 #include "viewcontainer.hxx"
22 #include "dbastrings.hrc"
23 #include "core_resource.hxx"
24 #include "core_resource.hrc"
25 #include "View.hxx"
27 #include <tools/debug.hxx>
28 #include <comphelper/enumhelper.hxx>
29 #include <comphelper/types.hxx>
30 #include <connectivity/dbtools.hxx>
31 #include <comphelper/extract.hxx>
32 #include <connectivity/dbexception.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/sdbc/XConnection.hpp>
37 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
38 #include <com/sun/star/sdbc/KeyRule.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
42 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
43 #include <com/sun/star/sdbcx/KeyType.hpp>
45 using namespace dbaccess;
46 using namespace dbtools;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdbc;
51 using namespace ::com::sun::star::sdb;
52 using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::util;
54 using namespace ::com::sun::star::container;
55 using namespace ::osl;
56 using namespace ::comphelper;
57 using namespace ::cppu;
58 using namespace ::connectivity::sdbcx;
60 //==========================================================================
61 //= OViewContainer
62 //==========================================================================
63 DBG_NAME(OViewContainer)
65 OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
66 ,::osl::Mutex& _rMutex
67 ,const Reference< XConnection >& _xCon
68 ,sal_Bool _bCase
69 ,IRefreshListener* _pRefreshListener
70 ,::dbtools::IWarningsContainer* _pWarningsContainer
71 ,oslInterlockedCount& _nInAppend)
72 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_pWarningsContainer,_nInAppend)
73 ,m_bInElementRemoved(false)
75 DBG_CTOR(OViewContainer, NULL);
78 OViewContainer::~OViewContainer()
80 DBG_DTOR(OViewContainer, NULL);
83 // XServiceInfo
84 IMPLEMENT_SERVICE_INFO2(OViewContainer, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER.ascii, SERVICE_SDBCX_TABLES.ascii)
86 ObjectType OViewContainer::createObject(const ::rtl::OUString& _rName)
88 ObjectType xProp;
89 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
90 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
92 if ( !xProp.is() )
94 ::rtl::OUString sCatalog,sSchema,sTable;
95 ::dbtools::qualifiedNameComponents(m_xMetaData,
96 _rName,
97 sCatalog,
98 sSchema,
99 sTable,
100 ::dbtools::eInDataManipulation);
101 return new View(m_xConnection,
102 isCaseSensitive(),
103 sCatalog,
104 sSchema,
105 sTable
109 return xProp;
112 Reference< XPropertySet > OViewContainer::createDescriptor()
114 Reference< XPropertySet > xRet;
115 // first we have to look if the master tables support this
116 // and if so then create a table object as well with the master tables
117 Reference<XColumnsSupplier > xMasterColumnsSup;
118 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
119 if(xDataFactory.is())
120 xRet = xDataFactory->createDataDescriptor();
121 else
122 xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
124 return xRet;
127 // XAppend
128 ObjectType OViewContainer::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
130 // append the new table with a create stmt
131 ::rtl::OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
133 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
134 Reference< XPropertySet > xProp = descriptor;
135 if(xAppend.is())
137 EnsureReset aReset(m_nInAppend);
139 xAppend->appendByDescriptor(descriptor);
140 if(m_xMasterContainer->hasByName(aName))
141 xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
143 else
145 ::rtl::OUString sComposedName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInTableDefinitions, false, false, true );
146 if(sComposedName.isEmpty())
147 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
149 ::rtl::OUString sCommand;
150 descriptor->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
152 ::rtl::OUStringBuffer aSQL;
153 aSQL.appendAscii( "CREATE VIEW " );
154 aSQL.append ( sComposedName );
155 aSQL.appendAscii( " AS " );
156 aSQL.append ( sCommand );
158 Reference<XConnection> xCon = m_xConnection;
159 OSL_ENSURE(xCon.is(),"Connection is null!");
160 if ( xCon.is() )
162 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
163 if ( xStmt.is() )
164 xStmt->execute( aSQL.makeStringAndClear() );
168 return createObject( _rForName );
171 // XDrop
172 void OViewContainer::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
174 if ( !m_bInElementRemoved )
176 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
177 if(xDrop.is())
178 xDrop->dropByName(_sElementName);
179 else
181 ::rtl::OUString sCatalog,sSchema,sTable,sComposedName;
183 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
184 if ( xTable.is() )
186 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
187 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
188 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
190 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInTableDefinitions );
193 if(sComposedName.isEmpty())
194 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
196 ::rtl::OUString aSql("DROP VIEW ");
197 aSql += sComposedName;
198 Reference<XConnection> xCon = m_xConnection;
199 OSL_ENSURE(xCon.is(),"Connection is null!");
200 if ( xCon.is() )
202 Reference< XStatement > xStmt = xCon->createStatement( );
203 if(xStmt.is())
204 xStmt->execute(aSql);
205 ::comphelper::disposeComponent(xStmt);
211 void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event ) throw (RuntimeException)
213 ::osl::MutexGuard aGuard(m_rMutex);
214 ::rtl::OUString sName;
215 if ( ( Event.Accessor >>= sName )
216 && ( !m_nInAppend )
217 && ( !hasByName( sName ) )
220 Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
221 ::rtl::OUString sType;
222 xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
223 if ( sType == ::rtl::OUString("VIEW") )
224 insertElement(sName,createObject(sName));
228 void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event ) throw (RuntimeException)
230 ::osl::MutexGuard aGuard(m_rMutex);
231 ::rtl::OUString sName;
232 if ( (Event.Accessor >>= sName) && hasByName(sName) )
234 m_bInElementRemoved = true;
237 dropByName(sName);
239 catch(Exception&)
241 m_bInElementRemoved = sal_False;
242 throw;
244 m_bInElementRemoved = false;
248 void SAL_CALL OViewContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (RuntimeException)
252 void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ ) throw (RuntimeException)
256 ::rtl::OUString OViewContainer::getTableTypeRestriction() const
258 // no restriction at all (other than the ones provided externally)
259 return ::rtl::OUString( "VIEW" );
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */