bool QSqlQuery::prev() { if ( !isSelect() || !isActive() ) return FALSE; if ( isForwardOnly() ) { #ifdef QT_CHECK_RANGE qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query" ); #endif return FALSE; } beforeSeek(); checkDetach(); bool b = FALSE; switch ( at() ) { case QSql::BeforeFirst: afterSeek(); return FALSE; case QSql::AfterLast: b = d->sqlResult->fetchLast(); afterSeek(); return b; default: if ( !d->sqlResult->fetchPrev() ) { d->sqlResult->setAt( QSql::BeforeFirst ); afterSeek(); return FALSE; } afterSeek(); return TRUE; } }
void Tram::draw(QPainter *painter) { painter->save(); QPoint buff; if(isSelect()) { buff = this->m_coordinate; for(int i = 0; i < SIZE; i++) { painter->setPen(Qt::yellow); painter->setBrush(QBrush(Qt::yellow)); drawElemScen(painter, buff.x() - 1, buff.y() - 1, SIZE_TRAM + 2); buff = this->m_trip->previous(buff); } } buff = this->m_coordinate; for(int i = 0; i < SIZE; i++) { if(m_state == Tram::OutOfOrder) { painter->setPen(QColor(255, 165, 0)); painter->setBrush(QBrush(QColor(255, 165, 0))); } else { painter->setPen(Qt::red); painter->setBrush(QBrush(Qt::red)); } drawElemScen(painter, buff.x(), buff.y(), SIZE_TRAM); buff = this->m_trip->previous(buff); } painter->setPen(Qt::darkGray); painter->setBrush(QBrush(Qt::darkGray)); painter->restore(); }
bool QSqlQuery::next() { if ( !isSelect() || !isActive() ) return FALSE; beforeSeek(); checkDetach(); bool b = FALSE; switch ( at() ) { case QSql::BeforeFirst: b = d->sqlResult->fetchFirst(); afterSeek(); return b; case QSql::AfterLast: afterSeek(); return FALSE; default: if ( !d->sqlResult->fetchNext() ) { d->sqlResult->setAt( QSql::AfterLast ); afterSeek(); return FALSE; } afterSeek(); return TRUE; } }
bool SqlQuery::exec() { if (!_stmt) { return false; } // Don't do anything for selects, that is how we use the lib :-| if( !isSelect() && !isPragma() ) { int rc, n = 0; do { rc = sqlite3_step(_stmt); if( rc == SQLITE_LOCKED ) { rc = sqlite3_reset(_stmt); /* This will also return SQLITE_LOCKED */ n++; OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC); } else if( rc == SQLITE_BUSY ) { OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC); n++; } } while( (n < SQLITE_REPEAT_COUNT) && ((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED))); _errId = rc; if (_errId != SQLITE_DONE && _errId != SQLITE_ROW) { _error = QString::fromUtf8(sqlite3_errmsg(_db)); qDebug() << "Sqlite exec statement error:" << _errId << _error << "in" <<_sql; } return (_errId == SQLITE_DONE); // either SQLITE_ROW or SQLITE_DONE } return true; }
QSqlRecord QPSQLResult::record() const { QSqlRecord info; if (!isActive() || !isSelect()) return info; int count = PQnfields(d->result); for (int i = 0; i < count; ++i) { QSqlField f; if (d->driver->isUtf8) f.setName(QString::fromUtf8(PQfname(d->result, i))); else f.setName(QString::fromLocal8Bit(PQfname(d->result, i))); f.setType(qDecodePSQLType(PQftype(d->result, i))); int len = PQfsize(d->result, i); int precision = PQfmod(d->result, i); // swap length and precision if length == -1 if (len == -1 && precision > -1) { len = precision - 4; precision = -1; } f.setLength(len); f.setPrecision(precision); f.setSqlType(PQftype(d->result, i)); info.append(f); } return info; }
bool DeckScene::onTouchBegan(Touch * touch, Event * unused_event) { if (!isInGame) { return false; } Point point = touch->getLocation(); for (int i = player->leftCard.size() - 1; i >= 0; i--) { auto card = player->leftCard.at(i); if (card->getBoundingBox().containsPoint(point)) { if (!card->isSelect()) { card->setSelect(true); card->setPositionY(card->getPositionY() + cardIntervalVertical); } else { card->setSelect(false); card->setPositionY(card->getPositionY() - cardIntervalVertical); } break; } } return true; }
bool MSqlQuery::exec(const QString &query) { if (!m_db) { // Database structure's been deleted return false; } // Database connection down. Try to restart it, give up if it's still // down if (!m_db->isOpen() && !Reconnect()) { LOG(VB_GENERAL, LOG_INFO, "MySQL server disconnected"); return false; } bool result = QSqlQuery::exec(query); // if the query failed with "MySQL server has gone away" // Close and reopen the database connection and retry the query if it // connects again if (!result && QSqlQuery::lastError().number() == 2006 && Reconnect()) result = QSqlQuery::exec(query); LOG(VB_DATABASE, LOG_INFO, QString("MSqlQuery::exec(%1) %2%3") .arg(m_db->MSqlDatabase::GetConnectionName()).arg(query) .arg(isSelect() ? QString(" <<<< Returns %1 row(s)") .arg(size()) : QString())); return result; }
bool QSqlQuery::last() { if (!isSelect() || !isActive()) return false; bool b = false; b = d->sqlResult->fetchLast(); return b; }
bool SqliteResult::reset(const QString &q) { if (!driver) return false; if (!driver->isOpen() || driver->isOpenError()) return false; setActive(false); setAt(QSql::BeforeFirst); query = q.stripWhiteSpace(); if (q.find("select", 0, false) == 0) setSelect(true); else setSelect(false); query.replace("'true'", "'1'"); query.replace("'false'", "'0'"); query.replace("=;", "= NULL;"); while (query.endsWith(";")) query.truncate(query.length() - 1); if (query.upper().endsWith("NOWAIT")) query.truncate(query.length() - 6); if (query.upper().endsWith("FOR UPDATE")) query.truncate(query.length() - 10); if (!isSelect()) { if (query.find("CREATE TABLE", 0, false) == 0) { Dataset *ds = ((SqliteDriver *) driver) ->dataBase() ->CreateDataset(); if (!ds) return false; if (!ds->exec(query.latin1())) { delete ds; return false; } delete ds; } else { if (dataSet) delete dataSet; dataSet = ((SqliteDriver *) driver) ->dataBase() ->CreateDataset(); if (!dataSet->exec(query.latin1())) return false; } return true; } if (dataSet) delete dataSet; dataSet = ((SqliteDriver *) driver) ->dataBase() ->CreateDataset(); if (dataSet->query(query.latin1())) { setActive(true); return true; } else return false; }
void moveKBCursorAbsolute(int newPos) { if(newPos >= 0) kbCursor = newPos; blink = 1; bCount = 0; if(isSelect()) endHighlight = kbCursor; }
QSqlRecord QSymSQLResult::record() const { if (!isActive() || !isSelect()) return QSqlRecord(); QSqlRecord res; d->initColumns(res); return res; }
QVariant QMYSQLResult::data( int field ) { if ( !isSelect() || field >= (int) d->fieldTypes.count() ) { qWarning( "QMYSQLResult::data: column %d out of range", field ); return QVariant(); } QString val( d->row[field] ); switch ( d->fieldTypes.at( field ) ) { case QVariant::LongLong: return QVariant( val.toLongLong() ); case QVariant::ULongLong: return QVariant( val.toULongLong() ); case QVariant::Int: return QVariant( val.toInt() ); case QVariant::UInt: return QVariant( val.toUInt() ); case QVariant::Double: return QVariant( val.toDouble() ); case QVariant::Date: if ( val.isEmpty() ) { return QVariant( QDate() ); } else { return QVariant( QDate::fromString( val, Qt::ISODate ) ); } case QVariant::Time: if ( val.isEmpty() ) { return QVariant( QTime() ); } else { return QVariant( QTime::fromString( val, Qt::ISODate ) ); } case QVariant::DateTime: if ( val.isEmpty() ) return QVariant( QDateTime() ); if ( val.length() == 14u ) // TIMESTAMPS have the format yyyyMMddhhmmss val.insert(4, "-").insert(7, "-").insert(10, 'T').insert(13, ':').insert(16, ':'); return QVariant( QDateTime::fromString( val, Qt::ISODate ) ); case QVariant::ByteArray: { unsigned long* fl = mysql_fetch_lengths( d->result ); QByteArray ba; ba.duplicate( d->row[field], fl[field] ); return QVariant( ba ); } default: case QVariant::String: case QVariant::CString: return QVariant( val ); } #ifdef QT_CHECK_RANGE qWarning("QMYSQLResult::data: unknown data type"); #endif return QVariant(); }
bool QSqlQuery::last() { if ( !isSelect() || !isActive() ) return FALSE; beforeSeek(); checkDetach(); bool b = FALSE; b = d->sqlResult->fetchLast(); afterSeek(); return b; }
int embeddedResult::size() { if (d->driver && isSelect()) if (d->preparedQuery) #if MYSQL_VERSION_ID >= 40108 return mysql_stmt_num_rows(d->stmt); #else return -1; #endif else return int(mysql_num_rows(d->result));
/*! Retrieves the first record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the \l{isActive()}{active} state and isSelect() must return true before calling this function or it will do nothing and return false. Returns true if successful. If unsuccessful the query position is set to an invalid position and false is returned. \sa next(), previous(), last(), seek(), at(), isActive(), isValid() */ bool QSqlQuery::first() { if (!isSelect() || !isActive()) return false; if (isForwardOnly() && at() > QSql::BeforeFirstRow) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } bool b = false; b = d->sqlResult->fetchFirst(); return b; }
void moveKBCursorRelative(int newPos) { kbCursor += newPos; if(kbCursor < 0) kbCursor = 0; blink = 1; bCount = 0; if(isSelect()) endHighlight = kbCursor; }
bool MSqlQuery::exec() { // Database connection down. Try to restart it, give up if it's still // down if (!m_db) { // Database structure's been deleted return false; } if (!m_db->isOpen() && !m_db->Reconnect()) { LOG(VB_GENERAL, LOG_INFO, "MySQL server disconnected"); return false; } bool result = QSqlQuery::exec(); // if the query failed with "MySQL server has gone away" // Close and reopen the database connection and retry the query if it // connects again if (!result && QSqlQuery::lastError().number() == 2006 && m_db->Reconnect()) result = QSqlQuery::exec(); if (VERBOSE_LEVEL_CHECK(VB_DATABASE) && logLevel <= LOG_DEBUG) { QString str = lastQuery(); // Database logging will cause an infinite loop here if not filtered // out if (!str.startsWith("INSERT INTO logging ")) { // Sadly, neither executedQuery() nor lastQuery() display // the values in bound queries against a MySQL5 database. // So, replace the named placeholders with their values. QMapIterator<QString, QVariant> b = boundValues(); while (b.hasNext()) { b.next(); str.replace(b.key(), '\'' + b.value().toString() + '\''); } LOG(VB_DATABASE, LOG_DEBUG, QString("MSqlQuery::exec(%1) %2%3") .arg(m_db->MSqlDatabase::GetConnectionName()).arg(str) .arg(isSelect() ? QString(" <<<< Returns %1 row(s)") .arg(size()) : QString())); } } return result; }
bool QSqlQuery::first() { if ( !isSelect() || !isActive() ) return FALSE; if ( isForwardOnly() && at() > QSql::BeforeFirst ) { #ifdef QT_CHECK_RANGE qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query" ); #endif return FALSE; } beforeSeek(); checkDetach(); bool b = FALSE; b = d->sqlResult->fetchFirst(); afterSeek(); return b; }
/*! Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the \l{isActive()}{active} state and isSelect() must return true before calling this function or it will do nothing and return false. The following rules apply: \list \li If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record. \li If the result is currently located after the last record, there is no change and false is returned. \li If the result is located somewhere in the middle, an attempt is made to retrieve the next record. \endlist If the record could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned. \sa previous(), first(), last(), seek(), at(), isActive(), isValid() */ bool QSqlQuery::next() { if (!isSelect() || !isActive()) return false; bool b = false; switch (at()) { case QSql::BeforeFirstRow: b = d->sqlResult->fetchFirst(); return b; case QSql::AfterLastRow: return false; default: if (!d->sqlResult->fetchNext()) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true; } }
bool SqlQuery::exec() { qCDebug(lcSql) << "SQL exec" << _sql; if (!_stmt) { qCWarning(lcSql) << "Can't exec query, statement unprepared."; return false; } // Don't do anything for selects, that is how we use the lib :-| if (!isSelect() && !isPragma()) { int rc, n = 0; do { rc = sqlite3_step(_stmt); if (rc == SQLITE_LOCKED) { rc = sqlite3_reset(_stmt); /* This will also return SQLITE_LOCKED */ n++; OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC); } else if (rc == SQLITE_BUSY) { OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC); n++; } } while ((n < SQLITE_REPEAT_COUNT) && ((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED))); _errId = rc; if (_errId != SQLITE_DONE && _errId != SQLITE_ROW) { _error = QString::fromUtf8(sqlite3_errmsg(_db)); qCWarning(lcSql) << "Sqlite exec statement error:" << _errId << _error << "in" << _sql; if (_errId == SQLITE_IOERR) { qCWarning(lcSql) << "IOERR extended errcode: " << sqlite3_extended_errcode(_db); #if SQLITE_VERSION_NUMBER >= 3012000 qCWarning(lcSql) << "IOERR system errno: " << sqlite3_system_errno(_db); #endif } } else { qCDebug(lcSql) << "Last exec affected" << numRowsAffected() << "rows."; } return (_errId == SQLITE_DONE); // either SQLITE_ROW or SQLITE_DONE } return true; }
bool embeddedResult::reset (const QString& query) { if (!driver() || !driver()->isOpen() || driver()->isOpenError() || !d->driver) return false; d->preparedQuery = false; cleanup(); //qDebug() << "In reset: " + query; const QByteArray encQuery(fromUnicode(d->driver->d->tc, query)); //const QByteArray encQuery = query.toLocal8Bit(); if (mysql_real_query(d->driver->d->mysql, encQuery.data(), encQuery.length())) { setLastError(qMakeError(QCoreApplication::translate("embeddedResult", "Unable to execute query"), QSqlError::StatementError, d->driver->d)); return false; } d->result = mysql_store_result(d->driver->d->mysql); if (!d->result && mysql_field_count(d->driver->d->mysql) > 0) { setLastError(qMakeError(QCoreApplication::translate("embeddedResult", "Unable to store result"), QSqlError::StatementError, d->driver->d)); return false; } int numFields = mysql_field_count(d->driver->d->mysql); setSelect(numFields != 0); d->fields.resize(numFields); d->rowsAffected = mysql_affected_rows(d->driver->d->mysql); if (isSelect()) { for(int i = 0; i < numFields; i++) { MYSQL_FIELD* field = mysql_fetch_field_direct(d->result, i); d->fields[i].type = qDecodeMYSQLType(field->type, field->flags); } setAt(QSql::BeforeFirstRow); } setActive(true); return isActive(); }
bool QMYSQLResult::reset ( const QString& query ) { if ( !driver() ) return FALSE; if ( !driver()-> isOpen() || driver()->isOpenError() ) return FALSE; cleanup(); const char *encQuery = query.ascii(); if ( mysql_real_query( d->mysql, encQuery, qstrlen(encQuery) ) ) { setLastError( qMakeError("Unable to execute query", QSqlError::Statement, d ) ); return FALSE; } if ( isForwardOnly() ) { if ( isActive() || isValid() ) // have to empty the results from previous query fetchLast(); d->result = mysql_use_result( d->mysql ); } else { d->result = mysql_store_result( d->mysql ); } if ( !d->result && mysql_field_count( d->mysql ) > 0 ) { setLastError( qMakeError( "Unable to store result", QSqlError::Statement, d ) ); return FALSE; } int numFields = mysql_field_count( d->mysql ); setSelect( !( numFields == 0) ); d->fieldTypes.resize( numFields ); if ( isSelect() ) { for( int i = 0; i < numFields; i++) { MYSQL_FIELD* field = mysql_fetch_field_direct( d->result, i ); if ( field->type == FIELD_TYPE_DECIMAL ) d->fieldTypes[i] = QVariant::String; else d->fieldTypes[i] = qDecodeMYSQLType( field->type, field->flags ); } } setActive( TRUE ); return TRUE; }
void Light::draw(QPainter * painter) { painter->save(); if(isSelect()) { painter->setPen(Qt::yellow); painter->setBrush(QBrush(Qt::yellow)); drawElemScen(painter, m_coordinate.x() - 1, m_coordinate.y() - 1, SIZE_LIGHT + 2); } switch(this->m_state) { case Light::Red: painter->setPen(Qt::red); painter->setBrush(QBrush(Qt::red)); break; case Light::Green: painter->setPen(Qt::green); painter->setBrush(QBrush(Qt::green)); break; } drawElemScen(painter, m_coordinate.x(), m_coordinate.y(), SIZE_LIGHT); painter->setPen(Qt::darkGray); painter->setBrush(QBrush(Qt::darkGray)); painter->restore(); }
/* Execute \a query. */ bool QSQLiteResult::reset (const QString& query) { // this is where we build a query. if (!driver()) return FALSE; if (!driver()-> isOpen() || driver()->isOpenError()) return FALSE; d->cleanup(); // Um, ok. callback based so.... pass private static function for this. setSelect(FALSE); char *err = 0; int res = sqlite_compile(d->access, d->utf8 ? (const char*)query.utf8().data() : query.ascii(), &(d->currentTail), &(d->currentMachine), &err); if (res != SQLITE_OK || err) { setLastError(QSqlError("Unable to execute statement", err, QSqlError::Statement, res)); sqlite_freemem(err); } //if (*d->currentTail != '\000' then there is more sql to eval if (!d->currentMachine) { setActive(FALSE); return FALSE; } // we have to fetch one row to find out about // the structure of the result set d->skippedStatus = d->fetchNext(0); setSelect(!d->rInf.isEmpty()); if (isSelect()) init(d->rInf.count()); setActive(TRUE); return TRUE; }
/*! Retrieves the previous record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the \l{isActive()}{active} state and isSelect() must return true before calling this function or it will do nothing and return false. The following rules apply: \list \li If the result is currently located before the first record, there is no change and false is returned. \li If the result is currently located after the last record, an attempt is made to retrieve the last record. \li If the result is somewhere in the middle, an attempt is made to retrieve the previous record. \endlist If the record could not be retrieved, the result is positioned before the first record and false is returned. If the record is successfully retrieved, true is returned. \sa next(), first(), last(), seek(), at(), isActive(), isValid() */ bool QSqlQuery::previous() { if (!isSelect() || !isActive()) return false; if (isForwardOnly()) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } bool b = false; switch (at()) { case QSql::BeforeFirstRow: return false; case QSql::AfterLastRow: b = d->sqlResult->fetchLast(); return b; default: if (!d->sqlResult->fetchPrevious()) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } return true; } }
bool MSqlQuery::exec() { if (!m_db) { // Database structure's been deleted return false; } if (m_last_prepared_query.isEmpty()) { LOG(VB_GENERAL, LOG_ERR, "MSqlQuery::exec(void) called without a prepared query."); return false; } #if DEBUG_RECONNECT if (random() < RAND_MAX / 50) { LOG(VB_GENERAL, LOG_INFO, "MSqlQuery disconnecting DB to test reconnection logic"); m_db->m_db.close(); } #endif // Database connection down. Try to restart it, give up if it's still // down if (!m_db->isOpen() && !Reconnect()) { LOG(VB_GENERAL, LOG_INFO, "MySQL server disconnected"); return false; } QElapsedTimer timer; timer.start(); bool result = QSqlQuery::exec(); qint64 elapsed = timer.elapsed(); // if the query failed with "MySQL server has gone away" // Close and reopen the database connection and retry the query if it // connects again if (!result && QSqlQuery::lastError().number() == 2006 && Reconnect()) result = QSqlQuery::exec(); if (!result) { QString err = MythDB::GetError("MSqlQuery", *this); MSqlBindings tmp = QSqlQuery::boundValues(); bool has_null_strings = false; for (MSqlBindings::iterator it = tmp.begin(); it != tmp.end(); ++it) { if (it->type() != QVariant::String) continue; if (it->isNull() || it->toString().isNull()) { has_null_strings = true; *it = QVariant(QString("")); } } if (has_null_strings) { bindValues(tmp); timer.restart(); result = QSqlQuery::exec(); elapsed = timer.elapsed(); } if (result) { LOG(VB_GENERAL, LOG_ERR, QString("Original query failed, but resend with empty " "strings in place of NULL strings worked. ") + "\n" + err); } } if (VERBOSE_LEVEL_CHECK(VB_DATABASE, LOG_INFO)) { QString str = lastQuery(); // Database logging will cause an infinite loop here if not filtered // out if (!str.startsWith("INSERT INTO logging ")) { // Sadly, neither executedQuery() nor lastQuery() display // the values in bound queries against a MySQL5 database. // So, replace the named placeholders with their values. QMapIterator<QString, QVariant> b = boundValues(); while (b.hasNext()) { b.next(); str.replace(b.key(), '\'' + b.value().toString() + '\''); } LOG(VB_DATABASE, LOG_INFO, QString("MSqlQuery::exec(%1) %2%3%4") .arg(m_db->MSqlDatabase::GetConnectionName()).arg(str) .arg(QString(" <<<< Took %1ms").arg(QString::number(elapsed))) .arg(isSelect() ? QString(", Returned %1 row(s)") .arg(size()) : QString())); } } return result; }
QSqlRecord QSQLite2Result::record() const { if (!isActive() || !isSelect()) return QSqlRecord(); return d->rInf; }
/*! Retrieves the record at position (offset) \a i, if available, and positions the query on the retrieved record. The first record is at position 0. Note that the query must be in an active state and isSelect() must return TRUE before calling this function. If \a relative is FALSE (the default), the following rules apply: \list \i If \a i is negative, the result is positioned before the first record and FALSE is returned. \i Otherwise, an attempt is made to move to the record at position \a i. If the record at position \a i could not be retrieved, the result is positioned after the last record and FALSE is returned. If the record is successfully retrieved, TRUE is returned. \endlist If \a relative is TRUE, the following rules apply: \list \i If the result is currently positioned before the first record or on the first record, and \a i is negative, there is no change, and FALSE is returned. \i If the result is currently located after the last record, and \a i is positive, there is no change, and FALSE is returned. \i If the result is currently located somewhere in the middle, and the relative offset \a i moves the result below zero, the result is positioned before the first record and FALSE is returned. \i Otherwise, an attempt is made to move to the record \a i records ahead of the current record (or \a i records behind the current record if \a i is negative). If the record at offset \a i could not be retrieved, the result is positioned after the last record if \a i >= 0, (or before the first record if \a i is negative), and FALSE is returned. If the record is successfully retrieved, TRUE is returned. \endlist \sa next() prev() first() last() at() isActive() isValid() */ bool QSqlQuery::seek( int i, bool relative ) { if ( !isSelect() || !isActive() ) return FALSE; beforeSeek(); checkDetach(); int actualIdx; if ( !relative ) { // arbitrary seek if ( i < 0 ) { d->sqlResult->setAt( QSql::BeforeFirst ); afterSeek(); return FALSE; } actualIdx = i; } else { switch ( at() ) { // relative seek case QSql::BeforeFirst: if ( i > 0 ) actualIdx = i; else { afterSeek(); return FALSE; } break; case QSql::AfterLast: if ( i < 0 ) { d->sqlResult->fetchLast(); actualIdx = at() + i; } else { afterSeek(); return FALSE; } break; default: if ( ( at() + i ) < 0 ) { d->sqlResult->setAt( QSql::BeforeFirst ); afterSeek(); return FALSE; } actualIdx = at() + i; break; } } // let drivers optimize if ( isForwardOnly() && actualIdx < at() ) { #ifdef QT_CHECK_RANGE qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query" ); #endif afterSeek(); return FALSE; } if ( actualIdx == ( at() + 1 ) && at() != QSql::BeforeFirst ) { if ( !d->sqlResult->fetchNext() ) { d->sqlResult->setAt( QSql::AfterLast ); afterSeek(); return FALSE; } afterSeek(); return TRUE; } if ( actualIdx == ( at() - 1 ) ) { if ( !d->sqlResult->fetchPrev() ) { d->sqlResult->setAt( QSql::BeforeFirst ); afterSeek(); return FALSE; } afterSeek(); return TRUE; } if ( !d->sqlResult->fetch( actualIdx ) ) { d->sqlResult->setAt( QSql::AfterLast ); afterSeek(); return FALSE; } afterSeek(); return TRUE; }
QVariant embeddedResult::data(int field) { if (!isSelect() || field >= d->fields.count()) { printf("embeddedResult::data: column %d out of range", field); return QVariant(); } if (!d->driver) return QVariant(); int fieldLength = 0; const embeddedResultPrivate::QMyField &f = d->fields.at(field); QString val; if (d->preparedQuery) { if (f.nullIndicator) return QVariant(f.type); if (f.type != QVariant::ByteArray) val = toUnicode(d->driver->d->tc, f.outField, f.bufLength); } else { if (d->row[field] == NULL) { // NULL value return QVariant(f.type); } fieldLength = mysql_fetch_lengths(d->result)[field]; if (f.type != QVariant::ByteArray) val = toUnicode(d->driver->d->tc, d->row[field], fieldLength); } switch(f.type) { case QVariant::LongLong: return QVariant(val.toLongLong()); case QVariant::ULongLong: return QVariant(val.toULongLong()); case QVariant::Int: return QVariant(val.toInt()); case QVariant::UInt: return QVariant(val.toUInt()); case QVariant::Double: { QVariant v; bool ok=false; double dbl = val.toDouble(&ok); switch(numericalPrecisionPolicy()) { case QSql::LowPrecisionInt32: v=QVariant(dbl).toInt(); break; case QSql::LowPrecisionInt64: v = QVariant(dbl).toLongLong(); break; case QSql::LowPrecisionDouble: v = QVariant(dbl); break; case QSql::HighPrecision: default: v = val; ok = true; break; } if(ok) return v; else return QVariant(); } return QVariant(val.toDouble()); case QVariant::Date: return qDateFromString(val); case QVariant::Time: return qTimeFromString(val); case QVariant::DateTime: return qDateTimeFromString(val); case QVariant::ByteArray: { QByteArray ba; if (d->preparedQuery) { ba = QByteArray(f.outField, f.bufLength); } else { ba = QByteArray(d->row[field], fieldLength); } return QVariant(ba); } default: case QVariant::String: return QVariant(val); } printf("embeddedResult::data: unknown data type"); return QVariant(); }
/*! Retrieves the record at position \a index, if available, and positions the query on the retrieved record. The first record is at position 0. Note that the query must be in an \l{isActive()} {active} state and isSelect() must return true before calling this function. If \a relative is false (the default), the following rules apply: \list \li If \a index is negative, the result is positioned before the first record and false is returned. \li Otherwise, an attempt is made to move to the record at position \a index. If the record at position \a index could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned. \endlist If \a relative is true, the following rules apply: \list \li If the result is currently positioned before the first record or on the first record, and \a index is negative, there is no change, and false is returned. \li If the result is currently located after the last record, and \a index is positive, there is no change, and false is returned. \li If the result is currently located somewhere in the middle, and the relative offset \a index moves the result below zero, the result is positioned before the first record and false is returned. \li Otherwise, an attempt is made to move to the record \a index records ahead of the current record (or \a index records behind the current record if \a index is negative). If the record at offset \a index could not be retrieved, the result is positioned after the last record if \a index >= 0, (or before the first record if \a index is negative), and false is returned. If the record is successfully retrieved, true is returned. \endlist \sa next(), previous(), first(), last(), at(), isActive(), isValid() */ bool QSqlQuery::seek(int index, bool relative) { if (!isSelect() || !isActive()) return false; int actualIdx; if (!relative) { // arbitrary seek if (index < 0) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } actualIdx = index; } else { switch (at()) { // relative seek case QSql::BeforeFirstRow: if (index > 0) actualIdx = index; else { return false; } break; case QSql::AfterLastRow: if (index < 0) { d->sqlResult->fetchLast(); actualIdx = at() + index; } else { return false; } break; default: if ((at() + index) < 0) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } actualIdx = at() + index; break; } } // let drivers optimize if (isForwardOnly() && actualIdx < at()) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } if (actualIdx == (at() + 1) && at() != QSql::BeforeFirstRow) { if (!d->sqlResult->fetchNext()) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true; } if (actualIdx == (at() - 1)) { if (!d->sqlResult->fetchPrevious()) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } return true; } if (!d->sqlResult->fetch(actualIdx)) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true; }