Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / viewcontainer.cxx
blob7725bdf82dc6043aeab7ad6dc129d4c3d171b698
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 "viewcontainer.hxx"
21 #include "dbastrings.hrc"
22 #include "core_resource.hxx"
23 #include "core_resource.hrc"
24 #include "View.hxx"
26 #include <tools/debug.hxx>
27 #include <comphelper/enumhelper.hxx>
28 #include <comphelper/types.hxx>
29 #include <connectivity/dbtools.hxx>
30 #include <comphelper/extract.hxx>
31 #include <connectivity/dbexception.hxx>
32 #include <rtl/ustrbuf.hxx>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/sdbc/XConnection.hpp>
36 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
37 #include <com/sun/star/sdbc/KeyRule.hpp>
38 #include <com/sun/star/sdbc/ColumnValue.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
41 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42 #include <com/sun/star/sdbcx/KeyType.hpp>
44 using namespace dbaccess;
45 using namespace dbtools;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::sdbc;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbcx;
52 using namespace ::com::sun::star::util;
53 using namespace ::com::sun::star::container;
54 using namespace ::osl;
55 using namespace ::comphelper;
56 using namespace ::cppu;
57 using namespace ::connectivity::sdbcx;
59 // OViewContainer
61 OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
62 ,::osl::Mutex& _rMutex
63 ,const Reference< XConnection >& _xCon
64 ,bool _bCase
65 ,IRefreshListener* _pRefreshListener
66 ,::dbtools::IWarningsContainer* _pWarningsContainer
67 ,oslInterlockedCount& _nInAppend)
68 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_pWarningsContainer,_nInAppend)
69 ,m_bInElementRemoved(false)
73 OViewContainer::~OViewContainer()
77 // XServiceInfo
78 IMPLEMENT_SERVICE_INFO2(OViewContainer, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES)
80 ObjectType OViewContainer::createObject(const OUString& _rName)
82 ObjectType xProp;
83 if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
84 xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
86 if ( !xProp.is() )
88 OUString sCatalog,sSchema,sTable;
89 ::dbtools::qualifiedNameComponents(m_xMetaData,
90 _rName,
91 sCatalog,
92 sSchema,
93 sTable,
94 ::dbtools::eInDataManipulation);
95 return new View(m_xConnection,
96 isCaseSensitive(),
97 sCatalog,
98 sSchema,
99 sTable
103 return xProp;
106 Reference< XPropertySet > OViewContainer::createDescriptor()
108 Reference< XPropertySet > xRet;
109 // first we have to look if the master tables support this
110 // and if so then create a table object as well with the master tables
111 Reference<XColumnsSupplier > xMasterColumnsSup;
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::eInTableDefinitions, false, false, 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 OUStringBuffer aSQL;
147 aSQL.appendAscii( "CREATE VIEW " );
148 aSQL.append ( sComposedName );
149 aSQL.appendAscii( " AS " );
150 aSQL.append ( sCommand );
152 Reference<XConnection> xCon = m_xConnection;
153 OSL_ENSURE(xCon.is(),"Connection is null!");
154 if ( xCon.is() )
156 ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
157 if ( xStmt.is() )
158 xStmt->execute( aSQL.makeStringAndClear() );
162 return createObject( _rForName );
165 // XDrop
166 void OViewContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
168 if ( !m_bInElementRemoved )
170 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
171 if(xDrop.is())
172 xDrop->dropByName(_sElementName);
173 else
175 OUString sCatalog,sSchema,sTable,sComposedName;
177 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
178 if ( xTable.is() )
180 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
181 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
182 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
184 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInTableDefinitions );
187 if(sComposedName.isEmpty())
188 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
190 OUString aSql("DROP VIEW ");
191 aSql += sComposedName;
192 Reference<XConnection> xCon = m_xConnection;
193 OSL_ENSURE(xCon.is(),"Connection is null!");
194 if ( xCon.is() )
196 Reference< XStatement > xStmt = xCon->createStatement( );
197 if(xStmt.is())
198 xStmt->execute(aSql);
199 ::comphelper::disposeComponent(xStmt);
205 void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
207 ::osl::MutexGuard aGuard(m_rMutex);
208 OUString sName;
209 if ( ( Event.Accessor >>= sName )
210 && ( !m_nInAppend )
211 && ( !hasByName( sName ) )
214 Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
215 OUString sType;
216 xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
217 if ( sType == "VIEW" )
218 insertElement(sName,createObject(sName));
222 void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
224 ::osl::MutexGuard aGuard(m_rMutex);
225 OUString sName;
226 if ( (Event.Accessor >>= sName) && hasByName(sName) )
228 m_bInElementRemoved = true;
231 dropByName(sName);
233 catch(Exception&)
235 m_bInElementRemoved = false;
236 throw;
238 m_bInElementRemoved = false;
242 void SAL_CALL OViewContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (RuntimeException, std::exception)
246 void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ ) throw (RuntimeException, std::exception)
250 OUString OViewContainer::getTableTypeRestriction() const
252 // no restriction at all (other than the ones provided externally)
253 return OUString( "VIEW" );
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */