update dev300-m58
[ooovba.git] / connectivity / source / commontools / dbexception.cxx
blobabf83ac7c98135ad821751001a2ba9e0f9c24e7c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbexception.cxx,v $
10 * $Revision: 1.23.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <connectivity/dbexception.hxx>
34 #include <comphelper/types.hxx>
35 #include <cppuhelper/exc_hlp.hxx>
36 #include <osl/diagnose.h>
37 #include <com/sun/star/sdb/SQLContext.hpp>
38 #include <com/sun/star/sdbc/SQLWarning.hpp>
39 #include <com/sun/star/sdb/SQLErrorEvent.hpp>
40 #include "TConnection.hxx"
41 #include "resource/common_res.hrc"
42 #include "resource/sharedresources.hxx"
44 //.........................................................................
45 namespace dbtools
47 //.........................................................................
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::comphelper;
53 using namespace ::connectivity;
55 //==============================================================================
56 //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
57 //==============================================================================
58 //------------------------------------------------------------------------------
59 SQLExceptionInfo::SQLExceptionInfo()
60 :m_eType(UNDEFINED)
64 //------------------------------------------------------------------------------
65 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError)
67 m_aContent <<= _rError;
68 implDetermineType();
71 //------------------------------------------------------------------------------
72 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError)
74 m_aContent <<= _rError;
75 implDetermineType();
78 //------------------------------------------------------------------------------
79 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError)
81 m_aContent <<= _rError;
82 implDetermineType();
85 //------------------------------------------------------------------------------
86 SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString& _rSimpleErrorMessage )
88 SQLException aError;
89 aError.Message = _rSimpleErrorMessage;
90 m_aContent <<= aError;
91 implDetermineType();
94 //------------------------------------------------------------------------------
95 SQLExceptionInfo::SQLExceptionInfo(const SQLExceptionInfo& _rCopySource)
96 :m_aContent(_rCopySource.m_aContent)
97 ,m_eType(_rCopySource.m_eType)
101 //------------------------------------------------------------------------------
102 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLException& _rError)
104 m_aContent <<= _rError;
105 implDetermineType();
106 return *this;
109 //------------------------------------------------------------------------------
110 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning& _rError)
112 m_aContent <<= _rError;
113 implDetermineType();
114 return *this;
117 //------------------------------------------------------------------------------
118 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext& _rError)
120 m_aContent <<= _rError;
121 implDetermineType();
122 return *this;
125 //------------------------------------------------------------------------------
126 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent& _rErrorEvent)
128 m_aContent = _rErrorEvent.Reason;
129 implDetermineType();
130 return *this;
133 //------------------------------------------------------------------------------
134 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any& _rCaughtSQLException)
136 m_aContent = _rCaughtSQLException;
137 implDetermineType();
138 return *this;
141 //------------------------------------------------------------------------------
142 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent& _rError)
144 m_aContent = _rError.Reason;
145 implDetermineType();
148 //------------------------------------------------------------------------------
149 SQLExceptionInfo::SQLExceptionInfo(const staruno::Any& _rError)
151 const staruno::Type& aSQLExceptionType = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
152 sal_Bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
153 if (bValid)
154 m_aContent = _rError;
155 // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
157 implDetermineType();
160 //------------------------------------------------------------------------------
161 void SQLExceptionInfo::implDetermineType()
163 staruno::Type aContentType = m_aContent.getValueType();
165 const Type& aSQLExceptionType = ::getCppuType( reinterpret_cast< SQLException* >( NULL ) );
166 const Type& aSQLWarningType = ::getCppuType( reinterpret_cast< SQLWarning* >( NULL ) );
167 const Type& aSQLContextType = ::getCppuType( reinterpret_cast< SQLContext* >( NULL ) );
169 if ( isAssignableFrom( aSQLContextType, m_aContent.getValueType() ) )
170 m_eType = SQL_CONTEXT;
171 else if ( isAssignableFrom( aSQLWarningType, m_aContent.getValueType() ) )
172 m_eType = SQL_WARNING;
173 else if ( isAssignableFrom( aSQLExceptionType, m_aContent.getValueType() ) )
174 m_eType = SQL_EXCEPTION;
175 else
177 m_eType = UNDEFINED;
178 m_aContent.clear();
182 //------------------------------------------------------------------------------
183 sal_Bool SQLExceptionInfo::isKindOf(TYPE _eType) const
185 switch (_eType)
187 case SQL_CONTEXT:
188 return (m_eType == SQL_CONTEXT);
189 case SQL_WARNING:
190 return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING);
191 case SQL_EXCEPTION:
192 return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING) || (m_eType == SQL_EXCEPTION);
193 case UNDEFINED:
194 return (m_eType == UNDEFINED);
196 return sal_False;
199 //------------------------------------------------------------------------------
200 SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLException*() const
202 OSL_ENSURE(isKindOf(SQL_EXCEPTION), "SQLExceptionInfo::operator SQLException* : invalid call !");
203 return reinterpret_cast<const ::com::sun::star::sdbc::SQLException*>(m_aContent.getValue());
206 //------------------------------------------------------------------------------
207 SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLWarning*() const
209 OSL_ENSURE(isKindOf(SQL_WARNING), "SQLExceptionInfo::operator SQLException* : invalid call !");
210 return reinterpret_cast<const ::com::sun::star::sdbc::SQLWarning*>(m_aContent.getValue());
213 //------------------------------------------------------------------------------
214 SQLExceptionInfo::operator const ::com::sun::star::sdb::SQLContext*() const
216 OSL_ENSURE(isKindOf(SQL_CONTEXT), "SQLExceptionInfo::operator SQLException* : invalid call !");
217 return reinterpret_cast<const ::com::sun::star::sdb::SQLContext*>(m_aContent.getValue());
220 //------------------------------------------------------------------------------
221 void SQLExceptionInfo::prepend( const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
223 SQLException aException;
224 aException.Message = _rErrorMessage;
225 aException.ErrorCode = _nErrorCode;
226 aException.SQLState = ::rtl::OUString::createFromAscii( _pAsciiSQLState ? _pAsciiSQLState : "S1000" );
227 aException.NextException = m_aContent;
228 m_aContent <<= aException;
230 m_eType = SQL_EXCEPTION;
233 //------------------------------------------------------------------------------
234 void SQLExceptionInfo::append( TYPE _eType, const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
236 // create the to-be-appended exception
237 Any aAppend;
238 switch ( _eType )
240 case SQL_EXCEPTION: aAppend <<= SQLException(); break;
241 case SQL_WARNING: aAppend <<= SQLWarning(); break;
242 case SQL_CONTEXT: aAppend <<= SQLContext(); break;
243 default:
244 OSL_ENSURE( false, "SQLExceptionInfo::append: invalid exception type: this will crash!" );
245 break;
248 SQLException* pAppendException( static_cast< SQLException* >( const_cast< void* >( aAppend.getValue() ) ) );
249 pAppendException->Message = _rErrorMessage;
250 pAppendException->SQLState = ::rtl::OUString::createFromAscii( _pAsciiSQLState );
251 pAppendException->ErrorCode = _nErrorCode;
253 // find the end of the current chain
254 Any* pChainIterator = &m_aContent;
255 SQLException* pLastException = NULL;
256 const Type& aSQLExceptionType( ::getCppuType< SQLException >() );
257 while ( pChainIterator )
259 if ( !pChainIterator->hasValue() )
260 break;
262 if ( !isAssignableFrom( aSQLExceptionType, pChainIterator->getValueType() ) )
263 break;
265 pLastException = static_cast< SQLException* >( const_cast< void* >( pChainIterator->getValue() ) );
266 pChainIterator = &pLastException->NextException;
269 // append
270 if ( pLastException )
271 pLastException->NextException = aAppend;
272 else
274 m_aContent = aAppend;
275 m_eType = _eType;
279 //------------------------------------------------------------------------------
280 void SQLExceptionInfo::doThrow()
282 if ( m_aContent.getValueTypeClass() == TypeClass_EXCEPTION )
283 ::cppu::throwException( m_aContent );
284 throw RuntimeException();
287 //==============================================================================
288 //= SQLExceptionIteratorHelper
289 //==============================================================================
291 //------------------------------------------------------------------------------
292 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const SQLExceptionInfo& _rChainStart )
293 :m_pCurrent( NULL )
294 ,m_eCurrentType( SQLExceptionInfo::UNDEFINED )
296 if ( _rChainStart.isValid() )
298 m_pCurrent = (const SQLException*)_rChainStart;
299 m_eCurrentType = _rChainStart.getType();
303 //------------------------------------------------------------------------------
304 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException& _rChainStart )
305 :m_pCurrent( &_rChainStart )
306 ,m_eCurrentType( SQLExceptionInfo::SQL_EXCEPTION )
310 //------------------------------------------------------------------------------
311 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLWarning& _rChainStart )
312 :m_pCurrent( &_rChainStart )
313 ,m_eCurrentType( SQLExceptionInfo::SQL_WARNING )
317 //------------------------------------------------------------------------------
318 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdb::SQLContext& _rChainStart )
319 :m_pCurrent( &_rChainStart )
320 ,m_eCurrentType( SQLExceptionInfo::SQL_CONTEXT )
324 //------------------------------------------------------------------------------
325 void SQLExceptionIteratorHelper::current( SQLExceptionInfo& _out_rInfo ) const
327 switch ( m_eCurrentType )
329 case SQLExceptionInfo::SQL_EXCEPTION:
330 _out_rInfo = *m_pCurrent;
331 break;
333 case SQLExceptionInfo::SQL_WARNING:
334 _out_rInfo = *static_cast< const SQLWarning* >( m_pCurrent );
335 break;
337 case SQLExceptionInfo::SQL_CONTEXT:
338 _out_rInfo = *static_cast< const SQLContext* >( m_pCurrent );
339 break;
341 default:
342 _out_rInfo = Any();
343 break;
347 //------------------------------------------------------------------------------
348 const ::com::sun::star::sdbc::SQLException* SQLExceptionIteratorHelper::next()
350 OSL_ENSURE( hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements)!" );
352 const ::com::sun::star::sdbc::SQLException* pReturn = current();
353 if ( !m_pCurrent )
354 return pReturn;
356 // check for the next element within the chain
357 const Type aTypeException( ::cppu::UnoType< SQLException >::get() );
359 Type aNextElementType = m_pCurrent->NextException.getValueType();
360 if ( !isAssignableFrom( aTypeException, aNextElementType ) )
362 // no SQLException at all in the next chain element
363 m_pCurrent = NULL;
364 m_eCurrentType = SQLExceptionInfo::UNDEFINED;
365 return pReturn;
368 m_pCurrent = static_cast< const SQLException* >( m_pCurrent->NextException.getValue() );
370 // no finally determine the proper type of the exception
371 const Type aTypeContext( ::cppu::UnoType< SQLContext >::get() );
372 if ( isAssignableFrom( aTypeContext, aNextElementType ) )
374 m_eCurrentType = SQLExceptionInfo::SQL_CONTEXT;
375 return pReturn;
378 const Type aTypeWarning( ::cppu::UnoType< SQLWarning >::get() );
379 if ( isAssignableFrom( aTypeWarning, aNextElementType ) )
381 m_eCurrentType = SQLExceptionInfo::SQL_WARNING;
382 return pReturn;
385 // a simple SQLException
386 m_eCurrentType = SQLExceptionInfo::SQL_EXCEPTION;
387 return pReturn;
390 //------------------------------------------------------------------------------
391 void SQLExceptionIteratorHelper::next( SQLExceptionInfo& _out_rInfo )
393 current( _out_rInfo );
394 next();
397 //------------------------------------------------------------
398 void throwFunctionSequenceException(const Reference< XInterface >& _Context, const Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
400 ::connectivity::SharedResources aResources;
401 throw SQLException(
402 aResources.getResourceString(STR_ERRORMSG_SEQUENCE),
403 _Context,
404 getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR ),
406 _Next
409 // -----------------------------------------------------------------------------
410 void throwInvalidIndexException(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
411 const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
413 ::connectivity::SharedResources aResources;
414 throw SQLException(
415 aResources.getResourceString(STR_INVALID_INDEX),
416 _Context,
417 getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX ),
419 _Next
422 // -----------------------------------------------------------------------------
423 void throwFunctionNotSupportedException(const ::rtl::OUString& _rMsg,
424 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
425 const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
427 throw SQLException(
428 _rMsg,
429 _Context,
430 getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
432 _Next
435 // -----------------------------------------------------------------------------
436 void throwFunctionNotSupportedException( const sal_Char* _pAsciiFunctionName, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
437 const ::com::sun::star::uno::Any* _pNextException ) throw ( ::com::sun::star::sdbc::SQLException )
439 ::connectivity::SharedResources aResources;
440 const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
441 STR_UNSUPPORTED_FUNCTION,
442 "$functionname$", ::rtl::OUString::createFromAscii( _pAsciiFunctionName )
443 ) );
444 throw SQLException(
445 sError,
446 _rxContext,
447 getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
449 _pNextException ? *_pNextException : Any()
452 // -----------------------------------------------------------------------------
453 void throwGenericSQLException(const ::rtl::OUString& _rMsg, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource)
454 throw (::com::sun::star::sdbc::SQLException)
456 throwGenericSQLException(_rMsg, _rxSource, Any());
459 // -----------------------------------------------------------------------------
460 void throwGenericSQLException(const ::rtl::OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
461 throw (SQLException)
463 throw SQLException( _rMsg, _rxSource, getStandardSQLState( SQL_GENERAL_ERROR ), 0, _rNextException);
466 // -----------------------------------------------------------------------------
467 void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
468 throw (SQLException)
470 ::connectivity::SharedResources aResources;
471 const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
472 STR_UNSUPPORTED_FEATURE,
473 "$featurename$", ::rtl::OUString::createFromAscii( _pAsciiFeatureName )
474 ) );
476 throw SQLException(
477 sError,
478 _rxContext,
479 getStandardSQLState( SQL_FEATURE_NOT_IMPLEMENTED ),
481 _pNextException ? *_pNextException : Any()
485 // -----------------------------------------------------------------------------
486 void throwSQLException( const sal_Char* _pAsciiMessage, const sal_Char* _pAsciiState,
487 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode, const Any* _pNextException ) throw (SQLException)
489 throw SQLException(
490 ::rtl::OUString::createFromAscii( _pAsciiMessage ),
491 _rxContext,
492 ::rtl::OUString::createFromAscii( _pAsciiState ),
493 _nErrorCode,
494 _pNextException ? *_pNextException : Any()
498 // -----------------------------------------------------------------------------
499 void throwSQLException( const sal_Char* _pAsciiMessage, StandardSQLState _eSQLState,
500 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
501 const Any* _pNextException ) throw (SQLException)
503 throwSQLException( _pAsciiMessage, getStandardSQLStateAscii( _eSQLState ), _rxContext, _nErrorCode, _pNextException );
506 // -----------------------------------------------------------------------------
507 void throwSQLException( const ::rtl::OUString& _rMessage, StandardSQLState _eSQLState,
508 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
509 const Any* _pNextException ) throw (SQLException)
511 throw SQLException(
512 _rMessage,
513 _rxContext,
514 getStandardSQLState( _eSQLState ),
515 _nErrorCode,
516 _pNextException ? *_pNextException : Any()
520 // -----------------------------------------------------------------------------
521 const sal_Char* getStandardSQLStateAscii( StandardSQLState _eState )
523 const sal_Char* pAsciiState = NULL;
524 switch ( _eState )
526 case SQL_WRONG_PARAMETER_NUMBER: pAsciiState = "07001"; break;
527 case SQL_INVALID_DESCRIPTOR_INDEX: pAsciiState = "07009"; break;
528 case SQL_UNABLE_TO_CONNECT: pAsciiState = "08001"; break;
529 case SQL_NUMERIC_OUT_OF_RANGE: pAsciiState = "22003"; break;
530 case SQL_INVALID_DATE_TIME: pAsciiState = "22007"; break;
531 case SQL_INVALID_CURSOR_STATE: pAsciiState = "24000"; break;
532 case SQL_TABLE_OR_VIEW_EXISTS: pAsciiState = "42S01"; break;
533 case SQL_TABLE_OR_VIEW_NOT_FOUND: pAsciiState = "42S02"; break;
534 case SQL_INDEX_ESISTS: pAsciiState = "42S11"; break;
535 case SQL_INDEX_NOT_FOUND: pAsciiState = "42S12"; break;
536 case SQL_COLUMN_EXISTS: pAsciiState = "42S21"; break;
537 case SQL_COLUMN_NOT_FOUND: pAsciiState = "42S22"; break;
538 case SQL_GENERAL_ERROR: pAsciiState = "HY000"; break;
539 case SQL_INVALID_SQL_DATA_TYPE: pAsciiState = "HY004"; break;
540 case SQL_OPERATION_CANCELED: pAsciiState = "HY008"; break;
541 case SQL_FUNCTION_SEQUENCE_ERROR: pAsciiState = "HY010"; break;
542 case SQL_INVALID_CURSOR_POSITION: pAsciiState = "HY109"; break;
543 case SQL_INVALID_BOOKMARK_VALUE: pAsciiState = "HY111"; break;
544 case SQL_FEATURE_NOT_IMPLEMENTED: pAsciiState = "HYC00"; break;
545 case SQL_FUNCTION_NOT_SUPPORTED: pAsciiState = "IM001"; break;
546 case SQL_CONNECTION_DOES_NOT_EXIST: pAsciiState = "08003"; break;
548 default:
549 break;
551 if ( !pAsciiState )
552 throw RuntimeException();
553 return pAsciiState;
556 // -----------------------------------------------------------------------------
557 ::rtl::OUString getStandardSQLState( StandardSQLState _eState )
559 return ::rtl::OUString::createFromAscii( getStandardSQLStateAscii( _eState ) );
562 // -----------------------------------------------------------------------------
563 //.........................................................................
564 } // namespace dbtools
565 //.........................................................................