1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 #include <cppuhelper/exc_hlp.hxx>
34 #include <tools/diagnose_ex.h>
35 #include <unotools/sharedunocomponent.hxx>
37 //........................................................................
38 namespace connectivity
{ namespace hsqldb
40 //........................................................................
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
;
58 //====================================================================
60 //====================================================================
61 //--------------------------------------------------------------------
62 HView::HView( const Reference
< XConnection
>& _rxConnection
, sal_Bool _bCaseSensitive
,
63 const OUString
& _rSchemaName
, const OUString
& _rName
)
64 :HView_Base( _bCaseSensitive
, _rName
, _rxConnection
->getMetaData(), 0, OUString(), _rSchemaName
, OUString() )
65 ,m_xConnection( _rxConnection
)
69 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 IMPLEMENT_FORWARD_XINTERFACE2( HView
, HView_Base
, HView_IBASE
)
76 IMPLEMENT_FORWARD_XTYPEPROVIDER2( HView
, HView_Base
, HView_IBASE
)
78 //--------------------------------------------------------------------
79 void SAL_CALL
HView::alterCommand( const OUString
& _rNewCommand
) throw (SQLException
, RuntimeException
)
81 // not really atomic ... as long as we do not have something like
82 // ALTER VIEW <name> TO <command>
83 // in HSQL, we need to do it this way ...
85 // I can imagine scenarios where this fails, e.g. when dropping the view
86 // succeedes, re-creating it fails, some other thread alters a table which
87 // the view was based on, and then we try to restore the view with the
88 // original command, which then fails, too.
90 // However, there's not much chance to prevent this kind of errors without
93 OUString
sQualifiedName( ::dbtools::composeTableName(
94 m_xMetaData
, m_CatalogName
, m_SchemaName
, m_Name
, true, ::dbtools::eInDataManipulation
) );
96 ::utl::SharedUNOComponent
< XStatement
> xStatement
; xStatement
.set( m_xConnection
->createStatement(), UNO_QUERY_THROW
);
98 // create a statement which can be used to re-create the original view, in case
99 // dropping it succeeds, but creating it with a new statement fails
100 OUStringBuffer aRestoreCommand
;
101 aRestoreCommand
.appendAscii( "CREATE VIEW " );
102 aRestoreCommand
.append ( sQualifiedName
);
103 aRestoreCommand
.appendAscii( " AS " );
104 aRestoreCommand
.append ( impl_getCommand_throw( true ) );
105 OUString
sRestoreCommand( aRestoreCommand
.makeStringAndClear() );
107 bool bDropSucceeded( false );
110 // drop the existing view
111 OUStringBuffer aCommand
;
112 aCommand
.appendAscii( "DROP VIEW " );
113 aCommand
.append ( sQualifiedName
);
114 xStatement
->execute( aCommand
.makeStringAndClear() );
115 bDropSucceeded
= true;
117 // create a new one with the same name
118 aCommand
.appendAscii( "CREATE VIEW " );
119 aCommand
.append ( sQualifiedName
);
120 aCommand
.appendAscii( " AS " );
121 aCommand
.append ( _rNewCommand
);
122 xStatement
->execute( aCommand
.makeStringAndClear() );
124 catch( const SQLException
& )
126 if ( bDropSucceeded
)
127 // drop succeeded, but creation failed -> re-create the view with the original
129 xStatement
->execute( sRestoreCommand
);
132 catch( const RuntimeException
& )
134 if ( bDropSucceeded
)
135 xStatement
->execute( sRestoreCommand
);
138 catch( const Exception
& )
140 if ( bDropSucceeded
)
141 xStatement
->execute( sRestoreCommand
);
142 DBG_UNHANDLED_EXCEPTION();
146 //--------------------------------------------------------------------
147 void SAL_CALL
HView::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
149 if ( _nHandle
== PROPERTY_ID_COMMAND
)
151 // retrieve the very current command, don't rely on the base classes cached value
152 // (which we initialized empty, anyway)
153 _rValue
<<= impl_getCommand_throw( false );
157 HView_Base::getFastPropertyValue( _rValue
, _nHandle
);
160 //--------------------------------------------------------------------
161 OUString
HView::impl_getCommand_throw( bool _bAllowSQLException
) const
167 OUStringBuffer aCommand
;
168 aCommand
.appendAscii( "SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.SYSTEM_VIEWS " );
169 HTools::appendTableFilterCrit( aCommand
, m_CatalogName
, m_SchemaName
, m_Name
, false );
170 ::utl::SharedUNOComponent
< XStatement
> xStatement
; xStatement
.set( m_xConnection
->createStatement(), UNO_QUERY_THROW
);
171 Reference
< XResultSet
> xResult( xStatement
->executeQuery( aCommand
.makeStringAndClear() ), UNO_QUERY_THROW
);
172 if ( !xResult
->next() )
174 // hmm. There is no view view the name as we know it. Can only mean some other instance
175 // dropped this view meanwhile ...
176 throw DisposedException();
179 Reference
< XRow
> xRow( xResult
, UNO_QUERY_THROW
);
180 sCommand
= xRow
->getString( 1 );
182 catch( const RuntimeException
& ) { throw; }
183 catch( const SQLException
& e
)
185 if ( _bAllowSQLException
)
187 throw WrappedTargetException( e
.Message
, static_cast< XAlterView
* >( const_cast< HView
* >( this ) ), ::cppu::getCaughtException() );
189 catch( const Exception
& )
191 DBG_UNHANDLED_EXCEPTION();
197 //........................................................................
198 } } // namespace connectivity::hsqldb
199 //........................................................................
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */