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