Qt 4.8
Public Functions | Protected Functions | Properties | Friends | List of all members
QSQLiteResult Class Reference

#include <qsql_sqlite.h>

Inheritance diagram for QSQLiteResult:
QSqlCachedResult QSqlResult

Public Functions

QVariant handle () const
 Returns the low-level database handle for this result set wrapped in a QVariant or an invalid QVariant if there is no handle. More...
 
 QSQLiteResult (const QSQLiteDriver *db)
 
 ~QSQLiteResult ()
 
- Public Functions inherited from QSqlCachedResult
virtual ~QSqlCachedResult ()
 
- Public Functions inherited from QSqlResult
virtual ~QSqlResult ()
 Destroys the object and frees any allocated resources. More...
 

Protected Functions

bool exec ()
 Executes the query, returning true if successful; otherwise returns false. More...
 
bool gotoNext (QSqlCachedResult::ValueCache &row, int idx)
 
QVariant lastInsertId () const
 Returns the object ID of the most recent inserted row if the database supports it. More...
 
int numRowsAffected ()
 Returns the number of rows affected by the last query executed, or -1 if it cannot be determined or if the query is a SELECT statement. More...
 
bool prepare (const QString &query)
 Prepares the given query for execution; the query will normally use placeholders so that it can be executed repeatedly. More...
 
QSqlRecord record () const
 Returns the current record if the query is active; otherwise returns an empty QSqlRecord. More...
 
bool reset (const QString &query)
 Sets the result to use the SQL statement query for subsequent data retrieval. More...
 
int size ()
 Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELECT statement. More...
 
void virtual_hook (int id, void *data)
 
- Protected Functions inherited from QSqlCachedResult
ValueCachecache ()
 
void cleanup ()
 
void clearValues ()
 
int colCount () const
 
QVariant data (int i)
 Returns the data for field index in the current row as a QVariant. More...
 
bool fetch (int i)
 Positions the result to an arbitrary (zero-based) row index. More...
 
bool fetchFirst ()
 Positions the result to the first record (row 0) in the result. More...
 
bool fetchLast ()
 Positions the result to the last record (last row) in the result. More...
 
bool fetchNext ()
 Positions the result to the next available record (row) in the result. More...
 
bool fetchPrevious ()
 Positions the result to the previous record (row) in the result. More...
 
void init (int colCount)
 
bool isNull (int i)
 Returns true if the field at position index in the current row is null; otherwise returns false. More...
 
 QSqlCachedResult (const QSqlDriver *db)
 
void virtual_hook (int id, void *data)
 
- Protected Functions inherited from QSqlResult
void addBindValue (const QVariant &val, QSql::ParamType type)
 Binds the value val of parameter type paramType to the next available position in the current record (row). More...
 
int at () const
 Returns the current (zero-based) row position of the result. More...
 
BindingSyntax bindingSyntax () const
 Returns the binding syntax used by prepared queries. More...
 
virtual void bindValue (int pos, const QVariant &val, QSql::ParamType type)
 Binds the value val of parameter type paramType to position index in the current record (row). More...
 
virtual void bindValue (const QString &placeholder, const QVariant &val, QSql::ParamType type)
 Binds the value val of parameter type paramType to the placeholder name in the current record (row). More...
 
QSql::ParamType bindValueType (const QString &placeholder) const
 Returns the parameter type for the value bound with the given placeholder name. More...
 
QSql::ParamType bindValueType (int pos) const
 Returns the parameter type for the value bound at position index. More...
 
QVariant boundValue (const QString &placeholder) const
 Returns the value bound by the given placeholder name in the current record (row). More...
 
QVariant boundValue (int pos) const
 Returns the value bound at position index in the current record (row). More...
 
int boundValueCount () const
 Returns the number of bound values in the result. More...
 
QString boundValueName (int pos) const
 Returns the name of the bound value at position index in the current record (row). More...
 
QVector< QVariant > & boundValues () const
 Returns a vector of the result's bound values for the current record (row). More...
 
void clear ()
 Clears the entire result set and releases any associated resources. More...
 
void detachFromResultSet ()
 
const QSqlDriverdriver () const
 Returns the driver associated with the result. More...
 
bool execBatch (bool arrayBind=false)
 Executes a prepared query in batch mode if the driver supports it, otherwise emulates a batch execution using bindValue() and exec(). More...
 
QString executedQuery () const
 Returns the query that was actually executed. More...
 
bool hasOutValues () const
 Returns true if at least one of the query's bound values is a QSql::Out or a QSql::InOut; otherwise returns false. More...
 
bool isActive () const
 Returns true if the result has records to be retrieved; otherwise returns false. More...
 
bool isForwardOnly () const
 Returns true if you can only scroll forward through the result set; otherwise returns false. More...
 
bool isSelect () const
 Returns true if the current result is from a SELECT statement; otherwise returns false. More...
 
bool isValid () const
 Returns true if the result is positioned on a valid record (that is, the result is not positioned before the first or after the last record); otherwise returns false. More...
 
