LibreOffice Module dbaccess (master) 1
viewcontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <strings.hxx>
21#include <viewcontainer.hxx>
22#include <View.hxx>
23
24#include <comphelper/types.hxx>
27
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/sdbc/XConnection.hpp>
30
31using namespace dbaccess;
32using namespace dbtools;
33using namespace ::com::sun::star::uno;
34using namespace ::com::sun::star::lang;
35using namespace ::com::sun::star::beans;
36using namespace ::com::sun::star::sdbc;
37using namespace ::com::sun::star::sdb;
38using namespace ::com::sun::star::sdbcx;
39using namespace ::com::sun::star::util;
40using namespace ::com::sun::star::container;
41using namespace ::osl;
42using namespace ::comphelper;
43using namespace ::cppu;
44using namespace ::connectivity::sdbcx;
45
46// OViewContainer
47
48OViewContainer::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)
56{
57}
58
60{
61}
62
63// XServiceInfo
64OUString SAL_CALL OViewContainer::getImplementationName()
65 {
66 return "com.sun.star.sdb.dbaccess.OViewContainer";
67 }
68sal_Bool SAL_CALL OViewContainer::supportsService(const OUString& _rServiceName)
69 {
70 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
71 for (const OUString& s : aSupported)
72 if (s == _rServiceName)
73 return true;
74
75 return false;
76 }
77css::uno::Sequence< OUString > SAL_CALL OViewContainer::getSupportedServiceNames()
78{
80}
81
82
84{
85 ObjectType xProp;
86 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
87 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
88
89 if ( !xProp.is() )
90 {
91 OUString sCatalog,sSchema,sTable;
92 ::dbtools::qualifiedNameComponents(m_xMetaData,
93 _rName,
95 sSchema,
96 sTable,
97 ::dbtools::EComposeRule::InDataManipulation);
98 return new View(m_xConnection,
100 sCatalog,
101 sSchema,
102 sTable
103 );
104 }
105
106 return xProp;
107}
108
109Reference< XPropertySet > OViewContainer::createDescriptor()
110{
111 Reference< XPropertySet > xRet;
112 // first we have to look if the master tables support this
113 // and if so then create a table object as well with the master tables
114 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
115 if(xDataFactory.is())
116 xRet = xDataFactory->createDataDescriptor();
117 else
118 xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
119
120 return xRet;
121}
122
123// XAppend
124ObjectType OViewContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
125{
126 // append the new table with a create stmt
127 OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
128
129 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
130 Reference< XPropertySet > xProp = descriptor;
131 if(xAppend.is())
132 {
133 EnsureReset aReset(m_nInAppend);
134
135 xAppend->appendByDescriptor(descriptor);
136 if(m_xMasterContainer->hasByName(aName))
137 xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
138 }
139 else
140 {
141 OUString sComposedName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::EComposeRule::InTableDefinitions, true );
142 if(sComposedName.isEmpty())
143 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
144
145 OUString sCommand;
146 descriptor->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
147
148 OUString aSQL = "CREATE VIEW " + sComposedName + " AS " + sCommand;
149
150 Reference<XConnection> xCon = m_xConnection;
151 OSL_ENSURE(xCon.is(),"Connection is null!");
152 if ( xCon.is() )
153 {
154 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
155 if ( xStmt.is() )
156 xStmt->execute( aSQL );
157 }
158 }
159
160 return createObject( _rForName );
161}
162
163// XDrop
164void OViewContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
165{
167 return;
168
169 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
170 if(xDrop.is())
171 xDrop->dropByName(_sElementName);
172 else
173 {
174 OUString sComposedName;
175
176 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
177 if ( xTable.is() )
178 {
179 OUString sCatalog,sSchema,sTable;
180 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
181 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
182 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
183
184 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
185 }
186
187 if(sComposedName.isEmpty())
188 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
189
190 OUString aSql = "DROP VIEW " + sComposedName;
191 Reference<XConnection> xCon = m_xConnection;
192 OSL_ENSURE(xCon.is(),"Connection is null!");
193 if ( xCon.is() )
194 {
195 Reference< XStatement > xStmt = xCon->createStatement( );
196 if(xStmt.is())
197 xStmt->execute(aSql);
198 ::comphelper::disposeComponent(xStmt);
199 }
200 }
201}
202
203void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event )
204{
205 ::osl::MutexGuard aGuard(m_rMutex);
206 OUString sName;
207 if ( ( Event.Accessor >>= sName )
208 && ( !m_nInAppend )
209 && ( !hasByName( sName ) )
210 )
211 {
212 Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
213 OUString sType;
214 xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
215 if ( sType == "VIEW" )
217 }
218}
219
220void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event )
221{
222 ::osl::MutexGuard aGuard(m_rMutex);
223 OUString sName;
224 if ( !((Event.Accessor >>= sName) && hasByName(sName)) )
225 return;
226
227 m_bInElementRemoved = true;
228 try
229 {
231 }
232 catch(Exception&)
233 {
234 m_bInElementRemoved = false;
235 throw;
236 }
237 m_bInElementRemoved = false;
238}
239
240void SAL_CALL OViewContainer::disposing( const css::lang::EventObject& /*Source*/ )
241{
242}
243
244void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ )
245{
246}
247
249{
250 // no restriction at all (other than the ones provided externally)
251 return "VIEW";
252}
253
254/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
OptionalString sComposedName
OptionalString sType
OptionalString sName
void insertElement(const OUString &_sElementName, const ObjectType &_xElement)
virtual void SAL_CALL dropByName(const OUString &elementName) override
ObjectType getObject(sal_Int32 _nIndex)
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
css::uno::Reference< css::container::XNameAccess > m_xMasterContainer
std::atomic< std::size_t > & m_nInAppend
css::uno::WeakReference< css::sdbc::XConnection > m_xConnection
css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData
virtual OUString getTableTypeRestriction() const override
returns a string denoting the only type of tables allowed in this container, or an empty string if th...
virtual ::connectivity::sdbcx::ObjectType createObject(const OUString &_rName) override
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &Event) override
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
virtual void disposing() override
tell the container to free all elements and all additional resources.
virtual connectivity::sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
virtual ~OViewContainer() override
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
OUString aName
@ Exception
OUString getString(const Any &_rAny)
css::uno::Sequence< OUString > getSupportedServiceNames()
ObjectType
constexpr OUStringLiteral PROPERTY_COMMAND(u"Command")
constexpr OUStringLiteral SERVICE_SDBCX_CONTAINER
Definition: strings.hxx:171
constexpr OUStringLiteral PROPERTY_SCHEMANAME(u"SchemaName")
constexpr OUStringLiteral PROPERTY_CATALOGNAME(u"CatalogName")
constexpr OUStringLiteral PROPERTY_TYPE(u"Type")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
constexpr OUStringLiteral SERVICE_SDBCX_TABLES
Definition: strings.hxx:193
unsigned char sal_Bool
sal_Int32 _nPos