nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / core / api / viewcontainer.cxx
blob796e23c9f49d4edfb2c7be7491db7b430d665ba1
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 .
20 #include <apitools.hxx>
21 #include <viewcontainer.hxx>
22 #include <View.hxx>
24 #include <comphelper/types.hxx>
25 #include <connectivity/dbtools.hxx>
26 #include <connectivity/dbexception.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/sdbc/XConnection.hpp>
31 using namespace dbaccess;
32 using namespace dbtools;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::sdb;
38 using namespace ::com::sun::star::sdbcx;
39 using namespace ::com::sun::star::util;
40 using namespace ::com::sun::star::container;
41 using namespace ::osl;
42 using namespace ::comphelper;
43 using namespace ::cppu;
44 using namespace ::connectivity::sdbcx;
46 // OViewContainer
48 OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
49 ,::osl::Mutex& _rMutex
50 ,const Reference< XConnection >& _xCon
51 ,bool _bCase
52 ,IRefreshListener* _pRefreshListener
53 ,std::atomic<std::size_t>& _nInAppend)
54 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_nInAppend)
55 ,m_bInElementRemoved(false)
59 OViewContainer::~OViewContainer()
63 // XServiceInfo
64 IMPLEMENT_SERVICE_INFO2(OViewContainer, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES)
66 ObjectType OViewContainer::createObject(const OUString& _rName)
68 ObjectType xProp;
69 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
70 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
72 if ( !xProp.is() )
74 OUString sCatalog,sSchema,sTable;
75 ::dbtools::qualifiedNameComponents(m_xMetaData,
76 _rName,
77 sCatalog,
78 sSchema,
79 sTable,
80 ::dbtools::EComposeRule::InDataManipulation);
81 return new View(m_xConnection,
82 isCaseSensitive(),
83 sCatalog,
84 sSchema,
85 sTable
89 return xProp;
92 Reference< XPropertySet > OViewContainer::createDescriptor()
94 Reference< XPropertySet > xRet;
95 // first we have to look if the master tables support this
96 // and if so then create a table object as well with the master tables
97 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
98 if(xDataFactory.is())
99 xRet = xDataFactory->createDataDescriptor();
100 else
101 xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
103 return xRet;
106 // XAppend
107 ObjectType OViewContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
109 // append the new table with a create stmt
110 OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
112 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
113 Reference< XPropertySet > xProp = descriptor;
114 if(xAppend.is())
116 EnsureReset aReset(m_nInAppend);
118 xAppend->appendByDescriptor(descriptor);
119 if(m_xMasterContainer->hasByName(aName))
120 xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
122 else
124 OUString sComposedName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::EComposeRule::InTableDefinitions, true );
125 if(sComposedName.isEmpty())
126 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
128 OUString sCommand;
129 descriptor->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
131 OUString aSQL = "CREATE VIEW " + sComposedName + " AS " + sCommand;
133 Reference<XConnection> xCon = m_xConnection;
134 OSL_ENSURE(xCon.is(),"Connection is null!");
135 if ( xCon.is() )
137 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
138 if ( xStmt.is() )
139 xStmt->execute( aSQL );
143 return createObject( _rForName );
146 // XDrop
147 void OViewContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
149 if ( m_bInElementRemoved )
150 return;
152 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
153 if(xDrop.is())
154 xDrop->dropByName(_sElementName);
155 else
157 OUString sComposedName;
159 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
160 if ( xTable.is() )
162 OUString sCatalog,sSchema,sTable;
163 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
164 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
165 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
167 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
170 if(sComposedName.isEmpty())
171 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
173 OUString aSql = "DROP VIEW " + sComposedName;
174 Reference<XConnection> xCon = m_xConnection;
175 OSL_ENSURE(xCon.is(),"Connection is null!");
176 if ( xCon.is() )
178 Reference< XStatement > xStmt = xCon->createStatement( );
179 if(xStmt.is())
180 xStmt->execute(aSql);
181 ::comphelper::disposeComponent(xStmt);
186 void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event )
188 ::osl::MutexGuard aGuard(m_rMutex);
189 OUString sName;
190 if ( ( Event.Accessor >>= sName )
191 && ( !m_nInAppend )
192 && ( !hasByName( sName ) )
195 Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
196 OUString sType;
197 xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
198 if ( sType == "VIEW" )
199 insertElement(sName,createObject(sName));
203 void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event )
205 ::osl::MutexGuard aGuard(m_rMutex);
206 OUString sName;
207 if ( !((Event.Accessor >>= sName) && hasByName(sName)) )
208 return;
210 m_bInElementRemoved = true;
213 dropByName(sName);
215 catch(Exception&)
217 m_bInElementRemoved = false;
218 throw;
220 m_bInElementRemoved = false;
223 void SAL_CALL OViewContainer::disposing( const css::lang::EventObject& /*Source*/ )
227 void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ )
231 OUString OViewContainer::getTableTypeRestriction() const
233 // no restriction at all (other than the ones provided externally)
234 return "VIEW";
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */