nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / commontools / dbexception.cxx
blobf15237d82c887b8e4c8ad72dbb431e140a7ffbc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <connectivity/dbexception.hxx>
21 #include <comphelper/types.hxx>
22 #include <cppuhelper/exc_hlp.hxx>
23 #include <o3tl/any.hxx>
24 #include <osl/diagnose.h>
25 #include <com/sun/star/sdb/SQLContext.hpp>
26 #include <com/sun/star/sdbc/SQLWarning.hpp>
27 #include <com/sun/star/sdb/SQLErrorEvent.hpp>
28 #include <strings.hrc>
29 #include <resource/sharedresources.hxx>
31 namespace dbtools
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::sdb;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::comphelper;
37 using namespace ::connectivity;
39 SQLExceptionInfo::SQLExceptionInfo()
40 :m_eType(TYPE::Undefined)
45 SQLExceptionInfo::SQLExceptionInfo(const css::sdbc::SQLException& _rError)
47 m_aContent <<= _rError;
48 implDetermineType();
52 SQLExceptionInfo::SQLExceptionInfo(const css::sdbc::SQLWarning& _rError)
54 m_aContent <<= _rError;
55 implDetermineType();
59 SQLExceptionInfo::SQLExceptionInfo(const css::sdb::SQLContext& _rError)
61 m_aContent <<= _rError;
62 implDetermineType();
66 SQLExceptionInfo::SQLExceptionInfo( const OUString& _rSimpleErrorMessage )
68 SQLException aError;
69 aError.Message = _rSimpleErrorMessage;
70 m_aContent <<= aError;
71 implDetermineType();
74 SQLExceptionInfo& SQLExceptionInfo::operator=(const css::sdbc::SQLException& _rError)
76 m_aContent <<= _rError;
77 implDetermineType();
78 return *this;
82 SQLExceptionInfo& SQLExceptionInfo::operator=(const css::sdbc::SQLWarning& _rError)
84 m_aContent <<= _rError;
85 implDetermineType();
86 return *this;
90 SQLExceptionInfo& SQLExceptionInfo::operator=(const css::sdb::SQLContext& _rError)
92 m_aContent <<= _rError;
93 implDetermineType();
94 return *this;
98 SQLExceptionInfo& SQLExceptionInfo::operator=(const css::sdb::SQLErrorEvent& _rErrorEvent)
100 m_aContent = _rErrorEvent.Reason;
101 implDetermineType();
102 return *this;
106 SQLExceptionInfo& SQLExceptionInfo::operator=(const css::uno::Any& _rCaughtSQLException)
108 m_aContent = _rCaughtSQLException;
109 implDetermineType();
110 return *this;
114 SQLExceptionInfo::SQLExceptionInfo(const css::uno::Any& _rError)
116 const css::uno::Type& aSQLExceptionType = cppu::UnoType<css::sdbc::SQLException>::get();
117 bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
118 if (bValid)
119 m_aContent = _rError;
120 // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
122 implDetermineType();
126 void SQLExceptionInfo::implDetermineType()
128 const Type& aSQLExceptionType = ::cppu::UnoType<SQLException>::get();
129 const Type& aSQLWarningType = ::cppu::UnoType<SQLWarning>::get();
130 const Type& aSQLContextType = ::cppu::UnoType<SQLContext>::get();
132 if ( isAssignableFrom( aSQLContextType, m_aContent.getValueType() ) )
133 m_eType = TYPE::SQLContext;
134 else if ( isAssignableFrom( aSQLWarningType, m_aContent.getValueType() ) )
135 m_eType = TYPE::SQLWarning;
136 else if ( isAssignableFrom( aSQLExceptionType, m_aContent.getValueType() ) )
137 m_eType = TYPE::SQLException;
138 else
140 m_eType = TYPE::Undefined;
141 m_aContent.clear();
146 bool SQLExceptionInfo::isKindOf(TYPE _eType) const
148 switch (_eType)
150 case TYPE::SQLContext:
151 return (m_eType == TYPE::SQLContext);
152 case TYPE::SQLWarning:
153 return (m_eType == TYPE::SQLContext) || (m_eType == TYPE::SQLWarning);
154 case TYPE::SQLException:
155 return (m_eType == TYPE::SQLContext) || (m_eType == TYPE::SQLWarning) || (m_eType == TYPE::SQLException);
156 case TYPE::Undefined:
157 return (m_eType == TYPE::Undefined);
159 return false;
163 SQLExceptionInfo::operator const css::sdbc::SQLException*() const
165 OSL_ENSURE(isKindOf(TYPE::SQLException), "SQLExceptionInfo::operator SQLException* : invalid call !");
166 return o3tl::doAccess<css::sdbc::SQLException>(m_aContent);
170 SQLExceptionInfo::operator const css::sdb::SQLContext*() const
172 OSL_ENSURE(isKindOf(TYPE::SQLContext), "SQLExceptionInfo::operator SQLException* : invalid call !");
173 return o3tl::doAccess<css::sdb::SQLContext>(m_aContent);
177 void SQLExceptionInfo::prepend( const OUString& _rErrorMessage )
179 SQLException aException;
180 aException.Message = _rErrorMessage;
181 aException.ErrorCode = 0;
182 aException.SQLState = "S1000";
183 aException.NextException = m_aContent;
184 m_aContent <<= aException;
186 m_eType = TYPE::SQLException;
190 void SQLExceptionInfo::append( TYPE _eType, const OUString& _rErrorMessage, const OUString& _rSQLState, const sal_Int32 _nErrorCode )
192 // create the to-be-appended exception
193 Any aAppend;
194 switch ( _eType )
196 case TYPE::SQLException: aAppend <<= SQLException(); break;
197 case TYPE::SQLWarning: aAppend <<= SQLWarning(); break;
198 case TYPE::SQLContext: aAppend <<= SQLContext(); break;
199 default:
200 OSL_FAIL( "SQLExceptionInfo::append: invalid exception type: this will crash!" );
201 break;
204 SQLException& pAppendException = const_cast<SQLException &>(*o3tl::forceAccess<SQLException>(aAppend));
205 pAppendException.Message = _rErrorMessage;
206 pAppendException.SQLState = _rSQLState;
207 pAppendException.ErrorCode = _nErrorCode;
209 // find the end of the current chain
210 Any* pChainIterator = &m_aContent;
211 SQLException* pLastException = nullptr;
212 const Type& aSQLExceptionType( cppu::UnoType<SQLException>::get() );
213 while ( pChainIterator )
215 if ( !pChainIterator->hasValue() )
216 break;
218 if ( !isAssignableFrom( aSQLExceptionType, pChainIterator->getValueType() ) )
219 break;
221 pLastException = const_cast< SQLException* >( o3tl::doAccess<SQLException>( *pChainIterator ) );
222 pChainIterator = &pLastException->NextException;
225 // append
226 if ( pLastException )
227 pLastException->NextException = aAppend;
228 else
230 m_aContent = aAppend;
231 m_eType = _eType;
236 void SQLExceptionInfo::doThrow()
238 if ( m_aContent.getValueTypeClass() == TypeClass_EXCEPTION )
239 ::cppu::throwException( m_aContent );
240 throw RuntimeException();
243 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const SQLExceptionInfo& _rChainStart )
244 :m_pCurrent( nullptr )
245 ,m_eCurrentType( SQLExceptionInfo::TYPE::Undefined )
247 if ( _rChainStart.isValid() )
249 m_pCurrent = _rChainStart;
250 m_eCurrentType = _rChainStart.getType();
255 SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const css::sdbc::SQLException& _rChainStart )
256 :m_pCurrent( &_rChainStart )
257 ,m_eCurrentType( SQLExceptionInfo::TYPE::SQLException )
262 void SQLExceptionIteratorHelper::current( SQLExceptionInfo& _out_rInfo ) const
264 switch ( m_eCurrentType )
266 case SQLExceptionInfo::TYPE::SQLException:
267 _out_rInfo = *m_pCurrent;
268 break;
270 case SQLExceptionInfo::TYPE::SQLWarning:
271 _out_rInfo = *static_cast< const SQLWarning* >( m_pCurrent );
272 break;
274 case SQLExceptionInfo::TYPE::SQLContext:
275 _out_rInfo = *static_cast< const SQLContext* >( m_pCurrent );
276 break;
278 default:
279 _out_rInfo = Any();
280 break;
285 const css::sdbc::SQLException* SQLExceptionIteratorHelper::next()
287 OSL_ENSURE( hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements)!" );
289 const css::sdbc::SQLException* pReturn = m_pCurrent;
290 if ( !m_pCurrent )
291 return pReturn;
293 // check for the next element within the chain
294 const Type aTypeException( ::cppu::UnoType< SQLException >::get() );
296 Type aNextElementType = m_pCurrent->NextException.getValueType();
297 if ( !isAssignableFrom( aTypeException, aNextElementType ) )
299 // no SQLException at all in the next chain element
300 m_pCurrent = nullptr;
301 m_eCurrentType = SQLExceptionInfo::TYPE::Undefined;
302 return pReturn;
305 m_pCurrent = o3tl::doAccess< SQLException >( m_pCurrent->NextException );
307 // no finally determine the proper type of the exception
308 const Type aTypeContext( ::cppu::UnoType< SQLContext >::get() );
309 if ( isAssignableFrom( aTypeContext, aNextElementType ) )
311 m_eCurrentType = SQLExceptionInfo::TYPE::SQLContext;
312 return pReturn;
315 const Type aTypeWarning( ::cppu::UnoType< SQLWarning >::get() );
316 if ( isAssignableFrom( aTypeWarning, aNextElementType ) )
318 m_eCurrentType = SQLExceptionInfo::TYPE::SQLWarning;
319 return pReturn;
322 // a simple SQLException
323 m_eCurrentType = SQLExceptionInfo::TYPE::SQLException;
324 return pReturn;
328 void SQLExceptionIteratorHelper::next( SQLExceptionInfo& _out_rInfo )
330 current( _out_rInfo );
331 next();
335 void throwFunctionSequenceException(const Reference< XInterface >& Context, const Any& Next)
337 ::connectivity::SharedResources aResources;
338 throw SQLException(
339 aResources.getResourceString(STR_ERRORMSG_SEQUENCE),
340 Context,
341 getStandardSQLState( StandardSQLState::FUNCTION_SEQUENCE_ERROR ),
343 Next
347 void throwInvalidIndexException(const css::uno::Reference< css::uno::XInterface >& Context,
348 const css::uno::Any& Next)
350 ::connectivity::SharedResources aResources;
351 throw SQLException(
352 aResources.getResourceString(STR_INVALID_INDEX),
353 Context,
354 getStandardSQLState( StandardSQLState::INVALID_DESCRIPTOR_INDEX ),
356 Next
360 void throwFunctionNotSupportedSQLException(const OUString& _rFunctionName,
361 const css::uno::Reference<css::uno::XInterface>& _rxContext)
363 ::connectivity::SharedResources aResources;
364 const OUString sError( aResources.getResourceStringWithSubstitution(
365 STR_UNSUPPORTED_FUNCTION,
366 "$functionname$", _rFunctionName
367 ) );
368 throw SQLException(
369 sError,
370 _rxContext,
371 getStandardSQLState( StandardSQLState::FUNCTION_NOT_SUPPORTED ),
373 css::uno::Any()
377 void throwFunctionNotSupportedRuntimeException(const OUString& _rFunctionName,
378 const css::uno::Reference<css::uno::XInterface>& _rxContext)
380 ::connectivity::SharedResources aResources;
381 const OUString sError( aResources.getResourceStringWithSubstitution(
382 STR_UNSUPPORTED_FUNCTION,
383 "$functionname$", _rFunctionName
384 ) );
385 throw RuntimeException(
386 sError,
387 _rxContext
391 void throwGenericSQLException(const OUString& _rMsg, const css::uno::Reference< css::uno::XInterface >& _rxSource)
393 throwGenericSQLException(_rMsg, _rxSource, Any());
397 void throwGenericSQLException(const OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
399 throw SQLException( _rMsg, _rxSource, getStandardSQLState( StandardSQLState::GENERAL_ERROR ), 0, _rNextException);
402 void throwFeatureNotImplementedSQLException( const OUString& _rFeatureName, const Reference< XInterface >& _rxContext, const Any& _rNextException )
404 ::connectivity::SharedResources aResources;
405 const OUString sError( aResources.getResourceStringWithSubstitution(
406 STR_UNSUPPORTED_FEATURE,
407 "$featurename$", _rFeatureName
408 ) );
410 throw SQLException(
411 sError,
412 _rxContext,
413 getStandardSQLState( StandardSQLState::FEATURE_NOT_IMPLEMENTED ),
415 _rNextException
419 void throwFeatureNotImplementedRuntimeException(const OUString& _rFeatureName, const Reference< XInterface >& _rxContext)
421 ::connectivity::SharedResources aResources;
422 const OUString sError( aResources.getResourceStringWithSubstitution(
423 STR_UNSUPPORTED_FEATURE,
424 "$featurename$", _rFeatureName
425 ) );
427 throw RuntimeException(sError, _rxContext);
430 void throwInvalidColumnException( const OUString& _rColumnName, const Reference< XInterface >& _rxContext)
432 ::connectivity::SharedResources aResources;
433 OUString sErrorMessage( aResources.getResourceStringWithSubstitution(
434 STR_INVALID_COLUMNNAME,
435 "$columnname$",_rColumnName) );
436 throwSQLException( sErrorMessage, StandardSQLState::COLUMN_NOT_FOUND, _rxContext );
439 void throwSQLException( const OUString& _rMessage, const OUString& _rSQLState,
440 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode )
442 throw SQLException(
443 _rMessage,
444 _rxContext,
445 _rSQLState,
446 _nErrorCode,
447 Any()
452 void throwSQLException( const OUString& _rMessage, StandardSQLState _eSQLState,
453 const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode )
455 throwSQLException( _rMessage, getStandardSQLState( _eSQLState ), _rxContext, _nErrorCode );
459 OUString getStandardSQLState( StandardSQLState _eState )
461 switch ( _eState )
463 case StandardSQLState::INVALID_DESCRIPTOR_INDEX: return "07009";
464 case StandardSQLState::INVALID_CURSOR_STATE: return "24000";
465 case StandardSQLState::COLUMN_NOT_FOUND: return "42S22";
466 case StandardSQLState::GENERAL_ERROR: return "HY000";
467 case StandardSQLState::INVALID_SQL_DATA_TYPE: return "HY004";
468 case StandardSQLState::FUNCTION_SEQUENCE_ERROR: return "HY010";
469 case StandardSQLState::INVALID_CURSOR_POSITION: return "HY109";
470 case StandardSQLState::FEATURE_NOT_IMPLEMENTED: return "HYC00";
471 case StandardSQLState::FUNCTION_NOT_SUPPORTED: return "IM001";
472 case StandardSQLState::CONNECTION_DOES_NOT_EXIST: return "08003";
473 default: return "HY001"; // General Error
478 } // namespace dbtools
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */