bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HView.cxx
blob29e5a4000a512204d7eb6d25e752db393f74e0ac
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/dbtools.hxx>
28 #include <com/sun/star/lang/WrappedTargetException.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/sdbc/SQLException.hpp>
33 #include <cppuhelper/exc_hlp.hxx>
34 #include <tools/diagnose_ex.h>
35 #include <unotools/sharedunocomponent.hxx>
38 namespace connectivity::hsqldb
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::UNO_QUERY_THROW;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::RuntimeException;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::sdbc::SQLException;
48 using ::com::sun::star::sdbc::XConnection;
49 using ::com::sun::star::lang::WrappedTargetException;
50 using ::com::sun::star::sdbc::XResultSet;
51 using ::com::sun::star::sdbc::XStatement;
52 using ::com::sun::star::lang::DisposedException;
53 using ::com::sun::star::sdbc::XRow;
55 HView::HView( const Reference< XConnection >& _rxConnection, bool _bCaseSensitive,
56 const OUString& _rSchemaName, const OUString& _rName )
57 :HView_Base( _bCaseSensitive, _rName, _rxConnection->getMetaData(), OUString(), _rSchemaName, OUString() )
58 ,m_xConnection( _rxConnection )
63 HView::~HView()
68 IMPLEMENT_FORWARD_XINTERFACE2( HView, HView_Base, HView_IBASE )
69 IMPLEMENT_FORWARD_XTYPEPROVIDER2( HView, HView_Base, HView_IBASE )
72 void SAL_CALL HView::alterCommand( const OUString& _rNewCommand )
74 // not really atomic ... as long as we do not have something like
75 // ALTER VIEW <name> TO <command>
76 // in HSQL, we need to do it this way ...
78 // I can imagine scenarios where this fails, e.g. when dropping the view
79 // succeeds, re-creating it fails, some other thread alters a table which
80 // the view was based on, and then we try to restore the view with the
81 // original command, which then fails, too.
83 // However, there's not much chance to prevent this kind of errors without
84 // backend support.
86 OUString sQualifiedName( ::dbtools::composeTableName(
87 m_xMetaData, m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::EComposeRule::InDataManipulation ) );
89 ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
91 // create a statement which can be used to re-create the original view, in case
92 // dropping it succeeds, but creating it with a new statement fails
93 OUString sRestoreCommand =
94 "CREATE VIEW " +
95 sQualifiedName +
96 " AS " +
97 impl_getCommand_throwSQLException();
99 bool bDropSucceeded( false );
102 // drop the existing view
103 OUString aCommand ="DROP VIEW " + sQualifiedName;
104 xStatement->execute( aCommand );
105 bDropSucceeded = true;
107 // create a new one with the same name
108 aCommand = "CREATE VIEW " + sQualifiedName + " AS " + _rNewCommand;
109 xStatement->execute( aCommand );
111 catch( const SQLException& )
113 if ( bDropSucceeded )
114 // drop succeeded, but creation failed -> re-create the view with the original
115 // statement
116 xStatement->execute( sRestoreCommand );
117 throw;
119 catch( const RuntimeException& )
121 if ( bDropSucceeded )
122 xStatement->execute( sRestoreCommand );
123 throw;
125 catch( const Exception& )
127 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
128 if ( bDropSucceeded )
129 xStatement->execute( sRestoreCommand );
134 void SAL_CALL HView::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
136 if ( _nHandle == PROPERTY_ID_COMMAND )
138 // retrieve the very current command, don't rely on the base classes cached value
139 // (which we initialized empty, anyway)
140 _rValue <<= impl_getCommand_wrapSQLException();
141 return;
144 HView_Base::getFastPropertyValue( _rValue, _nHandle );
147 OUString HView::impl_getCommand() const
149 OUStringBuffer aCommand;
150 aCommand.append( "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.SYSTEM_VIEWS " );
151 HTools::appendTableFilterCrit( aCommand, m_CatalogName, m_SchemaName, m_Name, false );
152 ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
153 Reference< XResultSet > xResult( xStatement->executeQuery( aCommand.makeStringAndClear() ), css::uno::UNO_SET_THROW );
154 if ( !xResult->next() )
156 // hmm. There is no view the name as we know it. Can only mean some other instance
157 // dropped this view meanwhile...
158 throw DisposedException();
161 Reference< XRow > xRow( xResult, UNO_QUERY_THROW );
162 return xRow->getString( 1 );
165 OUString HView::impl_getCommand_wrapSQLException() const
167 OUString sCommand;
171 sCommand = impl_getCommand();
173 catch( const RuntimeException& )
175 throw;
177 catch( const SQLException& e )
179 throw WrappedTargetException( e.Message, static_cast< XAlterView* >( const_cast< HView* >( this ) ), ::cppu::getCaughtException() );
181 catch( const Exception& )
183 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
186 return sCommand;
189 OUString HView::impl_getCommand_throwSQLException() const
191 OUString sCommand;
195 sCommand = impl_getCommand();
197 catch( const RuntimeException& )
199 throw;
201 catch( const SQLException& )
203 throw;
205 catch( const Exception& )
207 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
210 return sCommand;
213 } // namespace connectivity::hsqldb
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */