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

#include <qsql_mysql.h>

Inheritance diagram for QMYSQLResult:
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...
 
 QMYSQLResult (const QMYSQLDriver *db)
 
 ~QMYSQLResult ()
 
- Public Functions inherited from QSqlResult
virtual ~QSqlResult ()
 Destroys the object and frees any allocated resources. More...
 

Protected Functions

void cleanup ()
 
QVariant data (int field)
 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 isNull (int field)
 Returns true if the field at position index in the current row is null; otherwise returns false. More...
 
QVariant lastInsertId () const
 Returns the object ID of the most recent inserted row if the database supports it. More...
 
bool nextResult ()
 
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...
 
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 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...
 
virtual bool exec ()
 Executes the query, returning true if successful; otherwise returns false. 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...
 
virtual bool fetchPrevious ()
 Positions the result to the previous record (row) in the result. 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
 
virtual 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...
 
 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

QMYSQLResultPrivated
 

Friends

class QMYSQLDriver
 
class QMYSQLResultPrivate
 

Additional Inherited Members

- 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 69 of file qsql_mysql.h.

Constructors and Destructors

◆ QMYSQLResult()

QMYSQLResult::QMYSQLResult ( const QMYSQLDriver db)
explicit

Definition at line 407 of file qsql_mysql.cpp.

408 : QSqlResult(db)
409 {
410  d = new QMYSQLResultPrivate(db, this);
411 }
friend class QMYSQLResultPrivate
Definition: qsql_mysql.h:72
QSqlResult(const QSqlDriver *db)
Creates a QSqlResult using database driver db.
Definition: qsqlresult.cpp:257
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99

◆ ~QMYSQLResult()

QMYSQLResult::~QMYSQLResult ( )

Definition at line 413 of file qsql_mysql.cpp.

414 {
415  cleanup();
416  delete d;
417 }
void cleanup()
Definition: qsql_mysql.cpp:429
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99

Functions

◆ cleanup()

void QMYSQLResult::cleanup ( )
protected

Definition at line 429 of file qsql_mysql.cpp.

Referenced by reset(), virtual_hook(), and ~QMYSQLResult().

430 {
431  if (d->result)
432  mysql_free_result(d->result);
433 
434 // must iterate trough leftover result sets from multi-selects or stored procedures
435 // if this isn't done subsequent queries will fail with "Commands out of sync"
436 #if MYSQL_VERSION_ID >= 40100
437  while (d->driver && d->driver->d->mysql && mysql_next_result(d->driver->d->mysql) == 0) {
438  MYSQL_RES *res = mysql_store_result(d->driver->d->mysql);
439  if (res)
440  mysql_free_result(res);
441  }
442 #endif
443 
444 #if MYSQL_VERSION_ID >= 40108
445  if (d->stmt) {
446  if (mysql_stmt_close(d->stmt))
447  qWarning("QMYSQLResult::cleanup: unable to free statement handle");
448  d->stmt = 0;
449  }
450 
451  if (d->meta) {
452  mysql_free_result(d->meta);
453  d->meta = 0;
454  }
455 
456  int i;
457  for (i = 0; i < d->fields.count(); ++i)
458  delete[] d->fields[i].outField;
459 
460  if (d->outBinds) {
461  delete[] d->outBinds;
462  d->outBinds = 0;
463  }
464 
465  if (d->inBinds) {
466  delete[] d->inBinds;
467  d->inBinds = 0;
468  }
469 #endif
470 
471  d->hasBlobs = false;
472  d->fields.clear();
473  d->result = NULL;
474  d->row = NULL;
475  setAt(-1);
476  setActive(false);
477 }
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
Q_CORE_EXPORT void qWarning(const char *,...)
QVector< QMyField > fields
Definition: qsql_mysql.cpp:203
if(void) toggleToolbarShown
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ data()

QVariant QMYSQLResult::data ( int  index)
protectedvirtual

Returns the data for field index in the current row as a QVariant.

This function is only called if the result is in an active state and is positioned on a valid record and index is non-negative. Derived classes must reimplement this function and return the value of field index, or QVariant() if it cannot be determined.

Implements QSqlResult.

Definition at line 589 of file qsql_mysql.cpp.

Referenced by virtual_hook().

