Line data Source code
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 "viewcontainer.hxx"
21 : #include "dbastrings.hrc"
22 : #include "core_resource.hxx"
23 : #include "core_resource.hrc"
24 : #include "View.hxx"
25 :
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>
33 :
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>
43 :
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;
58 :
59 : // OViewContainer
60 :
61 0 : OViewContainer::OViewContainer(::cppu::OWeakObject& _rParent
62 : ,::osl::Mutex& _rMutex
63 : ,const Reference< XConnection >& _xCon
64 : ,sal_Bool _bCase
65 : ,IRefreshListener* _pRefreshListener
66 : ,::dbtools::IWarningsContainer* _pWarningsContainer
67 : ,oslInterlockedCount& _nInAppend)
68 : :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_pWarningsContainer,_nInAppend)
69 0 : ,m_bInElementRemoved(false)
70 : {
71 0 : }
72 :
73 0 : OViewContainer::~OViewContainer()
74 : {
75 0 : }
76 :
77 : // XServiceInfo
78 0 : IMPLEMENT_SERVICE_INFO2(OViewContainer, "com.sun.star.sdb.dbaccess.OViewContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES)
79 :
80 0 : ObjectType OViewContainer::createObject(const OUString& _rName)
81 : {
82 0 : ObjectType xProp;
83 0 : if ( m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName) )
84 0 : xProp.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
85 :
86 0 : if ( !xProp.is() )
87 : {
88 0 : OUString sCatalog,sSchema,sTable;
89 : ::dbtools::qualifiedNameComponents(m_xMetaData,
90 : _rName,
91 : sCatalog,
92 : sSchema,
93 : sTable,
94 0 : ::dbtools::eInDataManipulation);
95 : return new View(m_xConnection,
96 0 : isCaseSensitive(),
97 : sCatalog,
98 : sSchema,
99 : sTable
100 0 : );
101 : }
102 :
103 0 : return xProp;
104 : }
105 :
106 0 : Reference< XPropertySet > OViewContainer::createDescriptor()
107 : {
108 0 : 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 0 : Reference<XColumnsSupplier > xMasterColumnsSup;
112 0 : Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
113 0 : if(xDataFactory.is())
114 0 : xRet = xDataFactory->createDataDescriptor();
115 : else
116 0 : xRet = new ::connectivity::sdbcx::OView(isCaseSensitive(),m_xMetaData);
117 :
118 0 : return xRet;
119 : }
120 :
121 : // XAppend
122 0 : ObjectType OViewContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
123 : {
124 : // append the new table with a create stmt
125 0 : OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
126 :
127 0 : Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
128 0 : Reference< XPropertySet > xProp = descriptor;
129 0 : if(xAppend.is())
130 : {
131 0 : EnsureReset aReset(m_nInAppend);
132 :
133 0 : xAppend->appendByDescriptor(descriptor);
134 0 : if(m_xMasterContainer->hasByName(aName))
135 0 : xProp.set(m_xMasterContainer->getByName(aName),UNO_QUERY);
136 : }
137 : else
138 : {
139 0 : OUString sComposedName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInTableDefinitions, false, false, true );
140 0 : if(sComposedName.isEmpty())
141 0 : ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
142 :
143 0 : OUString sCommand;
144 0 : descriptor->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
145 :
146 0 : OUStringBuffer aSQL;
147 0 : aSQL.appendAscii( "CREATE VIEW " );
148 0 : aSQL.append ( sComposedName );
149 0 : aSQL.appendAscii( " AS " );
150 0 : aSQL.append ( sCommand );
151 :
152 0 : Reference<XConnection> xCon = m_xConnection;
153 : OSL_ENSURE(xCon.is(),"Connection is null!");
154 0 : if ( xCon.is() )
155 : {
156 0 : ::utl::SharedUNOComponent< XStatement > xStmt( xCon->createStatement() );
157 0 : if ( xStmt.is() )
158 0 : xStmt->execute( aSQL.makeStringAndClear() );
159 0 : }
160 : }
161 :
162 0 : return createObject( _rForName );
163 : }
164 :
165 : // XDrop
166 0 : void OViewContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
167 : {
168 0 : if ( !m_bInElementRemoved )
169 : {
170 0 : Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
171 0 : if(xDrop.is())
172 0 : xDrop->dropByName(_sElementName);
173 : else
174 : {
175 0 : OUString sCatalog,sSchema,sTable,sComposedName;
176 :
177 0 : Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
178 0 : if ( xTable.is() )
179 : {
180 0 : xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
181 0 : xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
182 0 : xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
183 :
184 0 : sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInTableDefinitions );
185 : }
186 :
187 0 : if(sComposedName.isEmpty())
188 0 : ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
189 :
190 0 : OUString aSql("DROP VIEW ");
191 0 : aSql += sComposedName;
192 0 : Reference<XConnection> xCon = m_xConnection;
193 : OSL_ENSURE(xCon.is(),"Connection is null!");
194 0 : if ( xCon.is() )
195 : {
196 0 : Reference< XStatement > xStmt = xCon->createStatement( );
197 0 : if(xStmt.is())
198 0 : xStmt->execute(aSql);
199 0 : ::comphelper::disposeComponent(xStmt);
200 0 : }
201 0 : }
202 : }
203 0 : }
204 :
205 0 : void SAL_CALL OViewContainer::elementInserted( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
206 : {
207 0 : ::osl::MutexGuard aGuard(m_rMutex);
208 0 : OUString sName;
209 0 : if ( ( Event.Accessor >>= sName )
210 0 : && ( !m_nInAppend )
211 0 : && ( !hasByName( sName ) )
212 : )
213 : {
214 0 : Reference<XPropertySet> xProp(Event.Element,UNO_QUERY);
215 0 : OUString sType;
216 0 : xProp->getPropertyValue(PROPERTY_TYPE) >>= sType;
217 0 : if ( sType == "VIEW" )
218 0 : insertElement(sName,createObject(sName));
219 0 : }
220 0 : }
221 :
222 0 : void SAL_CALL OViewContainer::elementRemoved( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
223 : {
224 0 : ::osl::MutexGuard aGuard(m_rMutex);
225 0 : OUString sName;
226 0 : if ( (Event.Accessor >>= sName) && hasByName(sName) )
227 : {
228 0 : m_bInElementRemoved = true;
229 : try
230 : {
231 0 : dropByName(sName);
232 : }
233 0 : catch(Exception&)
234 : {
235 0 : m_bInElementRemoved = false;
236 0 : throw;
237 : }
238 0 : m_bInElementRemoved = false;
239 0 : }
240 0 : }
241 :
242 0 : void SAL_CALL OViewContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (RuntimeException, std::exception)
243 : {
244 0 : }
245 :
246 0 : void SAL_CALL OViewContainer::elementReplaced( const ContainerEvent& /*Event*/ ) throw (RuntimeException, std::exception)
247 : {
248 0 : }
249 :
250 0 : OUString OViewContainer::getTableTypeRestriction() const
251 : {
252 : // no restriction at all (other than the ones provided externally)
253 0 : return OUString( "VIEW" );
254 : }
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|