[196] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Implementation of QSqlResult class
|
---|
| 4 | **
|
---|
| 5 | ** Created : 2000-11-03
|
---|
| 6 | **
|
---|
| 7 | ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
|
---|
| 8 | **
|
---|
| 9 | ** This file is part of the sql module of the Qt GUI Toolkit.
|
---|
| 10 | **
|
---|
| 11 | ** This file may be distributed under the terms of the Q Public License
|
---|
| 12 | ** as defined by Trolltech ASA of Norway and appearing in the file
|
---|
| 13 | ** LICENSE.QPL included in the packaging of this file.
|
---|
| 14 | **
|
---|
| 15 | ** This file may be distributed and/or modified under the terms of the
|
---|
| 16 | ** GNU General Public License version 2 as published by the Free Software
|
---|
| 17 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 18 | ** packaging of this file.
|
---|
| 19 | **
|
---|
| 20 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
| 21 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
| 22 | ** with the Software.
|
---|
| 23 | **
|
---|
| 24 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
| 25 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 26 | **
|
---|
| 27 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
| 28 | ** information about Qt Commercial License Agreements.
|
---|
| 29 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
| 30 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
| 31 | **
|
---|
| 32 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
| 33 | ** not clear to you.
|
---|
| 34 | **
|
---|
| 35 | **********************************************************************/
|
---|
| 36 |
|
---|
| 37 | #include "qsqlresult.h"
|
---|
| 38 | #include "private/qsqlextension_p.h"
|
---|
| 39 |
|
---|
| 40 | #ifndef QT_NO_SQL
|
---|
| 41 |
|
---|
| 42 | class QSqlResultPrivate
|
---|
| 43 | {
|
---|
| 44 | public:
|
---|
| 45 | const QSqlDriver* sqldriver;
|
---|
| 46 | int idx;
|
---|
| 47 | QString sql;
|
---|
| 48 | bool active;
|
---|
| 49 | bool isSel;
|
---|
| 50 | QSqlError error;
|
---|
| 51 | QSqlExtension * ext;
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | /*!
|
---|
| 55 | \class QSqlResult
|
---|
| 56 | \brief The QSqlResult class provides an abstract interface for
|
---|
| 57 | accessing data from SQL databases.
|
---|
| 58 |
|
---|
| 59 | \ingroup database
|
---|
| 60 | \module sql
|
---|
| 61 |
|
---|
| 62 | Normally you would use QSqlQuery instead of QSqlResult since QSqlQuery
|
---|
| 63 | provides a generic wrapper for database-specific implementations of
|
---|
| 64 | QSqlResult.
|
---|
| 65 |
|
---|
| 66 | \sa QSql
|
---|
| 67 | */
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | /*!
|
---|
| 71 | Protected constructor which creates a QSqlResult using database \a
|
---|
| 72 | db. The object is initialized to an inactive state.
|
---|
| 73 | */
|
---|
| 74 |
|
---|
| 75 | QSqlResult::QSqlResult( const QSqlDriver * db ): forwardOnly( FALSE )
|
---|
| 76 | {
|
---|
| 77 | d = new QSqlResultPrivate();
|
---|
| 78 | d->sqldriver = db;
|
---|
| 79 | d->idx = QSql::BeforeFirst;
|
---|
| 80 | d->isSel = FALSE;
|
---|
| 81 | d->active = FALSE;
|
---|
| 82 | d->ext = new QSqlExtension();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /*!
|
---|
| 86 | Destroys the object and frees any allocated resources.
|
---|
| 87 | */
|
---|
| 88 |
|
---|
| 89 | QSqlResult::~QSqlResult()
|
---|
| 90 | {
|
---|
| 91 | if ( d->ext )
|
---|
| 92 | delete d->ext;
|
---|
| 93 | delete d;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /*!
|
---|
| 97 | Sets the current query for the result to \a query. The result must
|
---|
| 98 | be reset() in order to execute the query on the database.
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | void QSqlResult::setQuery( const QString& query )
|
---|
| 102 | {
|
---|
| 103 | d->sql = query;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /*!
|
---|
| 107 | Returns the current SQL query text, or QString::null if there is none.
|
---|
| 108 | */
|
---|
| 109 |
|
---|
| 110 | QString QSqlResult::lastQuery() const
|
---|
| 111 | {
|
---|
| 112 | return d->sql;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /*!
|
---|
| 116 | Returns the current (zero-based) position of the result.
|
---|
| 117 | */
|
---|
| 118 |
|
---|
| 119 | int QSqlResult::at() const
|
---|
| 120 | {
|
---|
| 121 | return d->idx;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | /*!
|
---|
| 126 | Returns TRUE if the result is positioned on a valid record (that
|
---|
| 127 | is, the result is not positioned before the first or after the
|
---|
| 128 | last record); otherwise returns FALSE.
|
---|
| 129 | */
|
---|
| 130 |
|
---|
| 131 | bool QSqlResult::isValid() const
|
---|
| 132 | {
|
---|
| 133 | return ( d->idx != QSql::BeforeFirst && \
|
---|
| 134 | d->idx != QSql::AfterLast ) ? TRUE : FALSE;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*!
|
---|
| 138 | \fn bool QSqlResult::isNull( int i )
|
---|
| 139 |
|
---|
| 140 | Returns TRUE if the field at position \a i is NULL; otherwise
|
---|
| 141 | returns FALSE.
|
---|
| 142 | */
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | /*!
|
---|
| 146 | Returns TRUE if the result has records to be retrieved; otherwise
|
---|
| 147 | returns FALSE.
|
---|
| 148 | */
|
---|
| 149 |
|
---|
| 150 | bool QSqlResult::isActive() const
|
---|
| 151 | {
|
---|
| 152 | return d->active;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /*!
|
---|
| 156 | Protected function provided for derived classes to set the
|
---|
| 157 | internal (zero-based) result index to \a at.
|
---|
| 158 |
|
---|
| 159 | \sa at()
|
---|
| 160 | */
|
---|
| 161 |
|
---|
| 162 | void QSqlResult::setAt( int at )
|
---|
| 163 | {
|
---|
| 164 | d->idx = at;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | /*!
|
---|
| 169 | Protected function provided for derived classes to indicate
|
---|
| 170 | whether or not the current statement is a SQL SELECT statement.
|
---|
| 171 | The \a s parameter should be TRUE if the statement is a SELECT
|
---|
| 172 | statement, or FALSE otherwise.
|
---|
| 173 | */
|
---|
| 174 |
|
---|
| 175 | void QSqlResult::setSelect( bool s )
|
---|
| 176 | {
|
---|
| 177 | d->isSel = s;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /*!
|
---|
| 181 | Returns TRUE if the current result is from a SELECT statement;
|
---|
| 182 | otherwise returns FALSE.
|
---|
| 183 | */
|
---|
| 184 |
|
---|
| 185 | bool QSqlResult::isSelect() const
|
---|
| 186 | {
|
---|
| 187 | return d->isSel;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /*!
|
---|
| 191 | Returns the driver associated with the result.
|
---|
| 192 | */
|
---|
| 193 |
|
---|
| 194 | const QSqlDriver* QSqlResult::driver() const
|
---|
| 195 | {
|
---|
| 196 | return d->sqldriver;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | /*!
|
---|
| 201 | Protected function provided for derived classes to set the
|
---|
| 202 | internal active state to the value of \a a.
|
---|
| 203 |
|
---|
| 204 | \sa isActive()
|
---|
| 205 | */
|
---|
| 206 |
|
---|
| 207 | void QSqlResult::setActive( bool a )
|
---|
| 208 | {
|
---|
| 209 | d->active = a;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /*!
|
---|
| 213 | Protected function provided for derived classes to set the last
|
---|
| 214 | error to the value of \a e.
|
---|
| 215 |
|
---|
| 216 | \sa lastError()
|
---|
| 217 | */
|
---|
| 218 |
|
---|
| 219 | void QSqlResult::setLastError( const QSqlError& e )
|
---|
| 220 | {
|
---|
| 221 | d->error = e;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | /*!
|
---|
| 226 | Returns the last error associated with the result.
|
---|
| 227 | */
|
---|
| 228 |
|
---|
| 229 | QSqlError QSqlResult::lastError() const
|
---|
| 230 | {
|
---|
| 231 | return d->error;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /*!
|
---|
| 235 | \fn int QSqlResult::size()
|
---|
| 236 |
|
---|
| 237 | Returns the size of the result or -1 if it cannot be determined.
|
---|
| 238 | */
|
---|
| 239 |
|
---|
| 240 | /*!
|
---|
| 241 | \fn int QSqlResult::numRowsAffected()
|
---|
| 242 |
|
---|
| 243 | Returns the number of rows affected by the last query executed.
|
---|
| 244 | */
|
---|
| 245 |
|
---|
| 246 | /*!
|
---|
| 247 | \fn QVariant QSqlResult::data( int i )
|
---|
| 248 |
|
---|
| 249 | Returns the data for field \a i (zero-based) as a QVariant. This
|
---|
| 250 | function is only called if the result is in an active state and is
|
---|
| 251 | positioned on a valid record and \a i is non-negative.
|
---|
| 252 | Derived classes must reimplement this function and return the value
|
---|
| 253 | of field \a i, or QVariant() if it cannot be determined.
|
---|
| 254 | */
|
---|
| 255 |
|
---|
| 256 | /*!
|
---|
| 257 | \fn bool QSqlResult::reset( const QString& query )
|
---|
| 258 |
|
---|
| 259 | Sets the result to use the SQL statement \a query for subsequent
|
---|
| 260 | data retrieval. Derived classes must reimplement this function and
|
---|
| 261 | apply the \a query to the database. This function is called only
|
---|
| 262 | after the result is set to an inactive state and is positioned
|
---|
| 263 | before the first record of the new result. Derived classes should
|
---|
| 264 | return TRUE if the query was successful and ready to be used,
|
---|
| 265 | or FALSE otherwise.
|
---|
| 266 | */
|
---|
| 267 |
|
---|
| 268 | /*!
|
---|
| 269 | \fn bool QSqlResult::fetch( int i )
|
---|
| 270 |
|
---|
| 271 | Positions the result to an arbitrary (zero-based) index \a i. This
|
---|
| 272 | function is only called if the result is in an active state. Derived
|
---|
| 273 | classes must reimplement this function and position the result to the
|
---|
| 274 | index \a i, and call setAt() with an appropriate value. Return TRUE
|
---|
| 275 | to indicate success, or FALSE to signify failure.
|
---|
| 276 | */
|
---|
| 277 |
|
---|
| 278 | /*!
|
---|
| 279 | \fn bool QSqlResult::fetchFirst()
|
---|
| 280 |
|
---|
| 281 | Positions the result to the first record in the result. This
|
---|
| 282 | function is only called if the result is in an active state.
|
---|
| 283 | Derived classes must reimplement this function and position the result
|
---|
| 284 | to the first record, and call setAt() with an appropriate value.
|
---|
| 285 | Return TRUE to indicate success, or FALSE to signify failure.
|
---|
| 286 | */
|
---|
| 287 |
|
---|
| 288 | /*!
|
---|
| 289 | \fn bool QSqlResult::fetchLast()
|
---|
| 290 |
|
---|
| 291 | Positions the result to the last record in the result. This
|
---|
| 292 | function is only called if the result is in an active state.
|
---|
| 293 | Derived classes must reimplement this function and position the result
|
---|
| 294 | to the last record, and call setAt() with an appropriate value.
|
---|
| 295 | Return TRUE to indicate success, or FALSE to signify failure.
|
---|
| 296 | */
|
---|
| 297 |
|
---|
| 298 | /*!
|
---|
| 299 | Positions the result to the next available record in the result.
|
---|
| 300 | This function is only called if the result is in an active state.
|
---|
| 301 | The default implementation calls fetch() with the next index.
|
---|
| 302 | Derived classes can reimplement this function and position the result
|
---|
| 303 | to the next record in some other way, and call setAt() with an
|
---|
| 304 | appropriate value. Return TRUE to indicate success, or FALSE to
|
---|
| 305 | signify failure.
|
---|
| 306 | */
|
---|
| 307 |
|
---|
| 308 | bool QSqlResult::fetchNext()
|
---|
| 309 | {
|
---|
| 310 | return fetch( at() + 1 );
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /*!
|
---|
| 314 | Positions the result to the previous available record in the
|
---|
| 315 | result. This function is only called if the result is in an active
|
---|
| 316 | state. The default implementation calls fetch() with the previous
|
---|
| 317 | index. Derived classes can reimplement this function and position the
|
---|
| 318 | result to the next record in some other way, and call setAt() with
|
---|
| 319 | an appropriate value. Return TRUE to indicate success, or FALSE to
|
---|
| 320 | signify failure.
|
---|
| 321 | */
|
---|
| 322 |
|
---|
| 323 | bool QSqlResult::fetchPrev()
|
---|
| 324 | {
|
---|
| 325 | return fetch( at() - 1 );
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /*!
|
---|
| 329 | Returns TRUE if you can only scroll forward through a result set;
|
---|
| 330 | otherwise returns FALSE.
|
---|
| 331 | */
|
---|
| 332 | bool QSqlResult::isForwardOnly() const
|
---|
| 333 | {
|
---|
| 334 | return forwardOnly;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /*!
|
---|
| 338 | Sets forward only mode to \a forward. If forward is TRUE only
|
---|
| 339 | fetchNext() is allowed for navigating the results. Forward only
|
---|
| 340 | mode needs far less memory since results do not have to be cached.
|
---|
| 341 | forward only mode is off by default.
|
---|
| 342 |
|
---|
| 343 | \sa fetchNext()
|
---|
| 344 | */
|
---|
| 345 | void QSqlResult::setForwardOnly( bool forward )
|
---|
| 346 | {
|
---|
| 347 | forwardOnly = forward;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | // XXX BCI HACK - remove in 4.0
|
---|
| 351 | /*! \internal */
|
---|
| 352 | void QSqlResult::setExtension( QSqlExtension * ext )
|
---|
| 353 | {
|
---|
| 354 | if ( d->ext )
|
---|
| 355 | delete d->ext;
|
---|
| 356 | d->ext = ext;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /*! \internal */
|
---|
| 360 | QSqlExtension * QSqlResult::extension()
|
---|
| 361 | {
|
---|
| 362 | return d->ext;
|
---|
| 363 | }
|
---|
| 364 | #endif // QT_NO_SQL
|
---|