590 {
591 
592  if (!isSelect() || field >= d->fields.count()) {
593  qWarning("QMYSQLResult::data: column %d out of range", field);
594  return QVariant();
595  }
596 
597  if (!d->driver)
598  return QVariant();
599 
600  int fieldLength = 0;
601  const QMYSQLResultPrivate::QMyField &f = d->fields.at(field);
602  QString val;
603  if (d->preparedQuery) {
604  if (f.nullIndicator)
605  return QVariant(f.type);
606 
607  if (f.type != QVariant::ByteArray)
608  val = toUnicode(d->driver->d->tc, f.outField, f.bufLength);
609  } else {
610  if (d->row[field] == NULL) {
611  // NULL value
612  return QVariant(f.type);
613  }
614  fieldLength = mysql_fetch_lengths(d->result)[field];
615  if (f.type != QVariant::ByteArray)
616  val = toUnicode(d->driver->d->tc, d->row[field], fieldLength);
617  }
618 
619  switch(f.type) {
620  case QVariant::LongLong:
621  return QVariant(val.toLongLong());
622  case QVariant::ULongLong:
623  return QVariant(val.toULongLong());
624  case QVariant::Int:
625  return QVariant(val.toInt());
626  case QVariant::UInt:
627  return QVariant(val.toUInt());
628  case QVariant::Double: {
629  QVariant v;
630  bool ok=false;
631  double dbl = val.toDouble(&ok);
632  switch(numericalPrecisionPolicy()) {
634  v=QVariant(dbl).toInt();
635  break;
637  v = QVariant(dbl).toLongLong();
638  break;
640  v = QVariant(dbl);
641  break;
642  case QSql::HighPrecision:
643  default:
644  v = val;
645  ok = true;
646  break;
647  }
648  if(ok)
649  return v;
650  else
651  return QVariant();
652  }
653  return QVariant(val.toDouble());
654  case QVariant::Date:
655  return qDateFromString(val);
656  case QVariant::Time:
657  return qTimeFromString(val);
658  case QVariant::DateTime:
659  return qDateTimeFromString(val);
660  case QVariant::ByteArray: {
661 
662  QByteArray ba;
663  if (d->preparedQuery) {
664  ba = QByteArray(f.outField, f.bufLength);
665  } else {
666  ba = QByteArray(d->row[field], fieldLength);
667  }
668  return QVariant(ba);
669  }
670  default:
671  case QVariant::String:
672  return QVariant(val);
673  }
674  qWarning("QMYSQLResult::data: unknown data type");
675  return QVariant();
676 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QVariant qTimeFromString(const QString &val)
Definition: qsql_mysql.cpp:137
qlonglong toLongLong(bool *ok=0, int base=10) const
Returns the string converted to a long long using base base, which is 10 by default and must be betwe...
Definition: qstring.cpp:5943
qulonglong toULongLong(bool *ok=0, int base=10) const
Returns the string converted to an unsigned long long using base base, which is 10 by default and mus...
Definition: qstring.cpp:5984
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
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
QTextCodec * tc
Definition: qsql_mysql.cpp:90
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
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
static QVariant qDateTimeFromString(QString &val)
Definition: qsql_mysql.cpp:149
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
Q_CORE_EXPORT void qWarning(const char *,...)
QVector< QMyField > fields
Definition: qsql_mysql.cpp:203
static QVariant qDateFromString(const QString &val)
Definition: qsql_mysql.cpp:125
static QString toUnicode(QTextCodec *tc, const char *str)
Definition: qsql_mysql.cpp:95
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
uint toUInt(bool *ok=0, int base=10) const
Returns the string converted to an unsigned int using base base, which is 10 by default and must be b...
Definition: qstring.cpp:6120
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ fetch()

bool QMYSQLResult::fetch ( int  index)
protectedvirtual

Positions the result to an arbitrary (zero-based) row index.

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the row index, and call setAt() with an appropriate value. Return true to indicate success, or false to signify failure.

See also
isActive(), fetchFirst(), fetchLast(), fetchNext(), fetchPrevious()

Implements QSqlResult.

Definition at line 479 of file qsql_mysql.cpp.

Referenced by fetchFirst(), and fetchLast().

480 {
481  if(!d->driver)
482  return false;
483  if (isForwardOnly()) { // fake a forward seek
484  if (at() < i) {
485  int x = i - at();
486  while (--x && fetchNext()) {};
487  return fetchNext();
488  } else {
489  return false;
490  }
491  }
492  if (at() == i)
493  return true;
494  if (d->preparedQuery) {
495 #if MYSQL_VERSION_ID >= 40108
496  mysql_stmt_data_seek(d->stmt, i);
497 
498  int nRC = mysql_stmt_fetch(d->stmt);
499  if (nRC) {
500 #ifdef MYSQL_DATA_TRUNCATED
501  if (nRC == 1 || nRC == MYSQL_DATA_TRUNCATED)
502 #else
503  if (nRC == 1)
504 #endif
505  setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult",
506  "Unable to fetch data"), QSqlError::StatementError, d->stmt));
507  return false;
508  }
509 #else
510  return false;
511 #endif
512  } else {
513  mysql_data_seek(d->result, i);
514  d->row = mysql_fetch_row(d->result);
515  if (!d->row)
516  return false;
517  }
518 
519  setAt(i);
520  return true;
521 }
bool isForwardOnly() const
Returns true if you can only scroll forward through the result set; otherwise returns false...
Definition: qsqlresult.cpp:582
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
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
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
bool fetchNext()
Positions the result to the next available record (row) in the result.
Definition: qsql_mysql.cpp:523

