forked from qt-creator/qt-creator
Better error reporting when failing to open settings database
Mention the filename and the driver error text (though this last thing doesn't seem that useful). Also, don't spam with failures afterwards when the database failed to open.
This commit is contained in:
@@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#include <QtCore/QMap>
|
#include <QtCore/QMap>
|
||||||
#include <QtSql/QSqlDatabase>
|
#include <QtSql/QSqlDatabase>
|
||||||
|
#include <QtSql/QSqlError>
|
||||||
#include <QtSql/QSqlQuery>
|
#include <QtSql/QSqlQuery>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
@@ -101,16 +102,18 @@ SettingsDatabase::SettingsDatabase(const QString &path,
|
|||||||
|
|
||||||
d->m_db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("settings"));
|
d->m_db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("settings"));
|
||||||
d->m_db.setDatabaseName(fileName);
|
d->m_db.setDatabaseName(fileName);
|
||||||
if (!d->m_db.open())
|
if (!d->m_db.open()) {
|
||||||
qWarning() << "Warning: Failed to open settings database!";
|
qWarning().nospace() << "Warning: Failed to open settings database at " << fileName << " ("
|
||||||
|
<< d->m_db.lastError().driverText() << ")";
|
||||||
|
} else {
|
||||||
// Create the settings table if it doesn't exist yet
|
// Create the settings table if it doesn't exist yet
|
||||||
QSqlQuery query(d->m_db);
|
QSqlQuery query(d->m_db);
|
||||||
query.prepare(QLatin1String("CREATE TABLE IF NOT EXISTS settings ("
|
query.prepare(QLatin1String("CREATE TABLE IF NOT EXISTS settings ("
|
||||||
"key PRIMARY KEY ON CONFLICT REPLACE, "
|
"key PRIMARY KEY ON CONFLICT REPLACE, "
|
||||||
"value)"));
|
"value)"));
|
||||||
if (d->m_db.isOpen() && !query.exec())
|
if (!query.exec())
|
||||||
qWarning() << "Warning: Failed to prepare settings database!";
|
qWarning().nospace() << "Warning: Failed to prepare settings database! ("
|
||||||
|
<< query.lastError().driverText() << ")";
|
||||||
|
|
||||||
// Retrieve all available keys (values are retrieved lazily)
|
// Retrieve all available keys (values are retrieved lazily)
|
||||||
if (query.exec(QLatin1String("SELECT key FROM settings"))) {
|
if (query.exec(QLatin1String("SELECT key FROM settings"))) {
|
||||||
@@ -118,6 +121,7 @@ SettingsDatabase::SettingsDatabase(const QString &path,
|
|||||||
d->m_settings.insert(query.value(0).toString(), QVariant());
|
d->m_settings.insert(query.value(0).toString(), QVariant());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsDatabase::~SettingsDatabase()
|
SettingsDatabase::~SettingsDatabase()
|
||||||
@@ -135,6 +139,9 @@ void SettingsDatabase::setValue(const QString &key, const QVariant &value)
|
|||||||
// Add to cache
|
// Add to cache
|
||||||
d->m_settings.insert(effectiveKey, value);
|
d->m_settings.insert(effectiveKey, value);
|
||||||
|
|
||||||
|
if (!d->m_db.isOpen())
|
||||||
|
return;
|
||||||
|
|
||||||
// Instant apply (TODO: Delay writing out settings)
|
// Instant apply (TODO: Delay writing out settings)
|
||||||
QSqlQuery query(d->m_db);
|
QSqlQuery query(d->m_db);
|
||||||
query.prepare(QLatin1String("INSERT INTO settings VALUES (?, ?)"));
|
query.prepare(QLatin1String("INSERT INTO settings VALUES (?, ?)"));
|
||||||
@@ -154,7 +161,7 @@ QVariant SettingsDatabase::value(const QString &key, const QVariant &defaultValu
|
|||||||
SettingsMap::const_iterator i = d->m_settings.constFind(effectiveKey);
|
SettingsMap::const_iterator i = d->m_settings.constFind(effectiveKey);
|
||||||
if (i != d->m_settings.constEnd() && i.value().isValid()) {
|
if (i != d->m_settings.constEnd() && i.value().isValid()) {
|
||||||
value = i.value();
|
value = i.value();
|
||||||
} else {
|
} else if (d->m_db.isOpen()) {
|
||||||
// Try to read the value from the database
|
// Try to read the value from the database
|
||||||
QSqlQuery query(d->m_db);
|
QSqlQuery query(d->m_db);
|
||||||
query.prepare(QLatin1String("SELECT value FROM settings WHERE key = ?"));
|
query.prepare(QLatin1String("SELECT value FROM settings WHERE key = ?"));
|
||||||
@@ -183,13 +190,6 @@ void SettingsDatabase::remove(const QString &key)
|
|||||||
{
|
{
|
||||||
const QString effectiveKey = d->effectiveKey(key);
|
const QString effectiveKey = d->effectiveKey(key);
|
||||||
|
|
||||||
// Delete keys from the database
|
|
||||||
QSqlQuery query(d->m_db);
|
|
||||||
query.prepare(QLatin1String("DELETE FROM settings WHERE key = ? OR key LIKE ?"));
|
|
||||||
query.addBindValue(effectiveKey);
|
|
||||||
query.addBindValue(effectiveKey + QLatin1String("/%"));
|
|
||||||
query.exec();
|
|
||||||
|
|
||||||
// Remove keys from the cache
|
// Remove keys from the cache
|
||||||
foreach (const QString &k, d->m_settings.keys()) {
|
foreach (const QString &k, d->m_settings.keys()) {
|
||||||
// Either it's an exact match, or it matches up to a /
|
// Either it's an exact match, or it matches up to a /
|
||||||
@@ -200,6 +200,16 @@ void SettingsDatabase::remove(const QString &key)
|
|||||||
d->m_settings.remove(k);
|
d->m_settings.remove(k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!d->m_db.isOpen())
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Delete keys from the database
|
||||||
|
QSqlQuery query(d->m_db);
|
||||||
|
query.prepare(QLatin1String("DELETE FROM settings WHERE key = ? OR key LIKE ?"));
|
||||||
|
query.addBindValue(effectiveKey);
|
||||||
|
query.addBindValue(effectiveKey + QLatin1String("/%"));
|
||||||
|
query.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDatabase::beginGroup(const QString &prefix)
|
void SettingsDatabase::beginGroup(const QString &prefix)
|
||||||
|
|||||||
Reference in New Issue
Block a user