| 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 "qsqldriver.h" |
| 41 | |
| 42 | #include "qdatetime.h" |
| 43 | #include "qsqlerror.h" |
| 44 | #include "qsqlfield.h" |
| 45 | #include "qsqlindex.h" |
| 46 | #include "private/qobject_p.h" |
| 47 | #include "private/qsqldriver_p.h" |
| 48 | |
| 49 | #include <limits.h> |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | static QString prepareIdentifier(const QString &identifier, |
| 54 | QSqlDriver::IdentifierType type, const QSqlDriver *driver) |
| 55 | { |
| 56 | Q_ASSERT(driver != nullptr); |
| 57 | QString ret = identifier; |
| 58 | if (!driver->isIdentifierEscaped(identifier, type)) |
| 59 | ret = driver->escapeIdentifier(identifier, type); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | /*! |
| 64 | \class QSqlDriver |
| 65 | \brief The QSqlDriver class is an abstract base class for accessing |
| 66 | specific SQL databases. |
| 67 | |
| 68 | \ingroup database |
| 69 | \inmodule QtSql |
| 70 | |
| 71 | This class should not be used directly. Use QSqlDatabase instead. |
| 72 | |
| 73 | If you want to create your own SQL drivers, you can subclass this |
| 74 | class and reimplement its pure virtual functions and those |
| 75 | virtual functions that you need. See \l{How to Write Your Own |
| 76 | Database Driver} for more information. |
| 77 | |
| 78 | \sa QSqlDatabase, QSqlResult |
| 79 | */ |
| 80 | |
| 81 | /*! |
| 82 | Constructs a new driver with the given \a parent. |
| 83 | */ |
| 84 | |
| 85 | QSqlDriver::QSqlDriver(QObject *parent) |
| 86 | : QObject(*new QSqlDriverPrivate, parent) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | /*! \internal |
| 91 | */ |
| 92 | QSqlDriver::QSqlDriver(QSqlDriverPrivate &dd, QObject *parent) |
| 93 | : QObject(dd, parent) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | /*! |
| 98 | Destroys the object and frees any allocated resources. |
| 99 | */ |
| 100 | |
| 101 | QSqlDriver::~QSqlDriver() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | \since 5.0 |
| 107 | |
| 108 | \fn QSqlDriver::notification(const QString &name, QSqlDriver::NotificationSource source, const QVariant & payload) |
| 109 | |
| 110 | This signal is emitted when the database posts an event notification |
| 111 | that the driver subscribes to. \a name identifies the event notification, \a source indicates the signal source, |
| 112 | \a payload holds the extra data optionally delivered with the notification. |
| 113 | |
| 114 | \sa subscribeToNotification() |
| 115 | */ |
| 116 | |
| 117 | /*! |
| 118 | \fn bool QSqlDriver::open(const QString &db, const QString &user, const QString& password, |
| 119 | const QString &host, int port, const QString &options) |
| 120 | |
| 121 | Derived classes must reimplement this pure virtual function to |
| 122 | open a database connection on database \a db, using user name \a |
| 123 | user, password \a password, host \a host, port \a port and |
| 124 | connection options \a options. |
| 125 | |
| 126 | The function must return true on success and false on failure. |
| 127 | |
| 128 | \sa setOpen() |
| 129 | */ |
| 130 | |
| 131 | /*! |
| 132 | \fn bool QSqlDriver::close() |
| 133 | |
| 134 | Derived classes must reimplement this pure virtual function in |
| 135 | order to close the database connection. Return true on success, |
| 136 | false on failure. |
| 137 | |
| 138 | \sa open(), setOpen() |
| 139 | */ |
| 140 | |
| 141 | /*! |
| 142 | \fn QSqlResult *QSqlDriver::createResult() const |
| 143 | |
| 144 | Creates an empty SQL result on the database. Derived classes must |
| 145 | reimplement this function and return a QSqlResult object |
| 146 | appropriate for their database to the caller. |
| 147 | */ |
| 148 | |
| 149 | /*! |
| 150 | Returns \c true if the database connection is open; otherwise returns |
| 151 | false. |
| 152 | */ |
| 153 | |
| 154 | bool QSqlDriver::isOpen() const |
| 155 | { |
| 156 | Q_D(const QSqlDriver); |
| 157 | return d->isOpen; |
| 158 | } |
| 159 | |
| 160 | /*! |
| 161 | Returns \c true if the there was an error opening the database |
| 162 | connection; otherwise returns \c false. |
| 163 | */ |
| 164 | |
| 165 | bool QSqlDriver::isOpenError() const |
| 166 | { |
| 167 | Q_D(const QSqlDriver); |
| 168 | return d->isOpenError; |
| 169 | } |
| 170 | |
| 171 | /*! |
| 172 | \enum QSqlDriver::DriverFeature |
| 173 | |
| 174 | This enum contains a list of features a driver might support. Use |
| 175 | hasFeature() to query whether a feature is supported or not. |
| 176 | |
| 177 | \value Transactions Whether the driver supports SQL transactions. |
| 178 | \value QuerySize Whether the database is capable of reporting the size |
| 179 | of a query. Note that some databases do not support returning the size |
| 180 | (i.e. number of rows returned) of a query, in which case |
| 181 | QSqlQuery::size() will return -1. |
| 182 | \value BLOB Whether the driver supports Binary Large Object fields. |
| 183 | \value Unicode Whether the driver supports Unicode strings if the |
| 184 | database server does. |
| 185 | \value PreparedQueries Whether the driver supports prepared query execution. |
| 186 | \value NamedPlaceholders Whether the driver supports the use of named placeholders. |
| 187 | \value PositionalPlaceholders Whether the driver supports the use of positional placeholders. |
| 188 | \value LastInsertId Whether the driver supports returning the Id of the last touched row. |
| 189 | \value BatchOperations Whether the driver supports batched operations, see QSqlQuery::execBatch() |
| 190 | \value SimpleLocking Whether the driver disallows a write lock on a table while other queries have a read lock on it. |
| 191 | \value LowPrecisionNumbers Whether the driver allows fetching numerical values with low precision. |
| 192 | \value EventNotifications Whether the driver supports database event notifications. |
| 193 | \value FinishQuery Whether the driver can do any low-level resource cleanup when QSqlQuery::finish() is called. |
| 194 | \value MultipleResultSets Whether the driver can access multiple result sets returned from batched statements or stored procedures. |
| 195 | \value CancelQuery Whether the driver allows cancelling a running query. |
| 196 | |
| 197 | More information about supported features can be found in the |
| 198 | \l{sql-driver.html}{Qt SQL driver} documentation. |
| 199 | |
| 200 | \sa hasFeature() |
| 201 | */ |
| 202 | |
| 203 | /*! |
| 204 | \enum QSqlDriver::StatementType |
| 205 | |
| 206 | This enum contains a list of SQL statement (or clause) types the |
| 207 | driver can create. |
| 208 | |
| 209 | \value WhereStatement An SQL \c WHERE statement (e.g., \c{WHERE f = 5}). |
| 210 | \value SelectStatement An SQL \c SELECT statement (e.g., \c{SELECT f FROM t}). |
| 211 | \value UpdateStatement An SQL \c UPDATE statement (e.g., \c{UPDATE TABLE t set f = 1}). |
| 212 | \value InsertStatement An SQL \c INSERT statement (e.g., \c{INSERT INTO t (f) values (1)}). |
| 213 | \value DeleteStatement An SQL \c DELETE statement (e.g., \c{DELETE FROM t}). |
| 214 | |
| 215 | \sa sqlStatement() |
| 216 | */ |
| 217 | |
| 218 | /*! |
| 219 | \enum QSqlDriver::IdentifierType |
| 220 | |
| 221 | This enum contains a list of SQL identifier types. |
| 222 | |
| 223 | \value FieldName A SQL field name |
| 224 | \value TableName A SQL table name |
| 225 | */ |
| 226 | |
| 227 | /*! |
| 228 | \enum QSqlDriver::NotificationSource |
| 229 | |
| 230 | This enum contains a list of SQL notification sources. |
| 231 | |
| 232 | \value UnknownSource The notification source is unknown |
| 233 | \value SelfSource The notification source is this connection |
| 234 | \value OtherSource The notification source is another connection |
| 235 | */ |
| 236 | |
| 237 | /*! |
| 238 | \enum QSqlDriver::DbmsType |
| 239 | \internal |
| 240 | |
| 241 | This enum contains DBMS types. |
| 242 | |
| 243 | \value UnknownDbms |
| 244 | \value MSSqlServer |
| 245 | \value MySqlServer |
| 246 | \value PostgreSQL |
| 247 | \value Oracle |
| 248 | \value Sybase |
| 249 | \value SQLite |
| 250 | \value Interbase |
| 251 | \value DB2 |
| 252 | */ |
| 253 | |
| 254 | /*! |
| 255 | \fn bool QSqlDriver::hasFeature(DriverFeature feature) const |
| 256 | |
| 257 | Returns \c true if the driver supports feature \a feature; otherwise |
| 258 | returns \c false. |
| 259 | |
| 260 | Note that some databases need to be open() before this can be |
| 261 | determined. |
| 262 | |
| 263 | \sa DriverFeature |
| 264 | */ |
| 265 | |
| 266 | /*! |
| 267 | This function sets the open state of the database to \a open. |
| 268 | Derived classes can use this function to report the status of |
| 269 | open(). |
| 270 | |
| 271 | \sa open(), setOpenError() |
| 272 | */ |
| 273 | |
| 274 | void QSqlDriver::setOpen(bool open) |
| 275 | { |
| 276 | Q_D(QSqlDriver); |
| 277 | d->isOpen = open; |
| 278 | } |
| 279 | |
| 280 | /*! |
| 281 | This function sets the open error state of the database to \a |
| 282 | error. Derived classes can use this function to report the status |
| 283 | of open(). Note that if \a error is true the open state of the |
| 284 | database is set to closed (i.e., isOpen() returns \c false). |
| 285 | |
| 286 | \sa open(), setOpen() |
| 287 | */ |
| 288 | |
| 289 | void QSqlDriver::setOpenError(bool error) |
| 290 | { |
| 291 | Q_D(QSqlDriver); |
| 292 | d->isOpenError = error; |
| 293 | if (error) |
| 294 | d->isOpen = false; |
| 295 | } |
| 296 | |
| 297 | /*! |
| 298 | This function is called to begin a transaction. If successful, |
| 299 | return true, otherwise return false. The default implementation |
| 300 | does nothing and returns \c false. |
| 301 | |
| 302 | \sa commitTransaction(), rollbackTransaction() |
| 303 | */ |
| 304 | |
| 305 | bool QSqlDriver::beginTransaction() |
| 306 | { |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /*! |
| 311 | This function is called to commit a transaction. If successful, |
| 312 | return true, otherwise return false. The default implementation |
| 313 | does nothing and returns \c false. |
| 314 | |
| 315 | \sa beginTransaction(), rollbackTransaction() |
| 316 | */ |
| 317 | |
| 318 | bool QSqlDriver::commitTransaction() |
| 319 | { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | /*! |
| 324 | This function is called to rollback a transaction. If successful, |
| 325 | return true, otherwise return false. The default implementation |
| 326 | does nothing and returns \c false. |
| 327 | |
| 328 | \sa beginTransaction(), commitTransaction() |
| 329 | */ |
| 330 | |
| 331 | bool QSqlDriver::rollbackTransaction() |
| 332 | { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /*! |
| 337 | This function is used to set the value of the last error, \a error, |
| 338 | that occurred on the database. |
| 339 | |
| 340 | \sa lastError() |
| 341 | */ |
| 342 | |
| 343 | void QSqlDriver::setLastError(const QSqlError &error) |
| 344 | { |
| 345 | Q_D(QSqlDriver); |
| 346 | d->error = error; |
| 347 | } |
| 348 | |
| 349 | /*! |
| 350 | Returns a QSqlError object which contains information about the |
| 351 | last error that occurred on the database. |
| 352 | */ |
| 353 | |
| 354 | QSqlError QSqlDriver::lastError() const |
| 355 | { |
| 356 | Q_D(const QSqlDriver); |
| 357 | return d->error; |
| 358 | } |
| 359 | |
| 360 | /*! |
| 361 | Returns a list of the names of the tables in the database. The |
| 362 | default implementation returns an empty list. |
| 363 | |
| 364 | The \a tableType argument describes what types of tables |
| 365 | should be returned. Due to binary compatibility, the string |
| 366 | contains the value of the enum QSql::TableTypes as text. |
| 367 | An empty string should be treated as QSql::Tables for |
| 368 | backward compatibility. |
| 369 | */ |
| 370 | |
| 371 | QStringList QSqlDriver::tables(QSql::TableType) const |
| 372 | { |
| 373 | return QStringList(); |
| 374 | } |
| 375 | |
| 376 | /*! |
| 377 | Returns the primary index for table \a tableName. Returns an empty |
| 378 | QSqlIndex if the table doesn't have a primary index. The default |
| 379 | implementation returns an empty index. |
| 380 | */ |
| 381 | |
| 382 | QSqlIndex QSqlDriver::primaryIndex(const QString&) const |
| 383 | { |
| 384 | return QSqlIndex(); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /*! |
| 389 | Returns a QSqlRecord populated with the names of the fields in |
| 390 | table \a tableName. If no such table exists, an empty record is |
| 391 | returned. The default implementation returns an empty record. |
| 392 | */ |
| 393 | |
| 394 | QSqlRecord QSqlDriver::record(const QString & /* tableName */) const |
| 395 | { |
| 396 | return QSqlRecord(); |
| 397 | } |
| 398 | |
| 399 | /*! |
| 400 | Returns the \a identifier escaped according to the database rules. |
| 401 | \a identifier can either be a table name or field name, dependent |
| 402 | on \a type. |
| 403 | |
| 404 | The default implementation does nothing. |
| 405 | \sa isIdentifierEscaped() |
| 406 | */ |
| 407 | QString QSqlDriver::escapeIdentifier(const QString &identifier, IdentifierType) const |
| 408 | { |
| 409 | return identifier; |
| 410 | } |
| 411 | |
| 412 | /*! |
| 413 | Returns whether \a identifier is escaped according to the database rules. |
| 414 | \a identifier can either be a table name or field name, dependent |
| 415 | on \a type. |
| 416 | |
| 417 | Reimplement this function if you want to provide your own implementation in your |
| 418 | QSqlDriver subclass, |
| 419 | |
| 420 | \sa stripDelimiters(), escapeIdentifier() |
| 421 | */ |
| 422 | bool QSqlDriver::isIdentifierEscaped(const QString &identifier, IdentifierType type) const |
| 423 | { |
| 424 | Q_UNUSED(type); |
| 425 | return identifier.size() > 2 |
| 426 | && identifier.startsWith(QLatin1Char('"')) //left delimited |
| 427 | && identifier.endsWith(QLatin1Char('"')); //right delimited |
| 428 | } |
| 429 | |
| 430 | /*! |
| 431 | Returns the \a identifier with the leading and trailing delimiters removed, |
| 432 | \a identifier can either be a table name or field name, |
| 433 | dependent on \a type. If \a identifier does not have leading |
| 434 | and trailing delimiter characters, \a identifier is returned without |
| 435 | modification. |
| 436 | |
| 437 | Reimplement this function if you want to provide your own implementation in your |
| 438 | QSqlDriver subclass, |
| 439 | |
| 440 | \since 4.5 |
| 441 | \sa isIdentifierEscaped() |
| 442 | */ |
| 443 | QString QSqlDriver::stripDelimiters(const QString &identifier, IdentifierType type) const |
| 444 | { |
| 445 | QString ret; |
| 446 | if (isIdentifierEscaped(identifier, type)) { |
| 447 | ret = identifier.mid(1); |
| 448 | ret.chop(1); |
| 449 | } else { |
| 450 | ret = identifier; |
| 451 | } |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | /*! |
| 456 | Returns a SQL statement of type \a type for the table \a tableName |
| 457 | with the values from \a rec. If \a preparedStatement is true, the |
| 458 | string will contain placeholders instead of values. |
| 459 | |
| 460 | The generated flag in each field of \a rec determines whether the |
| 461 | field is included in the generated statement. |
| 462 | |
| 463 | This method can be used to manipulate tables without having to worry |
| 464 | about database-dependent SQL dialects. For non-prepared statements, |
| 465 | the values will be properly escaped. |
| 466 | |
| 467 | In the WHERE statement, each non-null field of \a rec specifies a |
| 468 | filter condition of equality to the field value, or if prepared, a |
| 469 | placeholder. However, prepared or not, a null field specifies the |
| 470 | condition IS NULL and never introduces a placeholder. The |
| 471 | application must not attempt to bind data for the null field during |
| 472 | execution. The field must be set to some non-null value if a |
| 473 | placeholder is desired. Furthermore, since non-null fields specify |
| 474 | equality conditions and SQL NULL is not equal to anything, even |
| 475 | itself, it is generally not useful to bind a null to a placeholder. |
| 476 | |
| 477 | */ |
| 478 | QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName, |
| 479 | const QSqlRecord &rec, bool preparedStatement) const |
| 480 | { |
| 481 | const auto tableNameString = tableName.isEmpty() ? QString() |
| 482 | : prepareIdentifier(tableName, QSqlDriver::TableName, this); |
| 483 | int i; |
| 484 | QString s; |
| 485 | s.reserve(128); |
| 486 | switch (type) { |
| 487 | case SelectStatement: |
| 488 | for (i = 0; i < rec.count(); ++i) { |
| 489 | if (rec.isGenerated(i)) |
| 490 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1String(", " )); |
| 491 | } |
| 492 | if (s.isEmpty()) |
| 493 | return s; |
| 494 | s.chop(2); |
| 495 | s = QLatin1String("SELECT " ) + s + QLatin1String(" FROM " ) + tableNameString; |
| 496 | break; |
| 497 | case WhereStatement: |
| 498 | { |
| 499 | const QString tableNamePrefix = tableNameString.isEmpty() |
| 500 | ? QString() : tableNameString + QLatin1Char('.'); |
| 501 | for (int i = 0; i < rec.count(); ++i) { |
| 502 | if (!rec.isGenerated(i)) |
| 503 | continue; |
| 504 | s.append(s.isEmpty() ? QLatin1String("WHERE " ) : QLatin1String(" AND " )); |
| 505 | s.append(tableNamePrefix); |
| 506 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)); |
| 507 | if (rec.isNull(i)) |
| 508 | s.append(QLatin1String(" IS NULL" )); |
| 509 | else if (preparedStatement) |
| 510 | s.append(QLatin1String(" = ?" )); |
| 511 | else |
| 512 | s.append(QLatin1String(" = " )).append(formatValue(rec.field(i))); |
| 513 | } |
| 514 | break; |
| 515 | } |
| 516 | case UpdateStatement: |
| 517 | s = s + QLatin1String("UPDATE " ) + tableNameString + QLatin1String(" SET " ); |
| 518 | for (i = 0; i < rec.count(); ++i) { |
| 519 | if (!rec.isGenerated(i)) |
| 520 | continue; |
| 521 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1Char('=')); |
| 522 | if (preparedStatement) |
| 523 | s.append(QLatin1Char('?')); |
| 524 | else |
| 525 | s.append(formatValue(rec.field(i))); |
| 526 | s.append(QLatin1String(", " )); |
| 527 | } |
| 528 | if (s.endsWith(QLatin1String(", " ))) |
| 529 | s.chop(2); |
| 530 | else |
| 531 | s.clear(); |
| 532 | break; |
| 533 | case DeleteStatement: |
| 534 | s = s + QLatin1String("DELETE FROM " ) + tableNameString; |
| 535 | break; |
| 536 | case InsertStatement: { |
| 537 | s = s + QLatin1String("INSERT INTO " ) + tableNameString + QLatin1String(" (" ); |
| 538 | QString vals; |
| 539 | for (i = 0; i < rec.count(); ++i) { |
| 540 | if (!rec.isGenerated(i)) |
| 541 | continue; |
| 542 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1String(", " )); |
| 543 | if (preparedStatement) |
| 544 | vals.append(QLatin1Char('?')); |
| 545 | else |
| 546 | vals.append(formatValue(rec.field(i))); |
| 547 | vals.append(QLatin1String(", " )); |
| 548 | } |
| 549 | if (vals.isEmpty()) { |
| 550 | s.clear(); |
| 551 | } else { |
| 552 | vals.chop(2); // remove trailing comma |
| 553 | s[s.length() - 2] = QLatin1Char(')'); |
| 554 | s.append(QLatin1String("VALUES (" )).append(vals).append(QLatin1Char(')')); |
| 555 | } |
| 556 | break; } |
| 557 | } |
| 558 | return s; |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | Returns a string representation of the \a field value for the |
| 563 | database. This is used, for example, when constructing INSERT and |
| 564 | UPDATE statements. |
| 565 | |
| 566 | The default implementation returns the value formatted as a string |
| 567 | according to the following rules: |
| 568 | |
| 569 | \list |
| 570 | |
| 571 | \li If \a field is character data, the value is returned enclosed |
| 572 | in single quotation marks, which is appropriate for many SQL |
| 573 | databases. Any embedded single-quote characters are escaped |
| 574 | (replaced with two single-quote characters). If \a trimStrings is |
| 575 | true (the default is false), all trailing whitespace is trimmed |
| 576 | from the field. |
| 577 | |
| 578 | \li If \a field is date/time data, the value is formatted in ISO |
| 579 | format and enclosed in single quotation marks. If the date/time |
| 580 | data is invalid, "NULL" is returned. |
| 581 | |
| 582 | \li If \a field is \l{QByteArray}{bytearray} data, and the |
| 583 | driver can edit binary fields, the value is formatted as a |
| 584 | hexadecimal string. |
| 585 | |
| 586 | \li For any other field type, toString() is called on its value |
| 587 | and the result of this is returned. |
| 588 | |
| 589 | \endlist |
| 590 | |
| 591 | \sa QVariant::toString() |
| 592 | |
| 593 | */ |
| 594 | QString QSqlDriver::formatValue(const QSqlField &field, bool trimStrings) const |
| 595 | { |
| 596 | const QLatin1String nullTxt("NULL" ); |
| 597 | |
| 598 | QString r; |
| 599 | if (field.isNull()) |
| 600 | r = nullTxt; |
| 601 | else { |
| 602 | switch (field.metaType().id()) { |
| 603 | case QMetaType::Int: |
| 604 | case QMetaType::UInt: |
| 605 | if (field.value().userType() == QMetaType::Bool) |
| 606 | r = field.value().toBool() ? QLatin1String("1" ) : QLatin1String("0" ); |
| 607 | else |
| 608 | r = field.value().toString(); |
| 609 | break; |
| 610 | #if QT_CONFIG(datestring) |
| 611 | case QMetaType::QDate: |
| 612 | if (field.value().toDate().isValid()) |
| 613 | r = QLatin1Char('\'') + field.value().toDate().toString(Qt::ISODate) |
| 614 | + QLatin1Char('\''); |
| 615 | else |
| 616 | r = nullTxt; |
| 617 | break; |
| 618 | case QMetaType::QTime: |
| 619 | if (field.value().toTime().isValid()) |
| 620 | r = QLatin1Char('\'') + field.value().toTime().toString(Qt::ISODate) |
| 621 | + QLatin1Char('\''); |
| 622 | else |
| 623 | r = nullTxt; |
| 624 | break; |
| 625 | case QMetaType::QDateTime: |
| 626 | if (field.value().toDateTime().isValid()) |
| 627 | r = QLatin1Char('\'') + |
| 628 | field.value().toDateTime().toString(Qt::ISODate) + QLatin1Char('\''); |
| 629 | else |
| 630 | r = nullTxt; |
| 631 | break; |
| 632 | #endif |
| 633 | case QMetaType::QString: |
| 634 | case QMetaType::QChar: |
| 635 | { |
| 636 | QString result = field.value().toString(); |
| 637 | if (trimStrings) { |
| 638 | int end = result.length(); |
| 639 | while (end && result.at(end-1).isSpace()) /* skip white space from end */ |
| 640 | end--; |
| 641 | result.truncate(end); |
| 642 | } |
| 643 | /* escape the "'" character */ |
| 644 | result.replace(QLatin1Char('\''), QLatin1String("''" )); |
| 645 | r = QLatin1Char('\'') + result + QLatin1Char('\''); |
| 646 | break; |
| 647 | } |
| 648 | case QMetaType::Bool: |
| 649 | r = QString::number(field.value().toBool()); |
| 650 | break; |
| 651 | case QMetaType::QByteArray : { |
| 652 | if (hasFeature(BLOB)) { |
| 653 | QByteArray ba = field.value().toByteArray(); |
| 654 | QString res; |
| 655 | static const char hexchars[] = "0123456789abcdef" ; |
| 656 | for (int i = 0; i < ba.size(); ++i) { |
| 657 | uchar s = (uchar) ba[i]; |
| 658 | res += QLatin1Char(hexchars[s >> 4]); |
| 659 | res += QLatin1Char(hexchars[s & 0x0f]); |
| 660 | } |
| 661 | r = QLatin1Char('\'') + res + QLatin1Char('\''); |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | Q_FALLTHROUGH(); |
| 666 | default: |
| 667 | r = field.value().toString(); |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | return r; |
| 672 | } |
| 673 | |
| 674 | /*! |
| 675 | Returns the low-level database handle wrapped in a QVariant or an |
| 676 | invalid variant if there is no handle. |
| 677 | |
| 678 | \warning Use this with uttermost care and only if you know what you're doing. |
| 679 | |
| 680 | \warning The handle returned here can become a stale pointer if the connection |
| 681 | is modified (for example, if you close the connection). |
| 682 | |
| 683 | \warning The handle can be NULL if the connection is not open yet. |
| 684 | |
| 685 | The handle returned here is database-dependent, you should query the type |
| 686 | name of the variant before accessing it. |
| 687 | |
| 688 | This example retrieves the handle for a connection to sqlite: |
| 689 | |
| 690 | \snippet code/src_sql_kernel_qsqldriver.cpp 0 |
| 691 | |
| 692 | This snippet returns the handle for PostgreSQL or MySQL: |
| 693 | |
| 694 | \snippet code/src_sql_kernel_qsqldriver.cpp 1 |
| 695 | |
| 696 | \sa QSqlResult::handle() |
| 697 | */ |
| 698 | QVariant QSqlDriver::handle() const |
| 699 | { |
| 700 | return QVariant(); |
| 701 | } |
| 702 | |
| 703 | /*! |
| 704 | This function is called to subscribe to event notifications from the database. |
| 705 | \a name identifies the event notification. |
| 706 | |
| 707 | If successful, return true, otherwise return false. |
| 708 | |
| 709 | The database must be open when this function is called. When the database is closed |
| 710 | by calling close() all subscribed event notifications are automatically unsubscribed. |
| 711 | Note that calling open() on an already open database may implicitly cause close() to |
| 712 | be called, which will cause the driver to unsubscribe from all event notifications. |
| 713 | |
| 714 | When an event notification identified by \a name is posted by the database the |
| 715 | notification() signal is emitted. |
| 716 | |
| 717 | Reimplement this function if you want to provide event notification support in your |
| 718 | own QSqlDriver subclass, |
| 719 | |
| 720 | \since 4.4 |
| 721 | \sa unsubscribeFromNotification(), subscribedToNotifications(), QSqlDriver::hasFeature() |
| 722 | */ |
| 723 | bool QSqlDriver::subscribeToNotification(const QString &name) |
| 724 | { |
| 725 | Q_UNUSED(name); |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | /*! |
| 730 | This function is called to unsubscribe from event notifications from the database. |
| 731 | \a name identifies the event notification. |
| 732 | |
| 733 | If successful, return true, otherwise return false. |
| 734 | |
| 735 | The database must be open when this function is called. All subscribed event |
| 736 | notifications are automatically unsubscribed from when the close() function is called. |
| 737 | |
| 738 | After calling \e this function the notification() signal will no longer be emitted |
| 739 | when an event notification identified by \a name is posted by the database. |
| 740 | |
| 741 | Reimplement this function if you want to provide event notification support in your |
| 742 | own QSqlDriver subclass, |
| 743 | |
| 744 | \since 4.4 |
| 745 | \sa subscribeToNotification(), subscribedToNotifications() |
| 746 | */ |
| 747 | bool QSqlDriver::unsubscribeFromNotification(const QString &name) |
| 748 | { |
| 749 | Q_UNUSED(name); |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | /*! |
| 754 | Returns a list of the names of the event notifications that are currently subscribed to. |
| 755 | |
| 756 | Reimplement this function if you want to provide event notification support in your |
| 757 | own QSqlDriver subclass, |
| 758 | |
| 759 | \since 4.4 |
| 760 | \sa subscribeToNotification(), unsubscribeFromNotification() |
| 761 | */ |
| 762 | QStringList QSqlDriver::subscribedToNotifications() const |
| 763 | { |
| 764 | return QStringList(); |
| 765 | } |
| 766 | |
| 767 | /*! |
| 768 | \since 4.6 |
| 769 | |
| 770 | Sets the default numerical precision policy used by queries created |
| 771 | by this driver to \a precisionPolicy. |
| 772 | |
| 773 | Note: Setting the default precision policy to \a precisionPolicy |
| 774 | doesn't affect any currently active queries. |
| 775 | |
| 776 | \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), |
| 777 | QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy() |
| 778 | */ |
| 779 | void QSqlDriver::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) |
| 780 | { |
| 781 | Q_D(QSqlDriver); |
| 782 | d->precisionPolicy = precisionPolicy; |
| 783 | } |
| 784 | |
| 785 | /*! |
| 786 | \since 4.6 |
| 787 | |
| 788 | Returns the current default precision policy for the database connection. |
| 789 | |
| 790 | \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), |
| 791 | QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy() |
| 792 | */ |
| 793 | QSql::NumericalPrecisionPolicy QSqlDriver::numericalPrecisionPolicy() const |
| 794 | { |
| 795 | Q_D(const QSqlDriver); |
| 796 | return d->precisionPolicy; |
| 797 | } |
| 798 | |
| 799 | /*! |
| 800 | \since 5.4 |
| 801 | \internal |
| 802 | |
| 803 | Returns the current DBMS type for the database connection. |
| 804 | */ |
| 805 | QSqlDriver::DbmsType QSqlDriver::dbmsType() const |
| 806 | { |
| 807 | Q_D(const QSqlDriver); |
| 808 | return d->dbmsType; |
| 809 | } |
| 810 | |
| 811 | /*! |
| 812 | \since 5.0 |
| 813 | \internal |
| 814 | |
| 815 | Tries to cancel the running query, if the underlying driver has the |
| 816 | capability to cancel queries. Returns \c true on success, otherwise false. |
| 817 | |
| 818 | This function can be called from a different thread. |
| 819 | |
| 820 | If you use this function as a slot, you need to use a Qt::DirectConnection |
| 821 | from a different thread. |
| 822 | |
| 823 | Reimplement this function to support canceling running queries in |
| 824 | your own QSqlDriver subclass. It must be implemented in a thread-safe |
| 825 | manner. |
| 826 | |
| 827 | \sa QSqlDriver::hasFeature() |
| 828 | */ |
| 829 | bool QSqlDriver::cancelQuery() |
| 830 | { |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | /*! |
| 835 | \since 6.0 |
| 836 | |
| 837 | Returns the maximum length for the identifier \a type according to the database settings. Returns |
| 838 | INT_MAX by default if the is no maximum for the database. |
| 839 | */ |
| 840 | |
| 841 | int QSqlDriver::maximumIdentifierLength(QSqlDriver::IdentifierType type) const |
| 842 | { |
| 843 | Q_UNUSED(type); |
| 844 | return INT_MAX; |
| 845 | } |
| 846 | |
| 847 | QT_END_NAMESPACE |
| 848 | |