Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <connectivity/dbexception.hxx>
21 : #include <comphelper/types.hxx>
22 : #include <cppuhelper/exc_hlp.hxx>
23 : #include <osl/diagnose.h>
24 : #include <com/sun/star/sdb/SQLContext.hpp>
25 : #include <com/sun/star/sdbc/SQLWarning.hpp>
26 : #include <com/sun/star/sdb/SQLErrorEvent.hpp>
27 : #include "TConnection.hxx"
28 : #include "resource/common_res.hrc"
29 : #include "resource/sharedresources.hxx"
30 :
31 :
32 : namespace dbtools
33 : {
34 :
35 :
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::sdb;
38 : using namespace ::com::sun::star::sdbc;
39 : using namespace ::comphelper;
40 : using namespace ::connectivity;
41 :
42 11 : SQLExceptionInfo::SQLExceptionInfo()
43 11 : :m_eType(UNDEFINED)
44 : {
45 11 : }
46 :
47 :
48 0 : SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError)
49 : {
50 0 : m_aContent <<= _rError;
51 0 : implDetermineType();
52 0 : }
53 :
54 :
55 0 : SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError)
56 : {
57 0 : m_aContent <<= _rError;
58 0 : implDetermineType();
59 0 : }
60 :
61 :
62 0 : SQLExceptionInfo::SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError)
63 : {
64 0 : m_aContent <<= _rError;
65 0 : implDetermineType();
66 0 : }
67 :
68 :
69 0 : SQLExceptionInfo::SQLExceptionInfo( const OUString& _rSimpleErrorMessage )
70 : {
71 0 : SQLException aError;
72 0 : aError.Message = _rSimpleErrorMessage;
73 0 : m_aContent <<= aError;
74 0 : implDetermineType();
75 0 : }
76 :
77 :
78 0 : SQLExceptionInfo::SQLExceptionInfo(const SQLExceptionInfo& _rCopySource)
79 : :m_aContent(_rCopySource.m_aContent)
80 0 : ,m_eType(_rCopySource.m_eType)
81 : {
82 0 : }
83 :
84 :
85 0 : const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLException& _rError)
86 : {
87 0 : m_aContent <<= _rError;
88 0 : implDetermineType();
89 0 : return *this;
90 : }
91 :
92 :
93 0 : const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdbc::SQLWarning& _rError)
94 : {
95 0 : m_aContent <<= _rError;
96 0 : implDetermineType();
97 0 : return *this;
98 : }
99 :
100 :
101 0 : const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLContext& _rError)
102 : {
103 0 : m_aContent <<= _rError;
104 0 : implDetermineType();
105 0 : return *this;
106 : }
107 :
108 :
109 0 : const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::sdb::SQLErrorEvent& _rErrorEvent)
110 : {
111 0 : m_aContent = _rErrorEvent.Reason;
112 0 : implDetermineType();
113 0 : return *this;
114 : }
115 :
116 :
117 0 : const SQLExceptionInfo& SQLExceptionInfo::operator=(const ::com::sun::star::uno::Any& _rCaughtSQLException)
118 : {
119 0 : m_aContent = _rCaughtSQLException;
120 0 : implDetermineType();
121 0 : return *this;
122 : }
123 :
124 :
125 3 : SQLExceptionInfo::SQLExceptionInfo(const css::uno::Any& _rError)
126 : {
127 3 : const css::uno::Type& aSQLExceptionType = cppu::UnoType<com::sun::star::sdbc::SQLException>::get();
128 3 : bool bValid = isAssignableFrom(aSQLExceptionType, _rError.getValueType());
129 3 : if (bValid)
130 0 : m_aContent = _rError;
131 : // no assertion here : if used with the NextException member of an SQLException bValid==sal_False is allowed.
132 :
133 3 : implDetermineType();
134 3 : }
135 :
136 :
137 3 : void SQLExceptionInfo::implDetermineType()
138 : {
139 3 : const Type& aSQLExceptionType = ::cppu::UnoType<SQLException>::get();
140 3 : const Type& aSQLWarningType = ::cppu::UnoType<SQLWarning>::get();
141 3 : const Type& aSQLContextType = ::cppu::UnoType<SQLContext>::get();
142 :
143 3 : if ( isAssignableFrom( aSQLContextType, m_aContent.getValueType() ) )
144 0 : m_eType = SQL_CONTEXT;
145 3 : else if ( isAssignableFrom( aSQLWarningType, m_aContent.getValueType() ) )
146 0 : m_eType = SQL_WARNING;
147 3 : else if ( isAssignableFrom( aSQLExceptionType, m_aContent.getValueType() ) )
148 0 : m_eType = SQL_EXCEPTION;
149 : else
150 : {
151 3 : m_eType = UNDEFINED;
152 3 : m_aContent.clear();
153 : }
154 3 : }
155 :
156 :
157 0 : bool SQLExceptionInfo::isKindOf(TYPE _eType) const
158 : {
159 0 : switch (_eType)
160 : {
161 : case SQL_CONTEXT:
162 0 : return (m_eType == SQL_CONTEXT);
163 : case SQL_WARNING:
164 0 : return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING);
165 : case SQL_EXCEPTION:
166 0 : return (m_eType == SQL_CONTEXT) || (m_eType == SQL_WARNING) || (m_eType == SQL_EXCEPTION);
167 : case UNDEFINED:
168 0 : return (m_eType == UNDEFINED);
169 : }
170 0 : return false;
171 : }
172 :
173 :
174 0 : SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLException*() const
175 : {
176 : OSL_ENSURE(isKindOf(SQL_EXCEPTION), "SQLExceptionInfo::operator SQLException* : invalid call !");
177 0 : return static_cast<const ::com::sun::star::sdbc::SQLException*>(m_aContent.getValue());
178 : }
179 :
180 :
181 0 : SQLExceptionInfo::operator const ::com::sun::star::sdbc::SQLWarning*() const
182 : {
183 : OSL_ENSURE(isKindOf(SQL_WARNING), "SQLExceptionInfo::operator SQLException* : invalid call !");
184 0 : return static_cast<const ::com::sun::star::sdbc::SQLWarning*>(m_aContent.getValue());
185 : }
186 :
187 :
188 0 : SQLExceptionInfo::operator const ::com::sun::star::sdb::SQLContext*() const
189 : {
190 : OSL_ENSURE(isKindOf(SQL_CONTEXT), "SQLExceptionInfo::operator SQLException* : invalid call !");
191 0 : return static_cast<const ::com::sun::star::sdb::SQLContext*>(m_aContent.getValue());
192 : }
193 :
194 :
195 0 : void SQLExceptionInfo::prepend( const OUString& _rErrorMessage, const OUString& _rSQLState, const sal_Int32 _nErrorCode )
196 : {
197 0 : SQLException aException;
198 0 : aException.Message = _rErrorMessage;
199 0 : aException.ErrorCode = _nErrorCode;
200 0 : aException.SQLState = !_rSQLState.isEmpty() ? _rSQLState : "S1000";
201 0 : aException.NextException = m_aContent;
202 0 : m_aContent <<= aException;
203 :
204 0 : m_eType = SQL_EXCEPTION;
205 0 : }
206 :
207 :
208 0 : void SQLExceptionInfo::append( TYPE _eType, const OUString& _rErrorMessage, const OUString& _rSQLState, const sal_Int32 _nErrorCode )
209 : {
210 : // create the to-be-appended exception
211 0 : Any aAppend;
212 0 : switch ( _eType )
213 : {
214 0 : case SQL_EXCEPTION: aAppend <<= SQLException(); break;
215 0 : case SQL_WARNING: aAppend <<= SQLWarning(); break;
216 0 : case SQL_CONTEXT: aAppend <<= SQLContext(); break;
217 : default:
218 : OSL_FAIL( "SQLExceptionInfo::append: invalid exception type: this will crash!" );
219 0 : break;
220 : }
221 :
222 0 : SQLException* pAppendException( static_cast< SQLException* >( const_cast< void* >( aAppend.getValue() ) ) );
223 0 : pAppendException->Message = _rErrorMessage;
224 0 : pAppendException->SQLState = _rSQLState;
225 0 : pAppendException->ErrorCode = _nErrorCode;
226 :
227 : // find the end of the current chain
228 0 : Any* pChainIterator = &m_aContent;
229 0 : SQLException* pLastException = NULL;
230 0 : const Type& aSQLExceptionType( cppu::UnoType<SQLException>::get() );
231 0 : while ( pChainIterator )
232 : {
233 0 : if ( !pChainIterator->hasValue() )
234 0 : break;
235 :
236 0 : if ( !isAssignableFrom( aSQLExceptionType, pChainIterator->getValueType() ) )
237 0 : break;
238 :
239 0 : pLastException = static_cast< SQLException* >( const_cast< void* >( pChainIterator->getValue() ) );
240 0 : pChainIterator = &pLastException->NextException;
241 : }
242 :
243 : // append
244 0 : if ( pLastException )
245 0 : pLastException->NextException = aAppend;
246 : else
247 : {
248 0 : m_aContent = aAppend;
249 0 : m_eType = _eType;
250 0 : }
251 0 : }
252 :
253 :
254 0 : void SQLExceptionInfo::doThrow()
255 : {
256 0 : if ( m_aContent.getValueTypeClass() == TypeClass_EXCEPTION )
257 0 : ::cppu::throwException( m_aContent );
258 0 : throw RuntimeException();
259 : }
260 :
261 0 : SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const SQLExceptionInfo& _rChainStart )
262 : :m_pCurrent( NULL )
263 0 : ,m_eCurrentType( SQLExceptionInfo::UNDEFINED )
264 : {
265 0 : if ( _rChainStart.isValid() )
266 : {
267 0 : m_pCurrent = static_cast<const SQLException*>(_rChainStart);
268 0 : m_eCurrentType = _rChainStart.getType();
269 : }
270 0 : }
271 :
272 :
273 0 : SQLExceptionIteratorHelper::SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException& _rChainStart )
274 : :m_pCurrent( &_rChainStart )
275 0 : ,m_eCurrentType( SQLExceptionInfo::SQL_EXCEPTION )
276 : {
277 0 : }
278 :
279 :
280 0 : void SQLExceptionIteratorHelper::current( SQLExceptionInfo& _out_rInfo ) const
281 : {
282 0 : switch ( m_eCurrentType )
283 : {
284 : case SQLExceptionInfo::SQL_EXCEPTION:
285 0 : _out_rInfo = *m_pCurrent;
286 0 : break;
287 :
288 : case SQLExceptionInfo::SQL_WARNING:
289 0 : _out_rInfo = *static_cast< const SQLWarning* >( m_pCurrent );
290 0 : break;
291 :
292 : case SQLExceptionInfo::SQL_CONTEXT:
293 0 : _out_rInfo = *static_cast< const SQLContext* >( m_pCurrent );
294 0 : break;
295 :
296 : default:
297 0 : _out_rInfo = Any();
298 0 : break;
299 : }
300 0 : }
301 :
302 :
303 0 : const ::com::sun::star::sdbc::SQLException* SQLExceptionIteratorHelper::next()
304 : {
305 : OSL_ENSURE( hasMoreElements(), "SQLExceptionIteratorHelper::next : invalid call (please use hasMoreElements)!" );
306 :
307 0 : const ::com::sun::star::sdbc::SQLException* pReturn = current();
308 0 : if ( !m_pCurrent )
309 0 : return pReturn;
310 :
311 : // check for the next element within the chain
312 0 : const Type aTypeException( ::cppu::UnoType< SQLException >::get() );
313 :
314 0 : Type aNextElementType = m_pCurrent->NextException.getValueType();
315 0 : if ( !isAssignableFrom( aTypeException, aNextElementType ) )
316 : {
317 : // no SQLException at all in the next chain element
318 0 : m_pCurrent = NULL;
319 0 : m_eCurrentType = SQLExceptionInfo::UNDEFINED;
320 0 : return pReturn;
321 : }
322 :
323 0 : m_pCurrent = static_cast< const SQLException* >( m_pCurrent->NextException.getValue() );
324 :
325 : // no finally determine the proper type of the exception
326 0 : const Type aTypeContext( ::cppu::UnoType< SQLContext >::get() );
327 0 : if ( isAssignableFrom( aTypeContext, aNextElementType ) )
328 : {
329 0 : m_eCurrentType = SQLExceptionInfo::SQL_CONTEXT;
330 0 : return pReturn;
331 : }
332 :
333 0 : const Type aTypeWarning( ::cppu::UnoType< SQLWarning >::get() );
334 0 : if ( isAssignableFrom( aTypeWarning, aNextElementType ) )
335 : {
336 0 : m_eCurrentType = SQLExceptionInfo::SQL_WARNING;
337 0 : return pReturn;
338 : }
339 :
340 : // a simple SQLException
341 0 : m_eCurrentType = SQLExceptionInfo::SQL_EXCEPTION;
342 0 : return pReturn;
343 : }
344 :
345 :
346 0 : void SQLExceptionIteratorHelper::next( SQLExceptionInfo& _out_rInfo )
347 : {
348 0 : current( _out_rInfo );
349 0 : next();
350 0 : }
351 :
352 :
353 1 : void throwFunctionSequenceException(const Reference< XInterface >& _Context, const Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
354 : {
355 1 : ::connectivity::SharedResources aResources;
356 : throw SQLException(
357 : aResources.getResourceString(STR_ERRORMSG_SEQUENCE),
358 : _Context,
359 : getStandardSQLState( SQL_FUNCTION_SEQUENCE_ERROR ),
360 : 0,
361 : _Next
362 1 : );
363 : }
364 :
365 0 : void throwInvalidIndexException(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
366 : const ::com::sun::star::uno::Any& _Next) throw ( ::com::sun::star::sdbc::SQLException )
367 : {
368 0 : ::connectivity::SharedResources aResources;
369 : throw SQLException(
370 : aResources.getResourceString(STR_INVALID_INDEX),
371 : _Context,
372 : getStandardSQLState( SQL_INVALID_DESCRIPTOR_INDEX ),
373 : 0,
374 : _Next
375 0 : );
376 : }
377 :
378 0 : void throwFunctionNotSupportedSQLException(const OUString& _rFunctionName,
379 : const css::uno::Reference<css::uno::XInterface>& _rxContext,
380 : const css::uno::Any& _rNextException) throw (css::sdbc::SQLException)
381 : {
382 0 : ::connectivity::SharedResources aResources;
383 : const OUString sError( aResources.getResourceStringWithSubstitution(
384 : STR_UNSUPPORTED_FUNCTION,
385 : "$functionname$", _rFunctionName
386 0 : ) );
387 : throw SQLException(
388 : sError,
389 : _rxContext,
390 : getStandardSQLState( SQL_FUNCTION_NOT_SUPPORTED ),
391 : 0,
392 : _rNextException
393 0 : );
394 : }
395 :
396 0 : void throwFunctionNotSupportedRuntimeException(const OUString& _rFunctionName,
397 : const css::uno::Reference<css::uno::XInterface>& _rxContext) throw (css::uno::RuntimeException)
398 : {
399 0 : ::connectivity::SharedResources aResources;
400 : const OUString sError( aResources.getResourceStringWithSubstitution(
401 : STR_UNSUPPORTED_FUNCTION,
402 : "$functionname$", _rFunctionName
403 0 : ) );
404 : throw RuntimeException(
405 : sError,
406 : _rxContext
407 0 : );
408 : }
409 :
410 33 : void throwGenericSQLException(const OUString& _rMsg, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource)
411 : throw (::com::sun::star::sdbc::SQLException)
412 : {
413 66 : throwGenericSQLException(_rMsg, _rxSource, Any());
414 0 : }
415 :
416 :
417 33 : void throwGenericSQLException(const OUString& _rMsg, const Reference< XInterface >& _rxSource, const Any& _rNextException)
418 : throw (SQLException)
419 : {
420 33 : throw SQLException( _rMsg, _rxSource, getStandardSQLState( SQL_GENERAL_ERROR ), 0, _rNextException);
421 : }
422 :
423 0 : void throwFeatureNotImplementedSQLException( const OUString& _rFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
424 : throw (SQLException)
425 : {
426 0 : ::connectivity::SharedResources aResources;
427 : const OUString sError( aResources.getResourceStringWithSubstitution(
428 : STR_UNSUPPORTED_FEATURE,
429 : "$featurename$", _rFeatureName
430 0 : ) );
431 :
432 : throw SQLException(
433 : sError,
434 : _rxContext,
435 : getStandardSQLState( SQL_FEATURE_NOT_IMPLEMENTED ),
436 : 0,
437 : _pNextException ? *_pNextException : Any()
438 0 : );
439 : }
440 :
441 0 : void throwFeatureNotImplementedRuntimeException(const OUString& _rFeatureName, const Reference< XInterface >& _rxContext)
442 : throw (RuntimeException)
443 : {
444 0 : ::connectivity::SharedResources aResources;
445 : const OUString sError( aResources.getResourceStringWithSubstitution(
446 : STR_UNSUPPORTED_FEATURE,
447 : "$featurename$", _rFeatureName
448 0 : ) );
449 :
450 0 : throw RuntimeException(sError, _rxContext);
451 : }
452 :
453 0 : void throwInvalidColumnException( const OUString& _rColumnName, const Reference< XInterface >& _rxContext)
454 : throw (SQLException)
455 : {
456 0 : ::connectivity::SharedResources aResources;
457 : OUString sErrorMessage( aResources.getResourceStringWithSubstitution(
458 : STR_INVALID_COLUMNNAME,
459 0 : "$columnname$",_rColumnName) );
460 0 : throwSQLException( sErrorMessage, SQL_COLUMN_NOT_FOUND, _rxContext );
461 0 : }
462 :
463 14 : void throwSQLException( const OUString& _rMessage, const OUString& _rSQLState,
464 : const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode, const Any* _pNextException ) throw (SQLException)
465 : {
466 : throw SQLException(
467 : _rMessage,
468 : _rxContext,
469 : _rSQLState,
470 : _nErrorCode,
471 : _pNextException ? *_pNextException : Any()
472 14 : );
473 : }
474 :
475 :
476 14 : void throwSQLException( const OUString& _rMessage, StandardSQLState _eSQLState,
477 : const Reference< XInterface >& _rxContext, const sal_Int32 _nErrorCode,
478 : const Any* _pNextException ) throw (SQLException)
479 : {
480 28 : throwSQLException( _rMessage, getStandardSQLState( _eSQLState ), _rxContext, _nErrorCode, _pNextException );
481 0 : }
482 :
483 :
484 477 : OUString getStandardSQLState( StandardSQLState _eState )
485 : {
486 477 : switch ( _eState )
487 : {
488 0 : case SQL_WRONG_PARAMETER_NUMBER: return OUString("07001");
489 0 : case SQL_INVALID_DESCRIPTOR_INDEX: return OUString("07009");
490 0 : case SQL_UNABLE_TO_CONNECT: return OUString("08001");
491 0 : case SQL_NUMERIC_OUT_OF_RANGE: return OUString("22003");
492 0 : case SQL_INVALID_DATE_TIME: return OUString("22007");
493 1 : case SQL_INVALID_CURSOR_STATE: return OUString("24000");
494 0 : case SQL_TABLE_OR_VIEW_EXISTS: return OUString("42S01");
495 0 : case SQL_TABLE_OR_VIEW_NOT_FOUND: return OUString("42S02");
496 0 : case SQL_INDEX_ESISTS: return OUString("42S11");
497 0 : case SQL_INDEX_NOT_FOUND: return OUString("42S12");
498 0 : case SQL_COLUMN_EXISTS: return OUString("42S21");
499 0 : case SQL_COLUMN_NOT_FOUND: return OUString("42S22");
500 470 : case SQL_GENERAL_ERROR: return OUString("HY000");
501 0 : case SQL_INVALID_SQL_DATA_TYPE: return OUString("HY004");
502 0 : case SQL_OPERATION_CANCELED: return OUString("HY008");
503 2 : case SQL_FUNCTION_SEQUENCE_ERROR: return OUString("HY010");
504 4 : case SQL_INVALID_CURSOR_POSITION: return OUString("HY109");
505 0 : case SQL_INVALID_BOOKMARK_VALUE: return OUString("HY111");
506 0 : case SQL_FEATURE_NOT_IMPLEMENTED: return OUString("HYC00");
507 0 : case SQL_FUNCTION_NOT_SUPPORTED: return OUString("IM001");
508 0 : case SQL_CONNECTION_DOES_NOT_EXIST: return OUString("08003");
509 0 : default: return OUString("HY001"); // General Error
510 : }
511 : }
512 :
513 :
514 :
515 : } // namespace dbtools
516 :
517 :
518 :
519 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|