1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
27cdf0e10cSrcweir #include <comphelper/types.hxx>
28cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdb/SQLErrorEvent.hpp>
33cdf0e10cSrcweir #include "TConnection.hxx"
34cdf0e10cSrcweir #include "resource/common_res.hrc"
35cdf0e10cSrcweir #include "resource/sharedresources.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //.........................................................................
38cdf0e10cSrcweir namespace dbtools
39cdf0e10cSrcweir {
40cdf0e10cSrcweir //.........................................................................
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
43cdf0e10cSrcweir 	using namespace ::com::sun::star::sdb;
44cdf0e10cSrcweir 	using namespace ::com::sun::star::sdbc;
45cdf0e10cSrcweir     using namespace ::comphelper;
46cdf0e10cSrcweir 	using namespace ::connectivity;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //==============================================================================
49cdf0e10cSrcweir //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
50cdf0e10cSrcweir //==============================================================================
51cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo()52cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo()
53cdf0e10cSrcweir 	:m_eType(UNDEFINED)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const::com::sun::star::sdbc::SQLException & _rError)58cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	m_aContent <<= _rError;
61cdf0e10cSrcweir 	implDetermineType();
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const::com::sun::star::sdbc::SQLWarning & _rError)65cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	m_aContent <<= _rError;
68cdf0e10cSrcweir 	implDetermineType();
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const::com::sun::star::sdb::SQLContext & _rError)72cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	m_aContent <<= _rError;
75cdf0e10cSrcweir 	implDetermineType();
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const::rtl::OUString & _rSimpleErrorMessage)79cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString& _rSimpleErrorMessage )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     SQLException aError;
82cdf0e10cSrcweir     aError.Message = _rSimpleErrorMessage;
83cdf0e10cSrcweir 	m_aContent <<= aError;
84cdf0e10cSrcweir 	implDetermineType();
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const SQLExceptionInfo & _rCopySource)88cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const SQLExceptionInfo& _rCopySource)
89cdf0e10cSrcweir 	:m_aContent(_rCopySource.m_aContent)
90cdf0e10cSrcweir 	,m_eType(_rCopySource.m_eType)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir //------------------------------------------------------------------------------
operator =(const::com::sun::star::sdbc::SQLException & _rError)95cdf0e10cSrcweir const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLException& _rError)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	m_aContent <<= _rError;
98cdf0e10cSrcweir 	implDetermineType();
99cdf0e10cSrcweir 	return *this;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir //------------------------------------------------------------------------------
operator =(const::com::sun::star::sdbc::SQLWarning & _rError)103cdf0e10cSrcweir const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning& _rError)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	m_aContent <<= _rError;
106cdf0e10cSrcweir 	implDetermineType();
107cdf0e10cSrcweir 	return *this;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir //------------------------------------------------------------------------------
operator =(const::com::sun::star::sdb::SQLContext & _rError)111cdf0e10cSrcweir const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext& _rError)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	m_aContent <<= _rError;
114cdf0e10cSrcweir 	implDetermineType();
115cdf0e10cSrcweir 	return *this;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //------------------------------------------------------------------------------
operator =(const::com::sun::star::sdb::SQLErrorEvent & _rErrorEvent)119cdf0e10cSrcweir const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent& _rErrorEvent)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	m_aContent = _rErrorEvent.Reason;
122cdf0e10cSrcweir 	implDetermineType();
123cdf0e10cSrcweir 	return *this;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //------------------------------------------------------------------------------
operator =(const::com::sun::star::uno::Any & _rCaughtSQLException)127cdf0e10cSrcweir const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any& _rCaughtSQLException)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	m_aContent = _rCaughtSQLException;
130cdf0e10cSrcweir 	implDetermineType();
131cdf0e10cSrcweir 	return *this;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const::com::sun::star::sdb::SQLErrorEvent & _rError)135cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent& _rError)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	m_aContent = _rError.Reason;
138cdf0e10cSrcweir 	implDetermineType();
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionInfo(const staruno::Any & _rError)142cdf0e10cSrcweir SQLExceptionInfo::SQLExceptionInfo(const staruno::Any& _rError)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	const staruno::Type& aSQLExceptionType = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
145cdf0e10cSrcweir 	sal_Bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
146cdf0e10cSrcweir 	if (bValid)
147cdf0e10cSrcweir 		m_aContent = _rError;
148cdf0e10cSrcweir 	// no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	implDetermineType();
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir //------------------------------------------------------------------------------
implDetermineType()154cdf0e10cSrcweir void SQLExceptionInfo::implDetermineType()
155cdf0e10cSrcweir {
156cdf0e10cSrcweir 	staruno::Type aContentType = m_aContent.getValueType();
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     const Type& aSQLExceptionType = ::getCppuType( reinterpret_cast< SQLException* >( NULL ) );
159cdf0e10cSrcweir 	const Type& aSQLWarningType = ::getCppuType( reinterpret_cast< SQLWarning* >( NULL ) );
160cdf0e10cSrcweir 	const Type& aSQLContextType  = ::getCppuType( reinterpret_cast< SQLContext* >( NULL ) );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     if ( isAssignableFrom( aSQLContextType, m_aContent.getValueType() ) )
163cdf0e10cSrcweir 		m_eType = SQL_CONTEXT;
164cdf0e10cSrcweir 	else if ( isAssignableFrom( aSQLWarningType, m_aContent.getValueType() ) )
165cdf0e10cSrcweir 		m_eType = SQL_WARNING;
166cdf0e10cSrcweir 	else if ( isAssignableFrom( aSQLExceptionType, m_aContent.getValueType() ) )
167cdf0e10cSrcweir 		m_eType = SQL_EXCEPTION;
168cdf0e10cSrcweir 	else
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir 		m_eType = UNDEFINED;
171cdf0e10cSrcweir         m_aContent.clear();
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir //------------------------------------------------------------------------------
isKindOf(TYPE _eType) const176cdf0e10cSrcweir sal_Bool SQLExceptionInfo::isKindOf(TYPE _eType) const
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	switch (_eType)
179cdf0e10cSrcweir 	{
180cdf0e10cSrcweir 		case SQL_CONTEXT:
181cdf0e10cSrcweir 			return (m_eType == SQL_CONTEXT);
182cdf0e10cSrcweir 		case SQL_WARNING:
183cdf0e10cSrcweir 			return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING);
184cdf0e10cSrcweir 		case SQL_EXCEPTION:
185cdf0e10cSrcweir 			return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING) || (m_eType == SQL_EXCEPTION);
186cdf0e10cSrcweir 		case UNDEFINED:
187cdf0e10cSrcweir 			return (m_eType == UNDEFINED);
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 	return sal_False;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir //------------------------------------------------------------------------------
193cdf0e10cSrcweir SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLException*() const
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	OSL_ENSURE(isKindOf(SQL_EXCEPTION), "SQLExceptionInfo::operator SQLException* : invalid call !");
196cdf0e10cSrcweir 	return reinterpret_cast<const ::com::sun::star::sdbc::SQLException*>(m_aContent.getValue());
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir //------------------------------------------------------------------------------
200cdf0e10cSrcweir SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLWarning*() const
201cdf0e10cSrcweir {
202cdf0e10cSrcweir 	OSL_ENSURE(isKindOf(SQL_WARNING), "SQLExceptionInfo::operator SQLException* : invalid call !");
203cdf0e10cSrcweir 	return reinterpret_cast<const ::com::sun::star::sdbc::SQLWarning*>(m_aContent.getValue());
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir //------------------------------------------------------------------------------
207cdf0e10cSrcweir SQLExceptionInfo::operator const ::com::sun::star::sdb::SQLContext*() const
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	OSL_ENSURE(isKindOf(SQL_CONTEXT), "SQLExceptionInfo::operator SQLException* : invalid call !");
210cdf0e10cSrcweir 	return reinterpret_cast<const ::com::sun::star::sdb::SQLContext*>(m_aContent.getValue());
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir //------------------------------------------------------------------------------
prepend(const::rtl::OUString & _rErrorMessage,const sal_Char * _pAsciiSQLState,const sal_Int32 _nErrorCode)214cdf0e10cSrcweir void SQLExceptionInfo::prepend( const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir     SQLException aException;
217cdf0e10cSrcweir     aException.Message = _rErrorMessage;
218cdf0e10cSrcweir     aException.ErrorCode = _nErrorCode;
219cdf0e10cSrcweir     aException.SQLState = ::rtl::OUString::createFromAscii( _pAsciiSQLState ? _pAsciiSQLState : "S1000" );
220cdf0e10cSrcweir     aException.NextException = m_aContent;
221cdf0e10cSrcweir     m_aContent <<= aException;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     m_eType = SQL_EXCEPTION;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //------------------------------------------------------------------------------
append(TYPE _eType,const::rtl::OUString & _rErrorMessage,const sal_Char * _pAsciiSQLState,const sal_Int32 _nErrorCode)227cdf0e10cSrcweir void SQLExceptionInfo::append( TYPE _eType, const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir     // create the to-be-appended exception
230cdf0e10cSrcweir     Any aAppend;
231cdf0e10cSrcweir     switch ( _eType )
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir     case SQL_EXCEPTION: aAppend <<= SQLException(); break;
234cdf0e10cSrcweir     case SQL_WARNING:   aAppend <<= SQLWarning();   break;
235cdf0e10cSrcweir     case SQL_CONTEXT:   aAppend <<= SQLContext();   break;
236cdf0e10cSrcweir     default:
237cdf0e10cSrcweir         OSL_ENSURE( false, "SQLExceptionInfo::append: invalid exception type: this will crash!" );
238cdf0e10cSrcweir         break;
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     SQLException* pAppendException( static_cast< SQLException* >( const_cast< void* >( aAppend.getValue() ) ) );
242cdf0e10cSrcweir     pAppendException->Message = _rErrorMessage;
243cdf0e10cSrcweir     pAppendException->SQLState = ::rtl::OUString::createFromAscii( _pAsciiSQLState );
244cdf0e10cSrcweir     pAppendException->ErrorCode = _nErrorCode;
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     // find the end of the current chain
247cdf0e10cSrcweir     Any* pChainIterator = &m_aContent;
248cdf0e10cSrcweir     SQLException* pLastException = NULL;
249cdf0e10cSrcweir     const Type& aSQLExceptionType( ::getCppuType< SQLException >() );
250cdf0e10cSrcweir     while ( pChainIterator )
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir         if ( !pChainIterator->hasValue() )
253cdf0e10cSrcweir             break;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	    if ( !isAssignableFrom( aSQLExceptionType, pChainIterator->getValueType() ) )
256cdf0e10cSrcweir             break;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         pLastException = static_cast< SQLException* >( const_cast< void* >( pChainIterator->getValue() ) );
259cdf0e10cSrcweir         pChainIterator = &pLastException->NextException;
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     // append
263cdf0e10cSrcweir     if ( pLastException )
264cdf0e10cSrcweir         pLastException->NextException = aAppend;
265cdf0e10cSrcweir     else
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         m_aContent = aAppend;
268cdf0e10cSrcweir         m_eType = _eType;
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir //------------------------------------------------------------------------------
doThrow()273cdf0e10cSrcweir void SQLExceptionInfo::doThrow()
274cdf0e10cSrcweir {
275cdf0e10cSrcweir     if ( m_aContent.getValueTypeClass() == TypeClass_EXCEPTION )
276cdf0e10cSrcweir         ::cppu::throwException( m_aContent );
277cdf0e10cSrcweir     throw RuntimeException();
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
280cdf0e10cSrcweir //==============================================================================
281cdf0e10cSrcweir //= SQLExceptionIteratorHelper
282cdf0e10cSrcweir //==============================================================================
283cdf0e10cSrcweir 
284cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionIteratorHelper(const SQLExceptionInfo & _rChainStart)285cdf0e10cSrcweir SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const SQLExceptionInfo& _rChainStart )
286cdf0e10cSrcweir 	:m_pCurrent( NULL )
287cdf0e10cSrcweir 	,m_eCurrentType( SQLExceptionInfo::UNDEFINED )
288cdf0e10cSrcweir {
289cdf0e10cSrcweir 	if ( _rChainStart.isValid() )
290cdf0e10cSrcweir 	{
291cdf0e10cSrcweir 		m_pCurrent = (const SQLException*)_rChainStart;
292cdf0e10cSrcweir 		m_eCurrentType = _rChainStart.getType();
293cdf0e10cSrcweir 	}
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionIteratorHelper(const::com::sun::star::sdbc::SQLException & _rChainStart)297cdf0e10cSrcweir SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException& _rChainStart )
298cdf0e10cSrcweir 	:m_pCurrent( &_rChainStart )
299cdf0e10cSrcweir     ,m_eCurrentType( SQLExceptionInfo::SQL_EXCEPTION )
300cdf0e10cSrcweir {
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionIteratorHelper(const::com::sun::star::sdbc::SQLWarning & _rChainStart)304cdf0e10cSrcweir SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLWarning& _rChainStart )
305cdf0e10cSrcweir 	:m_pCurrent( &_rChainStart )
306cdf0e10cSrcweir 	,m_eCurrentType( SQLExceptionInfo::SQL_WARNING )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir //------------------------------------------------------------------------------
SQLExceptionIteratorHelper(const::com::sun::star::sdb::SQLContext & _rChainStart)311cdf0e10cSrcweir SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdb::SQLContext& _rChainStart )
312cdf0e10cSrcweir 	:m_pCurrent( &_rChainStart )
313cdf0e10cSrcweir 	,m_eCurrentType( SQLExceptionInfo::SQL_CONTEXT )
314cdf0e10cSrcweir {
315cdf0e10cSrcweir }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir //------------------------------------------------------------------------------
current(SQLExceptionInfo & _out_rInfo) const318cdf0e10cSrcweir void SQLExceptionIteratorHelper::current( SQLExceptionInfo& _out_rInfo ) const
319cdf0e10cSrcweir {
320cdf0e10cSrcweir     switch ( m_eCurrentType )
321cdf0e10cSrcweir     {
322cdf0e10cSrcweir     case SQLExceptionInfo::SQL_EXCEPTION:
323cdf0e10cSrcweir         _out_rInfo = *m_pCurrent;
324cdf0e10cSrcweir         break;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     case SQLExceptionInfo::SQL_WARNING:
327cdf0e10cSrcweir         _out_rInfo = *static_cast< const SQLWarning* >( m_pCurrent );
328cdf0e10cSrcweir         break;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     case SQLExceptionInfo::SQL_CONTEXT:
331cdf0e10cSrcweir         _out_rInfo = *static_cast< const SQLContext* >( m_pCurrent );
332cdf0e10cSrcweir         break;
333cdf0e10cSrcweir 
334cdf0e10cSrcweir     default:
335cdf0e10cSrcweir         _out_rInfo = Any();
336cdf0e10cSrcweir         break;
337cdf0e10cSrcweir     }
338cdf0e10cSrcweir }
339cdf0e10cSrcweir 
340cdf0e10cSrcweir //------------------------------------------------------------------------------
next()341cdf0e10cSrcweir const ::com::sun::star::sdbc::SQLException* SQLExceptionIteratorHelper::next()
342cdf0e10cSrcweir {
343cdf0e10cSrcweir 	OSL_ENSURE( hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements)!" );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 	const ::com::sun::star::sdbc::SQLException* pReturn = current();
346cdf0e10cSrcweir     if ( !m_pCurrent )
347cdf0e10cSrcweir         return pReturn;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     // check for the next element within the chain
350cdf0e10cSrcweir     const Type aTypeException( ::cppu::UnoType< SQLException >::get() );
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     Type aNextElementType = m_pCurrent->NextException.getValueType();
353cdf0e10cSrcweir     if ( !isAssignableFrom( aTypeException, aNextElementType ) )
354cdf0e10cSrcweir     {
355cdf0e10cSrcweir         // no SQLException at all in the next chain element
356cdf0e10cSrcweir         m_pCurrent = NULL;
357cdf0e10cSrcweir         m_eCurrentType = SQLExceptionInfo::UNDEFINED;
358cdf0e10cSrcweir         return pReturn;
359cdf0e10cSrcweir     }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir     m_pCurrent = static_cast< const SQLException* >( m_pCurrent->NextException.getValue() );
362cdf0e10cSrcweir 
363cdf0e10cSrcweir     // no finally determine the proper type of the exception
364cdf0e10cSrcweir     const Type aTypeContext( ::cppu::UnoType< SQLContext >::get() );
365cdf0e10cSrcweir     if ( isAssignableFrom( aTypeContext, aNextElementType ) )
366cdf0e10cSrcweir     {
367cdf0e10cSrcweir         m_eCurrentType = SQLExceptionInfo::SQL_CONTEXT;
368cdf0e10cSrcweir         return pReturn;
369cdf0e10cSrcweir     }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     const Type aTypeWarning( ::cppu::UnoType< SQLWarning >::get() );
372cdf0e10cSrcweir     if ( isAssignableFrom( aTypeWarning, aNextElementType ) )
373cdf0e10cSrcweir     {
374cdf0e10cSrcweir         m_eCurrentType = SQLExceptionInfo::SQL_WARNING;
375cdf0e10cSrcweir         return pReturn;
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     // a simple SQLException
379cdf0e10cSrcweir     m_eCurrentType = SQLExceptionInfo::SQL_EXCEPTION;
380cdf0e10cSrcweir 	return pReturn;
381cdf0e10cSrcweir }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir //------------------------------------------------------------------------------
next(SQLExceptionInfo & _out_rInfo)384cdf0e10cSrcweir void SQLExceptionIteratorHelper::next( SQLExceptionInfo& _out_rInfo )
385cdf0e10cSrcweir {
386cdf0e10cSrcweir     current( _out_rInfo );
387cdf0e10cSrcweir     next();
388cdf0e10cSrcweir }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir //------------------------------------------------------------
throwFunctionSequenceException(const Reference<XInterface> & _Context,const Any & _Next)391cdf0e10cSrcweir void throwFunctionSequenceException(const Reference< XInterface >& _Context, const Any& _Next)  throw ( ::com::sun::star::sdbc::SQLException )
392cdf0e10cSrcweir {
393cdf0e10cSrcweir     ::connectivity::SharedResources aResources;
394cdf0e10cSrcweir 	throw SQLException(
395cdf0e10cSrcweir         aResources.getResourceString(STR_ERRORMSG_SEQUENCE),
396cdf0e10cSrcweir         _Context,
397cdf0e10cSrcweir         getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR ),
398cdf0e10cSrcweir         0,
399cdf0e10cSrcweir         _Next
400cdf0e10cSrcweir     );
401cdf0e10cSrcweir }
402cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwInvalidIndexException(const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _Context,const::com::sun::star::uno::Any & _Next)403cdf0e10cSrcweir void throwInvalidIndexException(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
404cdf0e10cSrcweir 		const ::com::sun::star::uno::Any& _Next)  throw ( ::com::sun::star::sdbc::SQLException )
405cdf0e10cSrcweir {
406cdf0e10cSrcweir     ::connectivity::SharedResources aResources;
407cdf0e10cSrcweir 	throw SQLException(
408cdf0e10cSrcweir         aResources.getResourceString(STR_INVALID_INDEX),
409cdf0e10cSrcweir         _Context,
410cdf0e10cSrcweir         getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX ),
411cdf0e10cSrcweir         0,
412cdf0e10cSrcweir         _Next
413cdf0e10cSrcweir     );
414cdf0e10cSrcweir }
415cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwFunctionNotSupportedException(const::rtl::OUString & _rMsg,const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _Context,const::com::sun::star::uno::Any & _Next)416cdf0e10cSrcweir void throwFunctionNotSupportedException(const ::rtl::OUString& _rMsg,
417cdf0e10cSrcweir         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
418cdf0e10cSrcweir         const ::com::sun::star::uno::Any& _Next)  throw ( ::com::sun::star::sdbc::SQLException )
419cdf0e10cSrcweir {
420cdf0e10cSrcweir 	throw SQLException(
421cdf0e10cSrcweir         _rMsg,
422cdf0e10cSrcweir         _Context,
423cdf0e10cSrcweir         getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
424cdf0e10cSrcweir         0,
425cdf0e10cSrcweir         _Next
426cdf0e10cSrcweir     );
427cdf0e10cSrcweir }
428cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwFunctionNotSupportedException(const sal_Char * _pAsciiFunctionName,const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _rxContext,const::com::sun::star::uno::Any * _pNextException)429cdf0e10cSrcweir void throwFunctionNotSupportedException( const sal_Char* _pAsciiFunctionName, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
430cdf0e10cSrcweir         const ::com::sun::star::uno::Any* _pNextException ) throw ( ::com::sun::star::sdbc::SQLException )
431cdf0e10cSrcweir {
432cdf0e10cSrcweir     ::connectivity::SharedResources aResources;
433cdf0e10cSrcweir     const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
434cdf0e10cSrcweir             STR_UNSUPPORTED_FUNCTION,
435cdf0e10cSrcweir             "$functionname$", ::rtl::OUString::createFromAscii( _pAsciiFunctionName )
436cdf0e10cSrcweir          ) );
437cdf0e10cSrcweir     throw SQLException(
438cdf0e10cSrcweir         sError,
439cdf0e10cSrcweir         _rxContext,
440cdf0e10cSrcweir         getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
441cdf0e10cSrcweir         0,
442cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
443cdf0e10cSrcweir     );
444cdf0e10cSrcweir }
445cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwGenericSQLException(const::rtl::OUString & _rMsg,const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _rxSource)446cdf0e10cSrcweir void throwGenericSQLException(const ::rtl::OUString& _rMsg, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource)
447cdf0e10cSrcweir 	throw (::com::sun::star::sdbc::SQLException)
448cdf0e10cSrcweir {
449cdf0e10cSrcweir 	throwGenericSQLException(_rMsg, _rxSource, Any());
450cdf0e10cSrcweir }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwGenericSQLException(const::rtl::OUString & _rMsg,const Reference<XInterface> & _rxSource,const Any & _rNextException)453cdf0e10cSrcweir void throwGenericSQLException(const ::rtl::OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
454cdf0e10cSrcweir 	throw (SQLException)
455cdf0e10cSrcweir {
456cdf0e10cSrcweir 	throw SQLException( _rMsg, _rxSource, getStandardSQLState( SQL_GENERAL_ERROR ), 0, _rNextException);
457cdf0e10cSrcweir }
458cdf0e10cSrcweir 
459cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwFeatureNotImplementedException(const sal_Char * _pAsciiFeatureName,const Reference<XInterface> & _rxContext,const Any * _pNextException)460cdf0e10cSrcweir void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
461cdf0e10cSrcweir     throw (SQLException)
462cdf0e10cSrcweir {
463cdf0e10cSrcweir     ::connectivity::SharedResources aResources;
464cdf0e10cSrcweir     const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
465cdf0e10cSrcweir             STR_UNSUPPORTED_FEATURE,
466cdf0e10cSrcweir             "$featurename$", ::rtl::OUString::createFromAscii( _pAsciiFeatureName )
467cdf0e10cSrcweir          ) );
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     throw SQLException(
470cdf0e10cSrcweir         sError,
471cdf0e10cSrcweir         _rxContext,
472cdf0e10cSrcweir         getStandardSQLState( SQL_FEATURE_NOT_IMPLEMENTED ),
473cdf0e10cSrcweir         0,
474cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
475cdf0e10cSrcweir     );
476cdf0e10cSrcweir }
477cdf0e10cSrcweir 
478cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwSQLException(const sal_Char * _pAsciiMessage,const sal_Char * _pAsciiState,const Reference<XInterface> & _rxContext,const sal_Int32 _nErrorCode,const Any * _pNextException)479cdf0e10cSrcweir void throwSQLException( const sal_Char* _pAsciiMessage, const sal_Char* _pAsciiState,
480cdf0e10cSrcweir         const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode, const Any* _pNextException ) throw (SQLException)
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     throw SQLException(
483cdf0e10cSrcweir         ::rtl::OUString::createFromAscii( _pAsciiMessage ),
484cdf0e10cSrcweir         _rxContext,
485cdf0e10cSrcweir         ::rtl::OUString::createFromAscii( _pAsciiState ),
486cdf0e10cSrcweir         _nErrorCode,
487cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
488cdf0e10cSrcweir     );
489cdf0e10cSrcweir }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwSQLException(const sal_Char * _pAsciiMessage,StandardSQLState _eSQLState,const Reference<XInterface> & _rxContext,const sal_Int32 _nErrorCode,const Any * _pNextException)492cdf0e10cSrcweir void throwSQLException( const sal_Char* _pAsciiMessage, StandardSQLState _eSQLState,
493cdf0e10cSrcweir         const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
494cdf0e10cSrcweir         const Any* _pNextException ) throw (SQLException)
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     throwSQLException( _pAsciiMessage, getStandardSQLStateAscii( _eSQLState ), _rxContext, _nErrorCode, _pNextException );
497cdf0e10cSrcweir }
498cdf0e10cSrcweir 
499cdf0e10cSrcweir // -----------------------------------------------------------------------------
throwSQLException(const::rtl::OUString & _rMessage,StandardSQLState _eSQLState,const Reference<XInterface> & _rxContext,const sal_Int32 _nErrorCode,const Any * _pNextException)500cdf0e10cSrcweir void throwSQLException( const ::rtl::OUString& _rMessage, StandardSQLState _eSQLState,
501cdf0e10cSrcweir         const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
502cdf0e10cSrcweir         const Any* _pNextException ) throw (SQLException)
503cdf0e10cSrcweir {
504cdf0e10cSrcweir     throw SQLException(
505cdf0e10cSrcweir         _rMessage,
506cdf0e10cSrcweir         _rxContext,
507cdf0e10cSrcweir         getStandardSQLState( _eSQLState ),
508cdf0e10cSrcweir         _nErrorCode,
509cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
510cdf0e10cSrcweir     );
511cdf0e10cSrcweir }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStandardSQLStateAscii(StandardSQLState _eState)514cdf0e10cSrcweir const sal_Char* getStandardSQLStateAscii( StandardSQLState _eState )
515cdf0e10cSrcweir {
516cdf0e10cSrcweir     const sal_Char* pAsciiState = NULL;
517cdf0e10cSrcweir     switch ( _eState )
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         case SQL_WRONG_PARAMETER_NUMBER:    pAsciiState = "07001"; break;
520cdf0e10cSrcweir         case SQL_INVALID_DESCRIPTOR_INDEX:  pAsciiState = "07009"; break;
521cdf0e10cSrcweir         case SQL_UNABLE_TO_CONNECT:         pAsciiState = "08001"; break;
522cdf0e10cSrcweir         case SQL_NUMERIC_OUT_OF_RANGE:      pAsciiState = "22003"; break;
523cdf0e10cSrcweir         case SQL_INVALID_DATE_TIME:         pAsciiState = "22007"; break;
524cdf0e10cSrcweir         case SQL_INVALID_CURSOR_STATE:      pAsciiState = "24000"; break;
525cdf0e10cSrcweir         case SQL_TABLE_OR_VIEW_EXISTS:      pAsciiState = "42S01"; break;
526cdf0e10cSrcweir         case SQL_TABLE_OR_VIEW_NOT_FOUND:   pAsciiState = "42S02"; break;
527cdf0e10cSrcweir         case SQL_INDEX_ESISTS:              pAsciiState = "42S11"; break;
528cdf0e10cSrcweir         case SQL_INDEX_NOT_FOUND:           pAsciiState = "42S12"; break;
529cdf0e10cSrcweir         case SQL_COLUMN_EXISTS:             pAsciiState = "42S21"; break;
530cdf0e10cSrcweir         case SQL_COLUMN_NOT_FOUND:          pAsciiState = "42S22"; break;
531cdf0e10cSrcweir         case SQL_GENERAL_ERROR:             pAsciiState = "HY000"; break;
532cdf0e10cSrcweir         case SQL_INVALID_SQL_DATA_TYPE:     pAsciiState = "HY004"; break;
533cdf0e10cSrcweir         case SQL_OPERATION_CANCELED:        pAsciiState = "HY008"; break;
534cdf0e10cSrcweir         case SQL_FUNCTION_SEQUENCE_ERROR:   pAsciiState = "HY010"; break;
535cdf0e10cSrcweir         case SQL_INVALID_CURSOR_POSITION:   pAsciiState = "HY109"; break;
536cdf0e10cSrcweir         case SQL_INVALID_BOOKMARK_VALUE:    pAsciiState = "HY111"; break;
537cdf0e10cSrcweir         case SQL_FEATURE_NOT_IMPLEMENTED:   pAsciiState = "HYC00"; break;
538cdf0e10cSrcweir         case SQL_FUNCTION_NOT_SUPPORTED:    pAsciiState = "IM001"; break;
539cdf0e10cSrcweir         case SQL_CONNECTION_DOES_NOT_EXIST: pAsciiState = "08003"; break;
540cdf0e10cSrcweir 
541cdf0e10cSrcweir         default:
542cdf0e10cSrcweir             break;
543cdf0e10cSrcweir     }
544cdf0e10cSrcweir     if ( !pAsciiState )
545cdf0e10cSrcweir         throw RuntimeException();
546cdf0e10cSrcweir     return pAsciiState;
547cdf0e10cSrcweir }
548cdf0e10cSrcweir 
549cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStandardSQLState(StandardSQLState _eState)550cdf0e10cSrcweir ::rtl::OUString getStandardSQLState( StandardSQLState _eState )
551cdf0e10cSrcweir {
552cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii( getStandardSQLStateAscii( _eState ) );
553cdf0e10cSrcweir }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir // -----------------------------------------------------------------------------
556cdf0e10cSrcweir //.........................................................................
557cdf0e10cSrcweir }	// namespace dbtools
558cdf0e10cSrcweir //.........................................................................
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 
561