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

#include <qsql_tds.h>

Inheritance diagram for QTDSResult:
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...
 
 QTDSResult (const QTDSDriver *db)
 
 ~QTDSResult ()
 
- 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

void cleanup ()
 
bool gotoNext (QSqlCachedResult::ValueCache &values, int index)
 
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...
 
- 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...
 
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...
 
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...
 
virtual QVariant lastInsertId () const
 Returns the object ID of the most recent inserted row if the database supports it. 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

QTDSResultPrivated
 

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 78 of file qsql_tds.h.

Constructors and Destructors

◆ QTDSResult()

QTDSResult::QTDSResult ( const QTDSDriver db)
explicit

Definition at line 294 of file qsql_tds.cpp.

295  : QSqlCachedResult(db)
296 {
297  d = new QTDSResultPrivate();
298  d->login = db->d->login;
299 
300  d->dbproc = dbopen(d->login, const_cast<char*>(db->d->hostName.toLatin1().constData()));
301  if (!d->dbproc)
302  return;
303  if (dbuse(d->dbproc, const_cast<char*>(db->d->db.toLatin1().constData())) == FAIL)
304  return;
305 
306  // insert d in error handler dict
307  errs()->insert(d->dbproc, d);
308  dbcmd(d->dbproc, "set quoted_identifier on");
309  dbsqlexec(d->dbproc);
310 }
LOGINREC * login
Definition: qsql_tds.cpp:139
QTDSDriverPrivate * d
Definition: qsql_tds.h:130
QTDSResultPrivate * d
Definition: qsql_tds.h:94
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QSqlCachedResult(const QSqlDriver *db)
LOGINREC * login
Definition: qsql_tds.cpp:150
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ ~QTDSResult()

QTDSResult::~QTDSResult ( )

Definition at line 312 of file qsql_tds.cpp.

313 {
314  cleanup();
315  if (d->dbproc)
316  dbclose(d->dbproc);
317  errs()->remove(d->dbproc);
318  delete d;
319 }
void cleanup()
Definition: qsql_tds.cpp:321
QTDSResultPrivate * d
Definition: qsql_tds.h:94
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

Functions

◆ cleanup()

void QTDSResult::cleanup ( )
protected

Definition at line 321 of file qsql_tds.cpp.

Referenced by reset(), and ~QTDSResult().

322 {
323  d->clearErrorMsgs();
324  d->rec.clear();
325  for (int i = 0; i < d->buffer.size() / 2; ++i)
326  free(d->buffer.at(i * 2));
327  d->buffer.clear();
328  // "can" stands for "cancel"... very clever.
329  dbcanquery(d->dbproc);
330  dbfreebuf(d->dbproc);
331 
333 }
QTDSResultPrivate * d
Definition: qsql_tds.h:94
void clearErrorMsgs()
Definition: qsql_tds.cpp:155
QVector< void * > buffer
Definition: qsql_tds.cpp:156
void clear()
Removes all the record&#39;s fields.
Definition: qsqlrecord.cpp:367
QSqlRecord rec
Definition: qsql_tds.cpp:157
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ gotoNext()

bool QTDSResult::gotoNext ( QSqlCachedResult::ValueCache values,
int  index 
)
protectedvirtual

Implements QSqlCachedResult.

Definition at line 345 of file qsql_tds.cpp.