◆ fetchFirst()

bool QMYSQLResult::fetchFirst ( )
protectedvirtual

Positions the result to the first record (row 0) in the result.

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the first record, and call setAt() with an appropriate value. Return true to indicate success, or false to signify failure.

See also
fetch(), fetchLast()

Implements QSqlResult.

Definition at line 579 of file qsql_mysql.cpp.

580 {
581  if (at() == 0)
582  return true;
583 
584  if (isForwardOnly())
585  return (at() == QSql::BeforeFirstRow) ? fetchNext() : false;
586  return fetch(0);
587 }
bool isForwardOnly() const
Returns true if you can only scroll forward through the result set; otherwise returns false...
Definition: qsqlresult.cpp:582
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
bool fetch(int i)
Positions the result to an arbitrary (zero-based) row index.
Definition: qsql_mysql.cpp:479
bool fetchNext()
Positions the result to the next available record (row) in the result.
Definition: qsql_mysql.cpp:523

◆ fetchLast()

bool QMYSQLResult::fetchLast ( )
protectedvirtual

Positions the result to the last record (last row) in the result.

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the last record, and call setAt() with an appropriate value. Return true to indicate success, or false to signify failure.

See also
fetch(), fetchFirst()

Implements QSqlResult.

Definition at line 552 of file qsql_mysql.cpp.

553 {
554  if(!d->driver)
555  return false;
556  if (isForwardOnly()) { // fake this since MySQL can't seek on forward only queries
557  bool success = fetchNext(); // did we move at all?
558  while (fetchNext()) {};
559  return success;
560  }
561 
562  my_ulonglong numRows;
563  if (d->preparedQuery) {
564 #if MYSQL_VERSION_ID >= 40108
565  numRows = mysql_stmt_num_rows(d->stmt);
566 #else
567  numRows = 0;
568 #endif
569  } else {
570  numRows = mysql_num_rows(d->result);
571  }
572  if (at() == int(numRows))
573  return true;
574  if (!numRows)
575  return false;
576  return fetch(numRows - 1);
577 }
bool isForwardOnly() const
Returns true if you can only scroll forward through the result set; otherwise returns false...
Definition: qsqlresult.cpp:582
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
bool fetch(int i)
Positions the result to an arbitrary (zero-based) row index.
Definition: qsql_mysql.cpp:479
bool fetchNext()
Positions the result to the next available record (row) in the result.
Definition: qsql_mysql.cpp:523

◆ fetchNext()

bool QMYSQLResult::fetchNext ( )
protectedvirtual

Positions the result to the next available record (row) in the result.

This function is only called if the result is in an active state. The default implementation calls fetch() with the next index. Derived classes can reimplement this function and position the result to the next record in some other way, and call setAt() with an appropriate value. Return true to indicate success, or false to signify failure.

See also
fetch(), fetchPrevious()

Reimplemented from QSqlResult.

Definition at line 523 of file qsql_mysql.cpp.

Referenced by fetch(), fetchFirst(), and fetchLast().

