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 //.........................................................................
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()
60 //------------------------------------------------------------------------------
61 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException
& _rError
)
63 m_aContent
<<= _rError
;
67 //------------------------------------------------------------------------------
68 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning
& _rError
)
70 m_aContent
<<= _rError
;
74 //------------------------------------------------------------------------------
75 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext
& _rError
)
77 m_aContent
<<= _rError
;
81 //------------------------------------------------------------------------------
82 SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString
& _rSimpleErrorMessage
)
85 aError
.Message
= _rSimpleErrorMessage
;
86 m_aContent
<<= aError
;
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
;
105 //------------------------------------------------------------------------------
106 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning
& _rError
)
108 m_aContent
<<= _rError
;
113 //------------------------------------------------------------------------------
114 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext
& _rError
)
116 m_aContent
<<= _rError
;
121 //------------------------------------------------------------------------------
122 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent
& _rErrorEvent
)
124 m_aContent
= _rErrorEvent
.Reason
;
129 //------------------------------------------------------------------------------
130 const SQLExceptionInfo
& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any
& _rCaughtSQLException
)
132 m_aContent
= _rCaughtSQLException
;
137 //------------------------------------------------------------------------------
138 SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent
& _rError
)
140 m_aContent
= _rError
.Reason
;
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());
150 m_aContent
= _rError
;
151 // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
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
;
178 //------------------------------------------------------------------------------
179 sal_Bool
SQLExceptionInfo::isKindOf(TYPE _eType
) const
184 return (m_eType
== SQL_CONTEXT
);
186 return (m_eType
== SQL_CONTEXT
) || (m_eType
== SQL_WARNING
);
188 return (m_eType
== SQL_CONTEXT
) || (m_eType
== SQL_WARNING
) || (m_eType
== SQL_EXCEPTION
);
190 return (m_eType
== UNDEFINED
);
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
236 case SQL_EXCEPTION
: aAppend
<<= SQLException(); break;
237 case SQL_WARNING
: aAppend
<<= SQLWarning(); break;
238 case SQL_CONTEXT
: aAppend
<<= SQLContext(); break;
240 OSL_FAIL( "SQLExceptionInfo::append: invalid exception type: this will crash!" );
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() )
258 if ( !isAssignableFrom( aSQLExceptionType
, pChainIterator
->getValueType() ) )
261 pLastException
= static_cast< SQLException
* >( const_cast< void* >( pChainIterator
->getValue() ) );
262 pChainIterator
= &pLastException
->NextException
;
266 if ( pLastException
)
267 pLastException
->NextException
= aAppend
;
270 m_aContent
= aAppend
;
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
)
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
;
329 case SQLExceptionInfo::SQL_WARNING
:
330 _out_rInfo
= *static_cast< const SQLWarning
* >( m_pCurrent
);
333 case SQLExceptionInfo::SQL_CONTEXT
:
334 _out_rInfo
= *static_cast< const SQLContext
* >( m_pCurrent
);
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();
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
360 m_eCurrentType
= SQLExceptionInfo::UNDEFINED
;
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
;
374 const Type
aTypeWarning( ::cppu::UnoType
< SQLWarning
>::get() );
375 if ( isAssignableFrom( aTypeWarning
, aNextElementType
) )
377 m_eCurrentType
= SQLExceptionInfo::SQL_WARNING
;
381 // a simple SQLException
382 m_eCurrentType
= SQLExceptionInfo::SQL_EXCEPTION
;
386 //------------------------------------------------------------------------------
387 void SQLExceptionIteratorHelper::next( SQLExceptionInfo
& _out_rInfo
)
389 current( _out_rInfo
);
393 //------------------------------------------------------------
394 void throwFunctionSequenceException(const Reference
< XInterface
>& _Context
, const Any
& _Next
) throw ( ::com::sun::star::sdbc::SQLException
)
396 ::connectivity::SharedResources aResources
;
398 aResources
.getResourceString(STR_ERRORMSG_SEQUENCE
),
400 getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR
),
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
;
411 aResources
.getResourceString(STR_INVALID_INDEX
),
413 getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX
),
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
)
426 getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED
),
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
)
440 throwFunctionNotSupportedException(
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
)
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
)
464 ::connectivity::SharedResources aResources
;
465 const ::rtl::OUString
sError( aResources
.getResourceStringWithSubstitution(
466 STR_UNSUPPORTED_FEATURE
,
467 "$featurename$", ::rtl::OUString::createFromAscii( _pAsciiFeatureName
)
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
)
484 ::rtl::OUString::createFromAscii( _pAsciiMessage
),
486 ::rtl::OUString::createFromAscii( _pAsciiState
),
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
)
508 getStandardSQLState( _eSQLState
),
510 _pNextException
? *_pNextException
: Any()
514 // -----------------------------------------------------------------------------
515 const sal_Char
* getStandardSQLStateAscii( StandardSQLState _eState
)
517 const sal_Char
* pAsciiState
= NULL
;
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;
546 throw RuntimeException();
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: */