tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / dbaccess / source / core / api / viewcontainer.cxx
blobb115cb3dc508d5edc2032f6f9f69a33351bc308f
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 <strings.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::sdbcx;
38 using namespace ::com::sun::star::container;
39 using namespace ::osl;
40 using namespace ::comphelper;
41 using namespace ::cppu;
42 using namespace ::connectivity::sdbcx;
44 // OViewContainer
46 OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
47 ,::osl::Mutex& _rMutex
48 ,const Reference< XConnection >& _xCon
49 ,bool _bCase
50 ,IRefreshListener* _pRefreshListener
51 ,std::atomic<std::size_t>& _nInAppend)
52 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_nInAppend)
53 ,m_bInElementRemoved(false)
57 OViewContainer::~OViewContainer()
61 // XServiceInfo
62 OUString SAL_CALL OViewContainer::getImplementationName()
64 return u"com.sun.star.sdb.dbaccess.OViewContainer"_ustr;
66 sal_Bool SAL_CALL OViewContainer::supportsService(const OUString& _rServiceName)
68 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
69 for (const OUString& s : aSupported)
70 if (s == _rServiceName)
71 return true;
73 return false;
75 css::uno::Sequence< OUString > SAL_CALL OViewContainer::getSupportedServiceNames()
77 return { SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES };
81 ObjectType OViewContainer::createObject(const OUString& _rName)
83 ObjectType xProp;
84 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
85 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
87 if ( !xProp.is() )
89 OUString sCatalog,sSchema,sTable;
90 ::dbtools::qualifiedNameComponents(m_xMetaData,
91 _rName,
92 sCatalog,
93 sSchema,
94 sTable,
95 ::dbtools::EComposeRule::InDataManipulation);
96 return new View(m_xConnection,
97 isCaseSensitive(),
98 sCatalog,
99 sSchema,
100 sTable
104 return xProp;
107 Reference< XPropertySet > OViewContainer::createDescriptor()
109 Reference< XPropertySet > xRet;
110 // first we have to look if the master tables support this
111 // and if so then create a table object as well with the master tables
112 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
113 if(xDataFactory.is())
114 xRet = xDataFactory->createDataDescriptor();
115 else
116 xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
118 return xRet;
121 // XAppend
122 ObjectType OViewContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
124 // append the new table with a create stmt
125 OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
127 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
128 Reference< XPropertySet > xProp = descriptor;
129 if(xAppend.is())
131 EnsureReset aReset(m_nInAppend);
133 xAppend->appendByDescriptor(descriptor);
134 if(m_xMasterContainer->hasByName(aName))
135 xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
137 else
139 OUString sComposedName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::EComposeRule::InTableDefinitions, true );
140 if(sComposedName.isEmpty())
141 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
143 OUString sCommand;
144 descriptor->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
146 OUString aSQL = "CREATE VIEW " + sComposedName + " AS " + sCommand;
148 Reference<XConnection> xCon = m_xConnection;
149 OSL_ENSURE(xCon.is(),"Connection is null!");
150 if ( xCon.is() )
152 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
153 if ( xStmt.is() )
154 xStmt->execute( aSQL );
158 return createObject( _rForName );
161 // XDrop
162 void OViewContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
164 if ( m_bInElementRemoved )
165 return;
167 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
168 if(xDrop.is())
169 xDrop->dropByName(_sElementName);
170 else
172 OUString sComposedName;
174 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
175 if ( xTable.is() )
177 OUString sCatalog,sSchema,sTable;
178 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
179 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
180 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
182 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
185 if(sComposedName.isEmpty())
186 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
188 OUString aSql = "DROP VIEW " + sComposedName;
189 Reference<XConnection> xCon = m_xConnection;
190 OSL_ENSURE(xCon.is(),"Connection is null!");
191 if ( xCon.is() )
193 Reference< XStatement > xStmt = xCon->createStatement( );
194 if(xStmt.is())
195 xStmt->execute(aSql);
196 ::comphelper::disposeComponent(xStmt);
201 void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event )
203 ::osl::MutexGuard aGuard(m_rMutex);
204 OUString sName;
205 if ( ( Event.Accessor >>= sName )
206 && ( !m_nInAppend )
207 && ( !hasByName( sName ) )
210 Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
211 OUString sType;
212 xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
213 if ( sType == "VIEW" )
214 insertElement(sName,createObject(sName));
218 void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event )
220 ::osl::MutexGuard aGuard(m_rMutex);
221 OUString sName;
222 if ( !((Event.Accessor >>= sName) && hasByName(sName)) )
223 return;
225 m_bInElementRemoved = true;
228 dropByName(sName);
230 catch(Exception&)
232 m_bInElementRemoved = false;
233 throw;
235 m_bInElementRemoved = false;
238 void SAL_CALL OViewContainer::disposing( const css::lang::EventObject& /*Source*/ )
242 void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ )
246 OUString OViewContainer::getTableTypeRestriction() const
248 // no restriction at all (other than the ones provided externally)
249 return u"VIEW"_ustr;
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */