1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <connectivity/sqlerror.hxx>
23 #include <com/sun/star/sdbc/SQLException.hpp>
25 #include <comphelper/officeresourcebundle.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <osl/diagnose.h>
33 namespace connectivity
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::uno::UNO_QUERY
;
39 using ::com::sun::star::uno::UNO_QUERY_THROW
;
40 using ::com::sun::star::uno::Exception
;
41 using ::com::sun::star::uno::RuntimeException
;
42 using ::com::sun::star::uno::Any
;
43 using ::com::sun::star::uno::makeAny
;
44 using ::com::sun::star::uno::XInterface
;
45 using ::com::sun::star::uno::XComponentContext
;
46 using ::com::sun::star::sdbc::SQLException
;
47 using ::com::sun::star::uno::Type
;
49 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
52 typedef SQLError::ParamValue ParamValue
;
59 SQLError_Impl( const Reference
<XComponentContext
> & _rxContext
);
62 // versions of the public SQLError methods which are just delegated to this impl-class
63 static const OUString
& getMessagePrefix();
64 OUString
getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
65 OUString
getSQLState( const ErrorCondition _eCondition
);
66 static ErrorCode
getErrorCode( const ErrorCondition _eCondition
);
67 void raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
68 void raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
69 void raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
70 SQLException
getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
73 /// returns the basic error message associated with the given error condition, without any parameter replacements
75 impl_getErrorMessage( const ErrorCondition
& _eCondition
);
77 /// returns the SQLState associated with the given error condition
79 impl_getSQLState( const ErrorCondition
& _eCondition
);
81 /// returns an SQLException describing the given error condition
83 impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
84 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
86 /// initializes our resource bundle
87 bool impl_initResources();
90 ::osl::Mutex m_aMutex
;
91 Reference
<XComponentContext
> m_aContext
;
92 ::std::unique_ptr
< ::comphelper::OfficeResourceBundle
> m_pResources
;
93 bool m_bAttemptedInit
;
96 SQLError_Impl::SQLError_Impl( const Reference
<XComponentContext
> & _rxContext
)
97 :m_aContext( _rxContext
)
99 ,m_bAttemptedInit( false )
104 SQLError_Impl::~SQLError_Impl()
109 const OUString
& SQLError_Impl::getMessagePrefix()
111 static const OUString
s_sMessagePrefix( "[OOoBase]" );
112 return s_sMessagePrefix
;
119 /** substitutes a given placeholder in the given message with the given value
121 void lcl_substitutePlaceholder(OUString
& _rMessage
, const sal_Char
* _pPlaceholder
, const ParamValue
& rParamValue
)
123 size_t nPlaceholderLen( strlen( _pPlaceholder
) );
124 sal_Int32 nIndex
= _rMessage
.indexOfAsciiL( _pPlaceholder
, nPlaceholderLen
);
126 bool bHasPlaceholder
= ( nIndex
!= -1 );
127 bool bWantsPlaceholder
= rParamValue
.is();
128 OSL_ENSURE( bHasPlaceholder
== bWantsPlaceholder
, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
130 if ( bHasPlaceholder
&& bWantsPlaceholder
)
131 _rMessage
= _rMessage
.replaceAt( nIndex
, nPlaceholderLen
, *rParamValue
);
135 sal_Int32
lcl_getResourceID( const ErrorCondition _eCondition
, bool _bSQLState
)
138 + 2 * ::sal::static_int_cast
< sal_Int32
, ErrorCondition
>( _eCondition
)
139 + ( _bSQLState
? 1 : 0 );
144 OUString
SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
146 OUString
sErrorMessage( impl_getErrorMessage( _eCondition
) );
148 lcl_substitutePlaceholder( sErrorMessage
, "$1$", _rParamValue1
);
149 lcl_substitutePlaceholder( sErrorMessage
, "$2$", _rParamValue2
);
150 lcl_substitutePlaceholder( sErrorMessage
, "$3$", _rParamValue3
);
152 return sErrorMessage
;
156 OUString
SQLError_Impl::getSQLState( const ErrorCondition _eCondition
)
158 return impl_getSQLState( _eCondition
);
162 ErrorCode
SQLError_Impl::getErrorCode( const ErrorCondition _eCondition
)
164 return 0 - ::sal::static_int_cast
< ErrorCode
, ErrorCondition
>( _eCondition
);
168 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
173 ::cppu::UnoType
< SQLException
>::get(),
181 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
186 ::cppu::UnoType
< SQLException
>::get(),
194 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
195 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
197 if ( !::cppu::UnoType
< SQLException
>::get().isAssignableFrom( _rExceptionType
) )
198 throw ::std::bad_cast();
200 // default-construct an exception of the desired type
201 Any
aException( NULL
, _rExceptionType
);
204 SQLException
* pException
= static_cast< SQLException
* >( aException
.pData
);
205 *pException
= impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
208 ::cppu::throwException( aException
);
212 SQLException
SQLError_Impl::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
213 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
215 return impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
219 SQLException
SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
220 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
223 getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
),
225 getSQLState( _eCondition
),
226 getErrorCode( _eCondition
),
232 OUString
SQLError_Impl::impl_getErrorMessage( const ErrorCondition
& _eCondition
)
234 OUStringBuffer aMessage
;
236 if ( impl_initResources() )
238 OUString
sResMessage( m_pResources
->loadString( lcl_getResourceID( _eCondition
, false ) ) );
239 OSL_ENSURE( !sResMessage
.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
240 aMessage
.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage
);
243 return aMessage
.makeStringAndClear();
247 OUString
SQLError_Impl::impl_getSQLState( const ErrorCondition
& _eCondition
)
251 if ( impl_initResources() )
253 sal_Int32
nResourceId( lcl_getResourceID( _eCondition
, true ) );
254 if ( m_pResources
->hasString( nResourceId
) )
255 sState
= m_pResources
->loadString( nResourceId
);
258 if ( sState
.isEmpty() )
259 sState
= OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
265 bool SQLError_Impl::impl_initResources()
267 if ( m_pResources
.get() )
269 if ( m_bAttemptedInit
)
272 ::osl::MutexGuard
aGuard( m_aMutex
);
273 m_bAttemptedInit
= true;
275 m_pResources
.reset( new ::comphelper::OfficeResourceBundle( m_aContext
, "sdberr" ) );
276 return m_pResources
.get() != NULL
;
279 SQLError::SQLError( const Reference
<XComponentContext
> & _rxContext
)
280 :m_pImpl( new SQLError_Impl( _rxContext
) )
285 SQLError::~SQLError()
290 const OUString
& SQLError::getMessagePrefix()
292 return SQLError_Impl::getMessagePrefix();
296 OUString
SQLError::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
298 return m_pImpl
->getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
302 ErrorCode
SQLError::getErrorCode( const ErrorCondition _eCondition
)
304 return SQLError_Impl::getErrorCode( _eCondition
);
308 void SQLError::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
310 m_pImpl
->raiseException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
314 void SQLError::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
316 m_pImpl
->raiseException( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
320 void SQLError::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
321 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
323 m_pImpl
->raiseTypedException( _eCondition
, _rxContext
, _rExceptionType
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
327 SQLException
SQLError::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
328 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
330 return m_pImpl
->getSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
334 } // namespace connectivity
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */