Qt 4.8
qsql_sqlite.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsql_sqlite.h"
43 
44 #include <qcoreapplication.h>
45 #include <qvariant.h>
46 #include <qsqlerror.h>
47 #include <qsqlfield.h>
48 #include <qsqlindex.h>
49 #include <qsqlquery.h>
50 #include <qstringlist.h>
51 #include <qvector.h>
52 #include <qdebug.h>
53 
54 #if defined Q_OS_WIN
55 # include <qt_windows.h>
56 #else
57 # include <unistd.h>
58 #endif
59 
60 #include <sqlite3.h>
61 
62 Q_DECLARE_METATYPE(sqlite3*)
63 Q_DECLARE_METATYPE(sqlite3_stmt*)
64 
66 
67 static QString _q_escapeIdentifier(const QString &identifier)
68 {
69  QString res = identifier;
70  if(!identifier.isEmpty() && identifier.left(1) != QString(QLatin1Char('"')) && identifier.right(1) != QString(QLatin1Char('"')) ) {
71  res.replace(QLatin1Char('"'), QLatin1String("\"\""));
72  res.prepend(QLatin1Char('"')).append(QLatin1Char('"'));
73  res.replace(QLatin1Char('.'), QLatin1String("\".\""));
74  }
75  return res;
76 }
77 
78 static QVariant::Type qGetColumnType(const QString &tpName)
79 {
80  const QString typeName = tpName.toLower();
81 
82  if (typeName == QLatin1String("integer")
83  || typeName == QLatin1String("int"))
84  return QVariant::Int;
85  if (typeName == QLatin1String("double")
86  || typeName == QLatin1String("float")
87  || typeName == QLatin1String("real")
88  || typeName.startsWith(QLatin1String("numeric")))
89  return QVariant::Double;
90  if (typeName == QLatin1String("blob"))
91  return QVariant::ByteArray;
92  return QVariant::String;
93 }
94 
96  int errorCode = -1)
97 {
98  return QSqlError(descr,
99  QString(reinterpret_cast<const QChar *>(sqlite3_errmsg16(access))),
100  type, errorCode);
101 }
102 
104 {
105 public:
106  inline QSQLiteDriverPrivate() : access(0) {}
107  sqlite3 *access;
109 };
110 
111 
113 {
114 public:
116  void cleanup();
117  bool fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch);
118  // initializes the recordInfo and the cache
119  void initColumns(bool emptyResultset);
120  void finalize();
121 
123  sqlite3 *access;
124 
125  sqlite3_stmt *stmt;
126 
127  bool skippedStatus; // the status of the fetchNext() that's skipped
128  bool skipRow; // skip the next fetchNext()?
131 };
132 
134  stmt(0), skippedStatus(false), skipRow(false)
135 {
136 }
137 
139 {
140  finalize();
141  rInf.clear();
142  skippedStatus = false;
143  skipRow = false;
145  q->setActive(false);
146  q->cleanup();
147 }
148 
150 {
151  if (!stmt)
152  return;
153 
154  sqlite3_finalize(stmt);
155  stmt = 0;
156 }
157 
158 void QSQLiteResultPrivate::initColumns(bool emptyResultset)
159 {
160  int nCols = sqlite3_column_count(stmt);
161  if (nCols <= 0)
162  return;
163 
164  q->init(nCols);
165 
166  for (int i = 0; i < nCols; ++i) {
167  QString colName = QString(reinterpret_cast<const QChar *>(
168  sqlite3_column_name16(stmt, i))
169  ).remove(QLatin1Char('"'));
170 
171  // must use typeName for resolving the type to match QSqliteDriver::record
172  QString typeName = QString(reinterpret_cast<const QChar *>(
173  sqlite3_column_decltype16(stmt, i)));
174  // sqlite3_column_type is documented to have undefined behavior if the result set is empty
175  int stp = emptyResultset ? -1 : sqlite3_column_type(stmt, i);
176 
177  QVariant::Type fieldType;
178 
179  if (!typeName.isEmpty()) {
180  fieldType = qGetColumnType(typeName);
181  } else {
182  // Get the proper type for the field based on stp value
183  switch (stp) {
184  case SQLITE_INTEGER:
185  fieldType = QVariant::Int;
186  break;
187  case SQLITE_FLOAT:
188  fieldType = QVariant::Double;
189  break;
190  case SQLITE_BLOB:
191  fieldType = QVariant::ByteArray;
192  break;
193  case SQLITE_TEXT:
194  fieldType = QVariant::String;
195  break;
196  case SQLITE_NULL:
197  default:
198  fieldType = QVariant::Invalid;
199  break;
200  }
201  }
202 
203  QSqlField fld(colName, fieldType);
204  fld.setSqlType(stp);
205  rInf.append(fld);
206  }
207 }
208 
210 {
211  int res;
212  int i;
213 
214  if (skipRow) {
215  // already fetched
216  Q_ASSERT(!initialFetch);
217  skipRow = false;
218  for(int i=0;i<firstRow.count();i++)
219  values[i]=firstRow[i];
220  return skippedStatus;
221  }
222  skipRow = initialFetch;
223 
224  if(initialFetch) {
225  firstRow.clear();
226  firstRow.resize(sqlite3_column_count(stmt));
227  }
228 
229  if (!stmt) {
230  q->setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult", "Unable to fetch row"),
231  QCoreApplication::translate("QSQLiteResult", "No query"), QSqlError::ConnectionError));
233  return false;
234  }
235  res = sqlite3_step(stmt);
236 
237  switch(res) {
238  case SQLITE_ROW:
239  // check to see if should fill out columns
240  if (rInf.isEmpty())
241  // must be first call.
242  initColumns(false);
243  if (idx < 0 && !initialFetch)
244  return true;
245  for (i = 0; i < rInf.count(); ++i) {
246  switch (sqlite3_column_type(stmt, i)) {
247  case SQLITE_BLOB:
248  values[i + idx] = QByteArray(static_cast<const char *>(
249  sqlite3_column_blob(stmt, i)),
250  sqlite3_column_bytes(stmt, i));
251  break;
252  case SQLITE_INTEGER:
253  values[i + idx] = sqlite3_column_int64(stmt, i);
254  break;
255  case SQLITE_FLOAT:
256  switch(q->numericalPrecisionPolicy()) {
258  values[i + idx] = sqlite3_column_int(stmt, i);
259  break;
261  values[i + idx] = sqlite3_column_int64(stmt, i);
262  break;
264  case QSql::HighPrecision:
265  default:
266  values[i + idx] = sqlite3_column_double(stmt, i);
267  break;
268  };
269  break;
270  case SQLITE_NULL:
271  values[i + idx] = QVariant(QVariant::String);
272  break;
273  default:
274  values[i + idx] = QString(reinterpret_cast<const QChar *>(
275  sqlite3_column_text16(stmt, i)),
276  sqlite3_column_bytes16(stmt, i) / sizeof(QChar));
277  break;
278  }
279  }
280  return true;
281  case SQLITE_DONE:
282  if (rInf.isEmpty())
283  // must be first call.
284  initColumns(true);
286  sqlite3_reset(stmt);
287  return false;
288  case SQLITE_CONSTRAINT:
289  case SQLITE_ERROR:
290  // SQLITE_ERROR is a generic error code and we must call sqlite3_reset()
291  // to get the specific error message.
292  res = sqlite3_reset(stmt);
294  "Unable to fetch row"), QSqlError::ConnectionError, res));
296  return false;
297  case SQLITE_MISUSE:
298  case SQLITE_BUSY:
299  default:
300  // something wrong, don't get col info, but still return false
302  "Unable to fetch row"), QSqlError::ConnectionError, res));
303  sqlite3_reset(stmt);
305  return false;
306  }
307  return false;
308 }
309 
311  : QSqlCachedResult(db)
312 {
313  d = new QSQLiteResultPrivate(this);
314  d->access = db->d->access;
315  db->d->results.append(this);
316 }
317 
319 {
320  const QSQLiteDriver * sqlDriver = qobject_cast<const QSQLiteDriver *>(driver());
321  if (sqlDriver)
322  sqlDriver->d->results.removeOne(this);
323  d->cleanup();
324  delete d;
325 }
326 
328 {
329  switch (id) {
331  if (d->stmt)
332  sqlite3_reset(d->stmt);
333  break;
334  default:
336  }
337 }
338 
339 bool QSQLiteResult::reset(const QString &query)
340 {
341  if (!prepare(query))
342  return false;
343  return exec();
344 }
345 
346 bool QSQLiteResult::prepare(const QString &query)
347 {
348  if (!driver() || !driver()->isOpen() || driver()->isOpenError())
349  return false;
350 
351  d->cleanup();
352 
353  setSelect(false);
354 
355  const void *pzTail = NULL;
356 
357 #if (SQLITE_VERSION_NUMBER >= 3003011)
358  int res = sqlite3_prepare16_v2(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
359  &d->stmt, &pzTail);
360 #else
361  int res = sqlite3_prepare16(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
362  &d->stmt, &pzTail);
363 #endif
364 
365  if (res != SQLITE_OK) {
367  "Unable to execute statement"), QSqlError::StatementError, res));
368  d->finalize();
369  return false;
370  } else if (pzTail && !QString(reinterpret_cast<const QChar *>(pzTail)).trimmed().isEmpty()) {
372  "Unable to execute multiple statements at a time"), QSqlError::StatementError, SQLITE_MISUSE));
373  d->finalize();
374  return false;
375  }
376  return true;
377 }
378 
380 {
382 
383  d->skippedStatus = false;
384  d->skipRow = false;
385  d->rInf.clear();
386  clearValues();
388 
389  int res = sqlite3_reset(d->stmt);
390  if (res != SQLITE_OK) {
392  "Unable to reset statement"), QSqlError::StatementError, res));
393  d->finalize();
394  return false;
395  }
396  int paramCount = sqlite3_bind_parameter_count(d->stmt);
397  if (paramCount == values.count()) {
398  for (int i = 0; i < paramCount; ++i) {
399  res = SQLITE_OK;
400  const QVariant value = values.at(i);
401 
402  if (value.isNull()) {
403  res = sqlite3_bind_null(d->stmt, i + 1);
404  } else {
405  switch (value.type()) {
406  case QVariant::ByteArray: {
407  const QByteArray *ba = static_cast<const QByteArray*>(value.constData());
408  res = sqlite3_bind_blob(d->stmt, i + 1, ba->constData(),
409  ba->size(), SQLITE_STATIC);
410  break; }
411  case QVariant::Int:
412  res = sqlite3_bind_int(d->stmt, i + 1, value.toInt());
413  break;
414  case QVariant::Double:
415  res = sqlite3_bind_double(d->stmt, i + 1, value.toDouble());
416  break;
417  case QVariant::UInt:
418  case QVariant::LongLong:
419  res = sqlite3_bind_int64(d->stmt, i + 1, value.toLongLong());
420  break;
421  case QVariant::String: {
422  // lifetime of string == lifetime of its qvariant
423  const QString *str = static_cast<const QString*>(value.constData());
424  res = sqlite3_bind_text16(d->stmt, i + 1, str->utf16(),
425  (str->size()) * sizeof(QChar), SQLITE_STATIC);
426  break; }
427  default: {
428  QString str = value.toString();
429  // SQLITE_TRANSIENT makes sure that sqlite buffers the data
430  res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(),
431  (str.size()) * sizeof(QChar), SQLITE_TRANSIENT);
432  break; }
433  }
434  }
435  if (res != SQLITE_OK) {
437  "Unable to bind parameters"), QSqlError::StatementError, res));
438  d->finalize();
439  return false;
440  }
441  }
442  } else {
444  "Parameter count mismatch"), QString(), QSqlError::StatementError));
445  return false;
446  }
447  d->skippedStatus = d->fetchNext(d->firstRow, 0, true);
448  if (lastError().isValid()) {
449  setSelect(false);
450  setActive(false);
451  return false;
452  }
453  setSelect(!d->rInf.isEmpty());
454  setActive(true);
455  return true;
456 }
457 
459 {
460  return d->fetchNext(row, idx, false);
461 }
462 
464 {
465  return -1;
466 }
467 
469 {
470  return sqlite3_changes(d->access);
471 }
472 
474 {
475  if (isActive()) {
476  qint64 id = sqlite3_last_insert_rowid(d->access);
477  if (id)
478  return id;
479  }
480  return QVariant();
481 }
482 
484 {
485  if (!isActive() || !isSelect())
486  return QSqlRecord();
487  return d->rInf;
488 }
489 
491 {
492  return QVariant::fromValue(d->stmt);
493 }
494 
496 
498  : QSqlDriver(parent)
499 {
500  d = new QSQLiteDriverPrivate();
501 }
502 
504  : QSqlDriver(parent)
505 {
506  d = new QSQLiteDriverPrivate();
507  d->access = connection;
508  setOpen(true);
509  setOpenError(false);
510 }
511 
512 
514 {
515  delete d;
516 }
517 
519 {
520  switch (f) {
521  case BLOB:
522  case Transactions:
523  case Unicode:
524  case LastInsertId:
525  case PreparedQueries:
527  case SimpleLocking:
528  case FinishQuery:
529  case LowPrecisionNumbers:
530  return true;
531  case QuerySize:
532  case NamedPlaceholders:
533  case BatchOperations:
534  case EventNotifications:
535  case MultipleResultSets:
536  return false;
537  }
538  return false;
539 }
540 
541 /*
542  SQLite dbs have no user name, passwords, hosts or ports.
543  just file names.
544 */
545 bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)
546 {
547  if (isOpen())
548  close();
549 
550  if (db.isEmpty())
551  return false;
552  bool sharedCache = false;
553  int openMode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, timeOut=5000;
554  QStringList opts=QString(conOpts).remove(QLatin1Char(' ')).split(QLatin1Char(';'));
555  foreach(const QString &option, opts) {
556  if (option.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) {
557  bool ok;
558  int nt = option.mid(21).toInt(&ok);
559  if (ok)
560  timeOut = nt;
561  }
562  if (option == QLatin1String("QSQLITE_OPEN_READONLY"))
563  openMode = SQLITE_OPEN_READONLY;
564  if (option == QLatin1String("QSQLITE_ENABLE_SHARED_CACHE"))
565  sharedCache = true;
566  }
567 
568  sqlite3_enable_shared_cache(sharedCache);
569 
570  if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, openMode, NULL) == SQLITE_OK) {
571  sqlite3_busy_timeout(d->access, timeOut);
572  setOpen(true);
573  setOpenError(false);
574  return true;
575  } else {
576  setLastError(qMakeError(d->access, tr("Error opening database"),
578  setOpenError(true);
579  return false;
580  }
581 }
582 
584 {
585  if (isOpen()) {
586  foreach (QSQLiteResult *result, d->results)
587  result->d->finalize();
588 
589  if (sqlite3_close(d->access) != SQLITE_OK)
590  setLastError(qMakeError(d->access, tr("Error closing database"),
592  d->access = 0;
593  setOpen(false);
594  setOpenError(false);
595  }
596 }
597 
599 {
600  return new QSQLiteResult(this);
601 }
602 
604 {
605  if (!isOpen() || isOpenError())
606  return false;
607 
608  QSqlQuery q(createResult());
609  if (!q.exec(QLatin1String("BEGIN"))) {
610  setLastError(QSqlError(tr("Unable to begin transaction"),
612  return false;
613  }
614 
615  return true;
616 }
617 
619 {
620  if (!isOpen() || isOpenError())
621  return false;
622 
623  QSqlQuery q(createResult());
624  if (!q.exec(QLatin1String("COMMIT"))) {
625  setLastError(QSqlError(tr("Unable to commit transaction"),
627  return false;
628  }
629 
630  return true;
631 }
632 
634 {
635  if (!isOpen() || isOpenError())
636  return false;
637 
638  QSqlQuery q(createResult());
639  if (!q.exec(QLatin1String("ROLLBACK"))) {
640  setLastError(QSqlError(tr("Unable to rollback transaction"),
642  return false;
643  }
644 
645  return true;
646 }
647 
649 {
650  QStringList res;
651  if (!isOpen())
652  return res;
653 
654  QSqlQuery q(createResult());
655  q.setForwardOnly(true);
656 
657  QString sql = QLatin1String("SELECT name FROM sqlite_master WHERE %1 "
658  "UNION ALL SELECT name FROM sqlite_temp_master WHERE %1");
659  if ((type & QSql::Tables) && (type & QSql::Views))
660  sql = sql.arg(QLatin1String("type='table' OR type='view'"));
661  else if (type & QSql::Tables)
662  sql = sql.arg(QLatin1String("type='table'"));
663  else if (type & QSql::Views)
664  sql = sql.arg(QLatin1String("type='view'"));
665  else
666  sql.clear();
667 
668  if (!sql.isEmpty() && q.exec(sql)) {
669  while(q.next())
670  res.append(q.value(0).toString());
671  }
672 
673  if (type & QSql::SystemTables) {
674  // there are no internal tables beside this one:
675  res.append(QLatin1String("sqlite_master"));
676  }
677 
678  return res;
679 }
680 
681 static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool onlyPIndex = false)
682 {
683  QString schema;
684  QString table(tableName);
685  int indexOfSeparator = tableName.indexOf(QLatin1Char('.'));
686  if (indexOfSeparator > -1) {
687  schema = tableName.left(indexOfSeparator).append(QLatin1Char('.'));
688  table = tableName.mid(indexOfSeparator + 1);
689  }
690  q.exec(QLatin1String("PRAGMA ") + schema + QLatin1String("table_info (") + _q_escapeIdentifier(table) + QLatin1String(")"));
691 
692  QSqlIndex ind;
693  while (q.next()) {
694  bool isPk = q.value(5).toInt();
695  if (onlyPIndex && !isPk)
696  continue;
698  QSqlField fld(q.value(1).toString(), qGetColumnType(typeName));
699  if (isPk && (typeName == QLatin1String("integer")))
700  // INTEGER PRIMARY KEY fields are auto-generated in sqlite
701  // INT PRIMARY KEY is not the same as INTEGER PRIMARY KEY!
702  fld.setAutoValue(true);
703  fld.setRequired(q.value(3).toInt() != 0);
704  fld.setDefaultValue(q.value(4));
705  ind.append(fld);
706  }
707  return ind;
708 }
709 
711 {
712  if (!isOpen())
713  return QSqlIndex();
714 
715  QString table = tblname;
717  table = stripDelimiters(table, QSqlDriver::TableName);
718 
719  QSqlQuery q(createResult());
720  q.setForwardOnly(true);
721  return qGetTableInfo(q, table, true);
722 }
723 
725 {
726  if (!isOpen())
727  return QSqlRecord();
728 
729  QString table = tbl;
731  table = stripDelimiters(table, QSqlDriver::TableName);
732 
733  QSqlQuery q(createResult());
734  q.setForwardOnly(true);
735  return qGetTableInfo(q, table);
736 }
737 
739 {
740  return QVariant::fromValue(d->access);
741 }
742 
744 {
745  Q_UNUSED(type);
746  return _q_escapeIdentifier(identifier);
747 }
748 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
The QSqlIndex class provides functions to manipulate and describe database indexes.
Definition: qsqlindex.h:55
T qobject_cast(QObject *object)
Definition: qobject.h:375
static QVariant::Type qGetColumnType(const QString &tpName)
Definition: qsql_sqlite.cpp:78
QVariant data(int i)
Returns the data for field index in the current row as a QVariant.
QVariant lastInsertId() const
Returns the object ID of the most recent inserted row if the database supports it.
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
QVector< QVariant > firstRow
int type
Definition: qmetatype.cpp:239
QStringList tables(QSql::TableType) const
Returns a list of the names of the tables in the database.
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QVariant handle() const
Returns the low-level database handle for this result set wrapped in a QVariant or an invalid QVarian...
virtual void setOpen(bool o)
This function sets the open state of the database to open.
Definition: qsqldriver.cpp:283
DriverFeature
This enum contains a list of features a driver might support.
Definition: qsqldriver.h:75
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
virtual void setOpenError(bool e)
This function sets the open error state of the database to error.
Definition: qsqldriver.cpp:297
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
void append(const QSqlField &field)
Appends the field field to the list of indexed fields.
Definition: qsqlindex.cpp:129
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
bool hasFeature(DriverFeature f) const
Returns true if the driver supports feature feature; otherwise returns false.
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString & prepend(QChar c)
Definition: qstring.h:261
QSQLiteDriverPrivate * d
Definition: qsql_sqlite.h:116
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: qsqlquery.h:63
static QSqlError qMakeError(sqlite3 *access, const QString &descr, QSqlError::ErrorType type, int errorCode=-1)
Definition: qsql_sqlite.cpp:95
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if there are no fields in the record; otherwise returns false.
Definition: qsqlrecord.cpp:380
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
Definition: qsqldriver.cpp:182
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
Definition: qsqldriver.h:68
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QSQLiteResult * q
static QString _q_escapeIdentifier(const QString &identifier)
Definition: qsql_sqlite.cpp:67
virtual void setAt(int at)
This function is provided for derived classes to set the internal (zero-based) row position to index...
Definition: qsqlresult.cpp:352
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
virtual void setLastError(const QSqlError &e)
This function is provided for derived classes to set the last error to error.
Definition: qsqlresult.cpp:417
bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const
Returns whether identifier is escaped according to the database rules.
Definition: qsqldriver.cpp:429
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
bool isOpenError() const
Returns true if the there was an error opening the database connection; otherwise returns false...
Definition: qsqldriver.cpp:192
ErrorType
This enum type describes the context in which the error occurred, e.
Definition: qsqlerror.h:56
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
Definition: qlist.h:796
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
bool fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch)
static bool isEmpty(const char *str)
virtual void setSelect(bool s)
This function is provided for derived classes to indicate whether or not the current statement is a S...
Definition: qsqlresult.cpp:367
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int access(const char *, int)
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
const char * typeName
Definition: qmetatype.cpp:239
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
TableType
Definition: qsql.h:74
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
bool commitTransaction()
This function is called to commit a transaction.
QString right(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n rightmost characters of the string.
Definition: qstring.cpp:3682
__int64 qint64
Definition: qglobal.h:942
bool gotoNext(QSqlCachedResult::ValueCache &row, int idx)
quint16 values[128]
#define nt(var, enu)
void close()
Derived classes must reimplement this pure virtual function in order to close the database connection...
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
bool prepare(const QString &query)
Prepares the given query for execution; the query will normally use placeholders so that it can be ex...
QSQLiteResult(const QSQLiteDriver *db)
QString escapeIdentifier(const QString &identifier, IdentifierType) const
Returns the identifier escaped according to the database rules.
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
void setForwardOnly(bool forward)
Sets forward only mode to forward.
Definition: qsqlquery.cpp:835
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool reset(const QString &query)
Sets the result to use the SQL statement query for subsequent data retrieval.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
friend class QSQLiteResultPrivate
Definition: qsql_sqlite.h:67
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int count() const
Returns the number of fields in the record.
Definition: qsqlrecord.cpp:573
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QSQLiteResultPrivate(QSQLiteResult *res)
int size()
Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELE...
QSqlError lastError() const
Returns error information about the last error (if any) that occurred with this query.
Definition: qsqlquery.cpp:754
QString & append(QChar c)
Definition: qstring.cpp:1777
QSqlResult * createResult() const
Creates an empty SQL result on the database.
QString trimmed(QString source)
Definition: generator.cpp:233
QString databaseText() const
Returns the text of the error as reported by the database.
Definition: qsqlerror.cpp:169
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QSQLiteDriver(QObject *parent=0)
const void * constData() const
Definition: qvariant.cpp:3065
QVariant handle() const
Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no hand...
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
bool rollbackTransaction()
This function is called to rollback a transaction.
static void cleanup()
Definition: qpicture.cpp:1508
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
Definition: qsqlrecord.cpp:312
QList< QSQLiteResult * > results
QSqlError lastError() const
Returns the last error associated with the result.
Definition: qsqlresult.cpp:427
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402
void clear()
Removes all the record&#39;s fields.
Definition: qsqlrecord.cpp:367
QSqlIndex primaryIndex(const QString &table) const
Returns the primary index for table tableName.
QSqlRecord record() const
Returns the current record if the query is active; otherwise returns an empty QSqlRecord.
static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool onlyPIndex=false)
bool isValid() const
Returns true if the result is positioned on a valid record (that is, the result is not positioned bef...
Definition: qsqlresult.cpp:320
bool open(const QString &db, const QString &user, const QString &password, const QString &host, int port, const QString &connOpts)
Derived classes must reimplement this pure virtual function to open a database connection on database...
friend class QSQLiteResult
Definition: qsql_sqlite.h:91
sqlite3_stmt * stmt
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
void init(int colCount)
bool exec()
Executes the query, returning true if successful; otherwise returns false.
QSqlRecord record(const QString &tablename) const
Returns a QSqlRecord populated with the names of the fields in table tableName.
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
bool exec(const QString &query)
Executes the SQL in query.
Definition: qsqlquery.cpp:355
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database...
Definition: qsqldriver.cpp:350
bool beginTransaction()
This function is called to begin a transaction.
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
void setAutoValue(bool autoVal)
Marks the field as an auto-generated value if autoVal is true.
Definition: qsqlfield.cpp:575
void virtual_hook(int id, void *data)
QString stripDelimiters(const QString &identifier, IdentifierType type) const
Returns the identifier with the leading and trailing delimiters removed, identifier can either be a t...
Definition: qsqldriver.cpp:455
QVector< QVariant > & boundValues() const
Returns a vector of the result&#39;s bound values for the current record (row).
Definition: qsqlresult.cpp:859
void initColumns(bool emptyResultset)
QVariant value(int i) const
Returns the value of field index in the current record.
Definition: qsqlquery.cpp:403
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: qsqlquery.cpp:594
The QSqlResult class provides an abstract interface for accessing data from specific SQL databases...
Definition: qsqlresult.h:63
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void virtual_hook(int id, void *data)
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
IdentifierType
This enum contains a list of SQL identifier types.
Definition: qsqldriver.h:83
int numRowsAffected()
Returns the number of rows affected by the last query executed, or -1 if it cannot be determined or i...
void setSqlType(int type)
Definition: qsqlfield.cpp:285
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290