524 {
525  if(!d->driver)
526  return false;
527  if (d->preparedQuery) {
528 #if MYSQL_VERSION_ID >= 40108
529  int nRC = mysql_stmt_fetch(d->stmt);
530  if (nRC) {
531 #ifdef MYSQL_DATA_TRUNCATED
532  if (nRC == 1 || nRC == MYSQL_DATA_TRUNCATED)
533 #else
534  if (nRC == 1)
535 #endif // MYSQL_DATA_TRUNCATED
536  setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult",
537  "Unable to fetch data"), QSqlError::StatementError, d->stmt));
538  return false;
539  }
540 #else
541  return false;
542 #endif
543  } else {
544  d->row = mysql_fetch_row(d->result);
545  if (!d->row)
546  return false;
547  }
548  setAt(at() + 1);
549  return true;
550 }
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
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
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179

◆ handle()

QVariant QMYSQLResult::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 419 of file qsql_mysql.cpp.

420 {
421 #if MYSQL_VERSION_ID >= 40108
422  if(d->preparedQuery)
423  return d->meta ? QVariant::fromValue(d->meta) : qVariantFromValue(d->stmt);
424  else
425 #endif
426  return QVariant::fromValue(d->result);
427 }
QVariant qVariantFromValue(const T &)
Definition: qvariant.h:451
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336

◆ isNull()

bool QMYSQLResult::isNull ( int  i)
protectedvirtual

Returns true if the field at position index in the current row is null; otherwise returns false.

Implements QSqlResult.

Definition at line 678 of file qsql_mysql.cpp.

679 {
680  if (d->preparedQuery)
681  return d->fields.at(field).nullIndicator;
682  else
683  return d->row[field] == NULL;
684 }
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
QVector< QMyField > fields
Definition: qsql_mysql.cpp:203

◆ lastInsertId()

QVariant QMYSQLResult::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 743 of file qsql_mysql.cpp.

744 {
745  if (!isActive() || !d->driver)
746  return QVariant();
747 
748  if (d->preparedQuery) {
749 #if MYSQL_VERSION_ID >= 40108
750  quint64 id = mysql_stmt_insert_id(d->stmt);
751  if (id)
752  return QVariant(id);
753 #endif
754  } else {
755  quint64 id = mysql_insert_id(d->driver->d->mysql);
756  if (id)
757  return QVariant(id);
758  }
759  return QVariant();
760 }
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
unsigned __int64 quint64
Definition: qglobal.h:943
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ nextResult()

bool QMYSQLResult::nextResult ( )
protected

Definition at line 787 of file qsql_mysql.cpp.

Referenced by virtual_hook().

788 {
789  if(!d->driver)
790  return false;
791 #if MYSQL_VERSION_ID >= 40100
792  setAt(-1);
793  setActive(false);
794 
795  if (d->result && isSelect())
796  mysql_free_result(d->result);
797  d->result = 0;
798  setSelect(false);
799 
800  for (int i = 0; i < d->fields.count(); ++i)
801  delete[] d->fields[i].outField;
802  d->fields.clear();
803 
804  int status = mysql_next_result(d->driver->d->mysql);
805  if (status > 0) {
806  setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute next query"),
808  return false;
809  } else if (status == -1) {
810  return false; // No more result sets
811  }
812 
813  d->result = mysql_store_result(d->driver->d->mysql);
814  int numFields = mysql_field_count(d->driver->d->mysql);
815  if (!d->result && numFields > 0) {
816  setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store next result"),
818  return false;
819  }
820 
821  setSelect(numFields > 0);
822  d->fields.resize(numFields);
823  d->rowsAffected = mysql_affected_rows(d->driver->d->mysql);
824 
825  if (isSelect()) {
826  for (int i = 0; i < numFields; i++) {
827  MYSQL_FIELD* field = mysql_fetch_field_direct(d->result, i);
828  d->fields[i].type = qDecodeMYSQLType(field->type, field->flags);
829  }
830  }
831 
832  setActive(true);
833  return true;
834 #else
835  return false;
836 #endif
837 }
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QMYSQLDriverPrivate *p)
Definition: qsql_mysql.cpp:231
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
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
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
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
QVector< QMyField > fields
Definition: qsql_mysql.cpp:203
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402
static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags)
Definition: qsql_mysql.cpp:241
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ numRowsAffected()

int QMYSQLResult::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 738 of file qsql_mysql.cpp.

739 {
740  return d->rowsAffected;
741 }
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99

◆ record()

QSqlRecord QMYSQLResult::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 762 of file qsql_mysql.cpp.