QSqlError lastError () const
 Returns the last error associated with the result. More...
 
QString lastQuery () const
 Returns the current SQL query text, or an empty string if there isn't one. More...
 
bool nextResult ()
 
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy () const
 
 QSqlResult (const QSqlDriver *db)
 Creates a QSqlResult using database driver db. More...
 
virtual bool savePrepare (const QString &sqlquery)
 Prepares the given query, using the underlying database functionality where possible. More...
 
virtual void setActive (bool a)
 This function is provided for derived classes to set the internal active state to active. More...
 
virtual void setAt (int at)
 This function is provided for derived classes to set the internal (zero-based) row position to index. More...
 
virtual void setForwardOnly (bool forward)
 Sets forward only mode to forward. More...
 
virtual void setLastError (const QSqlError &e)
 This function is provided for derived classes to set the last error to error. More...
 
void setNumericalPrecisionPolicy (QSql::NumericalPrecisionPolicy policy)
 
virtual void setQuery (const QString &query)
 Sets the current query for the result to query. More...
 
virtual void setSelect (bool s)
 This function is provided for derived classes to indicate whether or not the current statement is a SQL SELECT statement. More...
 

Properties

QSQLiteResultPrivated
 

Friends

class QSQLiteDriver
 
class QSQLiteResultPrivate
 

Additional Inherited Members

- Public Types inherited from QSqlCachedResult
typedef QVector< QVariantValueCache
 
- Protected Types inherited from QSqlResult
enum  BindingSyntax { PositionalBinding, NamedBinding }
 This enum type specifies the different syntaxes for specifying placeholders in prepared queries. More...
 
enum  VirtualHookOperation { BatchOperation, DetachFromResultSet, SetNumericalPrecision, NextResult }
 

Detailed Description

Definition at line 64 of file qsql_sqlite.h.

Constructors and Destructors

◆ QSQLiteResult()

QSQLiteResult::QSQLiteResult ( const QSQLiteDriver db)
explicit

Definition at line 310 of file qsql_sqlite.cpp.

311  : QSqlCachedResult(db)
312 {
313  d = new QSQLiteResultPrivate(this);
314  d->access = db->d->access;
315  db->d->results.append(this);
316 }
QSQLiteDriverPrivate * d
Definition: qsql_sqlite.h:116
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
friend class QSQLiteResultPrivate
Definition: qsql_sqlite.h:67
QSqlCachedResult(const QSqlDriver *db)
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
QList< QSQLiteResult * > results

◆ ~QSQLiteResult()

QSQLiteResult::~QSQLiteResult ( )

Definition at line 318 of file qsql_sqlite.cpp.

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 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
QSQLiteDriverPrivate * d
Definition: qsql_sqlite.h:116
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
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
QList< QSQLiteResult * > results

Functions

◆ exec()

bool QSQLiteResult::exec ( )
protectedvirtual

Executes the query, returning true if successful; otherwise returns false.

See also
prepare()

Reimplemented from QSqlResult.

Definition at line 379 of file qsql_sqlite.cpp.

Referenced by reset().

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 }
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
QVector< QVariant > firstRow
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
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 isEmpty() const
Returns true if there are no fields in the record; otherwise returns false.
Definition: qsqlrecord.cpp:380
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
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
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
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)
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
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
quint16 values[128]
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
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
const void * constData() const
Definition: qvariant.cpp:3065
QSqlError lastError() const
Returns the last error associated with the result.
Definition: qsqlresult.cpp:427
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
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
sqlite3_stmt * stmt
QVector< QVariant > & boundValues() const
Returns a vector of the result&#39;s bound values for the current record (row).
Definition: qsqlresult.cpp:859
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ gotoNext()

bool QSQLiteResult::gotoNext ( QSqlCachedResult::ValueCache row,
int  idx 
)
protectedvirtual

Implements QSqlCachedResult.

Definition at line 458 of file qsql_sqlite.cpp.

459 {
460  return d->fetchNext(row, idx, false);
461 }
bool fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch)
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85

◆ handle()

QVariant QSQLiteResult::handle ( ) const
virtual

Returns the low-level database handle for this result set wrapped in a QVariant or an invalid QVariant if there is no handle.

Warning
Use this with uttermost care and only if you know what you're doing.
The handle returned here can become a stale pointer if the result is modified (for example, if you clear it).
The handle can be NULL if the result was not executed yet.

The handle returned here is database-dependent, you should query the type name of the variant before accessing it.

This example retrieves the handle for a sqlite result:

QSqlQuery query = ...
QVariant v = query.result()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3_stmt*")) {
// v.data() returns a pointer to the handle
sqlite3_stmt *handle = *static_cast<sqlite3_stmt **>(v.data());
if (handle != 0) { // check that it is not NULL
...
}
}

This snippet returns the handle for PostgreSQL or MySQL:

