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 <config_options.h>
24 #include <connectivity/standardsqlstate.hxx>
25 #include <connectivity/dbtoolsdllapi.hxx>
26 #include <com/sun/star/uno/Reference.hxx>
28 namespace com::sun::star
46 //= Special exception if cancel is pressed in DBA UI
50 ParameterInteractionCancelled
= 1
54 //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
57 class OOO_DLLPUBLIC_DBTOOLS SQLExceptionInfo final
60 enum class TYPE
{ SQLException
, SQLWarning
, SQLContext
, Undefined
};
63 css::uno::Any m_aContent
;
64 TYPE m_eType
; // redundant (could be derived from m_aContent.getValueType())
68 SQLExceptionInfo(const css::sdbc::SQLException
& _rError
);
69 SQLExceptionInfo(const css::sdbc::SQLWarning
& _rError
);
70 SQLExceptionInfo(const css::sdb::SQLContext
& _rError
);
72 /** convenience constructor
74 If your error processing relies on SQLExceptions, and SQLExceptionInfos, you still may
75 need to display an error which consists of a simple message string only.
76 In those cases, you can use this constructor, which behaves as if you would have used
77 an SQLException containing exactly the given error message.
79 SQLExceptionInfo( const OUString
& _rSimpleErrorMessage
);
81 // use for events got via XSQLErrorListener::errorOccured
82 SQLExceptionInfo(const css::uno::Any
& _rError
);
83 // use with the Reason member of an SQLErrorEvent or with NextElement of an SQLException
85 /** prepends a plain error message to the chain of exceptions
86 @param _rSimpleErrorMessage
87 the error message to prepend
89 void prepend( const OUString
& _rErrorMessage
);
91 /** appends a plain message to the chain of exceptions
93 the type of exception to append. Must be SQL_EXCEPTION, SQL_WARNING, SQL_CONTEXT, for all other
94 values, the behavior is undefined.
98 the SQLState of the exception to append
100 the error code of the exception to append
102 void append( TYPE _eType
, const OUString
& _rErrorMessage
, const OUString
& _rSQLState
= OUString(), const sal_Int32 _nErrorCode
= 0 );
104 /** throws (properly typed) the exception contained in the object
106 isValid() returns <TRUE/>
108 @throws RuntimeException
109 if the instance does not contain an SQLException
113 SQLExceptionInfo
& operator=(const css::sdbc::SQLException
& _rError
);
114 SQLExceptionInfo
& operator=(const css::sdbc::SQLWarning
& _rError
);
115 SQLExceptionInfo
& operator=(const css::sdb::SQLContext
& _rError
);
116 SQLExceptionInfo
& operator=(const css::sdb::SQLErrorEvent
& _rErrorEvent
);
117 SQLExceptionInfo
& operator=(const css::uno::Any
& _rCaughtSQLException
);
119 bool isKindOf(TYPE _eType
) const;
120 // not just a simple comparison ! e.g. getType() == SQL_CONTEXT implies isKindOf(SQL_EXCEPTION) == sal_True !
121 bool isValid() const { return m_eType
!= TYPE::Undefined
; }
122 TYPE
getType() const { return m_eType
; }
124 operator const css::sdbc::SQLException
* () const;
125 operator const css::sdb::SQLContext
* () const;
127 const css::uno::Any
& get() const { return m_aContent
; }
132 m_eType
= TYPE::Undefined
;
135 // create an exception
136 static css::uno::Any
createException(TYPE eType
, const OUString
& rErrorMessage
, const OUString
& rSQLState
, const sal_Int32 nErrorCode
);
138 // find the end of the exception chain
139 static css::sdbc::SQLException
* getLastException(css::sdbc::SQLException
* pLastException
);
142 void implDetermineType();
146 //= SQLExceptionIteratorHelper - iterating through an SQLException chain
149 class UNLESS_MERGELIBS_MORE(OOO_DLLPUBLIC_DBTOOLS
) SQLExceptionIteratorHelper final
151 const css::sdbc::SQLException
* m_pCurrent
;
152 SQLExceptionInfo::TYPE m_eCurrentType
;
155 /** constructs an iterator instance from an SQLException
158 the start of the exception chain to iterate. Must live as long as the iterator
159 instances lives, at least.
161 SQLExceptionIteratorHelper( const css::sdbc::SQLException
& _rChainStart
);
163 /** constructs an iterator instance from an SQLExceptionInfo
166 the start of the exception chain to iterate. Must live as long as the iterator
167 instances lives, at least.
169 SQLExceptionIteratorHelper( const SQLExceptionInfo
& _rErrorInfo
);
171 /** determines whether there are more elements in the exception chain
173 bool hasMoreElements() const { return ( m_pCurrent
!= nullptr ); }
175 /** retrieves the current element in the chain, or <NULL/> if the chain has been completely
178 In opposite to the second <member>current</member>, this version allows typed access to
179 the respective SQLException.
181 void current( SQLExceptionInfo
& _out_rInfo
) const;
183 /** proceeds to the next element in the chain
185 @return the current element in the chain, as <b>before</em> the chain move.
187 const css::sdbc::SQLException
* next();
189 /** proceeds to the next element in the chain
191 In opposite to the second <member>current</member>, this version allows typed access to
192 the respective SQLException.
194 void next( SQLExceptionInfo
& _out_rInfo
);
198 //= StandardExceptions
201 /** returns a standard error string for a given SQLState
204 describes the state whose description is to retrieve. Must not be SQL_ERROR_UNSPECIFIED.
205 @throws RuntimeException
206 in case of an internal error
208 OOO_DLLPUBLIC_DBTOOLS OUString
getStandardSQLState( StandardSQLState _eState
);
211 /** throws an exception with SQL state IM001, saying that a certain function is not supported
213 @throws css::sdbc::SQLException
215 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwFunctionNotSupportedSQLException(
216 const OUString
& _rFunctionName
,
217 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
220 /// @throws css::uno::RuntimeException
221 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwFunctionNotSupportedRuntimeException(
222 const OUString
& _rFunctionName
,
223 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
226 /** throws a function sequence (HY010) exception
228 @throws css::sdbc::SQLException
230 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwFunctionSequenceException(
231 const css::uno::Reference
< css::uno::XInterface
>& Context
,
232 const css::uno::Any
& Next
= css::uno::Any()
236 /** throw an invalid index sqlexception
238 @throws css::sdbc::SQLException
240 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwInvalidIndexException(
241 const css::uno::Reference
< css::uno::XInterface
>& Context
,
242 const css::uno::Any
& Next
= css::uno::Any()
246 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
248 @throws css::sdbc::SQLException
250 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwGenericSQLException(
251 const OUString
& _rMsg
,
252 const css::uno::Reference
< css::uno::XInterface
>& _rxSource
256 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
258 @throws css::sdbc::SQLException
260 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwGenericSQLException(
261 const OUString
& _rMsg
,
262 const css::uno::Reference
< css::uno::XInterface
>& _rxSource
,
263 const css::uno::Any
& _rNextException
267 /** throw a SQLException with SQLState HYC00 (Optional feature not implemented)
269 a description of the feature which is not implemented. It's recommended that the feature
270 name is built from the name of the interface plus its method, for instance "XParameters::updateBinaryStream"
272 the context of the exception
273 @throws css::sdbc::SQLException
275 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwFeatureNotImplementedSQLException(
276 const OUString
& _rFeatureName
,
277 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
,
278 const css::uno::Any
& _rNextException
= css::uno::Any()
281 /** throw a RuntimeException (Optional feature not implemented)
283 a description of the feature which is not implemented. It's recommended that the feature
284 name is built from the name of the interface plus its method, for instance "XParameters::updateBinaryStream"
286 the context of the exception
287 @throws css::uno::RuntimeException
289 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwFeatureNotImplementedRuntimeException(
290 const OUString
& _rFeatureName
,
291 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
294 /** throw a SQLException with SQLState 42S22 (Column Not Found)
295 @param _rColumnNameName
296 The column that couldn't be found.
298 the context of the exception
299 @throws css::sdbc::SQLException
301 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwInvalidColumnException(
302 const OUString
& _rColumnName
,
303 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
307 /** @throws css::sdbc::SQLException
309 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwSQLException(
310 const OUString
& _rMessage
,
311 const OUString
& _rSQLState
,
312 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
,
313 const sal_Int32 _nErrorCode
317 /** @throws css::sdbc::SQLException
319 [[noreturn
]] OOO_DLLPUBLIC_DBTOOLS
void throwSQLException(
320 const OUString
& _rMessage
,
321 StandardSQLState _eSQLState
,
322 const css::uno::Reference
< css::uno::XInterface
>& _rxContext
,
323 const sal_Int32 _nErrorCode
= 0
327 } // namespace dbtools
330 #endif // INCLUDED_CONNECTIVITY_DBEXCEPTION_HXX
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */