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 "hsqldb/HView.hxx"
22 : #include "hsqldb/HTools.hxx"
23 :
24 : #include "propertyids.hxx"
25 :
26 : #include <connectivity/dbexception.hxx>
27 : #include <connectivity/dbtools.hxx>
28 :
29 : #include <com/sun/star/lang/WrappedTargetException.hpp>
30 : #include <com/sun/star/lang/DisposedException.hpp>
31 : #include <com/sun/star/sdbc/XRow.hpp>
32 :
33 : #include <cppuhelper/exc_hlp.hxx>
34 : #include <tools/diagnose_ex.h>
35 : #include <unotools/sharedunocomponent.hxx>
36 :
37 :
38 : namespace connectivity { namespace hsqldb
39 : {
40 :
41 :
42 : using ::com::sun::star::uno::Reference;
43 : using ::com::sun::star::uno::UNO_QUERY;
44 : using ::com::sun::star::uno::UNO_QUERY_THROW;
45 : using ::com::sun::star::uno::Exception;
46 : using ::com::sun::star::uno::RuntimeException;
47 : using ::com::sun::star::uno::Any;
48 : using ::com::sun::star::uno::makeAny;
49 : using ::com::sun::star::sdbc::XDatabaseMetaData;
50 : using ::com::sun::star::sdbc::SQLException;
51 : using ::com::sun::star::sdbc::XConnection;
52 : using ::com::sun::star::lang::WrappedTargetException;
53 : using ::com::sun::star::sdbc::XResultSet;
54 : using ::com::sun::star::sdbc::XStatement;
55 : using ::com::sun::star::lang::DisposedException;
56 : using ::com::sun::star::sdbc::XRow;
57 :
58 :
59 : //= HView
60 :
61 :
62 0 : HView::HView( const Reference< XConnection >& _rxConnection, bool _bCaseSensitive,
63 : const OUString& _rSchemaName, const OUString& _rName )
64 0 : :HView_Base( _bCaseSensitive, _rName, _rxConnection->getMetaData(), 0, OUString(), _rSchemaName, OUString() )
65 0 : ,m_xConnection( _rxConnection )
66 : {
67 0 : }
68 :
69 :
70 0 : HView::~HView()
71 : {
72 0 : }
73 :
74 :
75 0 : IMPLEMENT_FORWARD_XINTERFACE2( HView, HView_Base, HView_IBASE )
76 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( HView, HView_Base, HView_IBASE )
77 :
78 :
79 0 : void SAL_CALL HView::alterCommand( const OUString& _rNewCommand ) throw (SQLException, RuntimeException, std::exception)
80 : {
81 : // not really atomic ... as long as we do not have something like
82 : // ALTER VIEW <name> TO <command>
83 : // in HSQL, we need to do it this way ...
84 : //
85 : // I can imagine scenarios where this fails, e.g. when dropping the view
86 : // succeedes, re-creating it fails, some other thread alters a table which
87 : // the view was based on, and then we try to restore the view with the
88 : // original command, which then fails, too.
89 : //
90 : // However, there's not much chance to prevent this kind of errors without
91 : // backend support.
92 :
93 : OUString sQualifiedName( ::dbtools::composeTableName(
94 0 : m_xMetaData, m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::eInDataManipulation ) );
95 :
96 0 : ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
97 :
98 : // create a statement which can be used to re-create the original view, in case
99 : // dropping it succeeds, but creating it with a new statement fails
100 0 : OUStringBuffer aRestoreCommand;
101 0 : aRestoreCommand.appendAscii( "CREATE VIEW " );
102 0 : aRestoreCommand.append ( sQualifiedName );
103 0 : aRestoreCommand.appendAscii( " AS " );
104 0 : aRestoreCommand.append ( impl_getCommand_throw() );
105 0 : OUString sRestoreCommand( aRestoreCommand.makeStringAndClear() );
106 :
107 0 : bool bDropSucceeded( false );
108 : try
109 : {
110 : // drop the existing view
111 0 : OUStringBuffer aCommand;
112 0 : aCommand.appendAscii( "DROP VIEW " );
113 0 : aCommand.append ( sQualifiedName );
114 0 : xStatement->execute( aCommand.makeStringAndClear() );
115 0 : bDropSucceeded = true;
116 :
117 : // create a new one with the same name
118 0 : aCommand.appendAscii( "CREATE VIEW " );
119 0 : aCommand.append ( sQualifiedName );
120 0 : aCommand.appendAscii( " AS " );
121 0 : aCommand.append ( _rNewCommand );
122 0 : xStatement->execute( aCommand.makeStringAndClear() );
123 : }
124 0 : catch( const SQLException& )
125 : {
126 0 : if ( bDropSucceeded )
127 : // drop succeeded, but creation failed -> re-create the view with the original
128 : // statemnet
129 0 : xStatement->execute( sRestoreCommand );
130 0 : throw;
131 : }
132 0 : catch( const RuntimeException& )
133 : {
134 0 : if ( bDropSucceeded )
135 0 : xStatement->execute( sRestoreCommand );
136 0 : throw;
137 : }
138 0 : catch( const Exception& )
139 : {
140 0 : if ( bDropSucceeded )
141 0 : xStatement->execute( sRestoreCommand );
142 : DBG_UNHANDLED_EXCEPTION();
143 0 : }
144 0 : }
145 :
146 :
147 0 : void SAL_CALL HView::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
148 : {
149 0 : if ( _nHandle == PROPERTY_ID_COMMAND )
150 : {
151 : // retrieve the very current command, don't rely on the base classes cached value
152 : // (which we initialized empty, anyway)
153 : try
154 : {
155 0 : _rValue <<= impl_getCommand_throw();
156 : }
157 0 : catch (const SQLException& e)
158 : {
159 : throw WrappedTargetException(e.Message,
160 : static_cast< XAlterView* >( const_cast< HView* >( this ) ),
161 0 : ::cppu::getCaughtException() );
162 : }
163 : }
164 :
165 0 : HView_Base::getFastPropertyValue( _rValue, _nHandle );
166 0 : }
167 :
168 :
169 0 : OUString HView::impl_getCommand_throw() const
170 : {
171 0 : OUString sCommand;
172 :
173 : try
174 : {
175 0 : OUStringBuffer aCommand;
176 0 : aCommand.appendAscii( "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.SYSTEM_VIEWS " );
177 0 : HTools::appendTableFilterCrit( aCommand, m_CatalogName, m_SchemaName, m_Name, false );
178 0 : ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
179 0 : Reference< XResultSet > xResult( xStatement->executeQuery( aCommand.makeStringAndClear() ), UNO_QUERY_THROW );
180 0 : if ( !xResult->next() )
181 : {
182 : // hmm. There is no view view the name as we know it. Can only mean some other instance
183 : // dropped this view meanwhile ...
184 0 : throw DisposedException();
185 : }
186 :
187 0 : Reference< XRow > xRow( xResult, UNO_QUERY_THROW );
188 0 : sCommand = xRow->getString( 1 );
189 : }
190 0 : catch( const RuntimeException& ) { throw; }
191 0 : catch( const SQLException& ) { throw; }
192 0 : catch( const Exception& )
193 : {
194 : DBG_UNHANDLED_EXCEPTION();
195 : }
196 :
197 0 : return sCommand;
198 : }
199 :
200 :
201 : } } // namespace connectivity::hsqldb
202 :
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|