346 {
347  STATUS stat = dbnextrow(d->dbproc);
348  if (stat == NO_MORE_ROWS) {
350  return false;
351  }
352  if ((stat == FAIL) || (stat == BUF_FULL)) {
354  return false;
355  }
356 
357  if (index < 0)
358  return true;
359 
360  for (int i = 0; i < d->rec.count(); ++i) {
361  int idx = index + i;
362  switch (d->rec.field(i).type()) {
363  case QVariant::DateTime:
364  if (qIsNull(d->buffer.at(i * 2 + 1))) {
365  values[idx] = QVariant(QVariant::DateTime);
366  } else {
367  DBDATETIME *bdt = (DBDATETIME*) d->buffer.at(i * 2);
368  QDate date = QDate::fromString(QLatin1String("1900-01-01"), Qt::ISODate);
369  QTime time = QTime::fromString(QLatin1String("00:00:00"), Qt::ISODate);
370  values[idx] = QDateTime(date.addDays(bdt->dtdays), time.addMSecs(int(bdt->dttime / 0.3)));
371  }
372  break;
373  case QVariant::Int:
374  if (qIsNull(d->buffer.at(i * 2 + 1)))
375  values[idx] = QVariant(QVariant::Int);
376  else
377  values[idx] = *((int*)d->buffer.at(i * 2));
378  break;
379  case QVariant::Double:
380  case QVariant::String:
381  if (qIsNull(d->buffer.at(i * 2 + 1)))
382  values[idx] = QVariant(QVariant::String);
383  else
384  values[idx] = QString::fromLocal8Bit((const char*)d->buffer.at(i * 2)).trimmed();
385  break;
386  case QVariant::ByteArray: {
387  if (qIsNull(d->buffer.at(i * 2 + 1)))
388  values[idx] = QVariant(QVariant::ByteArray);
389  else
390  values[idx] = QByteArray((const char*)d->buffer.at(i * 2));
391  break;
392  }
393  default:
394  // should never happen, and we already fired
395  // a warning while binding.
396  values[idx] = QVariant();
397  break;
398  }
399  }
400 
401  return true;
402 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QSqlError lastError
Definition: qsql_tds.cpp:152
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
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
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 setLastError(const QSqlError &e)
This function is provided for derived classes to set the last error to error.
Definition: qsqlresult.cpp:417
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QTDSResultPrivate * d
Definition: qsql_tds.h:94
QDate addDays(int days) const
Returns a QDate object containing a date ndays later than the date of this object (or earlier if nday...
Definition: qdatetime.cpp:989
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394
QSqlField field(int i) const
Returns the field at position index.
Definition: qsqlrecord.cpp:289
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
int count() const
Returns the number of fields in the record.
Definition: qsqlrecord.cpp:573
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
QString trimmed(QString source)
Definition: generator.cpp:233
QVector< void * > buffer
Definition: qsql_tds.cpp:156
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
static bool qIsNull(const void *ind)
Definition: qsql_tds.cpp:340
QSqlRecord rec
Definition: qsql_tds.cpp:157
quint16 index
QTime addMSecs(int ms) const
Returns a QTime object containing a time ms milliseconds later than the time of this object (or earli...
Definition: qdatetime.cpp:1803
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ handle()

QVariant QTDSResult::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 335 of file qsql_tds.cpp.

336 {
337  return QVariant(qRegisterMetaType<DBPROCESS *>("DBPROCESS*"), &d->dbproc);
338 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QTDSResultPrivate * d
Definition: qsql_tds.h:94
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ numRowsAffected()

int QTDSResult::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 492 of file qsql_tds.cpp.

493 {
494 #ifdef DBNTWIN32
495  if (dbiscount(d->dbproc)) {
496  return DBCOUNT(d->dbproc);
497  }
498  return -1;
499 #else
500  return DBCOUNT(d->dbproc);
501 #endif
502 }
QTDSResultPrivate * d
Definition: qsql_tds.h:94
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ record()

QSqlRecord QTDSResult::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 504 of file qsql_tds.cpp.

505 {
506  return d->rec;
507 }
QTDSResultPrivate * d
Definition: qsql_tds.h:94
QSqlRecord rec
Definition: qsql_tds.cpp:157

◆ reset()

bool QTDSResult::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 404 of file qsql_tds.cpp.

405 {
406  cleanup();
407  if (!driver() || !driver()-> isOpen() || driver()->isOpenError())
408  return false;
409  setActive(false);
411  if (dbcmd(d->dbproc, const_cast<char*>(query.toLocal8Bit().constData())) == FAIL) {
413  return false;
414  }
415 
416  if (dbsqlexec(d->dbproc) == FAIL) {
418  dbfreebuf(d->dbproc);
419  return false;
420  }
421  if (dbresults(d->dbproc) != SUCCEED) {
423  dbfreebuf(d->dbproc);
424  return false;
425  }
426 
427  setSelect((DBCMDROW(d->dbproc) == SUCCEED)); // decide whether or not we are dealing with a SELECT query
428  int numCols = dbnumcols(d->dbproc);
429  if (numCols > 0) {
430  d->buffer.resize(numCols * 2);
431  init(numCols);
432  }
433  for (int i = 0; i < numCols; ++i) {
434  int dbType = dbcoltype(d->dbproc, i+1);
435  QVariant::Type vType = qDecodeTDSType(dbType);
436  QSqlField f(QString::fromAscii(dbcolname(d->dbproc, i+1)), vType);
437  f.setSqlType(dbType);
438  f.setLength(dbcollen(d->dbproc, i+1));
439  d->rec.append(f);
440 
441  RETCODE ret = -1;
442  void* p = 0;
443  switch (vType) {
444  case QVariant::Int:
445  p = malloc(4);
446  ret = dbbind(d->dbproc, i+1, INTBIND, (DBINT) 4, (unsigned char *)p);
447  break;
448  case QVariant::Double:
449  // use string binding to prevent loss of precision
450  p = malloc(50);
451  ret = dbbind(d->dbproc, i+1, STRINGBIND, 50, (unsigned char *)p);
452  break;
453  case QVariant::String:
454  p = malloc(dbcollen(d->dbproc, i+1) + 1);
455  ret = dbbind(d->dbproc, i+1, STRINGBIND, DBINT(dbcollen(d->dbproc, i+1) + 1), (unsigned char *)p);
456  break;
457  case QVariant::DateTime:
458  p = malloc(8);
459  ret = dbbind(d->dbproc, i+1, DATETIMEBIND, (DBINT) 8, (unsigned char *)p);
460  break;
461  case QVariant::ByteArray:
462  p = malloc(dbcollen(d->dbproc, i+1) + 1);
463  ret = dbbind(d->dbproc, i+1, BINARYBIND, DBINT(dbcollen(d->dbproc, i+1) + 1), (unsigned char *)p);
464  break;
465  default: //don't bind the field since we do not support it
466  qWarning("QTDSResult::reset: Unsupported type for field \"%s\"", dbcolname(d->dbproc, i+1));
467  break;
468  }
469  if (ret == SUCCEED) {
470  d->buffer[i * 2] = p;
471  ret = dbnullbind(d->dbproc, i+1, (DBINT*)(&d->buffer[i * 2 + 1]));
472  } else {
473  d->buffer[i * 2] = 0;
474  d->buffer[i * 2 + 1] = 0;
475  free(p);
476  }
477  if ((ret != SUCCEED) && (ret != -1)) {
479  return false;
480  }
481  }
482 
483  setActive(true);
484  return true;
485 }
QSqlError lastError
Definition: qsql_tds.cpp:152
const QSqlDriver * driver() const
Returns the driver associated with the result.
Definition: qsqlresult.cpp:389
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
QVariant::Type qDecodeTDSType(int type)
Definition: qsql_tds.cpp:238
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 setLastError(const QSqlError &e)
This function is provided for derived classes to set the last error to error.
Definition: qsqlresult.cpp:417
void cleanup()
Definition: qsql_tds.cpp:321
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
QTDSResultPrivate * d
Definition: qsql_tds.h:94
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
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QVector< void * > buffer
Definition: qsql_tds.cpp:156
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
Definition: qsqlrecord.cpp:312
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402
QSqlRecord rec
Definition: qsql_tds.cpp:157
void init(int colCount)
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
DBPROCESS * dbproc
Definition: qsql_tds.cpp:151

◆ size()

int QTDSResult::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 487 of file qsql_tds.cpp.

488 {
489  return -1;
490 }

Properties

◆ d

QTDSResultPrivate* QTDSResult::d
private

Definition at line 94 of file qsql_tds.h.

Referenced by cleanup(), gotoNext(), handle(), numRowsAffected(), QTDSResult(), record(), reset(), and ~QTDSResult().


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