Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/kernel/qsqlerror.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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; never executed: return dbg; | 0 | ||||||
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 | /*! | - | ||||||
94 | \obsolete | - | ||||||
95 | - | |||||||
96 | Constructs an error containing the driver error text \a | - | ||||||
97 | driverText, the database-specific error text \a databaseText, the | - | ||||||
98 | type \a type and the optional error number \a number. | - | ||||||
99 | */ | - | ||||||
100 | - | |||||||
101 | #if QT_DEPRECATED_SINCE(5, 3) | - | ||||||
102 | QSqlError::QSqlError(const QString& driverText, const QString& databaseText, ErrorType type, | - | ||||||
103 | int number) | - | ||||||
104 | { | - | ||||||
105 | d = new QSqlErrorPrivate; | - | ||||||
106 | - | |||||||
107 | d->driverError = driverText; | - | ||||||
108 | d->databaseError = databaseText; | - | ||||||
109 | d->errorType = type; | - | ||||||
110 | if (number != -1)
| 2-3 | ||||||
111 | d->errorCode = QString::number(number); executed 2 times by 2 tests: d->errorCode = QString::number(number); Executed by:
| 2 | ||||||
112 | } executed 5 times by 2 tests: end of block Executed by:
| 5 | ||||||
113 | #endif | - | ||||||
114 | - | |||||||
115 | /*! | - | ||||||
116 | Constructs an error containing the driver error text \a | - | ||||||
117 | driverText, the database-specific error text \a databaseText, the | - | ||||||
118 | type \a type and the error code \a code. | - | ||||||
119 | */ | - | ||||||
120 | - | |||||||
121 | QSqlError::QSqlError(const QString &driverText, const QString &databaseText, | - | ||||||
122 | ErrorType type, const QString &code) | - | ||||||
123 | { | - | ||||||
124 | d = new QSqlErrorPrivate; | - | ||||||
125 | - | |||||||
126 | d->driverError = driverText; | - | ||||||
127 | d->databaseError = databaseText; | - | ||||||
128 | d->errorType = type; | - | ||||||
129 | d->errorCode = code; | - | ||||||
130 | } executed 309819 times by 11 tests: end of block Executed by:
| 309819 | ||||||
131 | - | |||||||
132 | - | |||||||
133 | /*! | - | ||||||
134 | Creates a copy of \a other. | - | ||||||
135 | */ | - | ||||||
136 | QSqlError::QSqlError(const QSqlError& other) | - | ||||||
137 | { | - | ||||||
138 | d = new QSqlErrorPrivate; | - | ||||||
139 | - | |||||||
140 | *d = *other.d; | - | ||||||
141 | } executed 247759 times by 10 tests: end of block Executed by:
| 247759 | ||||||
142 | - | |||||||
143 | /*! | - | ||||||
144 | Assigns the \a other error's values to this error. | - | ||||||
145 | */ | - | ||||||
146 | - | |||||||
147 | QSqlError& QSqlError::operator=(const QSqlError& other) | - | ||||||
148 | { | - | ||||||
149 | *d = *other.d; | - | ||||||
150 | return *this; executed 307312 times by 9 tests: return *this; Executed by:
| 307312 | ||||||
151 | } | - | ||||||
152 | - | |||||||
153 | /*! | - | ||||||
154 | Compare the \a other error's values to this error and returns \c true, if it equal. | - | ||||||
155 | */ | - | ||||||
156 | - | |||||||
157 | bool QSqlError::operator==(const QSqlError& other) const | - | ||||||
158 | { | - | ||||||
159 | return (d->errorType == other.d->errorType); executed 1 time by 1 test: return (d->errorType == other.d->errorType); Executed by:
| 1 | ||||||
160 | } | - | ||||||
161 | - | |||||||
162 | - | |||||||
163 | /*! | - | ||||||
164 | Compare the \a other error's values to this error and returns \c true if it is not equal. | - | ||||||
165 | */ | - | ||||||
166 | - | |||||||
167 | bool QSqlError::operator!=(const QSqlError& other) const | - | ||||||
168 | { | - | ||||||
169 | return (d->errorType != other.d->errorType); executed 1 time by 1 test: return (d->errorType != other.d->errorType); Executed by:
| 1 | ||||||
170 | } | - | ||||||
171 | - | |||||||
172 | - | |||||||
173 | /*! | - | ||||||
174 | Destroys the object and frees any allocated resources. | - | ||||||
175 | */ | - | ||||||
176 | - | |||||||
177 | QSqlError::~QSqlError() | - | ||||||
178 | { | - | ||||||
179 | delete d; | - | ||||||
180 | } executed 557585 times by 20 tests: end of block Executed by:
| 557585 | ||||||
181 | - | |||||||
182 | /*! | - | ||||||
183 | Returns the text of the error as reported by the driver. This may | - | ||||||
184 | contain database-specific descriptions. It may also be empty. | - | ||||||
185 | - | |||||||
186 | \sa databaseText(), text() | - | ||||||
187 | */ | - | ||||||
188 | QString QSqlError::driverText() const | - | ||||||
189 | { | - | ||||||
190 | return d->driverError; executed 11603 times by 9 tests: return d->driverError; Executed by:
| 11603 | ||||||
191 | } | - | ||||||
192 | - | |||||||
193 | /*! | - | ||||||
194 | \fn void QSqlError::setDriverText(const QString &driverText) | - | ||||||
195 | \obsolete | - | ||||||
196 | - | |||||||
197 | Sets the driver error text to the value of \a driverText. | - | ||||||
198 | - | |||||||
199 | Use QSqlError(const QString &driverText, const QString &databaseText, | - | ||||||
200 | ErrorType type, int number) instead | - | ||||||
201 | - | |||||||
202 | \sa driverText(), setDatabaseText(), text() | - | ||||||
203 | */ | - | ||||||
204 | - | |||||||
205 | #if QT_DEPRECATED_SINCE(5, 1) | - | ||||||
206 | void QSqlError::setDriverText(const QString& driverText) | - | ||||||
207 | { | - | ||||||
208 | d->driverError = driverText; | - | ||||||
209 | } never executed: end of block | 0 | ||||||
210 | #endif | - | ||||||
211 | - | |||||||
212 | /*! | - | ||||||
213 | Returns the text of the error as reported by the database. This | - | ||||||
214 | may contain database-specific descriptions; it may be empty. | - | ||||||
215 | - | |||||||
216 | \sa driverText(), text() | - | ||||||
217 | */ | - | ||||||
218 | - | |||||||
219 | QString QSqlError::databaseText() const | - | ||||||
220 | { | - | ||||||
221 | return d->databaseError; executed 11571 times by 9 tests: return d->databaseError; Executed by:
| 11571 | ||||||
222 | } | - | ||||||
223 | - | |||||||
224 | /*! | - | ||||||
225 | \fn void QSqlError::setDatabaseText(const QString &databaseText) | - | ||||||
226 | \obsolete | - | ||||||
227 | - | |||||||
228 | Sets the database error text to the value of \a databaseText. | - | ||||||
229 | - | |||||||
230 | Use QSqlError(const QString &driverText, const QString &databaseText, | - | ||||||
231 | ErrorType type, int number) instead | - | ||||||
232 | - | |||||||
233 | \sa databaseText(), setDriverText(), text() | - | ||||||
234 | */ | - | ||||||
235 | - | |||||||
236 | #if QT_DEPRECATED_SINCE(5, 1) | - | ||||||
237 | void QSqlError::setDatabaseText(const QString& databaseText) | - | ||||||
238 | { | - | ||||||
239 | d->databaseError = databaseText; | - | ||||||
240 | } never executed: end of block | 0 | ||||||
241 | #endif | - | ||||||
242 | - | |||||||
243 | /*! | - | ||||||
244 | Returns the error type, or -1 if the type cannot be determined. | - | ||||||
245 | */ | - | ||||||
246 | - | |||||||
247 | QSqlError::ErrorType QSqlError::type() const | - | ||||||
248 | { | - | ||||||
249 | return d->errorType; executed 22 times by 3 tests: return d->errorType; Executed by:
| 22 | ||||||
250 | } | - | ||||||
251 | - | |||||||
252 | /*! | - | ||||||
253 | \fn void QSqlError::setType(ErrorType type) | - | ||||||
254 | \obsolete | - | ||||||
255 | - | |||||||
256 | Sets the error type to the value of \a type. | - | ||||||
257 | - | |||||||
258 | Use QSqlError(const QString &driverText, const QString &databaseText, | - | ||||||
259 | ErrorType type, int number) instead | - | ||||||
260 | - | |||||||
261 | \sa type() | - | ||||||
262 | */ | - | ||||||
263 | - | |||||||
264 | #if QT_DEPRECATED_SINCE(5, 1) | - | ||||||
265 | void QSqlError::setType(ErrorType type) | - | ||||||
266 | { | - | ||||||
267 | d->errorType = type; | - | ||||||
268 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||
269 | #endif | - | ||||||
270 | - | |||||||
271 | /*! | - | ||||||
272 | \fn int QSqlError::number() const | - | ||||||
273 | \obsolete | - | ||||||
274 | - | |||||||
275 | Returns the database-specific error number, or -1 if it cannot be | - | ||||||
276 | determined. | - | ||||||
277 | - | |||||||
278 | Returns 0 if the error code is not an integer. | - | ||||||
279 | - | |||||||
280 | \warning Some databases use alphanumeric error codes, which makes | - | ||||||
281 | number() unreliable if such a database is used. | - | ||||||
282 | - | |||||||
283 | Use nativeErrorCode() instead | - | ||||||
284 | - | |||||||
285 | \sa nativeErrorCode() | - | ||||||
286 | */ | - | ||||||
287 | - | |||||||
288 | #if QT_DEPRECATED_SINCE(5, 3) | - | ||||||
289 | int QSqlError::number() const | - | ||||||
290 | { | - | ||||||
291 | return d->errorCode.isEmpty() ? -1 : d->errorCode.toInt(); executed 12 times by 1 test: return d->errorCode.isEmpty() ? -1 : d->errorCode.toInt(); Executed by:
| 12 | ||||||
292 | } | - | ||||||
293 | #endif | - | ||||||
294 | - | |||||||
295 | /*! | - | ||||||
296 | \fn void QSqlError::setNumber(int number) | - | ||||||
297 | \obsolete | - | ||||||
298 | - | |||||||
299 | Sets the database-specific error number to \a number. | - | ||||||
300 | - | |||||||
301 | Use QSqlError(const QString &driverText, const QString &databaseText, | - | ||||||
302 | ErrorType type, int number) instead | - | ||||||
303 | - | |||||||
304 | \sa number() | - | ||||||
305 | */ | - | ||||||
306 | - | |||||||
307 | #if QT_DEPRECATED_SINCE(5, 1) | - | ||||||
308 | void QSqlError::setNumber(int number) | - | ||||||
309 | { | - | ||||||
310 | d->errorCode = QString::number(number); | - | ||||||
311 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
312 | #endif | - | ||||||
313 | - | |||||||
314 | /*! | - | ||||||
315 | Returns the database-specific error code, or an empty string if | - | ||||||
316 | it cannot be determined. | - | ||||||
317 | */ | - | ||||||
318 | - | |||||||
319 | QString QSqlError::nativeErrorCode() const | - | ||||||
320 | { | - | ||||||
321 | return d->errorCode; executed 11599 times by 9 tests: return d->errorCode; Executed by:
| 11599 | ||||||
322 | } | - | ||||||
323 | - | |||||||
324 | /*! | - | ||||||
325 | This is a convenience function that returns databaseText() and | - | ||||||
326 | driverText() concatenated into a single string. | - | ||||||
327 | - | |||||||
328 | \sa driverText(), databaseText() | - | ||||||
329 | */ | - | ||||||
330 | - | |||||||
331 | QString QSqlError::text() const | - | ||||||
332 | { | - | ||||||
333 | QString result = d->databaseError; | - | ||||||
334 | if (!d->databaseError.endsWith(QLatin1String("\n")))
| 0 | ||||||
335 | result += QLatin1Char(' '); never executed: result += QLatin1Char(' '); | 0 | ||||||
336 | result += d->driverError; | - | ||||||
337 | return result; never executed: return result; | 0 | ||||||
338 | } | - | ||||||
339 | - | |||||||
340 | /*! | - | ||||||
341 | Returns \c true if an error is set, otherwise false. | - | ||||||
342 | - | |||||||
343 | Example: | - | ||||||
344 | \snippet code/src_sql_kernel_qsqlerror.cpp 0 | - | ||||||
345 | - | |||||||
346 | \sa type() | - | ||||||
347 | */ | - | ||||||
348 | bool QSqlError::isValid() const | - | ||||||
349 | { | - | ||||||
350 | return d->errorType != NoError; executed 236165 times by 10 tests: return d->errorType != NoError; Executed by:
| 236165 | ||||||
351 | } | - | ||||||
352 | - | |||||||
353 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |