1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtSql module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qsqldriver.h"
|
---|
43 |
|
---|
44 | #include "qdatetime.h"
|
---|
45 | #include "qsqlerror.h"
|
---|
46 | #include "qsqlfield.h"
|
---|
47 | #include "qsqlindex.h"
|
---|
48 | #include "private/qobject_p.h"
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | class QSqlDriverPrivate : public QObjectPrivate
|
---|
53 | {
|
---|
54 | public:
|
---|
55 | QSqlDriverPrivate();
|
---|
56 | virtual ~QSqlDriverPrivate();
|
---|
57 |
|
---|
58 | public:
|
---|
59 | // @CHECK: this member is never used. It was named q, which expanded to q_func().
|
---|
60 | QSqlDriver *q_func();
|
---|
61 | uint isOpen : 1;
|
---|
62 | uint isOpenError : 1;
|
---|
63 | QSqlError error;
|
---|
64 | };
|
---|
65 |
|
---|
66 | inline QSqlDriverPrivate::QSqlDriverPrivate()
|
---|
67 | : QObjectPrivate(), isOpen(false), isOpenError(false)
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 | QSqlDriverPrivate::~QSqlDriverPrivate()
|
---|
72 | {
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*!
|
---|
76 | \class QSqlDriver
|
---|
77 | \brief The QSqlDriver class is an abstract base class for accessing
|
---|
78 | specific SQL databases.
|
---|
79 |
|
---|
80 | \ingroup database
|
---|
81 | \inmodule QtSql
|
---|
82 |
|
---|
83 | This class should not be used directly. Use QSqlDatabase instead.
|
---|
84 |
|
---|
85 | If you want to create your own SQL drivers, you can subclass this
|
---|
86 | class and reimplement its pure virtual functions and those
|
---|
87 | virtual functions that you need. See \l{How to Write Your Own
|
---|
88 | Database Driver} for more information.
|
---|
89 |
|
---|
90 | \sa QSqlDatabase, QSqlResult
|
---|
91 | */
|
---|
92 |
|
---|
93 | /*!
|
---|
94 | Constructs a new driver with the given \a parent.
|
---|
95 | */
|
---|
96 |
|
---|
97 | QSqlDriver::QSqlDriver(QObject *parent)
|
---|
98 | : QObject(*new QSqlDriverPrivate, parent)
|
---|
99 | {
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*!
|
---|
103 | Destroys the object and frees any allocated resources.
|
---|
104 | */
|
---|
105 |
|
---|
106 | QSqlDriver::~QSqlDriver()
|
---|
107 | {
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*!
|
---|
111 | \since 4.4
|
---|
112 |
|
---|
113 | \fn QSqlDriver::notification(const QString &name)
|
---|
114 |
|
---|
115 | This signal is emitted when the database posts an event notification
|
---|
116 | that the driver subscribes to. \a name identifies the event notification.
|
---|
117 |
|
---|
118 | \sa subscribeToNotification()
|
---|
119 | */
|
---|
120 |
|
---|
121 | /*!
|
---|
122 | \fn bool QSqlDriver::open(const QString &db, const QString &user, const QString& password,
|
---|
123 | const QString &host, int port, const QString &options)
|
---|
124 |
|
---|
125 | Derived classes must reimplement this pure virtual function to
|
---|
126 | open a database connection on database \a db, using user name \a
|
---|
127 | user, password \a password, host \a host, port \a port and
|
---|
128 | connection options \a options.
|
---|
129 |
|
---|
130 | The function must return true on success and false on failure.
|
---|
131 |
|
---|
132 | \sa setOpen()
|
---|
133 | */
|
---|
134 |
|
---|
135 | /*!
|
---|
136 | \fn bool QSqlDriver::close()
|
---|
137 |
|
---|
138 | Derived classes must reimplement this pure virtual function in
|
---|
139 | order to close the database connection. Return true on success,
|
---|
140 | false on failure.
|
---|
141 |
|
---|
142 | \sa open(), setOpen()
|
---|
143 | */
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | \fn QSqlResult *QSqlDriver::createResult() const
|
---|
147 |
|
---|
148 | Creates an empty SQL result on the database. Derived classes must
|
---|
149 | reimplement this function and return a QSqlResult object
|
---|
150 | appropriate for their database to the caller.
|
---|
151 | */
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | Returns true if the database connection is open; otherwise returns
|
---|
155 | false.
|
---|
156 | */
|
---|
157 |
|
---|
158 | bool QSqlDriver::isOpen() const
|
---|
159 | {
|
---|
160 | return d_func()->isOpen;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | Returns true if the there was an error opening the database
|
---|
165 | connection; otherwise returns false.
|
---|
166 | */
|
---|
167 |
|
---|
168 | bool QSqlDriver::isOpenError() const
|
---|
169 | {
|
---|
170 | return d_func()->isOpenError;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /*!
|
---|
174 | \enum QSqlDriver::DriverFeature
|
---|
175 |
|
---|
176 | This enum contains a list of features a driver might support. Use
|
---|
177 | hasFeature() to query whether a feature is supported or not.
|
---|
178 |
|
---|
179 | \value Transactions Whether the driver supports SQL transactions.
|
---|
180 | \value QuerySize Whether the database is capable of reporting the size
|
---|
181 | of a query. Note that some databases do not support returning the size
|
---|
182 | (i.e. number of rows returned) of a query, in which case
|
---|
183 | QSqlQuery::size() will return -1.
|
---|
184 | \value BLOB Whether the driver supports Binary Large Object fields.
|
---|
185 | \value Unicode Whether the driver supports Unicode strings if the
|
---|
186 | database server does.
|
---|
187 | \value PreparedQueries Whether the driver supports prepared query execution.
|
---|
188 | \value NamedPlaceholders Whether the driver supports the use of named placeholders.
|
---|
189 | \value PositionalPlaceholders Whether the driver supports the use of positional placeholders.
|
---|
190 | \value LastInsertId Whether the driver supports returning the Id of the last touched row.
|
---|
191 | \value BatchOperations Whether the driver supports batched operations, see QSqlQuery::execBatch()
|
---|
192 | \value SimpleLocking Whether the driver disallows a write lock on a table while other queries have a read lock on it.
|
---|
193 | \value LowPrecisionNumbers Whether the driver allows fetching numerical values with low precision.
|
---|
194 | \value EventNotifications Whether the driver supports database event notifications.
|
---|
195 | \value FinishQuery Whether the driver can do any low-level resource cleanup when QSqlQuery::finish() is called.
|
---|
196 | \value MultipleResultSets Whether the driver can access multiple result sets returned from batched statements or stored procedures.
|
---|
197 |
|
---|
198 | More information about supported features can be found in the
|
---|
199 | \l{sql-driver.html}{Qt SQL driver} documentation.
|
---|
200 |
|
---|
201 | \sa hasFeature()
|
---|
202 | */
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | \enum QSqlDriver::StatementType
|
---|
206 |
|
---|
207 | This enum contains a list of SQL statement (or clause) types the
|
---|
208 | driver can create.
|
---|
209 |
|
---|
210 | \value WhereStatement An SQL \c WHERE statement (e.g., \c{WHERE f = 5}).
|
---|
211 | \value SelectStatement An SQL \c SELECT statement (e.g., \c{SELECT f FROM t}).
|
---|
212 | \value UpdateStatement An SQL \c UPDATE statement (e.g., \c{UPDATE TABLE t set f = 1}).
|
---|
213 | \value InsertStatement An SQL \c INSERT statement (e.g., \c{INSERT INTO t (f) values (1)}).
|
---|
214 | \value DeleteStatement An SQL \c DELETE statement (e.g., \c{DELETE FROM t}).
|
---|
215 |
|
---|
216 | \sa sqlStatement()
|
---|
217 | */
|
---|
218 |
|
---|
219 | /*!
|
---|
220 | \enum QSqlDriver::IdentifierType
|
---|
221 |
|
---|
222 | This enum contains a list of SQL identifier types.
|
---|
223 |
|
---|
224 | \value FieldName A SQL field name
|
---|
225 | \value TableName A SQL table name
|
---|
226 | */
|
---|
227 |
|
---|
228 | /*!
|
---|
229 | \fn bool QSqlDriver::hasFeature(DriverFeature feature) const
|
---|
230 |
|
---|
231 | Returns true if the driver supports feature \a feature; otherwise
|
---|
232 | returns false.
|
---|
233 |
|
---|
234 | Note that some databases need to be open() before this can be
|
---|
235 | determined.
|
---|
236 |
|
---|
237 | \sa DriverFeature
|
---|
238 | */
|
---|
239 |
|
---|
240 | /*!
|
---|
241 | This function sets the open state of the database to \a open.
|
---|
242 | Derived classes can use this function to report the status of
|
---|
243 | open().
|
---|
244 |
|
---|
245 | \sa open(), setOpenError()
|
---|
246 | */
|
---|
247 |
|
---|
248 | void QSqlDriver::setOpen(bool open)
|
---|
249 | {
|
---|
250 | d_func()->isOpen = open;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*!
|
---|
254 | This function sets the open error state of the database to \a
|
---|
255 | error. Derived classes can use this function to report the status
|
---|
256 | of open(). Note that if \a error is true the open state of the
|
---|
257 | database is set to closed (i.e., isOpen() returns false).
|
---|
258 |
|
---|
259 | \sa open(), setOpen()
|
---|
260 | */
|
---|
261 |
|
---|
262 | void QSqlDriver::setOpenError(bool error)
|
---|
263 | {
|
---|
264 | d_func()->isOpenError = error;
|
---|
265 | if (error)
|
---|
266 | d_func()->isOpen = false;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /*!
|
---|
270 | This function is called to begin a transaction. If successful,
|
---|
271 | return true, otherwise return false. The default implementation
|
---|
272 | does nothing and returns false.
|
---|
273 |
|
---|
274 | \sa commitTransaction(), rollbackTransaction()
|
---|
275 | */
|
---|
276 |
|
---|
277 | bool QSqlDriver::beginTransaction()
|
---|
278 | {
|
---|
279 | return false;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*!
|
---|
283 | This function is called to commit a transaction. If successful,
|
---|
284 | return true, otherwise return false. The default implementation
|
---|
285 | does nothing and returns false.
|
---|
286 |
|
---|
287 | \sa beginTransaction(), rollbackTransaction()
|
---|
288 | */
|
---|
289 |
|
---|
290 | bool QSqlDriver::commitTransaction()
|
---|
291 | {
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*!
|
---|
296 | This function is called to rollback a transaction. If successful,
|
---|
297 | return true, otherwise return false. The default implementation
|
---|
298 | does nothing and returns false.
|
---|
299 |
|
---|
300 | \sa beginTransaction(), commitTransaction()
|
---|
301 | */
|
---|
302 |
|
---|
303 | bool QSqlDriver::rollbackTransaction()
|
---|
304 | {
|
---|
305 | return false;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /*!
|
---|
309 | This function is used to set the value of the last error, \a error,
|
---|
310 | that occurred on the database.
|
---|
311 |
|
---|
312 | \sa lastError()
|
---|
313 | */
|
---|
314 |
|
---|
315 | void QSqlDriver::setLastError(const QSqlError &error)
|
---|
316 | {
|
---|
317 | d_func()->error = error;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /*!
|
---|
321 | Returns a QSqlError object which contains information about the
|
---|
322 | last error that occurred on the database.
|
---|
323 | */
|
---|
324 |
|
---|
325 | QSqlError QSqlDriver::lastError() const
|
---|
326 | {
|
---|
327 | return d_func()->error;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /*!
|
---|
331 | Returns a list of the names of the tables in the database. The
|
---|
332 | default implementation returns an empty list.
|
---|
333 |
|
---|
334 | The \a tableType argument describes what types of tables
|
---|
335 | should be returned. Due to binary compatibility, the string
|
---|
336 | contains the value of the enum QSql::TableTypes as text.
|
---|
337 | An empty string should be treated as QSql::Tables for
|
---|
338 | backward compatibility.
|
---|
339 | */
|
---|
340 |
|
---|
341 | QStringList QSqlDriver::tables(QSql::TableType) const
|
---|
342 | {
|
---|
343 | return QStringList();
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*!
|
---|
347 | Returns the primary index for table \a tableName. Returns an empty
|
---|
348 | QSqlIndex if the table doesn't have a primary index. The default
|
---|
349 | implementation returns an empty index.
|
---|
350 | */
|
---|
351 |
|
---|
352 | QSqlIndex QSqlDriver::primaryIndex(const QString&) const
|
---|
353 | {
|
---|
354 | return QSqlIndex();
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /*!
|
---|
359 | Returns a QSqlRecord populated with the names of the fields in
|
---|
360 | table \a tableName. If no such table exists, an empty record is
|
---|
361 | returned. The default implementation returns an empty record.
|
---|
362 | */
|
---|
363 |
|
---|
364 | QSqlRecord QSqlDriver::record(const QString & /* tableName */) const
|
---|
365 | {
|
---|
366 | return QSqlRecord();
|
---|
367 | }
|
---|
368 |
|
---|
369 | /*!
|
---|
370 | Returns the \a identifier escaped according to the database rules.
|
---|
371 | \a identifier can either be a table name or field name, dependent
|
---|
372 | on \a type.
|
---|
373 |
|
---|
374 | The default implementation does nothing.
|
---|
375 | */
|
---|
376 | QString QSqlDriver::escapeIdentifier(const QString &identifier, IdentifierType) const
|
---|
377 | {
|
---|
378 | return identifier;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /*!
|
---|
382 | Returns a SQL statement of type \a type for the table \a tableName
|
---|
383 | with the values from \a rec. If \a preparedStatement is true, the
|
---|
384 | string will contain placeholders instead of values.
|
---|
385 |
|
---|
386 | This method can be used to manipulate tables without having to worry
|
---|
387 | about database-dependent SQL dialects. For non-prepared statements,
|
---|
388 | the values will be properly escaped.
|
---|
389 | */
|
---|
390 | QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
|
---|
391 | const QSqlRecord &rec, bool preparedStatement) const
|
---|
392 | {
|
---|
393 | int i;
|
---|
394 | QString s;
|
---|
395 | s.reserve(128);
|
---|
396 | switch (type) {
|
---|
397 | case SelectStatement:
|
---|
398 | for (i = 0; i < rec.count(); ++i) {
|
---|
399 | if (rec.isGenerated(i))
|
---|
400 | s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", "));
|
---|
401 | }
|
---|
402 | if (s.isEmpty())
|
---|
403 | return s;
|
---|
404 | s.chop(2);
|
---|
405 | s.prepend(QLatin1String("SELECT ")).append(QLatin1String(" FROM ")).append(escapeIdentifier(tableName, TableName));
|
---|
406 | break;
|
---|
407 | case WhereStatement:
|
---|
408 | if (preparedStatement) {
|
---|
409 | for (int i = 0; i < rec.count(); ++i)
|
---|
410 | s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(
|
---|
411 | QLatin1String(" = ? AND "));
|
---|
412 | } else {
|
---|
413 | for (i = 0; i < rec.count(); ++i) {
|
---|
414 | s.append(escapeIdentifier(rec.fieldName(i), FieldName));
|
---|
415 | QString val = formatValue(rec.field(i));
|
---|
416 | if (val == QLatin1String("NULL"))
|
---|
417 | s.append(QLatin1String(" IS NULL"));
|
---|
418 | else
|
---|
419 | s.append(QLatin1String(" = ")).append(val);
|
---|
420 | s.append(QLatin1String(" AND "));
|
---|
421 | }
|
---|
422 | }
|
---|
423 | if (!s.isEmpty()) {
|
---|
424 | s.prepend(QLatin1String("WHERE "));
|
---|
425 | s.chop(5); // remove tailing AND
|
---|
426 | }
|
---|
427 | break;
|
---|
428 | case UpdateStatement:
|
---|
429 | s.append(QLatin1String("UPDATE ")).append(escapeIdentifier(tableName, TableName)).append(
|
---|
430 | QLatin1String(" SET "));
|
---|
431 | for (i = 0; i < rec.count(); ++i) {
|
---|
432 | if (!rec.isGenerated(i) || !rec.value(i).isValid())
|
---|
433 | continue;
|
---|
434 | s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1Char('='));
|
---|
435 | if (preparedStatement)
|
---|
436 | s.append(QLatin1Char('?'));
|
---|
437 | else
|
---|
438 | s.append(formatValue(rec.field(i)));
|
---|
439 | s.append(QLatin1String(", "));
|
---|
440 | }
|
---|
441 | if (s.endsWith(QLatin1String(", ")))
|
---|
442 | s.chop(2);
|
---|
443 | else
|
---|
444 | s.clear();
|
---|
445 | break;
|
---|
446 | case DeleteStatement:
|
---|
447 | s.append(QLatin1String("DELETE FROM ")).append(escapeIdentifier(tableName, TableName));
|
---|
448 | break;
|
---|
449 | case InsertStatement: {
|
---|
450 | s.append(QLatin1String("INSERT INTO ")).append(escapeIdentifier(tableName, TableName)).append(QLatin1String(" ("));
|
---|
451 | QString vals;
|
---|
452 | for (i = 0; i < rec.count(); ++i) {
|
---|
453 | if (!rec.isGenerated(i) || !rec.value(i).isValid())
|
---|
454 | continue;
|
---|
455 | s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", "));
|
---|
456 | if (preparedStatement)
|
---|
457 | vals.append(QLatin1String("?"));
|
---|
458 | else
|
---|
459 | vals.append(formatValue(rec.field(i)));
|
---|
460 | vals.append(QLatin1String(", "));
|
---|
461 | }
|
---|
462 | if (vals.isEmpty()) {
|
---|
463 | s.clear();
|
---|
464 | } else {
|
---|
465 | vals.chop(2); // remove trailing comma
|
---|
466 | s[s.length() - 2] = QLatin1Char(')');
|
---|
467 | s.append(QLatin1String("VALUES (")).append(vals).append(QLatin1String(")"));
|
---|
468 | }
|
---|
469 | break; }
|
---|
470 | }
|
---|
471 | return s;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /*!
|
---|
475 | Returns a string representation of the \a field value for the
|
---|
476 | database. This is used, for example, when constructing INSERT and
|
---|
477 | UPDATE statements.
|
---|
478 |
|
---|
479 | The default implementation returns the value formatted as a string
|
---|
480 | according to the following rules:
|
---|
481 |
|
---|
482 | \list
|
---|
483 |
|
---|
484 | \i If \a field is character data, the value is returned enclosed
|
---|
485 | in single quotation marks, which is appropriate for many SQL
|
---|
486 | databases. Any embedded single-quote characters are escaped
|
---|
487 | (replaced with two single-quote characters). If \a trimStrings is
|
---|
488 | true (the default is false), all trailing whitespace is trimmed
|
---|
489 | from the field.
|
---|
490 |
|
---|
491 | \i If \a field is date/time data, the value is formatted in ISO
|
---|
492 | format and enclosed in single quotation marks. If the date/time
|
---|
493 | data is invalid, "NULL" is returned.
|
---|
494 |
|
---|
495 | \i If \a field is \link QByteArray bytearray\endlink data, and the
|
---|
496 | driver can edit binary fields, the value is formatted as a
|
---|
497 | hexadecimal string.
|
---|
498 |
|
---|
499 | \i For any other field type, toString() is called on its value
|
---|
500 | and the result of this is returned.
|
---|
501 |
|
---|
502 | \endlist
|
---|
503 |
|
---|
504 | \sa QVariant::toString()
|
---|
505 |
|
---|
506 | */
|
---|
507 | QString QSqlDriver::formatValue(const QSqlField &field, bool trimStrings) const
|
---|
508 | {
|
---|
509 | const QLatin1String nullTxt("NULL");
|
---|
510 |
|
---|
511 | QString r;
|
---|
512 | if (field.isNull())
|
---|
513 | r = nullTxt;
|
---|
514 | else {
|
---|
515 | switch (field.type()) {
|
---|
516 | case QVariant::Int:
|
---|
517 | case QVariant::UInt:
|
---|
518 | if (field.value().type() == QVariant::Bool)
|
---|
519 | r = field.value().toBool() ? QLatin1String("1") : QLatin1String("0");
|
---|
520 | else
|
---|
521 | r = field.value().toString();
|
---|
522 | break;
|
---|
523 | #ifndef QT_NO_DATESTRING
|
---|
524 | case QVariant::Date:
|
---|
525 | if (field.value().toDate().isValid())
|
---|
526 | r = QLatin1Char('\'') + field.value().toDate().toString(Qt::ISODate)
|
---|
527 | + QLatin1Char('\'');
|
---|
528 | else
|
---|
529 | r = nullTxt;
|
---|
530 | break;
|
---|
531 | case QVariant::Time:
|
---|
532 | if (field.value().toTime().isValid())
|
---|
533 | r = QLatin1Char('\'') + field.value().toTime().toString(Qt::ISODate)
|
---|
534 | + QLatin1Char('\'');
|
---|
535 | else
|
---|
536 | r = nullTxt;
|
---|
537 | break;
|
---|
538 | case QVariant::DateTime:
|
---|
539 | if (field.value().toDateTime().isValid())
|
---|
540 | r = QLatin1Char('\'') +
|
---|
541 | field.value().toDateTime().toString(Qt::ISODate) + QLatin1Char('\'');
|
---|
542 | else
|
---|
543 | r = nullTxt;
|
---|
544 | break;
|
---|
545 | #endif
|
---|
546 | case QVariant::String:
|
---|
547 | case QVariant::Char:
|
---|
548 | {
|
---|
549 | QString result = field.value().toString();
|
---|
550 | if (trimStrings) {
|
---|
551 | int end = result.length();
|
---|
552 | while (end && result.at(end-1).isSpace()) /* skip white space from end */
|
---|
553 | end--;
|
---|
554 | result.truncate(end);
|
---|
555 | }
|
---|
556 | /* escape the "'" character */
|
---|
557 | result.replace(QLatin1Char('\''), QLatin1String("''"));
|
---|
558 | r = QLatin1Char('\'') + result + QLatin1Char('\'');
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | case QVariant::Bool:
|
---|
562 | if (field.value().toBool())
|
---|
563 | r = QLatin1String("1");
|
---|
564 | else
|
---|
565 | r = QLatin1String("0");
|
---|
566 | break;
|
---|
567 | case QVariant::ByteArray : {
|
---|
568 | if (hasFeature(BLOB)) {
|
---|
569 | QByteArray ba = field.value().toByteArray();
|
---|
570 | QString res;
|
---|
571 | static const char hexchars[] = "0123456789abcdef";
|
---|
572 | for (int i = 0; i < ba.size(); ++i) {
|
---|
573 | uchar s = (uchar) ba[i];
|
---|
574 | res += QLatin1Char(hexchars[s >> 4]);
|
---|
575 | res += QLatin1Char(hexchars[s & 0x0f]);
|
---|
576 | }
|
---|
577 | r = QLatin1Char('\'') + res + QLatin1Char('\'');
|
---|
578 | break;
|
---|
579 | }
|
---|
580 | }
|
---|
581 | default:
|
---|
582 | r = field.value().toString();
|
---|
583 | break;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | return r;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /*!
|
---|
590 | Returns the low-level database handle wrapped in a QVariant or an
|
---|
591 | invalid variant if there is no handle.
|
---|
592 |
|
---|
593 | \warning Use this with uttermost care and only if you know what you're doing.
|
---|
594 |
|
---|
595 | \warning The handle returned here can become a stale pointer if the connection
|
---|
596 | is modified (for example, if you close the connection).
|
---|
597 |
|
---|
598 | \warning The handle can be NULL if the connection is not open yet.
|
---|
599 |
|
---|
600 | The handle returned here is database-dependent, you should query the type
|
---|
601 | name of the variant before accessing it.
|
---|
602 |
|
---|
603 | This example retrieves the handle for a connection to sqlite:
|
---|
604 |
|
---|
605 | \snippet doc/src/snippets/code/src_sql_kernel_qsqldriver.cpp 0
|
---|
606 |
|
---|
607 | This snippet returns the handle for PostgreSQL or MySQL:
|
---|
608 |
|
---|
609 | \snippet doc/src/snippets/code/src_sql_kernel_qsqldriver.cpp 1
|
---|
610 |
|
---|
611 | \sa QSqlResult::handle()
|
---|
612 | */
|
---|
613 | QVariant QSqlDriver::handle() const
|
---|
614 | {
|
---|
615 | return QVariant();
|
---|
616 | }
|
---|
617 |
|
---|
618 | /*!
|
---|
619 | \fn QSqlRecord QSqlDriver::record(const QSqlQuery& query) const
|
---|
620 |
|
---|
621 | Use query.record() instead.
|
---|
622 | */
|
---|
623 |
|
---|
624 | /*!
|
---|
625 | \fn QSqlRecord QSqlDriver::recordInfo(const QString& tablename) const
|
---|
626 |
|
---|
627 | Use record() instead.
|
---|
628 | */
|
---|
629 |
|
---|
630 | /*!
|
---|
631 | \fn QSqlRecord QSqlDriver::recordInfo(const QSqlQuery& query) const
|
---|
632 |
|
---|
633 | Use query.record() instead.
|
---|
634 | */
|
---|
635 |
|
---|
636 | /*!
|
---|
637 | \fn QString QSqlDriver::nullText() const
|
---|
638 |
|
---|
639 | sqlStatement() is now used to generate SQL. Use tr("NULL") for example, instead.
|
---|
640 | */
|
---|
641 |
|
---|
642 | /*!
|
---|
643 | \fn QString QSqlDriver::formatValue(const QSqlField *field, bool trimStrings) const
|
---|
644 |
|
---|
645 | Use the other formatValue() overload instead.
|
---|
646 | */
|
---|
647 |
|
---|
648 | /*!
|
---|
649 | This function is called to subscribe to event notifications from the database.
|
---|
650 | \a name identifies the event notification.
|
---|
651 |
|
---|
652 | If successful, return true, otherwise return false.
|
---|
653 |
|
---|
654 | The database must be open when this function is called. When the database is closed
|
---|
655 | by calling close() all subscribed event notifications are automatically unsubscribed.
|
---|
656 | Note that calling open() on an already open database may implicitly cause close() to
|
---|
657 | be called, which will cause the driver to unsubscribe from all event notifications.
|
---|
658 |
|
---|
659 | When an event notification identified by \a name is posted by the database the
|
---|
660 | notification() signal is emitted.
|
---|
661 |
|
---|
662 | \warning Because of binary compatibility constraints, this function is not virtual.
|
---|
663 | If you want to provide event notification support in your own QSqlDriver subclass,
|
---|
664 | reimplement the subscribeToNotificationImplementation() slot in your subclass instead.
|
---|
665 | The subscribeToNotification() function will dynamically detect the slot and call it.
|
---|
666 |
|
---|
667 | \since 4.4
|
---|
668 | \sa unsubscribeFromNotification() subscribedToNotifications() QSqlDriver::hasFeature()
|
---|
669 | */
|
---|
670 | bool QSqlDriver::subscribeToNotification(const QString &name)
|
---|
671 | {
|
---|
672 | bool result;
|
---|
673 | QMetaObject::invokeMethod(const_cast<QSqlDriver *>(this),
|
---|
674 | "subscribeToNotificationImplementation", Qt::DirectConnection,
|
---|
675 | Q_RETURN_ARG(bool, result),
|
---|
676 | Q_ARG(QString, name));
|
---|
677 | return result;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /*!
|
---|
681 | This function is called to unsubscribe from event notifications from the database.
|
---|
682 | \a name identifies the event notification.
|
---|
683 |
|
---|
684 | If successful, return true, otherwise return false.
|
---|
685 |
|
---|
686 | The database must be open when this function is called. All subscribed event
|
---|
687 | notifications are automatically unsubscribed from when the close() function is called.
|
---|
688 |
|
---|
689 | After calling \e this function the notification() signal will no longer be emitted
|
---|
690 | when an event notification identified by \a name is posted by the database.
|
---|
691 |
|
---|
692 | \warning Because of binary compatibility constraints, this function is not virtual.
|
---|
693 | If you want to provide event notification support in your own QSqlDriver subclass,
|
---|
694 | reimplement the unsubscribeFromNotificationImplementation() slot in your subclass instead.
|
---|
695 | The unsubscribeFromNotification() function will dynamically detect the slot and call it.
|
---|
696 |
|
---|
697 | \since 4.4
|
---|
698 | \sa subscribeToNotification() subscribedToNotifications()
|
---|
699 | */
|
---|
700 | bool QSqlDriver::unsubscribeFromNotification(const QString &name)
|
---|
701 | {
|
---|
702 | bool result;
|
---|
703 | QMetaObject::invokeMethod(const_cast<QSqlDriver *>(this),
|
---|
704 | "unsubscribeFromNotificationImplementation", Qt::DirectConnection,
|
---|
705 | Q_RETURN_ARG(bool, result),
|
---|
706 | Q_ARG(QString, name));
|
---|
707 | return result;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /*!
|
---|
711 | Returns a list of the names of the event notifications that are currently subscribed to.
|
---|
712 |
|
---|
713 | \warning Because of binary compatibility constraints, this function is not virtual.
|
---|
714 | If you want to provide event notification support in your own QSqlDriver subclass,
|
---|
715 | reimplement the subscribedToNotificationsImplementation() slot in your subclass instead.
|
---|
716 | The subscribedToNotifications() function will dynamically detect the slot and call it.
|
---|
717 |
|
---|
718 | \since 4.4
|
---|
719 | \sa subscribeToNotification() unsubscribeFromNotification()
|
---|
720 | */
|
---|
721 | QStringList QSqlDriver::subscribedToNotifications() const
|
---|
722 | {
|
---|
723 | QStringList result;
|
---|
724 | QMetaObject::invokeMethod(const_cast<QSqlDriver *>(this),
|
---|
725 | "subscribedToNotificationsImplementation", Qt::DirectConnection,
|
---|
726 | Q_RETURN_ARG(QStringList, result));
|
---|
727 | return result;
|
---|
728 | }
|
---|
729 |
|
---|
730 | /*!
|
---|
731 | This slot is called to subscribe to event notifications from the database.
|
---|
732 | \a name identifies the event notification.
|
---|
733 |
|
---|
734 | If successful, return true, otherwise return false.
|
---|
735 |
|
---|
736 | The database must be open when this \e slot is called. When the database is closed
|
---|
737 | by calling close() all subscribed event notifications are automatically unsubscribed.
|
---|
738 | Note that calling open() on an already open database may implicitly cause close() to
|
---|
739 | be called, which will cause the driver to unsubscribe from all event notifications.
|
---|
740 |
|
---|
741 | When an event notification identified by \a name is posted by the database the
|
---|
742 | notification() signal is emitted.
|
---|
743 |
|
---|
744 | Reimplement this slot to provide your own QSqlDriver subclass with event notification
|
---|
745 | support; because of binary compatibility constraints, the subscribeToNotification()
|
---|
746 | function (introduced in Qt 4.4) is not virtual. Instead, subscribeToNotification()
|
---|
747 | will dynamically detect and call \e this slot. The default implementation does nothing
|
---|
748 | and returns false.
|
---|
749 |
|
---|
750 | \since 4.4
|
---|
751 | \sa subscribeToNotification()
|
---|
752 | */
|
---|
753 | bool QSqlDriver::subscribeToNotificationImplementation(const QString &name)
|
---|
754 | {
|
---|
755 | Q_UNUSED(name);
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /*!
|
---|
760 | This slot is called to unsubscribe from event notifications from the database.
|
---|
761 | \a name identifies the event notification.
|
---|
762 |
|
---|
763 | If successful, return true, otherwise return false.
|
---|
764 |
|
---|
765 | The database must be open when \e this slot is called. All subscribed event
|
---|
766 | notifications are automatically unsubscribed from when the close() function is called.
|
---|
767 |
|
---|
768 | After calling \e this slot the notification() signal will no longer be emitted
|
---|
769 | when an event notification identified by \a name is posted by the database.
|
---|
770 |
|
---|
771 | Reimplement this slot to provide your own QSqlDriver subclass with event notification
|
---|
772 | support; because of binary compatibility constraints, the unsubscribeFromNotification()
|
---|
773 | function (introduced in Qt 4.4) is not virtual. Instead, unsubscribeFromNotification()
|
---|
774 | will dynamically detect and call \e this slot. The default implementation does nothing
|
---|
775 | and returns false.
|
---|
776 |
|
---|
777 | \since 4.4
|
---|
778 | \sa unsubscribeFromNotification()
|
---|
779 | */
|
---|
780 | bool QSqlDriver::unsubscribeFromNotificationImplementation(const QString &name)
|
---|
781 | {
|
---|
782 | Q_UNUSED(name);
|
---|
783 | return false;
|
---|
784 | }
|
---|
785 |
|
---|
786 | /*!
|
---|
787 | Returns a list of the names of the event notifications that are currently subscribed to.
|
---|
788 |
|
---|
789 | Reimplement this slot to provide your own QSqlDriver subclass with event notification
|
---|
790 | support; because of binary compatibility constraints, the subscribedToNotifications()
|
---|
791 | function (introduced in Qt 4.4) is not virtual. Instead, subscribedToNotifications()
|
---|
792 | will dynamically detect and call \e this slot. The default implementation simply
|
---|
793 | returns an empty QStringList.
|
---|
794 |
|
---|
795 | \since 4.4
|
---|
796 | \sa subscribedToNotifications()
|
---|
797 | */
|
---|
798 | QStringList QSqlDriver::subscribedToNotificationsImplementation() const
|
---|
799 | {
|
---|
800 | return QStringList();
|
---|
801 | }
|
---|
802 |
|
---|
803 | QT_END_NAMESPACE
|
---|