Files
libreoffice/connectivity/source/commontools/dbexception.cxx

430 lines
18 KiB
C++
Raw Normal View History

2000-10-05 07:50:51 +00:00
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
2000-10-05 07:50:51 +00:00
*
* $RCSfile: dbexception.cxx,v $
2000-10-05 07:50:51 +00:00
*
* $Revision: 1.14 $
2000-10-05 07:50:51 +00:00
*
* last change: $Author: obo $ $Date: 2005-12-21 13:14:32 $
2000-10-05 07:50:51 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
2000-10-05 07:50:51 +00:00
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
2000-10-05 07:50:51 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
2000-10-05 07:50:51 +00:00
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2000-10-05 07:50:51 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
2000-10-05 07:50:51 +00:00
*
************************************************************************/
#ifndef _DBHELPER_DBEXCEPTION_HXX_
#include <connectivity/dbexception.hxx>
#endif
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif
2000-10-24 14:00:32 +00:00
#ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
#include <com/sun/star/sdb/SQLContext.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_SQLWARNING_HPP_
#include <com/sun/star/sdbc/SQLWarning.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_SQLERROREVENT_HPP_
#include <com/sun/star/sdb/SQLErrorEvent.hpp>
#endif
2001-05-14 10:42:44 +00:00
#ifndef CONNECTIVITY_CONNECTION_HXX
#include "TConnection.hxx"
2000-10-24 14:00:32 +00:00
#endif
2000-10-05 07:50:51 +00:00
using namespace comphelper;
//.........................................................................
namespace dbtools
{
//.........................................................................
using namespace ::connectivity;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
2000-10-05 07:50:51 +00:00
//==============================================================================
//= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
//==============================================================================
//------------------------------------------------------------------------------
SQLExceptionInfo::SQLExceptionInfo()
:m_eType(UNDEFINED)
{
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError)
2000-10-05 07:50:51 +00:00
{
m_aContent <<= _rError;
implDetermineType();
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError)
2000-10-05 07:50:51 +00:00
{
m_aContent <<= _rError;
implDetermineType();
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError)
2000-10-05 07:50:51 +00:00
{
m_aContent <<= _rError;
implDetermineType();
}
//------------------------------------------------------------------------------
SQLExceptionInfo::SQLExceptionInfo( const ::rtl::OUString& _rSimpleErrorMessage )
{
SQLException aError;
aError.Message = _rSimpleErrorMessage;
m_aContent <<= aError;
implDetermineType();
}
2000-10-05 07:50:51 +00:00
//------------------------------------------------------------------------------
SQLExceptionInfo::SQLExceptionInfo(const SQLExceptionInfo& _rCopySource)
:m_aContent(_rCopySource.m_aContent)
,m_eType(_rCopySource.m_eType)
{
}
//------------------------------------------------------------------------------
const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLException& _rError)
{
m_aContent <<= _rError;
implDetermineType();
return *this;
}
//------------------------------------------------------------------------------
const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning& _rError)
{
m_aContent <<= _rError;
implDetermineType();
return *this;
}
//------------------------------------------------------------------------------
const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext& _rError)
{
m_aContent <<= _rError;
implDetermineType();
return *this;
}
2000-10-05 07:50:51 +00:00
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent& _rError)
2000-10-05 07:50:51 +00:00
{
2000-10-24 14:00:32 +00:00
const staruno::Type& aSQLExceptionType = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
2000-10-05 07:50:51 +00:00
staruno::Type aReasonType = _rError.Reason.getValueType();
sal_Bool bValid = isAssignableFrom(aSQLExceptionType, aReasonType);
2001-03-21 12:47:12 +00:00
OSL_ENSURE(bValid, "SQLExceptionInfo::SQLExceptionInfo : invalid argument (does not contain an SQLException) !");
2000-10-05 07:50:51 +00:00
if (bValid)
m_aContent = _rError.Reason;
implDetermineType();
}
//------------------------------------------------------------------------------
SQLExceptionInfo::SQLExceptionInfo(const staruno::Any& _rError)
{
2000-10-24 14:00:32 +00:00
const staruno::Type& aSQLExceptionType = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
2000-10-05 07:50:51 +00:00
sal_Bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
if (bValid)
m_aContent = _rError;
// no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
implDetermineType();
}
//------------------------------------------------------------------------------
void SQLExceptionInfo::implDetermineType()
{
staruno::Type aContentType = m_aContent.getValueType();
2000-10-24 14:00:32 +00:00
if (isA(aContentType, static_cast< ::com::sun::star::sdb::SQLContext*>(NULL)))
2000-10-05 07:50:51 +00:00
m_eType = SQL_CONTEXT;
2000-10-24 14:00:32 +00:00
else if (isA(aContentType, static_cast< ::com::sun::star::sdbc::SQLWarning*>(NULL)))
2000-10-05 07:50:51 +00:00
m_eType = SQL_WARNING;
2000-10-24 14:00:32 +00:00
else if (isA(aContentType, static_cast< ::com::sun::star::sdbc::SQLException*>(NULL)))
2000-10-05 07:50:51 +00:00
m_eType = SQL_EXCEPTION;
else
m_eType = UNDEFINED;
}
//------------------------------------------------------------------------------
sal_Bool SQLExceptionInfo::isKindOf(TYPE _eType) const
{
switch (_eType)
{
case SQL_CONTEXT:
return (m_eType == SQL_CONTEXT);
case SQL_WARNING:
return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING);
case SQL_EXCEPTION:
return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING) || (m_eType == SQL_EXCEPTION);
case UNDEFINED:
return (m_eType == UNDEFINED);
}
return sal_False;
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLException*() const
2000-10-05 07:50:51 +00:00
{
2001-03-21 12:47:12 +00:00
OSL_ENSURE(isKindOf(SQL_EXCEPTION), "SQLExceptionInfo::operator SQLException* : invalid call !");
2000-10-24 14:00:32 +00:00
return reinterpret_cast<const ::com::sun::star::sdbc::SQLException*>(m_aContent.getValue());
2000-10-05 07:50:51 +00:00
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLWarning*() const
2000-10-05 07:50:51 +00:00
{
2001-03-21 12:47:12 +00:00
OSL_ENSURE(isKindOf(SQL_WARNING), "SQLExceptionInfo::operator SQLException* : invalid call !");
2000-10-24 14:00:32 +00:00
return reinterpret_cast<const ::com::sun::star::sdbc::SQLWarning*>(m_aContent.getValue());
2000-10-05 07:50:51 +00:00
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionInfo::operator const ::com::sun::star::sdb::SQLContext*() const
2000-10-05 07:50:51 +00:00
{
2001-03-21 12:47:12 +00:00
OSL_ENSURE(isKindOf(SQL_CONTEXT), "SQLExceptionInfo::operator SQLException* : invalid call !");
2000-10-24 14:00:32 +00:00
return reinterpret_cast<const ::com::sun::star::sdb::SQLContext*>(m_aContent.getValue());
2000-10-05 07:50:51 +00:00
}
//==============================================================================
//= SQLExceptionIteratorHelper - iterating through an SQLException chain
//==============================================================================
//------------------------------------------------------------------------------
SQLExceptionIteratorHelper::SQLExceptionIteratorHelper(const SQLExceptionInfo& _rStart, NODES_INCLUDED _eMask)
:m_pCurrent(NULL)
,m_eCurrentType(SQLExceptionInfo::UNDEFINED)
// no other chance without RTTI
,m_eMask(_eMask)
{
if (_rStart.isValid())
{
m_pCurrent = (const SQLException*)_rStart;
m_eCurrentType = _rStart.getType();
}
}
2000-10-05 07:50:51 +00:00
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionIteratorHelper::SQLExceptionIteratorHelper(const ::com::sun::star::sdbc::SQLException* _pStart, NODES_INCLUDED _eMask)
2000-10-05 07:50:51 +00:00
:m_pCurrent(_pStart)
,m_eCurrentType(SQLExceptionInfo::SQL_EXCEPTION)
// no other chance without RTTI
,m_eMask(_eMask)
{
// initially check the start of the chain against the include mask
if (m_pCurrent && (m_eMask > NI_EXCEPTIONS))
2000-10-05 07:50:51 +00:00
next();
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionIteratorHelper::SQLExceptionIteratorHelper(const ::com::sun::star::sdbc::SQLWarning* _pStart, NODES_INCLUDED _eMask)
2000-10-05 07:50:51 +00:00
:m_pCurrent(_pStart)
,m_eCurrentType(SQLExceptionInfo::SQL_WARNING)
// no other chance without RTTI
,m_eMask(_eMask)
{
// initially check the start of the chain against the include mask
if (m_pCurrent && (m_eMask > NI_WARNINGS))
2000-10-05 07:50:51 +00:00
next();
}
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
SQLExceptionIteratorHelper::SQLExceptionIteratorHelper(const ::com::sun::star::sdb::SQLContext* _pStart, NODES_INCLUDED _eMask)
2000-10-05 07:50:51 +00:00
:m_pCurrent(_pStart)
,m_eCurrentType(SQLExceptionInfo::SQL_CONTEXT)
// no other chance without RTTI
,m_eMask(_eMask)
{
// initially check the start of the chain against the include mask
if (m_pCurrent && (m_eMask > NI_CONTEXTINFOS))
2000-10-05 07:50:51 +00:00
next();
}
//------------------------------------------------------------------------------
void SQLExceptionIteratorHelper::next(SQLExceptionInfo& _rOutInfo)
{
SQLExceptionInfo::TYPE eType = m_eCurrentType;
const SQLException* pNext = next();
switch (eType)
{
case SQLExceptionInfo::SQL_EXCEPTION:
_rOutInfo = *pNext;
break;
case SQLExceptionInfo::SQL_WARNING:
_rOutInfo = *static_cast<const SQLWarning*>(pNext);
break;
case SQLExceptionInfo::SQL_CONTEXT:
_rOutInfo = *static_cast<const SQLContext*>(pNext);
break;
default:
2001-03-21 12:47:12 +00:00
OSL_ENSURE(sal_False, "SQLExceptionIteratorHelper::next: invalid type!");
}
}
2000-10-05 07:50:51 +00:00
//------------------------------------------------------------------------------
2000-10-24 14:00:32 +00:00
const ::com::sun::star::sdbc::SQLException* SQLExceptionIteratorHelper::next()
2000-10-05 07:50:51 +00:00
{
2001-03-21 12:47:12 +00:00
OSL_ENSURE(hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements) !");
2000-10-05 07:50:51 +00:00
2000-10-24 14:00:32 +00:00
const ::com::sun::star::sdbc::SQLException* pReturn = m_pCurrent;
2000-10-05 07:50:51 +00:00
if (m_pCurrent)
{ // check for the next element within the chain
2000-10-24 14:00:32 +00:00
const staruno::Type& aSqlExceptionCompare = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLException*>(NULL));
const staruno::Type& aSqlWarningCompare = ::getCppuType(reinterpret_cast< ::com::sun::star::sdbc::SQLWarning*>(NULL));
const staruno::Type& aSqlContextCompare = ::getCppuType(reinterpret_cast< ::com::sun::star::sdb::SQLContext*>(NULL));
2000-10-05 07:50:51 +00:00
2000-10-24 14:00:32 +00:00
const ::com::sun::star::sdbc::SQLException* pSearch = m_pCurrent;
2000-10-05 07:50:51 +00:00
SQLExceptionInfo::TYPE eSearchType = m_eCurrentType;
sal_Bool bIncludeThis = sal_False;
while (pSearch && !bIncludeThis)
{
if (!pSearch->NextException.hasValue())
{ // last chain element
pSearch = NULL;
break;
}
staruno::Type aNextElementType = pSearch->NextException.getValueType();
if (!isAssignableFrom(aSqlExceptionCompare, aNextElementType))
{
// the next chain element isn't an SQLException
2001-03-21 12:47:12 +00:00
OSL_ENSURE(sal_False, "SQLExceptionIteratorHelper::next : the exception chain is invalid !");
2000-10-05 07:50:51 +00:00
pSearch = NULL;
break;
}
// the next element
SQLExceptionInfo aInfo(pSearch->NextException);
eSearchType = aInfo.getType();
switch (eSearchType)
{
case SQLExceptionInfo::SQL_CONTEXT:
2000-10-24 14:00:32 +00:00
pSearch = reinterpret_cast<const ::com::sun::star::sdb::SQLContext*>(pSearch->NextException.getValue());
2000-10-05 07:50:51 +00:00
bIncludeThis = eSearchType >= NI_CONTEXTINFOS;
break;
case SQLExceptionInfo::SQL_WARNING:
2000-10-24 14:00:32 +00:00
pSearch = reinterpret_cast<const ::com::sun::star::sdbc::SQLWarning*>(pSearch->NextException.getValue());
2000-10-05 07:50:51 +00:00
bIncludeThis = eSearchType >= NI_WARNINGS;
break;
case SQLExceptionInfo::SQL_EXCEPTION:
2000-10-24 14:00:32 +00:00
pSearch = reinterpret_cast<const ::com::sun::star::sdbc::SQLException*>(pSearch->NextException.getValue());
2000-10-05 07:50:51 +00:00
bIncludeThis = eSearchType >= NI_EXCEPTIONS;
break;
default:
pSearch = NULL;
bIncludeThis = sal_False;
break;
}
}
m_pCurrent = pSearch;
m_eCurrentType = eSearchType;
}
return pReturn;
}
2000-10-24 14:00:32 +00:00
using namespace ::com::sun::star::uno;
2001-04-19 06:05:17 +00:00
//------------------------------------------------------------
2001-05-14 10:42:44 +00:00
void throwFunctionSequenceException(const Reference< XInterface >& _Context, const Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
2001-04-19 06:05:17 +00:00
{
2001-05-14 10:42:44 +00:00
throw SQLException(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ERRORMSG_SEQUENCE), _Context, OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HY0000), 0, _Next);
2001-04-19 06:05:17 +00:00
}
2001-05-14 10:42:44 +00:00
// -----------------------------------------------------------------------------
void throwInvalidIndexException(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
{
static ::rtl::OUString sStatus = ::rtl::OUString::createFromAscii("07009");
2002-09-27 12:55:58 +00:00
throw SQLException(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_INVALID_INDEX),_Context,sStatus,0,_Next);
2001-05-14 10:42:44 +00:00
}
// -----------------------------------------------------------------------------
2001-11-08 06:11:36 +00:00
void throwFunctionNotSupportedException(const ::rtl::OUString& _rMsg,
const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
{
static ::rtl::OUString sStatus = ::rtl::OUString::createFromAscii("IM001");
throw SQLException(_rMsg,_Context,sStatus,0,_Next);
}
// -----------------------------------------------------------------------------
void throwFunctionNotSupportedException( const sal_Char* _pAsciiFunctionName, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
const ::com::sun::star::uno::Any* _pNextException ) throw ( ::com::sun::star::sdbc::SQLException )
{
::rtl::OUString sMessage = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": Driver does not support this function: " ) );
sMessage += ::rtl::OUString::createFromAscii( _pAsciiFunctionName );
::rtl::OUString sState( RTL_CONSTASCII_USTRINGPARAM( "IM001" ) );
throw SQLException( sMessage, _rxContext, sState, 0, _pNextException ? *_pNextException : Any() );
}
// -----------------------------------------------------------------------------
2001-05-14 10:42:44 +00:00
void throwGenericSQLException(const ::rtl::OUString& _rMsg, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource)
throw (::com::sun::star::sdbc::SQLException)
{
throwGenericSQLException(_rMsg, _rxSource, Any());
}
// -----------------------------------------------------------------------------
void throwGenericSQLException(const ::rtl::OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
throw (SQLException)
2001-05-14 10:42:44 +00:00
{
static ::rtl::OUString sStatus = ::rtl::OUString::createFromAscii("S1000");
throw SQLException(_rMsg, _rxSource, sStatus, 0, _rNextException);
2001-05-14 10:42:44 +00:00
}
// -----------------------------------------------------------------------------
void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
throw (SQLException)
{
::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": feature not implemented." ) );
::rtl::OUString sState( RTL_CONSTASCII_USTRINGPARAM( "HYC00" ) );
throw SQLException( sMessage, _rxContext, sState, 0, _pNextException ? *_pNextException : Any() );
}
// -----------------------------------------------------------------------------
void throwSQLException( const sal_Char* _pAsciiMessage, const sal_Char* _pAsciiState,
const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode, const Any* _pNextException ) throw (SQLException)
{
throw SQLException(
::rtl::OUString::createFromAscii( _pAsciiMessage ),
_rxContext,
::rtl::OUString::createFromAscii( _pAsciiState ),
_nErrorCode,
_pNextException ? *_pNextException : Any()
);
}
2001-05-14 10:42:44 +00:00
// -----------------------------------------------------------------------------
2000-10-05 07:50:51 +00:00
//.........................................................................
} // namespace dbtools
//.........................................................................