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 ************************************************************************/
30 #include "connectivity/sqlerror.hxx"
32 /** === begin UNO includes === **/
33 #include <com/sun/star/sdbc/SQLException.hpp>
34 /** === end UNO includes === **/
36 #include <comphelper/officeresourcebundle.hxx>
37 #include <cppuhelper/exc_hlp.hxx>
38 #include <rtl/ustrbuf.hxx>
42 //........................................................................
43 namespace connectivity
45 //........................................................................
47 /** === begin UNO using === **/
48 using ::com::sun::star::uno::Reference
;
49 using ::com::sun::star::uno::UNO_QUERY
;
50 using ::com::sun::star::uno::UNO_QUERY_THROW
;
51 using ::com::sun::star::uno::Exception
;
52 using ::com::sun::star::uno::RuntimeException
;
53 using ::com::sun::star::uno::Any
;
54 using ::com::sun::star::uno::makeAny
;
55 using ::com::sun::star::uno::XInterface
;
56 using ::com::sun::star::sdbc::SQLException
;
57 using ::com::sun::star::uno::Type
;
58 /** === end UNO using === **/
60 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
63 typedef SQLError::ParamValue ParamValue
;
66 //====================================================================
67 //= SQLError_Impl - declaration
68 //====================================================================
72 SQLError_Impl( const ::comphelper::ComponentContext
& _rContext
);
75 // versions of the public SQLError methods which are just delegated to this impl-class
76 static const ::rtl::OUString
& getMessagePrefix();
77 ::rtl::OUString
getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
78 ::rtl::OUString
getSQLState( const ErrorCondition _eCondition
);
79 static ErrorCode
getErrorCode( const ErrorCondition _eCondition
);
80 void raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
81 void raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
82 void raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
83 SQLException
getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
86 /// returns the basic error message associated with the given error condition, without any parameter replacements
88 impl_getErrorMessage( const ErrorCondition
& _eCondition
);
90 /// returns the SQLState associated with the given error condition
92 impl_getSQLState( const ErrorCondition
& _eCondition
);
94 /// returns an SQLException describing the given error condition
96 impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
97 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
99 /// initializes our resource bundle
100 bool impl_initResources();
103 ::osl::Mutex m_aMutex
;
104 ::comphelper::ComponentContext m_aContext
;
105 ::std::auto_ptr
< ::comphelper::OfficeResourceBundle
> m_pResources
;
106 bool m_bAttemptedInit
;
109 //====================================================================
110 //= SQLError_Impl - implementation
111 //====================================================================
112 //--------------------------------------------------------------------
113 SQLError_Impl::SQLError_Impl( const ::comphelper::ComponentContext
& _rContext
)
114 :m_aContext( _rContext
)
116 ,m_bAttemptedInit( false )
120 //--------------------------------------------------------------------
121 SQLError_Impl::~SQLError_Impl()
125 //--------------------------------------------------------------------
126 const ::rtl::OUString
& SQLError_Impl::getMessagePrefix()
128 static ::rtl::OUString
s_sMessagePrefix( RTL_CONSTASCII_USTRINGPARAM( "[OOoBase]" ) );
129 return s_sMessagePrefix
;
132 //--------------------------------------------------------------------
135 //................................................................
136 /** substitutes a given placeholder in the given message with the given value
138 void lcl_substitutePlaceholder( ::rtl::OUString
& _rMessage
, const sal_Char
* _pPlaceholder
, ParamValue _rParamValue
)
140 size_t nPlaceholderLen( strlen( _pPlaceholder
) );
141 sal_Int32 nIndex
= _rMessage
.indexOfAsciiL( _pPlaceholder
, nPlaceholderLen
);
143 bool bHasPlaceholder
= ( nIndex
!= -1 );
144 bool bWantsPlaceholder
= _rParamValue
.is();
145 OSL_ENSURE( bHasPlaceholder
== bWantsPlaceholder
, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
147 if ( bHasPlaceholder
&& bWantsPlaceholder
)
148 _rMessage
= _rMessage
.replaceAt( nIndex
, nPlaceholderLen
, *_rParamValue
);
151 //................................................................
152 sal_Int32
lcl_getResourceID( const ErrorCondition _eCondition
, bool _bSQLState
)
155 + 2 * ::sal::static_int_cast
< sal_Int32
, ErrorCondition
>( _eCondition
)
156 + ( _bSQLState
? 1 : 0 );
160 //--------------------------------------------------------------------
161 ::rtl::OUString
SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
163 ::rtl::OUString
sErrorMessage( impl_getErrorMessage( _eCondition
) );
165 lcl_substitutePlaceholder( sErrorMessage
, "$1$", _rParamValue1
);
166 lcl_substitutePlaceholder( sErrorMessage
, "$2$", _rParamValue2
);
167 lcl_substitutePlaceholder( sErrorMessage
, "$3$", _rParamValue3
);
169 return sErrorMessage
;
172 //--------------------------------------------------------------------
173 ::rtl::OUString
SQLError_Impl::getSQLState( const ErrorCondition _eCondition
)
175 return impl_getSQLState( _eCondition
);
178 //--------------------------------------------------------------------
179 ErrorCode
SQLError_Impl::getErrorCode( const ErrorCondition _eCondition
)
181 return 0 - ::sal::static_int_cast
< ErrorCode
, ErrorCondition
>( _eCondition
);
184 //--------------------------------------------------------------------
185 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
190 ::cppu::UnoType
< SQLException
>::get(),
197 //--------------------------------------------------------------------
198 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
203 ::cppu::UnoType
< SQLException
>::get(),
210 //--------------------------------------------------------------------
211 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
212 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
214 if ( !::cppu::UnoType
< SQLException
>::get().isAssignableFrom( _rExceptionType
) )
215 throw ::std::bad_cast();
217 // default-construct an exception of the desired type
218 Any
aException( NULL
, _rExceptionType
);
221 SQLException
* pException
= static_cast< SQLException
* >( aException
.pData
);
222 *pException
= impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
225 ::cppu::throwException( aException
);
228 //--------------------------------------------------------------------
229 SQLException
SQLError_Impl::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
230 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
232 return impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
235 //--------------------------------------------------------------------
236 SQLException
SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
237 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
240 getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
),
242 getSQLState( _eCondition
),
243 getErrorCode( _eCondition
),
248 //--------------------------------------------------------------------
249 ::rtl::OUString
SQLError_Impl::impl_getErrorMessage( const ErrorCondition
& _eCondition
)
251 ::rtl::OUStringBuffer aMessage
;
253 if ( impl_initResources() )
255 ::rtl::OUString
sResMessage( m_pResources
->loadString( lcl_getResourceID( _eCondition
, false ) ) );
256 OSL_ENSURE( !sResMessage
.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
257 aMessage
.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage
);
260 return aMessage
.makeStringAndClear();
263 //--------------------------------------------------------------------
264 ::rtl::OUString
SQLError_Impl::impl_getSQLState( const ErrorCondition
& _eCondition
)
266 ::rtl::OUString sState
;
268 if ( impl_initResources() )
270 sal_Int32
nResourceId( lcl_getResourceID( _eCondition
, true ) );
271 if ( m_pResources
->hasString( nResourceId
) )
272 sState
= m_pResources
->loadString( nResourceId
);
275 if ( sState
.isEmpty() )
276 sState
= ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
281 //--------------------------------------------------------------------
282 bool SQLError_Impl::impl_initResources()
284 if ( m_pResources
.get() )
286 if ( m_bAttemptedInit
)
289 ::osl::MutexGuard
aGuard( m_aMutex
);
290 m_bAttemptedInit
= true;
292 m_pResources
.reset( new ::comphelper::OfficeResourceBundle( m_aContext
.getUNOContext(), "sdberr" ) );
293 return m_pResources
.get() != NULL
;
296 //====================================================================
298 //====================================================================
299 //--------------------------------------------------------------------
300 SQLError::SQLError( const ::comphelper::ComponentContext
& _rContext
)
301 :m_pImpl( new SQLError_Impl( _rContext
) )
305 //--------------------------------------------------------------------
306 SQLError::~SQLError()
310 //--------------------------------------------------------------------
311 const ::rtl::OUString
& SQLError::getMessagePrefix()
313 return SQLError_Impl::getMessagePrefix();
316 //--------------------------------------------------------------------
317 ::rtl::OUString
SQLError::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
319 return m_pImpl
->getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
322 //--------------------------------------------------------------------
323 ::rtl::OUString
SQLError::getSQLState( const ErrorCondition _eCondition
) const
325 return m_pImpl
->getSQLState( _eCondition
);
328 //--------------------------------------------------------------------
329 ErrorCode
SQLError::getErrorCode( const ErrorCondition _eCondition
)
331 return SQLError_Impl::getErrorCode( _eCondition
);
334 //--------------------------------------------------------------------
335 void SQLError::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
337 m_pImpl
->raiseException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
340 //--------------------------------------------------------------------
341 void SQLError::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
343 m_pImpl
->raiseException( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
346 //--------------------------------------------------------------------
347 void SQLError::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
348 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
350 m_pImpl
->raiseTypedException( _eCondition
, _rxContext
, _rExceptionType
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
353 //--------------------------------------------------------------------
354 SQLException
SQLError::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
355 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
357 return m_pImpl
->getSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
360 //........................................................................
361 } // namespace connectivity
362 //........................................................................
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */