bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HView.cxx
blob810acdf08561d9409ee8df15a7dff12085938655
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <hsqldb/HView.hxx>
22 #include <hsqldb/HTools.hxx>
24 #include <propertyids.hxx>
26 #include <connectivity/dbexception.hxx>
27 #include <connectivity/dbtools.hxx>
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 #include <com/sun/star/sdbc/SQLException.hpp>
34 #include <cppuhelper/exc_hlp.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <unotools/sharedunocomponent.hxx>
39 namespace connectivity { namespace hsqldb
43 using ::com::sun::star::uno::Reference;
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::sdbc::SQLException;
49 using ::com::sun::star::sdbc::XConnection;
50 using ::com::sun::star::lang::WrappedTargetException;
51 using ::com::sun::star::sdbc::XResultSet;
52 using ::com::sun::star::sdbc::XStatement;
53 using ::com::sun::star::lang::DisposedException;
54 using ::com::sun::star::sdbc::XRow;
56 HView::HView( const Reference< XConnection >& _rxConnection, bool _bCaseSensitive,
57 const OUString& _rSchemaName, const OUString& _rName )
58 :HView_Base( _bCaseSensitive, _rName, _rxConnection->getMetaData(), OUString(), _rSchemaName, OUString() )
59 ,m_xConnection( _rxConnection )
64 HView::~HView()
69 IMPLEMENT_FORWARD_XINTERFACE2( HView, HView_Base, HView_IBASE )
70 IMPLEMENT_FORWARD_XTYPEPROVIDER2( HView, HView_Base, HView_IBASE )
73 void SAL_CALL HView::alterCommand( const OUString& _rNewCommand )
75 // not really atomic ... as long as we do not have something like
76 // ALTER VIEW <name> TO <command>
77 // in HSQL, we need to do it this way ...
79 // I can imagine scenarios where this fails, e.g. when dropping the view
80 // succeeds, re-creating it fails, some other thread alters a table which
81 // the view was based on, and then we try to restore the view with the
82 // original command, which then fails, too.
84 // However, there's not much chance to prevent this kind of errors without
85 // backend support.
87 OUString sQualifiedName( ::dbtools::composeTableName(
88 m_xMetaData, m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::EComposeRule::InDataManipulation ) );
90 ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
92 // create a statement which can be used to re-create the original view, in case
93 // dropping it succeeds, but creating it with a new statement fails
94 OUStringBuffer aRestoreCommand;
95 aRestoreCommand.append( "CREATE VIEW " );
96 aRestoreCommand.append ( sQualifiedName );
97 aRestoreCommand.append( " AS " );
98 aRestoreCommand.append ( impl_getCommand_throwSQLException() );
99 OUString sRestoreCommand( aRestoreCommand.makeStringAndClear() );
101 bool bDropSucceeded( false );
104 // drop the existing view
105 OUStringBuffer aCommand;
106 aCommand.append( "DROP VIEW " );
107 aCommand.append ( sQualifiedName );
108 xStatement->execute( aCommand.makeStringAndClear() );
109 bDropSucceeded = true;
111 // create a new one with the same name
112 aCommand.append( "CREATE VIEW " );
113 aCommand.append ( sQualifiedName );
114 aCommand.append( " AS " );
115 aCommand.append ( _rNewCommand );
116 xStatement->execute( aCommand.makeStringAndClear() );
118 catch( const SQLException& )
120 if ( bDropSucceeded )
121 // drop succeeded, but creation failed -> re-create the view with the original
122 // statement
123 xStatement->execute( sRestoreCommand );
124 throw;
126 catch( const RuntimeException& )
128 if ( bDropSucceeded )
129 xStatement->execute( sRestoreCommand );
130 throw;
132 catch( const Exception& )
134 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
135 if ( bDropSucceeded )
136 xStatement->execute( sRestoreCommand );
141 void SAL_CALL HView::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
143 if ( _nHandle == PROPERTY_ID_COMMAND )
145 // retrieve the very current command, don't rely on the base classes cached value
146 // (which we initialized empty, anyway)
147 _rValue <<= impl_getCommand_wrapSQLException();
148 return;
151 HView_Base::getFastPropertyValue( _rValue, _nHandle );
154 OUString HView::impl_getCommand() const
156 OUStringBuffer aCommand;
157 aCommand.append( "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.SYSTEM_VIEWS " );
158 HTools::appendTableFilterCrit( aCommand, m_CatalogName, m_SchemaName, m_Name, false );
159 ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
160 Reference< XResultSet > xResult( xStatement->executeQuery( aCommand.makeStringAndClear() ), css::uno::UNO_SET_THROW );
161 if ( !xResult->next() )
163 // hmm. There is no view the name as we know it. Can only mean some other instance
164 // dropped this view meanwhile...
165 throw DisposedException();
168 Reference< XRow > xRow( xResult, UNO_QUERY_THROW );
169 return xRow->getString( 1 );
172 OUString HView::impl_getCommand_wrapSQLException() const
174 OUString sCommand;
178 sCommand = impl_getCommand();
180 catch( const RuntimeException& )
182 throw;
184 catch( const SQLException& e )
186 throw WrappedTargetException( e.Message, static_cast< XAlterView* >( const_cast< HView* >( this ) ), ::cppu::getCaughtException() );
188 catch( const Exception& )
190 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
193 return sCommand;
196 OUString HView::impl_getCommand_throwSQLException() const
198 OUString sCommand;
202 sCommand = impl_getCommand();
204 catch( const RuntimeException& )
206 throw;
208 catch( const SQLException& )
210 throw;
212 catch( const Exception& )
214 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
217 return sCommand;
220 } } // namespace connectivity::hsqldb
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */