//---------------------------------------------------------------------------- QSqlDatabase ctkPluginStorageSQL::getConnection(bool create) const { if (m_connectionNames.hasLocalData() && QSqlDatabase::contains(m_connectionNames.localData())) { return QSqlDatabase::database(m_connectionNames.localData()); } if (!create) { throw ctkPluginDatabaseException(QString("No database connection."), ctkPluginDatabaseException::DB_NOT_OPEN_ERROR); } m_connectionNames.setLocalData(getConnectionName()); QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE", m_connectionNames.localData()); database.setDatabaseName(getDatabasePath()); if (!database.isValid()) { close(); throw ctkPluginDatabaseException(QString("Invalid database connection: %1").arg(m_connectionNames.localData()), ctkPluginDatabaseException::DB_CONNECTION_INVALID); } if (!database.open()) { close(); throw ctkPluginDatabaseException(QString("Could not open database connection: %1 (%2)").arg(m_connectionNames.localData()).arg(database.lastError().text()), ctkPluginDatabaseException::DB_SQL_ERROR); } return database; }
//---------------------------------------------------------------------------- void ctkPluginStorageSQL::open() { createDatabaseDirectory(); QSqlDatabase database = getConnection(); //Check if the sqlite version supports foreign key constraints QSqlQuery query(database); if (!query.exec("PRAGMA foreign_keys")) { close(); throw ctkPluginDatabaseException(QString("Check for foreign key support failed."), ctkPluginDatabaseException::DB_SQL_ERROR); } if (!query.next()) { close(); throw ctkPluginDatabaseException(QString("SQLite db does not support foreign keys. It is either older than 3.6.19 or was compiled with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER"), ctkPluginDatabaseException::DB_SQL_ERROR); } query.finish(); query.clear(); //Enable foreign key support if (!query.exec("PRAGMA foreign_keys = ON")) { close(); throw ctkPluginDatabaseException(QString("Enabling foreign key support failed."), ctkPluginDatabaseException::DB_SQL_ERROR); } query.finish(); //Check database structure (tables) and recreate tables if neccessary //If one of the tables is missing remove all tables and recreate them //This operation is required in order to avoid data coruption if (!checkTables()) { if (dropTables()) { createTables(); } else { //dropTables() should've handled error message //and warning close(); } } // silently remove any plugin marked as uninstalled cleanupDB(); //Update database based on the recorded timestamps updateDB(); initNextFreeIds(); }
//---------------------------------------------------------------------------- void ctkPluginStorageSQL::createDatabaseDirectory() const { QString path = getDatabasePath(); QFileInfo fileInfo(path); if (!fileInfo.dir().exists()) { if (!QDir::root().mkpath(fileInfo.path())) { close(); throw ctkPluginDatabaseException(QString("Could not create database directory: %1").arg(fileInfo.path()), ctkPluginDatabaseException::DB_CREATE_DIR_ERROR); } } }
//---------------------------------------------------------------------------- void ctkPluginStorageSQL::open() { if (m_isDatabaseOpen) return; QString path; //Create full path to database if(m_databasePath.isEmpty ()) m_databasePath = getDatabasePath(); path = m_databasePath; QFileInfo dbFileInfo(path); if (!dbFileInfo.dir().exists()) { if(!QDir::root().mkpath(dbFileInfo.path())) { close(); QString errorText("Could not create database directory: %1"); throw ctkPluginDatabaseException(errorText.arg(dbFileInfo.path()), ctkPluginDatabaseException::DB_CREATE_DIR_ERROR); } } m_connectionName = dbFileInfo.completeBaseName(); QSqlDatabase database; if (QSqlDatabase::contains(m_connectionName)) { database = QSqlDatabase::database(m_connectionName); } else { database = QSqlDatabase::addDatabase("QSQLITE", m_connectionName); database.setDatabaseName(path); } if (!database.isValid()) { close(); throw ctkPluginDatabaseException(QString("Invalid database connection: %1").arg(m_connectionName), ctkPluginDatabaseException::DB_CONNECTION_INVALID); } //Create or open database if (!database.isOpen()) { if (!database.open()) { close(); throw ctkPluginDatabaseException(QString("Could not open database. ") + database.lastError().text(), ctkPluginDatabaseException::DB_SQL_ERROR); } } m_isDatabaseOpen = true; //Check if the sqlite version supports foreign key constraints QSqlQuery query(database); if (!query.exec("PRAGMA foreign_keys")) { close(); throw ctkPluginDatabaseException(QString("Check for foreign key support failed."), ctkPluginDatabaseException::DB_SQL_ERROR); } if (!query.next()) { close(); throw ctkPluginDatabaseException(QString("SQLite db does not support foreign keys. It is either older than 3.6.19 or was compiled with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER"), ctkPluginDatabaseException::DB_SQL_ERROR); } query.finish(); query.clear(); //Enable foreign key support if (!query.exec("PRAGMA foreign_keys = ON")) { close(); throw ctkPluginDatabaseException(QString("Enabling foreign key support failed."), ctkPluginDatabaseException::DB_SQL_ERROR); } query.finish(); //Check database structure (tables) and recreate tables if neccessary //If one of the tables is missing remove all tables and recreate them //This operation is required in order to avoid data coruption if (!checkTables()) { if (dropTables()) { createTables(); } else { //dropTables() should've handled error message //and warning close(); } } // silently remove any plugin marked as uninstalled cleanupDB(); //Update database based on the recorded timestamps updateDB(); initNextFreeIds(); }