if (v.typeName() == "PGresult*") {
PGresult *handle = *static_cast<PGresult **>(v.data());
if (handle != 0) ...
}
if (v.typeName() == "MYSQL_STMT*") {
MYSQL_STMT *handle = *static_cast<MYSQL_STMT **>(v.data());
if (handle != 0) ...
}
See also
QSqlDriver::handle()

Reimplemented from QSqlResult.

Definition at line 490 of file qsql_sqlite.cpp.

491 {
492  return QVariant::fromValue(d->stmt);
493 }
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
sqlite3_stmt * stmt

◆ lastInsertId()

QVariant QSQLiteResult::lastInsertId ( ) const
protectedvirtual

Returns the object ID of the most recent inserted row if the database supports it.

An invalid QVariant will be returned if the query did not insert any value or if the database does not report the id back. If more than one row was touched by the insert, the behavior is undefined.

Note that for Oracle databases the row's ROWID will be returned, while for MySQL databases the row's auto-increment field will be returned.

See also
QSqlDriver::hasFeature()

Reimplemented from QSqlResult.

Definition at line 473 of file qsql_sqlite.cpp.

474 {
475  if (isActive()) {
476  qint64 id = sqlite3_last_insert_rowid(d->access);
477  if (id)
478  return id;
479  }
480  return QVariant();
481 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
__int64 qint64
Definition: qglobal.h:942
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85

◆ numRowsAffected()

int QSQLiteResult::numRowsAffected ( )
protectedvirtual

Returns the number of rows affected by the last query executed, or -1 if it cannot be determined or if the query is a SELECT statement.

See also
size()

Implements QSqlResult.

Definition at line 468 of file qsql_sqlite.cpp.

469 {
470  return sqlite3_changes(d->access);
471 }
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85

◆ prepare()

bool QSQLiteResult::prepare ( const QString query)
protectedvirtual

Prepares the given query for execution; the query will normally use placeholders so that it can be executed repeatedly.

Returns true if the query is prepared successfully; otherwise returns false.

See also
exec()

Reimplemented from QSqlResult.

Definition at line 346 of file qsql_sqlite.cpp.

Referenced by reset().

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 }
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
static QSqlError qMakeError(sqlite3 *access, const QString &descr, QSqlError::ErrorType type, int errorCode=-1)
Definition: qsql_sqlite.cpp:95
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
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
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
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
QString trimmed(QString source)
Definition: generator.cpp:233
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
sqlite3_stmt * stmt
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ record()

QSqlRecord QSQLiteResult::record ( ) const
protectedvirtual

Returns the current record if the query is active; otherwise returns an empty QSqlRecord.

The default implementation always returns an empty QSqlRecord.

See also
isActive()

Reimplemented from QSqlResult.

Definition at line 483 of file qsql_sqlite.cpp.

484 {
485  if (!isActive() || !isSelect())
486  return QSqlRecord();
487  return d->rInf;
488 }
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379

◆ reset()

bool QSQLiteResult::reset ( const QString query)
protectedvirtual

Sets the result to use the SQL statement query for subsequent data retrieval.

Derived classes must reimplement this function and apply the query to the database. This function is only called after the result is set to an inactive state and is positioned before the first record of the new result. Derived classes should return true if the query was successful and ready to be used, or false otherwise.

See also
setQuery()

Implements QSqlResult.

Definition at line 339 of file qsql_sqlite.cpp.

340 {
341  if (!prepare(query))
342  return false;
343  return exec();
344 }
bool prepare(const QString &query)
Prepares the given query for execution; the query will normally use placeholders so that it can be ex...
bool exec()
Executes the query, returning true if successful; otherwise returns false.

◆ size()

int QSQLiteResult::size ( )
protectedvirtual

Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELECT statement.

See also
numRowsAffected()

Implements QSqlResult.

Definition at line 463 of file qsql_sqlite.cpp.

464 {
465  return -1;
466 }

◆ virtual_hook()

void QSQLiteResult::virtual_hook ( int  id,
void *  data 
)
protectedvirtual
Warning
This function is not part of the public interface.

Reimplemented from QSqlResult.

Definition at line 327 of file qsql_sqlite.cpp.

328 {
329  switch (id) {
331  if (d->stmt)
332  sqlite3_reset(d->stmt);
333  break;
334  default:
336  }
337 }
QVariant data(int i)
Returns the data for field index in the current row as a QVariant.
QSQLiteResultPrivate * d
Definition: qsql_sqlite.h:85
sqlite3_stmt * stmt
void virtual_hook(int id, void *data)

Friends and Related Functions

◆ QSQLiteDriver

friend class QSQLiteDriver
friend

Definition at line 66 of file qsql_sqlite.h.

◆ QSQLiteResultPrivate

friend class QSQLiteResultPrivate
friend

Definition at line 67 of file qsql_sqlite.h.

Referenced by QSQLiteResult().

Properties

◆ d

QSQLiteResultPrivate* QSQLiteResult::d
private

The documentation for this class was generated from the following files: