Qt 4.8
Public Types | Public Functions | Protected Functions | Private Functions | Properties | List of all members
QSqlCachedResult Class Referenceabstract

#include <qsqlcachedresult_p.h>

Inheritance diagram for QSqlCachedResult:
QSqlResult QIBaseResult QOCIResult QSQLite2Result QSQLiteResult QTDSResult

Public Types

typedef QVector< QVariantValueCache
 

Public Functions

virtual ~QSqlCachedResult ()
 
- Public Functions inherited from QSqlResult
virtual 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...
 
virtual ~QSqlResult ()
 Destroys the object and frees any allocated resources. More...
 

Protected Functions

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...
 
virtual bool gotoNext (ValueCache &values, int index)=0
 
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 int numRowsAffected ()=0
 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...
 
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 QSqlRecord record () const
 Returns the current record if the query is active; otherwise returns an empty QSqlRecord. More...
 
virtual bool reset (const QString &sqlquery)=0
 Sets the result to use the SQL statement query for subsequent data retrieval. 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...
 
virtual int size ()=0
 Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELECT statement. More...
 

Private Functions

bool cacheNext ()
 

Properties

QSqlCachedResultPrivated
 

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 65 of file qsqlcachedresult_p.h.

Typedefs

◆ ValueCache

Definition at line 70 of file qsqlcachedresult_p.h.

Constructors and Destructors

◆ ~QSqlCachedResult()

QSqlCachedResult::~QSqlCachedResult ( )
virtual

Definition at line 150 of file qsqlcachedresult.cpp.

151 {
152  delete d;
153 }
QSqlCachedResultPrivate * d

◆ QSqlCachedResult()

QSqlCachedResult::QSqlCachedResult ( const QSqlDriver db)
protected

Definition at line 145 of file qsqlcachedresult.cpp.

145  : QSqlResult (db)
146 {
147  d = new QSqlCachedResultPrivate();
148 }
QSqlResult(const QSqlDriver *db)
Creates a QSqlResult using database driver db.
Definition: qsqlresult.cpp:257
QSqlCachedResultPrivate * d

Functions

◆ cache()

QSqlCachedResult::ValueCache & QSqlCachedResult::cache ( )
protected

Definition at line 300 of file qsqlcachedresult.cpp.

301 {
302  return d->cache;
303 }
QSqlCachedResult::ValueCache cache
QSqlCachedResultPrivate * d

◆ cacheNext()

bool QSqlCachedResult::cacheNext ( )
private

Definition at line 276 of file qsqlcachedresult.cpp.

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

277 {
278  if (d->atEnd)
279  return false;
280 
281  if(isForwardOnly()) {
282  d->cache.clear();
283  d->cache.resize(d->colCount);
284  }
285 
286  if (!gotoNext(d->cache, d->nextIndex())) {
287  d->revertLast();
288  d->atEnd = true;
289  return false;
290  }
291  setAt(at() + 1);
292  return true;
293 }
QSqlCachedResult::ValueCache cache
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
virtual void setAt(int at)
This function is provided for derived classes to set the internal (zero-based) row position to index...
Definition: qsqlresult.cpp:352
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QSqlCachedResultPrivate * d
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
virtual bool gotoNext(ValueCache &values, int index)=0

◆ cleanup()

void QSqlCachedResult::cleanup ( )
protected

Definition at line 262 of file qsqlcachedresult.cpp.

Referenced by QTDSResult::cleanup(), QSQLite2ResultPrivate::cleanup(), QSQLiteResultPrivate::cleanup(), QIBaseResultPrivate::cleanup(), QIBaseResult::exec(), QOCIResult::prepare(), and virtual_hook().

263 {
265  setActive(false);
266  d->cleanup();
267 }
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
QSqlCachedResultPrivate * d
virtual void setActive(bool a)
This function is provided for derived classes to set the internal active state to active...
Definition: qsqlresult.cpp:402

◆ clearValues()

void QSqlCachedResult::clearValues ( )
protected

Definition at line 269 of file qsqlcachedresult.cpp.

Referenced by QSQLiteResult::exec().

270 {
272  d->rowCacheEnd = 0;
273  d->atEnd = false;
274 }
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
QSqlCachedResultPrivate * d

◆ colCount()

int QSqlCachedResult::colCount ( ) const
protected

Definition at line 295 of file qsqlcachedresult.cpp.

Referenced by QIBaseResult::exec().

296 {
297  return d->colCount;
298 }
QSqlCachedResultPrivate * d

◆ data()

QVariant QSqlCachedResult::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 244 of file qsqlcachedresult.cpp.

245 {
246  int idx = d->forwardOnly ? i : at() * d->colCount + i;
247  if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd)
248  return QVariant();
249 
250  return d->cache.at(idx);
251 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QSqlCachedResult::ValueCache cache
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
QSqlCachedResultPrivate * d
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350

◆ fetch()

bool QSqlCachedResult::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 160 of file qsqlcachedresult.cpp.

