1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qsqlerror.h" |
5 | #include "qdebug.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | using namespace Qt::StringLiterals; |
10 | |
11 | #ifndef QT_NO_DEBUG_STREAM |
12 | QDebug operator<<(QDebug dbg, const QSqlError &s) |
13 | { |
14 | QDebugStateSaver saver(dbg); |
15 | dbg.nospace(); |
16 | dbg << "QSqlError(" << s.nativeErrorCode() << ", " << s.driverText() |
17 | << ", " << s.databaseText() << ')'; |
18 | return dbg; |
19 | } |
20 | #endif |
21 | |
22 | class QSqlErrorPrivate : public QSharedData |
23 | { |
24 | public: |
25 | QString driverError; |
26 | QString databaseError; |
27 | QSqlError::ErrorType errorType; |
28 | QString errorCode; |
29 | }; |
30 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlErrorPrivate) |
31 | |
32 | /*! |
33 | \class QSqlError |
34 | \brief The QSqlError class provides SQL database error information. |
35 | |
36 | \ingroup database |
37 | \inmodule QtSql |
38 | |
39 | A QSqlError object can provide database-specific error data, |
40 | including the driverText() and databaseText() messages (or both |
41 | concatenated together as text()), and the nativeErrorCode() and |
42 | type(). |
43 | |
44 | \sa QSqlDatabase::lastError(), QSqlQuery::lastError() |
45 | */ |
46 | |
47 | /*! |
48 | \enum QSqlError::ErrorType |
49 | |
50 | This enum type describes the context in which the error occurred, e.g., a connection error, a statement error, etc. |
51 | |
52 | \value NoError No error occurred. |
53 | \value ConnectionError Connection error. |
54 | \value StatementError SQL statement syntax error. |
55 | \value TransactionError Transaction failed error. |
56 | \value UnknownError Unknown error. |
57 | */ |
58 | |
59 | /*! \fn QSqlError::QSqlError(QSqlError &&other) |
60 | Move-constructs a QSqlError instance, making it point at the same |
61 | object that \a other was pointing to. |
62 | |
63 | \note The moved-from object \a other is placed in a |
64 | partially-formed state, in which the only valid operations are |
65 | destruction and assignment of a new value. |
66 | |
67 | \since 5.10 |
68 | */ |
69 | |
70 | /*! \fn QSqlError::operator=(QSqlError &&other) |
71 | Move-assigns \a other to this QSqlError instance. |
72 | |
73 | \note The moved-from object \a other is placed in a |
74 | partially-formed state, in which the only valid operations are |
75 | destruction and assignment of a new value. |
76 | |
77 | \since 5.10 |
78 | */ |
79 | |
80 | /*! \fn QSqlError::swap(QSqlError &other) |
81 | \memberswap{error} |
82 | \since 5.10 |
83 | */ |
84 | |
85 | /*! |
86 | Constructs an error containing the driver error text \a |
87 | driverText, the database-specific error text \a databaseText, the |
88 | type \a type and the error code \a code. |
89 | */ |
90 | QSqlError::QSqlError(const QString &driverText, const QString &databaseText, |
91 | ErrorType type, const QString &code) |
92 | : d(new QSqlErrorPrivate) |
93 | { |
94 | d->driverError = driverText; |
95 | d->databaseError = databaseText; |
96 | d->errorType = type; |
97 | d->errorCode = code; |
98 | } |
99 | |
100 | |
101 | /*! |
102 | Creates a copy of \a other. |
103 | */ |
104 | QSqlError::QSqlError(const QSqlError &other) |
105 | = default; |
106 | |
107 | /*! |
108 | Assigns the \a other error's values to this error. |
109 | */ |
110 | |
111 | QSqlError &QSqlError::operator=(const QSqlError &other) |
112 | = default; |
113 | |
114 | |
115 | /*! |
116 | Compare the \a other error's type() and nativeErrorCode() |
117 | to this error and returns \c true, if it equal. |
118 | */ |
119 | |
120 | bool QSqlError::operator==(const QSqlError &other) const |
121 | { |
122 | return (d->errorType == other.d->errorType && |
123 | d->errorCode == other.d->errorCode); |
124 | } |
125 | |
126 | |
127 | /*! |
128 | Compare the \a other error's type() and nativeErrorCode() |
129 | to this error and returns \c true if it is not equal. |
130 | */ |
131 | |
132 | bool QSqlError::operator!=(const QSqlError &other) const |
133 | { |
134 | return (d->errorType != other.d->errorType || |
135 | d->errorCode != other.d->errorCode); |
136 | } |
137 | |
138 | |
139 | /*! |
140 | Destroys the object and frees any allocated resources. |
141 | */ |
142 | |
143 | QSqlError::~QSqlError() |
144 | = default; |
145 | |
146 | /*! |
147 | Returns the text of the error as reported by the driver. This may |
148 | contain database-specific descriptions. It may also be empty. |
149 | |
150 | \sa databaseText(), text() |
151 | */ |
152 | QString QSqlError::driverText() const |
153 | { |
154 | return d->driverError; |
155 | } |
156 | |
157 | /*! |
158 | Returns the text of the error as reported by the database. This |
159 | may contain database-specific descriptions; it may be empty. |
160 | |
161 | \sa driverText(), text() |
162 | */ |
163 | |
164 | QString QSqlError::databaseText() const |
165 | { |
166 | return d->databaseError; |
167 | } |
168 | |
169 | /*! |
170 | Returns the error type, or -1 if the type cannot be determined. |
171 | */ |
172 | |
173 | QSqlError::ErrorType QSqlError::type() const |
174 | { |
175 | return d->errorType; |
176 | } |
177 | |
178 | /*! |
179 | Returns the database-specific error code, or an empty string if |
180 | it cannot be determined. |
181 | \note Some drivers (like DB2 or ODBC) may return more than one |
182 | error code. When this happens, \c ; is used as separator between |
183 | the error codes. |
184 | */ |
185 | |
186 | QString QSqlError::nativeErrorCode() const |
187 | { |
188 | return d->errorCode; |
189 | } |
190 | |
191 | /*! |
192 | This is a convenience function that returns databaseText() and |
193 | driverText() concatenated into a single string. |
194 | |
195 | \sa driverText(), databaseText() |
196 | */ |
197 | |
198 | QString QSqlError::text() const |
199 | { |
200 | QString result = d->databaseError; |
201 | if (!d->databaseError.isEmpty() && !d->driverError.isEmpty() && !d->databaseError.endsWith(c: u'\n')) |
202 | result += u' '; |
203 | result += d->driverError; |
204 | return result; |
205 | } |
206 | |
207 | /*! |
208 | Returns \c true if an error is set, otherwise false. |
209 | |
210 | Example: |
211 | \snippet code/src_sql_kernel_qsqlerror.cpp 0 |
212 | |
213 | \sa type() |
214 | */ |
215 | bool QSqlError::isValid() const |
216 | { |
217 | return d->errorType != NoError; |
218 | } |
219 | |
220 | QT_END_NAMESPACE |
221 | |