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