Replaced all QSQLDatabase::exec() calls.

This commit is contained in:
Martin Rotter 2013-12-21 15:39:55 +01:00
parent a5d6b23f89
commit f837e43d07
3 changed files with 28 additions and 29 deletions

View File

@ -72,19 +72,20 @@ QSqlDatabase DatabaseFactory::initialize(const QString &connection_name) {
qPrintable(database.lastError().text()));
}
else {
// TODO: smazat QSQLDatabase::exec() všude
// a nahradit jej funkcí QSQLquery::exec()
QSqlQuery query_db(database);
database.exec("PRAGMA encoding = \"UTF-8\"");
database.exec("PRAGMA synchronous = OFF");
database.exec("PRAGMA journal_mode = MEMORY");
database.exec("PRAGMA count_changes = OFF");
database.exec("PRAGMA temp_store = MEMORY");
// TODO: smazat QSQLDatabase::exec() všude
// a nahradit jej funkcí QSQLquery::exec()
query_db.exec("PRAGMA encoding = \"UTF-8\"");
query_db.exec("PRAGMA synchronous = OFF");
query_db.exec("PRAGMA journal_mode = MEMORY");
query_db.exec("PRAGMA count_changes = OFF");
query_db.exec("PRAGMA temp_store = MEMORY");
// Sample query which checks for existence of tables.
QSqlQuery query = database.exec("SELECT value FROM Information WHERE key = 'schema_version'");
query_db.exec("SELECT value FROM Information WHERE key = 'schema_version'");
if (query.lastError().isValid()) {
if (query_db.lastError().isValid()) {
qWarning("Error occurred. Database is not initialized. Initializing now.");
QFile file_init(APP_MISC_PATH + QDir::separator() + APP_DB_INIT_FILE);
@ -97,29 +98,30 @@ QSqlDatabase DatabaseFactory::initialize(const QString &connection_name) {
QStringList statements = QString(file_init.readAll()).split(APP_DB_INIT_SPLIT,
QString::SkipEmptyParts);
database.exec("BEGIN TRANSACTION");
query_db.exec("BEGIN TRANSACTION");
foreach(const QString &statement, statements) {
query = database.exec(statement);
if (query.lastError().isValid()) {
query_db.exec(statement);
if (query_db.lastError().isValid()) {
qFatal("Database initialization failed. Initialization script '%s' is not correct.",
APP_DB_INIT_FILE);
}
}
database.exec("COMMIT");
query_db.exec("COMMIT");
qDebug("Database backend should be ready now.");
}
else {
query.next();
query_db.next();
qDebug("Database connection '%s' to file '%s' seems to be established.",
qPrintable(connection_name),
qPrintable(QDir::toNativeSeparators(database.databaseName())));
qDebug("Database has version '%s'.", qPrintable(query.value(0).toString()));
qDebug("Database has version '%s'.", qPrintable(query_db.value(0).toString()));
}
query.finish();
query_db.finish();
}
return database;

View File

@ -209,7 +209,7 @@ void FeedsModel::loadFromDatabase() {
FeedAssignment feeds;
// Obtain data for categories from the database.
QSqlQuery query_categories = database.exec("SELECT * FROM Categories;");
QSqlQuery query_categories("SELECT * FROM Categories;", database);
if (query_categories.lastError().isValid()) {
qFatal("Query for obtaining categories failed.");
@ -238,7 +238,7 @@ void FeedsModel::loadFromDatabase() {
}
// All categories are now loaded.
QSqlQuery query_feeds = database.exec("SELECT * FROM Feeds;");
QSqlQuery query_feeds("SELECT * FROM Feeds;", database);
if (query_feeds.lastError().isValid()) {
qFatal("Query for obtaining feeds failed.");

View File

@ -63,23 +63,20 @@ QString FeedsModelFeed::typeToString(FeedsModelFeed::Type type) {
void FeedsModelFeed::updateCounts(bool including_total_count) {
QSqlDatabase database = DatabaseFactory::getInstance()->addConnection("FeedsModelFeed");
QSqlQuery query_all(database);
if (including_total_count) {
// Obtain count of all messages.
QSqlQuery query_all = database.exec(QString("SELECT count() FROM messages "
"WHERE feed = %1 AND deleted = 0;").arg(id()));
if (query_all.next()){
if (query_all.exec(QString("SELECT count() FROM messages "
"WHERE feed = %1 AND deleted = 0;").arg(id())) &&
query_all.next()) {
m_totalCount = query_all.value(0).toInt();
}
}
// Obtain count of unread messages.
QSqlQuery query_unread = database.exec(QString("SELECT count() FROM messages "
"WHERE feed = %1 AND deleted = 0 AND read = 0;").arg(id()));
if (query_unread.next()) {
m_unreadCount = query_unread.value(0).toInt();
if (query_all.exec(QString("SELECT count() FROM messages "
"WHERE feed = %1 AND deleted = 0 AND read = 0;").arg(id())) &&
query_all.next()) {
m_unreadCount = query_all.value(0).toInt();
}
}