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