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 .
20 #ifndef INCLUDED_CONNECTIVITY_DBEXCEPTION_HXX
21 #define INCLUDED_CONNECTIVITY_DBEXCEPTION_HXX
23 #include <com/sun/star/sdbc/SQLException.hpp>
24 #include <connectivity/standardsqlstate.hxx>
25 #include <connectivity/dbtoolsdllapi.hxx>
45 //.........................................................................
48 //.........................................................................
50 //==============================================================================
51 //= Special exception if cancel is pressed in DBA UI
52 //==============================================================================
55 ParameterInteractionCancelled
= 1
58 //==============================================================================
59 //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
60 //==============================================================================
62 class OOO_DLLPUBLIC_DBTOOLS SQLExceptionInfo
65 enum TYPE
{ SQL_EXCEPTION
, SQL_WARNING
, SQL_CONTEXT
, UNDEFINED
};
68 ::com::sun::star::uno::Any m_aContent
;
69 TYPE m_eType
; // redundant (could be derived from m_aContent.getValueType())
73 SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException
& _rError
);
74 SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning
& _rError
);
75 SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext
& _rError
);
77 /** convenience constructor
79 If your error processing relies on SQLExceptions, and SQLExceptionInfos, you still may
80 need to display an error which consists of a simple message string only.
81 In those cases, you can use this constructor, which behaves as if you would have used
82 an SQLException containing exactly the given error message.
84 SQLExceptionInfo( const OUString
& _rSimpleErrorMessage
);
86 SQLExceptionInfo(const SQLExceptionInfo
& _rCopySource
);
88 // use for events got via XSQLErrorListener::errorOccured
89 SQLExceptionInfo(const ::com::sun::star::uno::Any
& _rError
);
90 // use with the Reason member of an SQLErrorEvent or with NextElement of an SQLException
92 /** prepends a plain error message to the chain of exceptions
93 @param _rSimpleErrorMessage
94 the error message to prepend
96 the SQLState of the to-be-constructed SQLException, or NULL if this should be defaulted to HY000
98 the ErrorCode of the to-be-constructed SQLException
100 void prepend( const OUString
& _rErrorMessage
, const OUString
& _rSQLState
= OUString(), const sal_Int32 _nErrorCode
= 0 );
102 /** appends a plain message to the chain of exceptions
104 the type of exception to append. Must be SQL_EXCEPTION, SQL_WARNING, SQL_CONTEXT, for all other
105 values, the behavior is undefined.
106 @param _rErrorMessage
107 the message to append
109 the SQLState of the exception to append
111 the error code of the exception to append
113 void append( TYPE _eType
, const OUString
& _rErrorMessage
, const OUString
& _rSQLState
= OUString(), const sal_Int32 _nErrorCode
= 0 );
115 /** throws (properly typed) the exception contained in the object
117 isValid() returns <TRUE/>
119 @throws RuntimeException
120 if the instance does not contain an SQLException
124 const SQLExceptionInfo
& operator=(const ::com::sun::star::sdbc::SQLException
& _rError
);
125 const SQLExceptionInfo
& operator=(const ::com::sun::star::sdbc::SQLWarning
& _rError
);
126 const SQLExceptionInfo
& operator=(const ::com::sun::star::sdb::SQLContext
& _rError
);
127 const SQLExceptionInfo
& operator=(const ::com::sun::star::sdb::SQLErrorEvent
& _rErrorEvent
);
128 const SQLExceptionInfo
& operator=(const ::com::sun::star::uno::Any
& _rCaughtSQLException
);
130 sal_Bool
isKindOf(TYPE _eType
) const;
131 // not just a simple comparisation ! e.g. getType() == SQL_CONTEXT implies isKindOf(SQL_EXCEPTION) == sal_True !
132 sal_Bool
isValid() const { return m_eType
!= UNDEFINED
; }
133 TYPE
getType() const { return m_eType
; }
135 operator const ::com::sun::star::sdbc::SQLException
* () const;
136 operator const ::com::sun::star::sdbc::SQLWarning
* () const;
137 operator const ::com::sun::star::sdb::SQLContext
* () const;
139 const ::com::sun::star::uno::Any
& get() const { return m_aContent
; }
148 void implDetermineType();
151 //==============================================================================
152 //= SQLExceptionIteratorHelper - iterating through an SQLException chain
153 //==============================================================================
155 class OOO_DLLPUBLIC_DBTOOLS SQLExceptionIteratorHelper
158 const ::com::sun::star::sdbc::SQLException
* m_pCurrent
;
159 SQLExceptionInfo::TYPE m_eCurrentType
;
162 /** constructs an iterator instance from an SQLException
165 the start of the exception chain to iterate. Must live as long as the iterator
166 instances lives, at least.
168 SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException
& _rChainStart
);
170 /** constructs an iterator instance from an SQLExceptionInfo
173 the start of the exception chain to iterate. Must live as long as the iterator
174 instances lives, at least.
176 SQLExceptionIteratorHelper( const SQLExceptionInfo
& _rErrorInfo
);
178 /** determines whether there are more elements in the exception chain
180 sal_Bool
hasMoreElements() const { return ( m_pCurrent
!= NULL
); }
182 /** returns the type of the current element in the exception chain
184 SQLExceptionInfo::TYPE
currentType() const { return m_eCurrentType
; }
186 /** retrieves the current element in the chain, or <NULL/> if the chain has been completely
189 const ::com::sun::star::sdbc::SQLException
* current() const { return m_pCurrent
; }
191 /** retrieves the current element in the chain, or <NULL/> if the chain has been completely
194 In opposite to the second <member>current</member>, this version allows typed access to
195 the respective SQLException.
197 void current( SQLExceptionInfo
& _out_rInfo
) const;
199 /** proceeds to the next element in the chain
201 @return the current element in the chain, as <b>before</em> the chain move.
203 const ::com::sun::star::sdbc::SQLException
* next();
205 /** proceeds to the next element in the chain
207 In opposite to the second <member>current</member>, this version allows typed access to
208 the respective SQLException.
210 void next( SQLExceptionInfo
& _out_rInfo
);
213 //==================================================================================
214 //= StandardExceptions
215 //==================================================================================
216 //----------------------------------------------------------------------------------
217 /** returns a standard error string for a given SQLState
220 describes the state whose description is to retrieve. Must not be SQL_ERROR_UNSPECIFIED.
221 @raises RuntimeException
222 in case of an internal error
224 OOO_DLLPUBLIC_DBTOOLS OUString
getStandardSQLState( StandardSQLState _eState
);
226 //----------------------------------------------------------------------------------
227 /** throws an exception with SQL state IM001, saying that a certain function is not supported
229 OOO_DLLPUBLIC_DBTOOLS
void throwFunctionNotSupportedException(
230 const OUString
& _rFunctionName
,
231 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
232 const ::com::sun::star::uno::Any
& _rNextException
= ::com::sun::star::uno::Any()
234 throw ( ::com::sun::star::sdbc::SQLException
);
236 //----------------------------------------------------------------------------------
237 /** throws a function sequence (HY010) exception
239 OOO_DLLPUBLIC_DBTOOLS
void throwFunctionSequenceException(
240 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _Context
,
241 const ::com::sun::star::uno::Any
& _Next
= ::com::sun::star::uno::Any()
243 throw ( ::com::sun::star::sdbc::SQLException
);
245 //----------------------------------------------------------------------------------
246 /** throw a invalid index sqlexception
248 OOO_DLLPUBLIC_DBTOOLS
void throwInvalidIndexException(
249 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _Context
,
250 const ::com::sun::star::uno::Any
& _Next
= ::com::sun::star::uno::Any()
252 throw ( ::com::sun::star::sdbc::SQLException
);
254 //----------------------------------------------------------------------------------
255 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
257 OOO_DLLPUBLIC_DBTOOLS
void throwGenericSQLException(
258 const OUString
& _rMsg
,
259 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxSource
261 throw (::com::sun::star::sdbc::SQLException
);
263 //----------------------------------------------------------------------------------
264 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
266 OOO_DLLPUBLIC_DBTOOLS
void throwGenericSQLException(
267 const OUString
& _rMsg
,
268 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxSource
,
269 const ::com::sun::star::uno::Any
& _rNextException
271 throw (::com::sun::star::sdbc::SQLException
);
273 //----------------------------------------------------------------------------------
274 /** throw a SQLException with SQLState HYC00 (Optional feature not implemented)
276 a description of the feature which is not implemented. It's recommended that the feature
277 name is built from the name of the interface plus its method, for instance "XParameters::updateBinaryStream"
279 the context of the exception
280 @param _pNextException
281 the next exception to chain into the thrown exception, if any
283 OOO_DLLPUBLIC_DBTOOLS
void throwFeatureNotImplementedException(
284 const OUString
& _rFeatureName
,
285 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
286 const ::com::sun::star::uno::Any
* _pNextException
= NULL
288 throw (::com::sun::star::sdbc::SQLException
);
290 //----------------------------------------------------------------------------------
291 /** throw a SQLException with SQLState 42S22 (Column Not Found)
292 @param _rColumnNameName
293 The column that couldn't be found.
295 the context of the exception
297 OOO_DLLPUBLIC_DBTOOLS
void throwInvalidColumnException(
298 const OUString
& _rColumnName
,
299 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
301 throw (::com::sun::star::sdbc::SQLException
);
303 //----------------------------------------------------------------------------------
304 /** throws an SQLException
306 OOO_DLLPUBLIC_DBTOOLS
void throwSQLException(
307 const OUString
& _rMessage
,
308 const OUString
& _rSQLState
,
309 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
310 const sal_Int32 _nErrorCode
= 0,
311 const ::com::sun::star::uno::Any
* _pNextException
= NULL
313 throw (::com::sun::star::sdbc::SQLException
);
315 //----------------------------------------------------------------------------------
316 /** throws an SQLException
318 OOO_DLLPUBLIC_DBTOOLS
void throwSQLException(
319 const OUString
& _rMessage
,
320 StandardSQLState _eSQLState
,
321 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
322 const sal_Int32 _nErrorCode
= 0,
323 const ::com::sun::star::uno::Any
* _pNextException
= NULL
325 throw (::com::sun::star::sdbc::SQLException
);
327 //.........................................................................
328 } // namespace dbtools
329 //.........................................................................
331 #endif // INCLUDED_CONNECTIVITY_DBEXCEPTION_HXX
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */