LibreOffice Module connectivity (master) 1
HView.cxx
Go to the documentation of this file.
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
27
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>
32
36
37
39{
40
41
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;
54
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 )
59 {
60 }
61
62
64 {
65 }
66
67
70
71
72 void SAL_CALL HView::alterCommand( const OUString& _rNewCommand )
73 {
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 ...
77 //
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.
82 //
83 // However, there's not much chance to prevent this kind of errors without
84 // backend support.
85
86 OUString sQualifiedName( ::dbtools::composeTableName(
87 m_xMetaData, m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::EComposeRule::InDataManipulation ) );
88
89 ::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( m_xConnection->createStatement(), UNO_QUERY_THROW );
90
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();
98
99 bool bDropSucceeded( false );
100 try
101 {
102 // drop the existing view
103 OUString aCommand ="DROP VIEW " + sQualifiedName;
104 xStatement->execute( aCommand );
105 bDropSucceeded = true;
106
107 // create a new one with the same name
108 aCommand = "CREATE VIEW " + sQualifiedName + " AS " + _rNewCommand;
109 xStatement->execute( aCommand );
110 }
111 catch( const SQLException& )
112 {
113 if ( bDropSucceeded )
114 // drop succeeded, but creation failed -> re-create the view with the original
115 // statement
116 xStatement->execute( sRestoreCommand );
117 throw;
118 }
119 catch( const RuntimeException& )
120 {
121 if ( bDropSucceeded )
122 xStatement->execute( sRestoreCommand );
123 throw;
124 }
125 catch( const Exception& )
126 {
127 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
128 if ( bDropSucceeded )
129 xStatement->execute( sRestoreCommand );
130 }
131 }
132
133
134 void SAL_CALL HView::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
135 {
136 if ( _nHandle == PROPERTY_ID_COMMAND )
137 {
138 // retrieve the very current command, don't rely on the base classes cached value
139 // (which we initialized empty, anyway)
141 return;
142 }
143
144 HView_Base::getFastPropertyValue( _rValue, _nHandle );
145 }
146
147 OUString HView::impl_getCommand() const
148 {
149 OUStringBuffer aCommand(
150 "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.SYSTEM_VIEWS " );
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() )
155 {
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();
159 }
160
161 Reference< XRow > xRow( xResult, UNO_QUERY_THROW );
162 return xRow->getString( 1 );
163 }
164
166 {
167 OUString sCommand;
168
169 try
170 {
171 sCommand = impl_getCommand();
172 }
173 catch( const RuntimeException& )
174 {
175 throw;
176 }
177 catch( const SQLException& e )
178 {
179 throw WrappedTargetException( e.Message, static_cast< XAlterView* >( const_cast< HView* >( this ) ), ::cppu::getCaughtException() );
180 }
181 catch( const Exception& )
182 {
183 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
184 }
185
186 return sCommand;
187 }
188
190 {
191 OUString sCommand;
192
193 try
194 {
195 sCommand = impl_getCommand();
196 }
197 catch( const RuntimeException& )
198 {
199 throw;
200 }
201 catch( const SQLException& )
202 {
203 throw;
204 }
205 catch( const Exception& )
206 {
207 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
208 }
209
210 return sCommand;
211 }
212
213} // namespace connectivity::hsqldb
214
215
216/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_Name
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
static void appendTableFilterCrit(OUStringBuffer &_inout_rBuffer, std::u16string_view _rCatalog, std::u16string_view _rSchema, std::u16string_view _rName, bool _bShortForm)
appends a proper WHERE clause to the given buffer, which filters for a given table name
Definition: HTools.cxx:26
OUString impl_getCommand_wrapSQLException() const
retrieves the current command of the View
Definition: HView.cxx:165
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &_rValue, sal_Int32 _nHandle) const override
Definition: HView.cxx:134
virtual ~HView() override
Definition: HView.cxx:63
HView(const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, bool _bCaseSensitive, const OUString &_rSchemaName, const OUString &_rName)
Definition: HView.cxx:55
OUString impl_getCommand_throwSQLException() const
retrieves the current command of the View
Definition: HView.cxx:189
OUString impl_getCommand() const
retrieves the current command of the View
Definition: HView.cxx:147
css::uno::Reference< css::sdbc::XConnection > m_xConnection
Definition: HView.hxx:79
bool set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_Query _query)
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection, OHsqlConnection_BASE, OConnectionWrapper)
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
Reference< XConnection > m_xConnection
#define PROPERTY_ID_COMMAND
Definition: propertyids.hxx:72
OUString aCommand