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