示例#1
0
int AnimDataSource::incrementRevision() {
	QVariantList dataList;

	// When something has changed in the database the overall revision of the
	// table is incremented by one, this in order for the AsyncDataModel and its
	// DataQuery to be able to update only the affected items.
	dataList << "animinfo";
	mSqlConnector->executeAndWait(
			"UPDATE revision SET revision_id = revision_id + 1 WHERE table_name=:animinfo",
			dataList, 1);

	DataAccessReply reply = mSqlConnector->executeAndWait(
			"SELECT revision_id FROM revision WHERE table_name=:animinfo",
			dataList, 2);

	if (!reply.hasError()) {
		QVariantList resultList = reply.result().toList();
		QVariantMap resultMap = resultList.at(0).toMap();
		mRevision = resultMap["revision_id"].toInt();
	} else {
		qDebug() << "Revision could not be updated.";
	}

	return mRevision;
}
示例#2
0
bool CityModel::updateFavoriteCity(QString city, bool isFavorite)
{
    // Update the SQL table.
    QString query;

    QTextStream(&query) << "UPDATE cities SET favorite='" << isFavorite << "' WHERE name='" << city << "'";
    DataAccessReply reply = mSqlConnector->executeAndWait(query, UPDATE_FAVORITES);

    if (reply.hasError()) {
        qWarning() << "updateFavoriteCity error " << reply;
        return false;
    }

    return true;
}
int WeatherDataSource::incrementRevision()
{
    QVariantList dataList;
    dataList << "weather";
    mSqlConnector->executeAndWait("UPDATE revision SET revision_id = revision_id + 1 WHERE table_name=:weather",
            dataList, 1);

    DataAccessReply reply = mSqlConnector->executeAndWait("SELECT revision_id FROM revision WHERE table_name=:weather", dataList, 2);

    QVariantList resultList = reply.result().toList();
    QVariantMap resultMap = resultList.at(0).toMap();
    mRevision = resultMap["revision_id"].toInt();

    qDebug() << "Revision is now " << mRevision;
    return mRevision;
}
示例#4
0
void CityModel::changeContinent(QString continent)
{
    QString query;

    // Remove previous items from the model, all items will be replaced by
    // cities from the continent given as a parameter.
    this->clear();

    if (continent.contains("All")) {
        // We have one special case where we load "All Cities", if this 
        // is selected we do not load all cities at once but instead load 
        // an initial batch, then we continue to load the rest in another thread.
        mSqlConnector->beginTransaction(BEGIN_TRANSACTION_ID);

        // We load the first batch in a synchronous fashion, but only request ASYNCH_BATCH_SIZE items.
        // the rest will be loaded asynchronously in another thread by issuing execute() on the SqlConnection
        // see the onLoadAsyncResultData function below.
        QTextStream(&query) << "select * from cities limit " << ASYNCH_BATCH_SIZE;
        DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

        if (reply.hasError()) {
            qWarning() << "initial load error: " << reply;
            mSqlConnector->rollbackTransaction(ROLLBACK_ID);
        } else {
            onLoadAsyncResultData(reply);

            // Post a request that will be performed asynchronous.
            query.clear();
            QTextStream(&query) << "select * from cities limit " << ASYNCH_BATCH_SIZE << " offset " << +ASYNCH_BATCH_SIZE;
            mSqlConnector->execute(query, ASYNCH_LOAD_ID);

            mSqlConnector->endTransaction(END_TRANSACTION_ID);
        }
    } else {
        // When loading a specific continent, we load the entire list synchronously.
        query = "select * from cities where continent='" + continent + "'";
        DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

        if (reply.hasError()) {
            qWarning() << "onChangeContinent load error for " << continent << " " << reply;
        } else {
            // Load result directly.
            onLoadAsyncResultData(reply);
        }
    }
}
示例#5
0
void CityModel::loadFavoriteCities()
{
    // First of all we clear the model to make sure no items are lingering.
    this->clear();

    // Request all cities which has the favorite property set to true.
    QString query = "select * from cities where favorite='1'";

    // For the favorites list we do not load the data asynchronously, we assume that
    // there will not be a huge number of favorite cities, thus executeAndWait is used.
    DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

    if (reply.hasError()) {
        qWarning() << "onLoadFavorites load error " << reply;
    } else {
        // If no problem occurred load the data by calling the slot function directly.
        onLoadAsyncResultData(reply);
    }
}
示例#6
0
void CityModel::onSetFavoriteCity(QString city)
{
    if (updateFavoriteCity(city, true)) {

        // After setting a new city as favorite we load the corresponding item data
        // and add it to the model, this will automatically make it appear in the list.
        QString query = "select * from cities WHERE name='" + city + "'";
        DataAccessReply reply = mSqlConnector->executeAndWait(query, INITIAL_LOAD_ID);

        if (reply.hasError()) {
            qWarning() << "onLoadAsyncResultData: " << reply.id() << ", SQL error: " << reply;
        } else {
            QVariantList cityData = reply.result().value<QVariantList>();

            // Check if the city is already in the list, if its not we add it to the model, first is 
            // used since in our city data there is only one instance of each city.
            QVariantMap addedItem = cityData.first().toMap();
            if (find(addedItem).isEmpty()) {
                insert(addedItem);
            }
        }
    }
}