Referenced by fetchLast(), and fetchPrevious().

161 {
162  if ((!isActive()) || (i < 0))
163  return false;
164  if (at() == i)
165  return true;
166  if (d->forwardOnly) {
167  // speed hack - do not copy values if not needed
168  if (at() > i || at() == QSql::AfterLastRow)
169  return false;
170  while(at() < i - 1) {
171  if (!gotoNext(d->cache, -1))
172  return false;
173  setAt(at() + 1);
174  }
175  if (!gotoNext(d->cache, 0))
176  return false;
177  setAt(at() + 1);
178  return true;
179  }
180  if (d->canSeek(i)) {
181  setAt(i);
182  return true;
183  }
184  if (d->rowCacheEnd > 0)
185  setAt(d->cacheCount());
186  while (at() < i + 1) {
187  if (!cacheNext()) {
188  if (d->canSeek(i))
189  break;
190  return false;
191  }
192  }
193  setAt(i);
194 
195  return true;
196 }
bool isActive() const
Returns true if the result has records to be retrieved; otherwise returns false.
Definition: qsqlresult.cpp:340
QSqlCachedResult::ValueCache cache
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
QSqlCachedResultPrivate * d
virtual bool gotoNext(ValueCache &values, int index)=0

◆ fetchFirst()

bool QSqlCachedResult::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 212 of file qsqlcachedresult.cpp.

213 {
214  if (d->forwardOnly && at() != QSql::BeforeFirstRow) {
215  return false;
216  }
217  if (d->canSeek(0)) {
218  setAt(0);
219  return true;
220  }
221  return cacheNext();
222 }
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
QSqlCachedResultPrivate * d

◆ fetchLast()

bool QSqlCachedResult::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 224 of file qsqlcachedresult.cpp.

225 {
226  if (d->atEnd) {
227  if (d->forwardOnly)
228  return false;
229  else
230  return fetch(d->cacheCount() - 1);
231  }
232 
233  int i = at();
234  while (fetchNext())
235  ++i; /* brute force */
236  if (d->forwardOnly && at() == QSql::AfterLastRow) {
237  setAt(i);
238  return true;
239  } else {
240  return fetch(i);
241  }
242 }
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
bool fetchNext()
Positions the result to the next available record (row) in the result.
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
QSqlCachedResultPrivate * d
bool fetch(int i)
Positions the result to an arbitrary (zero-based) row index.

◆ fetchNext()

bool QSqlCachedResult::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 198 of file qsqlcachedresult.cpp.

Referenced by fetchLast().

199 {
200  if (d->canSeek(at() + 1)) {
201  setAt(at() + 1);
202  return true;
203  }
204  return cacheNext();
205 }
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
QSqlCachedResultPrivate * d

◆ fetchPrevious()

bool QSqlCachedResult::fetchPrevious ( )
protectedvirtual

Positions the result to the previous 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 previous 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.

Reimplemented from QSqlResult.

Definition at line 207 of file qsqlcachedresult.cpp.

208 {
209  return fetch(at() - 1);
210 }
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.

◆ gotoNext()

virtual bool QSqlCachedResult::gotoNext ( ValueCache values,
int  index 
)
protectedpure virtual

Implemented in QTDSResult, QOCIResult, QSQLite2Result, QSQLiteResult, and QIBaseResult.

Referenced by cacheNext(), and fetch().

◆ init()

void QSqlCachedResult::init ( int  colCount)
protected

Definition at line 155 of file qsqlcachedresult.cpp.

Referenced by QIBaseResult::exec(), QOCIResult::exec(), QSQLite2ResultPrivate::init(), QSQLiteResultPrivate::initColumns(), and QTDSResult::reset().

156 {
158 }
bool isForwardOnly() const
Returns true if you can only scroll forward through the result set; otherwise returns false...
Definition: qsqlresult.cpp:582
QSqlCachedResultPrivate * d
void init(int count, bool fo)

◆ isNull()

bool QSqlCachedResult::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 253 of file qsqlcachedresult.cpp.

254 {
255  int idx = d->forwardOnly ? i : at() * d->colCount + i;
256  if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd)
257  return true;
258 
259  return d->cache.at(idx).isNull();
260 }
QSqlCachedResult::ValueCache cache
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
int at() const
Returns the current (zero-based) row position of the result.
Definition: qsqlresult.cpp:306
QSqlCachedResultPrivate * d
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350

◆ virtual_hook()

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

Reimplemented from QSqlResult.

Definition at line 305 of file qsqlcachedresult.cpp.

Referenced by QSQLite2Result::virtual_hook(), QSQLiteResult::virtual_hook(), and QOCIResult::virtual_hook().

306 {
307  switch (id) {
310  cleanup();
311  break;
312  default:
314  }
315 }
QVariant data(int i)
Returns the data for field index in the current row as a QVariant.
virtual void virtual_hook(int id, void *data)
Definition: qsqlresult.cpp:962

Properties

◆ d

QSqlCachedResultPrivate* QSqlCachedResult::d
private

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