Commit a7b2232a authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add a check for validity

parent a2447974
......@@ -33,6 +33,8 @@ class KisResourceCacheDb::Private
{
public:
bool valid {false};
QSqlError initDb();
};
......@@ -43,12 +45,18 @@ KisResourceCacheDb::KisResourceCacheDb()
if (err.isValid()) {
qWarning() << "Could not initialize the database:" << err;
}
d->valid = !err.isValid();
}
KisResourceCacheDb::~KisResourceCacheDb()
{
}
bool KisResourceCacheDb::isValid() const
{
return d->valid;
}
QSqlError KisResourceCacheDb::Private::initDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
......@@ -80,14 +88,58 @@ QSqlError KisResourceCacheDb::Private::initDb()
Q_FOREACH(const QString &table, tables) {
QFile f(":/create_" + table + ".sql");
qDebug() << "Running SQL" << f;
if (f.open(QFile::ReadOnly)) {
QString sql(f.readAll());
if (!query.exec(sql)) {
return db.lastError();
}
}
else {
return QSqlError("Error executing SQL", QString("Could not find SQL file %1").arg(table), QSqlError::StatementError);
}
}
{
QStringList originTypes = QStringList() << "INSTALLATION" // Installed by Krita
<< "BUNDLE" // Bundle installed by the user
<< "ADOBE_LIBRARY" // ABR or ASL or similar Adobe resource library
<< "USER"; // Installed or created by the user
QFile f(":/fill_origin_types.sql");
f.open(QFile::ReadOnly);
QString sql = f.readAll();
Q_FOREACH(const QString &originType, originTypes) {
query.addBindValue(originTypes.indexOf(originType));
query.addBindValue(originType);
if (!query.exec(sql)) {
qDebug() << "Could not insert" << originType;
return db.lastError();
}
}
}
{
QStringList resourceTypes = QStringList() << "BRUSH_TIP"
<< "GRADIENT"
<< "PAINTOP_PRESET"
<< "COLORSET"
<< "PATTERN"
<< "SYMBOL_LIBRARY"
<< "TEMPLATE"
<< "WORKSPACE"
<< "SESSION";
QFile f(":/fill_resource_types.sql");
f.open(QFile::ReadOnly);
QString sql = f.readAll();
Q_FOREACH(const QString &resourceType, resourceTypes) {
query.addBindValue(resourceTypes.indexOf(resourceType));
query.addBindValue(resourceType);
if (!query.exec(sql)) {
qDebug() << "Could not insert" << resourceType;
return db.lastError();
}
}
}
return QSqlError();
}
......@@ -43,6 +43,12 @@ public:
KisResourceCacheDb();
~KisResourceCacheDb();
/**
* @brief isValid
* @return true if the database has been correctly created
*/
bool isValid() const;
private:
class Private;
......
CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY,
name TEXT
)
CREATE TABLE IF NOT EXISTS translations (
id INTEGER PRIMARY KEY,
name TEXT
)
CREATE TABLE IF NOT EXISTS versioned_resources (
id INTEGER PRIMARY KEY,
name TEXT
)
INSERT into origin_types (id, type) VALUES(?,?);
INSERT into origin_types (id, name) VALUES(?,?);
INSERT into resource_types (id, name) VALUES(?,?);
......@@ -42,6 +42,9 @@ void TestResourceCacheDb::testCreateDatabase()
{
KisResourceCacheDb cacheDb;
Q_UNUSED(cacheDb);
QVERIFY(cacheDb.isValid());
QSqlDatabase sqlDb = QSqlDatabase::database();
QStringList tables = QStringList() << "origin_types"
......@@ -60,6 +63,11 @@ void TestResourceCacheDb::testCreateDatabase()
}
void TestResourceCacheDb::testLookupTables()
{
}
void TestResourceCacheDb::cleanupTestCase()
{
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement");
......
......@@ -28,6 +28,7 @@ class TestResourceCacheDb : public QObject
private Q_SLOTS:
void initTestCase();
void testCreateDatabase();
void testLookupTables();
void cleanupTestCase();
private:
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment