Bump for 3.6-28
[LibreOffice.git] / connectivity / source / commontools / dbexception.cxx
blob9ca90e37a16897f25e82bb82e8134ef2fc2ce1aa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <connectivity/dbexception.hxx>
30 #include <comphelper/types.hxx>
31 #include <cppuhelper/exc_hlp.hxx>
32 #include <osl/diagnose.h>
33 #include <com/sun/star/sdb/SQLContext.hpp>
34 #include <com/sun/star/sdbc/SQLWarning.hpp>
35 #include <com/sun/star/sdb/SQLErrorEvent.hpp>
36 #include "TConnection.hxx"
37 #include "resource/common_res.hrc"
38 #include "resource/sharedresources.hxx"
40 //.........................................................................
41 namespace dbtools
43 //.........................................................................
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::sdb;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::comphelper;
49 using namespace ::connectivity;
51 //==============================================================================
52 //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
53 //==============================================================================
54 //------------------------------------------------------------------------------
55 SQLExceptionInfo::SQLExceptionInfo()
56 :m_eType(UNDEFINED)
60 //------------------------------------------------------------------------------
61 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError)
63 m_aContent <<= _rError;
64 implDetermineType();
67 //------------------------------------------------------------------------------
68 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError)
70 m_aContent <<= _rError;
71 implDetermineType();
74 //------------------------------------------------------------------------------
75 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError)
77 m_aContent <<= _rError;
78 implDetermineType();
81 //------------------------------------------------------------------------------
82 SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString& _rSimpleErrorMessage )
84 SQLException aError;
85 aError.Message = _rSimpleErrorMessage;
86 m_aContent <<= aError;
87 implDetermineType();
90 //------------------------------------------------------------------------------
91 SQLExceptionInfo::SQLExceptionInfo(const SQLExceptionInfo& _rCopySource)
92 :m_aContent(_rCopySource.m_aContent)
93 ,m_eType(_rCopySource.m_eType)
97 //------------------------------------------------------------------------------
98 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLException& _rError)
100 m_aContent <<= _rError;
101 implDetermineType();
102 return *this;
105 //------------------------------------------------------------------------------
106 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning& _rError)
108 m_aContent <<= _rError;
109 implDetermineType();
110 return *this;
113 //------------------------------------------------------------------------------
114 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext& _rError)
116 m_aContent <<= _rError;
117 implDetermineType();
118 return *this;
121 //------------------------------------------------------------------------------
122 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent& _rErrorEvent)
124 m_aContent = _rErrorEvent.Reason;
125 implDetermineType();
126 return *this;
129 //------------------------------------------------------------------------------
130 const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any& _rCaughtSQLException)
132 m_aContent = _rCaughtSQLException;
133 implDetermineType();
134 return *this;
137 //------------------------------------------------------------------------------
138 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent& _rError)
140 m_aContent = _rError.Reason;
141 implDetermineType();
144 //------------------------------------------------------------------------------
145 SQLExceptionInfo::SQLExceptionInfo(const staruno::Any& _rError)
147 const staruno::Type& aSQLExceptionType = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
148 sal_Bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
149 if (bValid)
150 m_aContent = _rError;
151 // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
153 implDetermineType();
156 //------------------------------------------------------------------------------
157 void SQLExceptionInfo::implDetermineType()
159 staruno::Type aContentType = m_aContent.getValueType();
161 const Type& aSQLExceptionType = ::getCppuType( reinterpret_cast< SQLException* >( NULL ) );
162 const Type& aSQLWarningType = ::getCppuType( reinterpret_cast< SQLWarning* >( NULL ) );
163 const Type& aSQLContextType = ::getCppuType( reinterpret_cast< SQLContext* >( NULL ) );
165 if ( isAssignableFrom( aSQLContextType, m_aContent.getValueType() ) )
166 m_eType = SQL_CONTEXT;
167 else if ( isAssignableFrom( aSQLWarningType, m_aContent.getValueType() ) )
168 m_eType = SQL_WARNING;
169 else if ( isAssignableFrom( aSQLExceptionType, m_aContent.getValueType() ) )
170 m_eType = SQL_EXCEPTION;
171 else
173 m_eType = UNDEFINED;
174 m_aContent.clear();
178 //------------------------------------------------------------------------------
179 sal_Bool SQLExceptionInfo::isKindOf(TYPE _eType) const
181 switch (_eType)
183 case SQL_CONTEXT:
184 return (m_eType == SQL_CONTEXT);
185 case SQL_WARNING:
186 return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING);
187 case SQL_EXCEPTION:
188 return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING) || (m_eType == SQL_EXCEPTION);
189 case UNDEFINED:
190 return (m_eType == UNDEFINED);
192 return sal_False;
195 //------------------------------------------------------------------------------
196 SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLException*() const
198 OSL_ENSURE(isKindOf(SQL_EXCEPTION), "SQLExceptionInfo::operator SQLException* : invalid call !");
199 return reinterpret_cast<const ::com::sun::star::sdbc::SQLException*>(m_aContent.getValue());
202 //------------------------------------------------------------------------------
203 SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLWarning*() const
205 OSL_ENSURE(isKindOf(SQL_WARNING), "SQLExceptionInfo::operator SQLException* : invalid call !");
206 return reinterpret_cast<const ::com::sun::star::sdbc::SQLWarning*>(m_aContent.getValue());
209 //------------------------------------------------------------------------------
210 SQLExceptionInfo::operator const ::com::sun::star::sdb::SQLContext*() const
212 OSL_ENSURE(isKindOf(SQL_CONTEXT), "SQLExceptionInfo::operator SQLException* : invalid call !");
213 return reinterpret_cast<const ::com::sun::star::sdb::SQLContext*>(m_aContent.getValue());
216 //------------------------------------------------------------------------------
217 void SQLExceptionInfo::prepend( const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
219 SQLException aException;
220 aException.Message = _rErrorMessage;
221 aException.ErrorCode = _nErrorCode;
222 aException.SQLState = _pAsciiSQLState ? ::rtl::OUString::createFromAscii( _pAsciiSQLState ) : ::rtl::OUString("S1000" );
223 aException.NextException = m_aContent;
224 m_aContent <<= aException;
226 m_eType = SQL_EXCEPTION;
229 //------------------------------------------------------------------------------
230 void SQLExceptionInfo::append( TYPE _eType, const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState, const sal_Int32 _nErrorCode )
232 // create the to-be-appended exception
233 Any aAppend;
234 switch ( _eType )
236 case SQL_EXCEPTION: aAppend <<= SQLException(); break;
237 case SQL_WARNING: aAppend <<= SQLWarning(); break;
238 case SQL_CONTEXT: aAppend <<= SQLContext(); break;
239 default:
240 OSL_FAIL( "SQLExceptionInfo::append: invalid exception type: this will crash!" );
241 break;
244 SQLException* pAppendException( static_cast< SQLException* >( const_cast< void* >( aAppend.getValue() ) ) );
245 pAppendException->Message = _rErrorMessage;
246 pAppendException->SQLState = ::rtl::OUString::createFromAscii( _pAsciiSQLState );
247 pAppendException->ErrorCode = _nErrorCode;
249 // find the end of the current chain
250 Any* pChainIterator = &m_aContent;
251 SQLException* pLastException = NULL;
252 const Type& aSQLExceptionType( ::getCppuType< SQLException >() );
253 while ( pChainIterator )
255 if ( !pChainIterator->hasValue() )
256 break;
258 if ( !isAssignableFrom( aSQLExceptionType, pChainIterator->getValueType() ) )
259 break;
261 pLastException = static_cast< SQLException* >( const_cast< void* >( pChainIterator->getValue() ) );
262 pChainIterator = &pLastException->NextException;
265 // append
266 if ( pLastException )
267 pLastException->NextException = aAppend;
268 else
270 m_aContent = aAppend;
271 m_eType = _eType;
275 //------------------------------------------------------------------------------
276 void SQLExceptionInfo::doThrow()
278 if ( m_aContent.getValueTypeClass() == TypeClass_EXCEPTION )
279 ::cppu::throwException( m_aContent );
280 throw RuntimeException();
283 //==============================================================================
284 //= SQLExceptionIteratorHelper
285 //==============================================================================
287 //------------------------------------------------------------------------------
288 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const SQLExceptionInfo& _rChainStart )
289 :m_pCurrent( NULL )
290 ,m_eCurrentType( SQLExceptionInfo::UNDEFINED )
292 if ( _rChainStart.isValid() )
294 m_pCurrent = (const SQLException*)_rChainStart;
295 m_eCurrentType = _rChainStart.getType();
299 //------------------------------------------------------------------------------
300 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException& _rChainStart )
301 :m_pCurrent( &_rChainStart )
302 ,m_eCurrentType( SQLExceptionInfo::SQL_EXCEPTION )
306 //------------------------------------------------------------------------------
307 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLWarning& _rChainStart )
308 :m_pCurrent( &_rChainStart )
309 ,m_eCurrentType( SQLExceptionInfo::SQL_WARNING )
313 //------------------------------------------------------------------------------
314 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdb::SQLContext& _rChainStart )
315 :m_pCurrent( &_rChainStart )
316 ,m_eCurrentType( SQLExceptionInfo::SQL_CONTEXT )
320 //------------------------------------------------------------------------------
321 void SQLExceptionIteratorHelper::current( SQLExceptionInfo& _out_rInfo ) const
323 switch ( m_eCurrentType )
325 case SQLExceptionInfo::SQL_EXCEPTION:
326 _out_rInfo = *m_pCurrent;
327 break;
329 case SQLExceptionInfo::SQL_WARNING:
330 _out_rInfo = *static_cast< const SQLWarning* >( m_pCurrent );
331 break;
333 case SQLExceptionInfo::SQL_CONTEXT:
334 _out_rInfo = *static_cast< const SQLContext* >( m_pCurrent );
335 break;
337 default:
338 _out_rInfo = Any();
339 break;
343 //------------------------------------------------------------------------------
344 const ::com::sun::star::sdbc::SQLException* SQLExceptionIteratorHelper::next()
346 OSL_ENSURE( hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements)!" );
348 const ::com::sun::star::sdbc::SQLException* pReturn = current();
349 if ( !m_pCurrent )
350 return pReturn;
352 // check for the next element within the chain
353 const Type aTypeException( ::cppu::UnoType< SQLException >::get() );
355 Type aNextElementType = m_pCurrent->NextException.getValueType();
356 if ( !isAssignableFrom( aTypeException, aNextElementType ) )
358 // no SQLException at all in the next chain element
359 m_pCurrent = NULL;
360 m_eCurrentType = SQLExceptionInfo::UNDEFINED;
361 return pReturn;
364 m_pCurrent = static_cast< const SQLException* >( m_pCurrent->NextException.getValue() );
366 // no finally determine the proper type of the exception
367 const Type aTypeContext( ::cppu::UnoType< SQLContext >::get() );
368 if ( isAssignableFrom( aTypeContext, aNextElementType ) )
370 m_eCurrentType = SQLExceptionInfo::SQL_CONTEXT;
371 return pReturn;
374 const Type aTypeWarning( ::cppu::UnoType< SQLWarning >::get() );
375 if ( isAssignableFrom( aTypeWarning, aNextElementType ) )
377 m_eCurrentType = SQLExceptionInfo::SQL_WARNING;
378 return pReturn;
381 // a simple SQLException
382 m_eCurrentType = SQLExceptionInfo::SQL_EXCEPTION;
383 return pReturn;
386 //------------------------------------------------------------------------------
387 void SQLExceptionIteratorHelper::next( SQLExceptionInfo& _out_rInfo )
389 current( _out_rInfo );
390 next();
393 //------------------------------------------------------------
394 void throwFunctionSequenceException(const Reference< XInterface >& _Context, const Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
396 ::connectivity::SharedResources aResources;
397 throw SQLException(
398 aResources.getResourceString(STR_ERRORMSG_SEQUENCE),
399 _Context,
400 getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR ),
402 _Next
405 // -----------------------------------------------------------------------------
406 void throwInvalidIndexException(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
407 const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
409 ::connectivity::SharedResources aResources;
410 throw SQLException(
411 aResources.getResourceString(STR_INVALID_INDEX),
412 _Context,
413 getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX ),
415 _Next
418 // -----------------------------------------------------------------------------
419 void throwFunctionNotSupportedException(const ::rtl::OUString& _rMsg,
420 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
421 const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
423 throw SQLException(
424 _rMsg,
425 _Context,
426 getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
428 _Next
431 // -----------------------------------------------------------------------------
432 void throwFunctionNotSupportedException( const sal_Char* _pAsciiFunctionName, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
433 const ::com::sun::star::uno::Any* _pNextException ) throw ( ::com::sun::star::sdbc::SQLException )
435 ::connectivity::SharedResources aResources;
436 const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
437 STR_UNSUPPORTED_FUNCTION,
438 "$functionname$", ::rtl::OUString::createFromAscii( _pAsciiFunctionName )
439 ) );
440 throwFunctionNotSupportedException(
441 sError,
442 _rxContext,
443 _pNextException ? *_pNextException : Any()
446 // -----------------------------------------------------------------------------
447 void throwGenericSQLException(const ::rtl::OUString& _rMsg, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource)
448 throw (::com::sun::star::sdbc::SQLException)
450 throwGenericSQLException(_rMsg, _rxSource, Any());
453 // -----------------------------------------------------------------------------
454 void throwGenericSQLException(const ::rtl::OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
455 throw (SQLException)
457 throw SQLException( _rMsg, _rxSource, getStandardSQLState( SQL_GENERAL_ERROR ), 0, _rNextException);
460 // -----------------------------------------------------------------------------
461 void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
462 throw (SQLException)
464 ::connectivity::SharedResources aResources;
465 const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
466 STR_UNSUPPORTED_FEATURE,
467 "$featurename$", ::rtl::OUString::createFromAscii( _pAsciiFeatureName )
468 ) );
470 throw SQLException(
471 sError,
472 _rxContext,
473 getStandardSQLState( SQL_FEATURE_NOT_IMPLEMENTED ),
475 _pNextException ? *_pNextException : Any()
479 // -----------------------------------------------------------------------------
480 void throwSQLException( const sal_Char* _pAsciiMessage, const sal_Char* _pAsciiState,
481 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode, const Any* _pNextException ) throw (SQLException)
483 throw SQLException(
484 ::rtl::OUString::createFromAscii( _pAsciiMessage ),
485 _rxContext,
486 ::rtl::OUString::createFromAscii( _pAsciiState ),
487 _nErrorCode,
488 _pNextException ? *_pNextException : Any()
492 // -----------------------------------------------------------------------------
493 void throwSQLException( const sal_Char* _pAsciiMessage, StandardSQLState _eSQLState,
494 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
495 const Any* _pNextException ) throw (SQLException)
497 throwSQLException( _pAsciiMessage, getStandardSQLStateAscii( _eSQLState ), _rxContext, _nErrorCode, _pNextException );
500 // -----------------------------------------------------------------------------
501 void throwSQLException( const ::rtl::OUString& _rMessage, StandardSQLState _eSQLState,
502 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
503 const Any* _pNextException ) throw (SQLException)
505 throw SQLException(
506 _rMessage,
507 _rxContext,
508 getStandardSQLState( _eSQLState ),
509 _nErrorCode,
510 _pNextException ? *_pNextException : Any()
514 // -----------------------------------------------------------------------------
515 const sal_Char* getStandardSQLStateAscii( StandardSQLState _eState )
517 const sal_Char* pAsciiState = NULL;
518 switch ( _eState )
520 case SQL_WRONG_PARAMETER_NUMBER: pAsciiState = "07001"; break;
521 case SQL_INVALID_DESCRIPTOR_INDEX: pAsciiState = "07009"; break;
522 case SQL_UNABLE_TO_CONNECT: pAsciiState = "08001"; break;
523 case SQL_NUMERIC_OUT_OF_RANGE: pAsciiState = "22003"; break;
524 case SQL_INVALID_DATE_TIME: pAsciiState = "22007"; break;
525 case SQL_INVALID_CURSOR_STATE: pAsciiState = "24000"; break;
526 case SQL_TABLE_OR_VIEW_EXISTS: pAsciiState = "42S01"; break;
527 case SQL_TABLE_OR_VIEW_NOT_FOUND: pAsciiState = "42S02"; break;
528 case SQL_INDEX_ESISTS: pAsciiState = "42S11"; break;
529 case SQL_INDEX_NOT_FOUND: pAsciiState = "42S12"; break;
530 case SQL_COLUMN_EXISTS: pAsciiState = "42S21"; break;
531 case SQL_COLUMN_NOT_FOUND: pAsciiState = "42S22"; break;
532 case SQL_GENERAL_ERROR: pAsciiState = "HY000"; break;
533 case SQL_INVALID_SQL_DATA_TYPE: pAsciiState = "HY004"; break;
534 case SQL_OPERATION_CANCELED: pAsciiState = "HY008"; break;
535 case SQL_FUNCTION_SEQUENCE_ERROR: pAsciiState = "HY010"; break;
536 case SQL_INVALID_CURSOR_POSITION: pAsciiState = "HY109"; break;
537 case SQL_INVALID_BOOKMARK_VALUE: pAsciiState = "HY111"; break;
538 case SQL_FEATURE_NOT_IMPLEMENTED: pAsciiState = "HYC00"; break;
539 case SQL_FUNCTION_NOT_SUPPORTED: pAsciiState = "IM001"; break;
540 case SQL_CONNECTION_DOES_NOT_EXIST: pAsciiState = "08003"; break;
542 default:
543 break;
545 if ( !pAsciiState )
546 throw RuntimeException();
547 return pAsciiState;
550 // -----------------------------------------------------------------------------
551 ::rtl::OUString getStandardSQLState( StandardSQLState _eState )
553 return ::rtl::OUString::createFromAscii( getStandardSQLStateAscii( _eState ) );
556 // -----------------------------------------------------------------------------
557 //.........................................................................
558 } // namespace dbtools
559 //.........................................................................
562 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */