1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSql module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qsqlerror.h" |
41 | #include "qdebug.h" |
42 | |
43 | QT_BEGIN_NAMESPACE |
44 | |
45 | #ifndef QT_NO_DEBUG_STREAM |
46 | QDebug operator<<(QDebug dbg, const QSqlError &s) |
47 | { |
48 | QDebugStateSaver saver(dbg); |
49 | dbg.nospace(); |
50 | dbg << "QSqlError(" << s.nativeErrorCode() << ", " << s.driverText() |
51 | << ", " << s.databaseText() << ')'; |
52 | return dbg; |
53 | } |
54 | #endif |
55 | |
56 | class QSqlErrorPrivate |
57 | { |
58 | public: |
59 | QString driverError; |
60 | QString databaseError; |
61 | QSqlError::ErrorType errorType; |
62 | QString errorCode; |
63 | }; |
64 | |
65 | |
66 | /*! |
67 | \class QSqlError |
68 | \brief The QSqlError class provides SQL database error information. |
69 | |
70 | \ingroup database |
71 | \inmodule QtSql |
72 | |
73 | A QSqlError object can provide database-specific error data, |
74 | including the driverText() and databaseText() messages (or both |
75 | concatenated together as text()), and the nativeErrorCode() and |
76 | type(). |
77 | |
78 | \sa QSqlDatabase::lastError(), QSqlQuery::lastError() |
79 | */ |
80 | |
81 | /*! |
82 | \enum QSqlError::ErrorType |
83 | |
84 | This enum type describes the context in which the error occurred, e.g., a connection error, a statement error, etc. |
85 | |
86 | \value NoError No error occurred. |
87 | \value ConnectionError Connection error. |
88 | \value StatementError SQL statement syntax error. |
89 | \value TransactionError Transaction failed error. |
90 | \value UnknownError Unknown error. |
91 | */ |
92 | |
93 | /*! \fn QSqlError::QSqlError(QSqlError &&other) |
94 | Move-constructs a QSqlError instance, making it point at the same |
95 | object that \a other was pointing to. |
96 | |
97 | \note The moved-from object \a other is placed in a |
98 | partially-formed state, in which the only valid operations are |
99 | destruction and assignment of a new value. |
100 | |
101 | \since 5.10 |
102 | */ |
103 | |
104 | /*! \fn QSqlError::operator=(QSqlError &&other) |
105 | Move-assigns \a other to this QSqlError instance. |
106 | |
107 | \note The moved-from object \a other is placed in a |
108 | partially-formed state, in which the only valid operations are |
109 | destruction and assignment of a new value. |
110 | |
111 | \since 5.10 |
112 | */ |
113 | |
114 | /*! \fn QSqlError::swap(QSqlError &other) |
115 | Swaps error \a other with this error. This operation is very fast |
116 | and never fails. |
117 | |
118 | \since 5.10 |
119 | */ |
120 | |
121 | /*! |
122 | Constructs an error containing the driver error text \a |
123 | driverText, the database-specific error text \a databaseText, the |
124 | type \a type and the error code \a code. |
125 | |
126 | \note DB2: It is possible for DB2 to report more than one error code. |
127 | When this happens, \c ; is used as separator between the error codes. |
128 | */ |
129 | QSqlError::QSqlError(const QString &driverText, const QString &databaseText, |
130 | ErrorType type, const QString &code) |
131 | : d(new QSqlErrorPrivate) |
132 | { |
133 | d->driverError = driverText; |
134 | d->databaseError = databaseText; |
135 | d->errorType = type; |
136 | d->errorCode = code; |
137 | } |
138 | |
139 | |
140 | /*! |
141 | Creates a copy of \a other. |
142 | */ |
143 | QSqlError::QSqlError(const QSqlError& other) |
144 | : d(new QSqlErrorPrivate(*other.d)) |
145 | { |
146 | } |
147 | |
148 | /*! |
149 | Assigns the \a other error's values to this error. |
150 | */ |
151 | |
152 | QSqlError& QSqlError::operator=(const QSqlError& other) |
153 | { |
154 | if (d) |
155 | *d = *other.d; |
156 | else |
157 | d = new QSqlErrorPrivate(*other.d); |
158 | return *this; |
159 | } |
160 | |
161 | /*! |
162 | Compare the \a other error's values to this error and returns \c true, if it equal. |
163 | */ |
164 | |
165 | bool QSqlError::operator==(const QSqlError& other) const |
166 | { |
167 | return (d->errorType == other.d->errorType); |
168 | } |
169 | |
170 | |
171 | /*! |
172 | Compare the \a other error's values to this error and returns \c true if it is not equal. |
173 | */ |
174 | |
175 | bool QSqlError::operator!=(const QSqlError& other) const |
176 | { |
177 | return (d->errorType != other.d->errorType); |
178 | } |
179 | |
180 | |
181 | /*! |
182 | Destroys the object and frees any allocated resources. |
183 | */ |
184 | |
185 | QSqlError::~QSqlError() |
186 | { |
187 | delete d; |
188 | } |
189 | |
190 | /*! |
191 | Returns the text of the error as reported by the driver. This may |
192 | contain database-specific descriptions. It may also be empty. |
193 | |
194 | \sa databaseText(), text() |
195 | */ |
196 | QString QSqlError::driverText() const |
197 | { |
198 | return d->driverError; |
199 | } |
200 | |
201 | /*! |
202 | Returns the text of the error as reported by the database. This |
203 | may contain database-specific descriptions; it may be empty. |
204 | |
205 | \sa driverText(), text() |
206 | */ |
207 | |
208 | QString QSqlError::databaseText() const |
209 | { |
210 | return d->databaseError; |
211 | } |
212 | |
213 | /*! |
214 | Returns the error type, or -1 if the type cannot be determined. |
215 | */ |
216 | |
217 | QSqlError::ErrorType QSqlError::type() const |
218 | { |
219 | return d->errorType; |
220 | } |
221 | |
222 | /*! |
223 | Returns the database-specific error code, or an empty string if |
224 | it cannot be determined. |
225 | */ |
226 | |
227 | QString QSqlError::nativeErrorCode() const |
228 | { |
229 | return d->errorCode; |
230 | } |
231 | |
232 | /*! |
233 | This is a convenience function that returns databaseText() and |
234 | driverText() concatenated into a single string. |
235 | |
236 | \sa driverText(), databaseText() |
237 | */ |
238 | |
239 | QString QSqlError::text() const |
240 | { |
241 | QString result = d->databaseError; |
242 | if (!d->databaseError.isEmpty() && !d->driverError.isEmpty() && !d->databaseError.endsWith(QLatin1String("\n" ))) |
243 | result += QLatin1Char(' '); |
244 | result += d->driverError; |
245 | return result; |
246 | } |
247 | |
248 | /*! |
249 | Returns \c true if an error is set, otherwise false. |
250 | |
251 | Example: |
252 | \snippet code/src_sql_kernel_qsqlerror.cpp 0 |
253 | |
254 | \sa type() |
255 | */ |
256 | bool QSqlError::isValid() const |
257 | { |
258 | return d->errorType != NoError; |
259 | } |
260 | |
261 | QT_END_NAMESPACE |
262 | |