763 {
765  MYSQL_RES *res;
766  if (!isActive() || !isSelect() || !d->driver)
767  return info;
768 
769 #if MYSQL_VERSION_ID >= 40108
770  res = d->preparedQuery ? d->meta : d->result;
771 #else
772  res = d->result;
773 #endif
774 
775  if (!mysql_errno(d->driver->d->mysql)) {
776  mysql_field_seek(res, 0);
777  MYSQL_FIELD* field = mysql_fetch_field(res);
778  while(field) {
779  info.append(qToField(field, d->driver->d->tc));
780  field = mysql_fetch_field(res);
781  }
782  }
783  mysql_field_seek(res, 0);
784  return info;
785 }
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
static mach_timebase_info_data_t info
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
QTextCodec * tc
Definition: qsql_mysql.cpp:90
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
static QSqlField qToField(MYSQL_FIELD *field, QTextCodec *tc)
Definition: qsql_mysql.cpp:292
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
Definition: qsqlrecord.cpp:312
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ reset()

bool QMYSQLResult::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 686 of file qsql_mysql.cpp.

687 {
688  if (!driver() || !driver()->isOpen() || driver()->isOpenError() || !d->driver)
689  return false;
690 
691  d->preparedQuery = false;
692 
693  cleanup();
694 
695  const QByteArray encQuery(fromUnicode(d->driver->d->tc, query));
696  if (mysql_real_query(d->driver->d->mysql, encQuery.data(), encQuery.length())) {
697  setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute query"),
699  return false;
700  }
701  d->result = mysql_store_result(d->driver->d->mysql);
702  if (!d->result && mysql_field_count(d->driver->d->mysql) > 0) {
703  setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store result"),
705  return false;
706  }
707  int numFields = mysql_field_count(d->driver->d->mysql);
708  setSelect(numFields != 0);
709  d->fields.resize(numFields);
710  d->rowsAffected = mysql_affected_rows(d->driver->d->mysql);
711 
712  if (isSelect()) {
713  for(int i = 0; i < numFields; i++) {
714  MYSQL_FIELD* field = mysql_fetch_field_direct(d->result, i);
715  d->fields[i].type = qDecodeMYSQLType(field->type, field->flags);
716  }
718  }
719  setActive(true);
720  return isActive();
721 }
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
QTextCodec * tc
Definition: qsql_mysql.cpp:90
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QMYSQLDriverPrivate *p)
Definition: qsql_mysql.cpp:231
void cleanup()
Definition: qsql_mysql.cpp:429
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
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
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
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
QVector< QMyField > fields
Definition: qsql_mysql.cpp:203
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402
static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags)
Definition: qsql_mysql.cpp:241
static QByteArray fromUnicode(QTextCodec *tc, const QString &str)
Definition: qsql_mysql.cpp:115
QMYSQLDriverPrivate * d
Definition: qsql_mysql.h:136

◆ size()

int QMYSQLResult::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 723 of file qsql_mysql.cpp.

724 {
725  if (d->driver && isSelect())
726  if (d->preparedQuery)
727 #if MYSQL_VERSION_ID >= 40108
728  return mysql_stmt_num_rows(d->stmt);
729 #else
730  return -1;
731 #endif
732  else
733  return int(mysql_num_rows(d->result));
734  else
735  return -1;
736 }
MYSQL_RES * result
Definition: qsql_mysql.cpp:180
QMYSQLResultPrivate * d
Definition: qsql_mysql.h:99
const QMYSQLDriver * driver
Definition: qsql_mysql.cpp:179
bool isSelect() const
Returns true if the current result is from a SELECT statement; otherwise returns false.
Definition: qsqlresult.cpp:379

◆ virtual_hook()

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

Reimplemented from QSqlResult.

Definition at line 839 of file qsql_mysql.cpp.

840 {
841  switch (id) {
843  Q_ASSERT(data);
844  *static_cast<bool*>(data) = nextResult();
845  break;
846  default:
848  }
849 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual void virtual_hook(int id, void *data)
Definition: qsqlresult.cpp:962
QVariant data(int field)
Returns the data for field index in the current row as a QVariant.
Definition: qsql_mysql.cpp:589
bool nextResult()
Definition: qsql_mysql.cpp:787

Friends and Related Functions

◆ QMYSQLDriver

friend class QMYSQLDriver
friend

Definition at line 71 of file qsql_mysql.h.

◆ QMYSQLResultPrivate

friend class QMYSQLResultPrivate
friend

Definition at line 72 of file qsql_mysql.h.

Referenced by QMYSQLResult().

Properties

◆ d

QMYSQLResultPrivate* QMYSQLResult::d
private

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