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

#include <qsql_psql.h>

Inheritance diagram for QPSQLResult:
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...
 
 QPSQLResult (const QPSQLDriver *db, const QPSQLDriverPrivate *p)
 
void virtual_hook (int id, void *data)
 
 ~QPSQLResult ()
 
- Public Functions inherited from QSqlResult
virtual ~QSqlResult ()
 Destroys the object and frees any allocated resources. More...
 

Protected Functions

void cleanup ()
 
QVariant data (int i)
 Returns the data for field index in the current row as a QVariant. More...
 
bool exec ()
 Executes the query, returning true if successful; otherwise returns false. 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 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...
 
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...
 
- 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...
 
virtual bool fetchNext ()
 Positions the result to the next available record (row) in the result. 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
 
 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

QPSQLResultPrivated
 

Friends

class QPSQLResultPrivate
 

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 66 of file qsql_psql.h.

Constructors and Destructors

◆ QPSQLResult()

QPSQLResult::QPSQLResult ( const QPSQLDriver db,
const QPSQLDriverPrivate p 
)

Definition at line 296 of file qsql_psql.cpp.

297  : QSqlResult(db)
298 {
299  d = new QPSQLResultPrivate(this);
300  d->driver = p;
302 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
QSqlResult(const QSqlDriver *db)
Creates a QSqlResult using database driver db.
Definition: qsqlresult.cpp:257
friend class QPSQLResultPrivate
Definition: qsql_psql.h:68
bool hasFeature(DriverFeature f) const
Returns true if the driver supports feature feature; otherwise returns false.
Definition: qsql_psql.cpp:796
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196

◆ ~QPSQLResult()

QPSQLResult::~QPSQLResult ( )

Definition at line 304 of file qsql_psql.cpp.

305 {
306  cleanup();
307 
310 
311  delete d;
312 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
QString preparedStmtId
Definition: qsql_psql.cpp:200
static void qDeallocatePreparedStmt(QPSQLResultPrivate *d)
Definition: qsql_psql.cpp:285
void cleanup()
Definition: qsql_psql.cpp:319
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505

Functions

◆ cleanup()

void QPSQLResult::cleanup ( )
protected

Definition at line 319 of file qsql_psql.cpp.

Referenced by exec(), prepare(), reset(), and ~QPSQLResult().

320 {
321  if (d->result)
322  PQclear(d->result);
323  d->result = 0;
325  d->currentSize = -1;
326  setActive(false);
327 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
PGresult * result
Definition: qsql_psql.cpp:197
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
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402

◆ data()

QVariant QPSQLResult::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 353 of file qsql_psql.cpp.

354 {
355  if (i >= PQnfields(d->result)) {
356  qWarning("QPSQLResult::data: column %d out of range", i);
357  return QVariant();
358  }
359  int ptype = PQftype(d->result, i);
361  const char *val = PQgetvalue(d->result, at(), i);
362  if (PQgetisnull(d->result, at(), i))
363  return QVariant(type);
364  switch (type) {
365  case QVariant::Bool:
366  return QVariant((bool)(val[0] == 't'));
367  case QVariant::String:
368  return d->driver->isUtf8 ? QString::fromUtf8(val) : QString::fromAscii(val);
369  case QVariant::LongLong:
370  if (val[0] == '-')
371  return QString::fromLatin1(val).toLongLong();
372  else
373  return QString::fromLatin1(val).toULongLong();
374  case QVariant::Int:
375  return atoi(val);
376  case QVariant::Double:
377  if (ptype == QNUMERICOID) {
379  QVariant retval;
380  bool convert;
381  double dbl=QString::fromAscii(val).toDouble(&convert);
383  retval = (qlonglong)dbl;
385  retval = (int)dbl;
387  retval = dbl;
388  if (!convert)
389  return QVariant();
390  return retval;
391  }
392  return QString::fromAscii(val);
393  }
394  return QString::fromAscii(val).toDouble();
395  case QVariant::Date:
396  if (val[0] == '\0') {
397  return QVariant(QDate());
398  } else {
399 #ifndef QT_NO_DATESTRING
401 #else
402  return QVariant(QString::fromLatin1(val));
403 #endif
404  }
405  case QVariant::Time: {
406  const QString str = QString::fromLatin1(val);
407 #ifndef QT_NO_DATESTRING
408  if (str.isEmpty())
409  return QVariant(QTime());
410  if (str.at(str.length() - 3) == QLatin1Char('+') || str.at(str.length() - 3) == QLatin1Char('-'))
411  // strip the timezone
412  // TODO: fix this when timestamp support comes into QDateTime
413  return QVariant(QTime::fromString(str.left(str.length() - 3), Qt::ISODate));
414  return QVariant(QTime::fromString(str, Qt::ISODate));
415 #else
416  return QVariant(str);
417 #endif
418  }
419  case QVariant::DateTime: {
420  QString dtval = QString::fromLatin1(val);
421 #ifndef QT_NO_DATESTRING
422  if (dtval.length() < 10)
423  return QVariant(QDateTime());
424  // remove the timezone
425  // TODO: fix this when timestamp support comes into QDateTime
426  if (dtval.at(dtval.length() - 3) == QLatin1Char('+') || dtval.at(dtval.length() - 3) == QLatin1Char('-'))
427  dtval.chop(3);
428  // milliseconds are sometimes returned with 2 digits only
429  if (dtval.at(dtval.length() - 3).isPunct())
430  dtval += QLatin1Char('0');
431  if (dtval.isEmpty())
432  return QVariant(QDateTime());
433  else
435 #else
436  return QVariant(dtval);
437 #endif
438  }
439  case QVariant::ByteArray: {
440  size_t len;
441  unsigned char *data = PQunescapeBytea((unsigned char*)val, &len);
442  QByteArray ba((const char*)data, len);
443  qPQfreemem(data);
444  return QVariant(ba);
445  }
446  default:
447  case QVariant::Invalid:
448  qWarning("QPSQLResult::data: unknown data type");
449  }
450  return QVariant();
451 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
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
int type
Definition: qmetatype.cpp:239
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
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
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
PGresult * result
Definition: qsql_psql.cpp:197
The QDate class provides date functions.
Definition: qdatetime.h:55
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
static QDateTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDateTime represented by the string, using the format given, or an invalid datetime if th...
Definition: qdatetime.cpp:3487
void qPQfreemem(void *buffer)
Definition: qsql_psql.cpp:115
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static QTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the time represented in the string as a QTime using the format given, or an invalid time if t...
Definition: qdatetime.cpp:1928
static QVariant::Type qDecodePSQLType(int t)
Definition: qsql_psql.cpp:239
static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
Definition: qvariant.cpp:627
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
QVariant data(int i)
Returns the data for field index in the current row as a QVariant.
Definition: qsql_psql.cpp:353
qint64 qlonglong
Definition: qglobal.h:951
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define QNUMERICOID
Definition: qsql_psql.cpp:89

◆ exec()

bool QPSQLResult::exec ( )
protectedvirtual

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

See also
prepare()

Reimplemented from QSqlResult.

Definition at line 612 of file qsql_psql.cpp.

Referenced by QPSQLDriverPrivate::detectBackslashEscape(), QPSQLDriverPrivate::getPSQLVersion(), QPSQLDriverPrivate::setDatestyle(), and QPSQLDriverPrivate::setEncodingUtf8().

613 {
615  return QSqlResult::exec();
616 
617  cleanup();
618 
619  QString stmt;
620  const QString params = qCreateParamString(boundValues(), d->q->driver());
621  if (params.isEmpty())
622  stmt = QString::fromLatin1("EXECUTE %1").arg(d->preparedStmtId);
623  else
624  stmt = QString::fromLatin1("EXECUTE %1 (%2)").arg(d->preparedStmtId).arg(params);
625 
626  d->result = d->driver->exec(stmt);
627 
628  return d->processResults();
629 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
static QString qCreateParamString(const QVector< QVariant > boundValues, const QSqlDriver *driver)
Definition: qsql_psql.cpp:552
QPSQLResult * q
Definition: qsql_psql.cpp:195
PGresult * result
Definition: qsql_psql.cpp:197
QString preparedStmtId
Definition: qsql_psql.cpp:200
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
virtual bool exec()
Executes the query, returning true if successful; otherwise returns false.
Definition: qsqlresult.cpp:675
void cleanup()
Definition: qsql_psql.cpp:319
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QVector< QVariant > & boundValues() const
Returns a vector of the result&#39;s bound values for the current record (row).
Definition: qsqlresult.cpp:859
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196

◆ fetch()

bool QPSQLResult::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 329 of file qsql_psql.cpp.

Referenced by fetchFirst(), and fetchLast().

330 {
331  if (!isActive())
332  return false;
333  if (i < 0)
334  return false;
335  if (i >= d->currentSize)
336  return false;
337  if (at() == i)
338  return true;
339  setAt(i);
340  return true;
341 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
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

◆ fetchFirst()

bool QPSQLResult::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 343 of file qsql_psql.cpp.

344 {
345  return fetch(0);
346 }
bool fetch(int i)
Positions the result to an arbitrary (zero-based) row index.
Definition: qsql_psql.cpp:329

◆ fetchLast()

bool QPSQLResult::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 348 of file qsql_psql.cpp.

349 {
350  return fetch(PQntuples(d->result) - 1);
351 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
PGresult * result
Definition: qsql_psql.cpp:197
bool fetch(int i)
Positions the result to an arbitrary (zero-based) row index.
Definition: qsql_psql.cpp:329

◆ handle()

QVariant QPSQLResult::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 314 of file qsql_psql.cpp.

315 {
316  return QVariant::fromValue(d->result);
317 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
PGresult * result
Definition: qsql_psql.cpp:197
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336

◆ isNull()

bool QPSQLResult::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 453 of file qsql_psql.cpp.

454 {
455  PQgetvalue(d->result, at(), field);
456  return PQgetisnull(d->result, at(), field);
457 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
PGresult * result
Definition: qsql_psql.cpp:197
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306

◆ lastInsertId()

QVariant QPSQLResult::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 480 of file qsql_psql.cpp.

481 {
482  if (isActive()) {
483  Oid id = PQoidValue(d->result);
484  if (id != InvalidOid)
485  return QVariant(id);
486  }
487  return QVariant();
488 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
PGresult * result
Definition: qsql_psql.cpp:197

◆ numRowsAffected()

int QPSQLResult::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 475 of file qsql_psql.cpp.

476 {
477  return QString::fromLatin1(PQcmdTuples(d->result)).toInt();
478 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
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
PGresult * result
Definition: qsql_psql.cpp:197
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188

◆ prepare()

bool QPSQLResult::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 584 of file qsql_psql.cpp.

585 {
587  return QSqlResult::prepare(query);
588 
589  cleanup();
590 
591  if (!d->preparedStmtId.isEmpty())
593 
594  const QString stmtId = qMakePreparedStmtId();
595  const QString stmt = QString::fromLatin1("PREPARE %1 AS ").arg(stmtId).append(qReplacePlaceholderMarkers(query));
596 
597  PGresult *result = d->driver->exec(stmt);
598 
599  if (PQresultStatus(result) != PGRES_COMMAND_OK) {
601  "Unable to prepare statement"), QSqlError::StatementError, d->driver, result));
602  PQclear(result);
604  return false;
605  }
606 
607  PQclear(result);
608  d->preparedStmtId = stmtId;
609  return true;
610 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
QString qMakePreparedStmtId()
Definition: qsql_psql.cpp:575
QString preparedStmtId
Definition: qsql_psql.cpp:200
The QString class provides a Unicode character string.
Definition: qstring.h:83
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 void qDeallocatePreparedStmt(QPSQLResultPrivate *d)
Definition: qsql_psql.cpp:285
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, const QPSQLDriverPrivate *p, PGresult *result=0)
Definition: qsql_psql.cpp:205
virtual bool prepare(const QString &query)
Prepares the given query for execution; the query will normally use placeholders so that it can be ex...
Definition: qsqlresult.cpp:641
struct pg_result PGresult
Definition: qsql_psql.h:57
void cleanup()
Definition: qsql_psql.cpp:319
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString & append(QChar c)
Definition: qstring.cpp:1777
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QString qReplacePlaceholderMarkers(const QString &query)
Definition: qsql_psql.cpp:529
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196

◆ record()

QSqlRecord QPSQLResult::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 490 of file qsql_psql.cpp.

491 {
493  if (!isActive() || !isSelect())
494  return info;
495 
496  int count = PQnfields(d->result);
497  for (int i = 0; i < count; ++i) {
498  QSqlField f;
499  if (d->driver->isUtf8)
500  f.setName(QString::fromUtf8(PQfname(d->result, i)));
501  else
502  f.setName(QString::fromLocal8Bit(PQfname(d->result, i)));
503  f.setType(qDecodePSQLType(PQftype(d->result, i)));
504  int len = PQfsize(d->result, i);
505  int precision = PQfmod(d->result, i);
506  // swap length and precision if length == -1
507  if (len == -1 && precision > -1) {
508  len = precision - 4;
509  precision = -1;
510  }
511  f.setLength(len);
512  f.setPrecision(precision);
513  f.setSqlType(PQftype(d->result, i));
514  info.append(f);
515  }
516  return info;
517 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
void setType(QVariant::Type type)
Set&#39;s the field&#39;s variant type to type.
Definition: qsqlfield.cpp:404
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
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
void setName(const QString &name)
Sets the name of the field to name.
Definition: qsqlfield.cpp:347
void setPrecision(int precision)
Sets the field&#39;s precision.
Definition: qsqlfield.cpp:265
PGresult * result
Definition: qsql_psql.cpp:197
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
void setLength(int fieldLength)
Sets the field&#39;s length to fieldLength.
Definition: qsqlfield.cpp:254
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static QVariant::Type qDecodePSQLType(int t)
Definition: qsql_psql.cpp:239
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
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196
void setSqlType(int type)
Definition: qsqlfield.cpp:285

◆ reset()

bool QPSQLResult::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 459 of file qsql_psql.cpp.

460 {
461  cleanup();
462  if (!driver())
463  return false;
464  if (!driver()->isOpen() || driver()->isOpenError())
465  return false;
466  d->result = d->driver->exec(query);
467  return d->processResults();
468 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
PGresult * result
Definition: qsql_psql.cpp:197
void cleanup()
Definition: qsql_psql.cpp:319
PGresult * exec(const char *stmt) const
Definition: qsql_psql.cpp:175
const QPSQLDriverPrivate * driver
Definition: qsql_psql.cpp:196

◆ size()

int QPSQLResult::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 470 of file qsql_psql.cpp.

471 {
472  return d->currentSize;
473 }
QPSQLResultPrivate * d
Definition: qsql_psql.h:92

◆ virtual_hook()

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

Reimplemented from QSqlResult.

Definition at line 519 of file qsql_psql.cpp.

520 {
521  Q_ASSERT(data);
522 
523  switch (id) {
524  default:
526  }
527 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual void virtual_hook(int id, void *data)
Definition: qsqlresult.cpp:962
QVariant data(int i)
Returns the data for field index in the current row as a QVariant.
Definition: qsql_psql.cpp:353

Friends and Related Functions

◆ QPSQLResultPrivate

friend class QPSQLResultPrivate
friend

Definition at line 68 of file qsql_psql.h.

Referenced by QPSQLResult().

Properties

◆ d

QPSQLResultPrivate* QPSQLResult::d
private

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