1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 //.........................................................................
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()
64 //------------------------------------------------------------------------------
65 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException
& _rError
)
67 m_aContent
<<= _rError
;
71 //------------------------------------------------------------------------------
72 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning
& _rError
)
74 m_aContent
<<= _rError
;
78 //------------------------------------------------------------------------------
79 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext
& _rError
)
81 m_aContent
<<= _rError
;
85 //------------------------------------------------------------------------------
86 SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString
& _rSimpleErrorMessage
)
89 aError
.Message
= _rSimpleErrorMessage
;
90 m_aContent
<<= aError
;
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
;
109 //------------------------------------------------------------------------------
110 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning
& _rError
)
112 m_aContent
<<= _rError
;
117 //------------------------------------------------------------------------------
118 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext
& _rError
)
120 m_aContent
<<= _rError
;
125 //------------------------------------------------------------------------------
126 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent
& _rErrorEvent
)
128 m_aContent
= _rErrorEvent
.Reason
;
133 //------------------------------------------------------------------------------
134 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any
& _rCaughtSQLException
)
136 m_aContent
= _rCaughtSQLException
;
141 //------------------------------------------------------------------------------
142 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent
& _rError
)
144 m_aContent
= _rError
.Reason
;
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());
154 m_aContent
= _rError
;
155 // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
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
;
182 //------------------------------------------------------------------------------
183 sal_Bool
SQLExceptionInfo::isKindOf(TYPE _eType
) const
188 return (m_eType
== SQL_CONTEXT
);
190 return (m_eType
== SQL_CONTEXT
) || (m_eType
== SQL_WARNING
);
192 return (m_eType
== SQL_CONTEXT
) || (m_eType
== SQL_WARNING
) || (m_eType
== SQL_EXCEPTION
);
194 return (m_eType
== UNDEFINED
);
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
240 case SQL_EXCEPTION
: aAppend
<<= SQLException(); break;
241 case SQL_WARNING
: aAppend
<<= SQLWarning(); break;
242 case SQL_CONTEXT
: aAppend
<<= SQLContext(); break;
244 OSL_ENSURE( false, "SQLExceptionInfo::append: invalid exception type: this will crash!" );
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() )
262 if ( !isAssignableFrom( aSQLExceptionType
, pChainIterator
->getValueType() ) )
265 pLastException
= static_cast< SQLException
* >( const_cast< void* >( pChainIterator
->getValue() ) );
266 pChainIterator
= &pLastException
->NextException
;
270 if ( pLastException
)
271 pLastException
->NextException
= aAppend
;
274 m_aContent
= aAppend
;
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
)
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
;
333 case SQLExceptionInfo::SQL_WARNING
:
334 _out_rInfo
= *static_cast< const SQLWarning
* >( m_pCurrent
);
337 case SQLExceptionInfo::SQL_CONTEXT
:
338 _out_rInfo
= *static_cast< const SQLContext
* >( m_pCurrent
);
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();
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
364 m_eCurrentType
= SQLExceptionInfo::UNDEFINED
;
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
;
378 const Type
aTypeWarning( ::cppu::UnoType
< SQLWarning
>::get() );
379 if ( isAssignableFrom( aTypeWarning
, aNextElementType
) )
381 m_eCurrentType
= SQLExceptionInfo::SQL_WARNING
;
385 // a simple SQLException
386 m_eCurrentType
= SQLExceptionInfo::SQL_EXCEPTION
;
390 //------------------------------------------------------------------------------
391 void SQLExceptionIteratorHelper::next( SQLExceptionInfo
& _out_rInfo
)
393 current( _out_rInfo
);
397 //------------------------------------------------------------
398 void throwFunctionSequenceException(const Reference
< XInterface
>& _Context
, const Any
& _Next
) throw ( ::com::sun::star::sdbc::SQLException
)
400 ::connectivity::SharedResources aResources
;
402 aResources
.getResourceString(STR_ERRORMSG_SEQUENCE
),
404 getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR
),
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
;
415 aResources
.getResourceString(STR_INVALID_INDEX
),
417 getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX
),
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
)
430 getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED
),
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
)
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
)
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
)
470 ::connectivity::SharedResources aResources
;
471 const ::rtl::OUString
sError( aResources
.getResourceStringWithSubstitution(
472 STR_UNSUPPORTED_FEATURE
,
473 "$featurename$", ::rtl::OUString::createFromAscii( _pAsciiFeatureName
)
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
)
490 ::rtl::OUString::createFromAscii( _pAsciiMessage
),
492 ::rtl::OUString::createFromAscii( _pAsciiState
),
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
)
514 getStandardSQLState( _eSQLState
),
516 _pNextException
? *_pNextException
: Any()
520 // -----------------------------------------------------------------------------
521 const sal_Char
* getStandardSQLStateAscii( StandardSQLState _eState
)
523 const sal_Char
* pAsciiState
= NULL
;
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;
552 throw RuntimeException();
556 // -----------------------------------------------------------------------------
557 ::rtl::OUString
getStandardSQLState( StandardSQLState _eState
)
559 return ::rtl::OUString::createFromAscii( getStandardSQLStateAscii( _eState
) );
562 // -----------------------------------------------------------------------------
563 //.........................................................................
564 } // namespace dbtools
565 //.........................................................................