bool ComicDatabaseResource::openDb() { if (!QFile::exists(dbFilePath()) && !createDbFile()) return false; db.setDatabaseName(dbFilePath()); return db.open(); }
bool ComicDatabaseResource::createDbFile() { QDir dbDir; QFile dbFile(dbFilePath()); return dbDir.mkpath(dbDirPath()) && dbFile.open(QIODevice::ReadWrite); }
void BasicApplication::initializeDirectories() { Path dir = Path::forDirectory(config().getString("application.dir")); config().setString("blissart.binDir", dir.toString()); dir.popDirectory(); config().setString("blissart.installDir", dir.toString()); dir.pushDirectory("etc"); File(dir).createDirectory(); config().setString("blissart.configDir", dir.toString()); dir.popDirectory(); // The storage subsystem's realm... if (!_storageDir.empty()) { Path sdPath = Path::forDirectory(_storageDir); File(sdPath).createDirectories(); logger().information("Using storage directory: " + sdPath.toString()); config().setString("blissart.storageDir", sdPath.toString()); } else { dir.pushDirectory("storage"); File(dir).createDirectory(); config().setString("blissart.storageDir", dir.toString()); dir.popDirectory(); } // Where the database subsystem roams... if (!_dbFile.empty()) { Path dbFilePath(Path::expand(_dbFile)); dbFilePath.makeFile(); if (dbFilePath.depth() > 0) { Path dbParentDir(dbFilePath); dbParentDir.setFileName(""); //dbParentDir.popDirectory(); File(dbParentDir).createDirectories(); } logger().information("Using database file: " + dbFilePath.toString()); config().setString("blissart.databaseFile", dbFilePath.toString()); } else { dir.pushDirectory("db"); File(dir).createDirectory(); config().setString("blissart.databaseDir", dir.toString()); dir.setFileName("openBliSSART.db"); config().setString("blissart.databaseFile", dir.toString()); } }
void MainWindow::dbReset() { if (m_db.isOpen()) { m_db.close(); } QFileInfo fi(dbFilePath()); if (!fi.exists()) { logMsg("No sqlite database found as " + m_dbPath, 0); return; } QFile f(m_dbPath); if (f.remove()) { logMsg("Successfully removed sqlite db " + m_dbPath, 1); } else { logMsg("Failed to remove sqlite db " + m_dbPath + " - " + f.errorString(), -1); } }
/** * @brief DbCoordinator::openDatabase Open a database or create a new * one if non existent. * @param path the path to the folder that contains the database * @param dbName the name of the database to open. Relative or Absolute path. */ void DbCoordinator::openDatabase(QString path, QString dbName) { if (isOpened()) { qDebug() << "Database is already opened!"; return; } QString dbFilePath(QString(path + "/" + dbName)); QFile dbFile(dbFilePath); bool needsGenerateSchema = false; if(!dbFile.exists()) { needsGenerateSchema = true; } m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(dbFilePath); bool db_opened = m_db.open(); if (db_opened) { qDebug() << "Opened Database for access."; opened = true; if (needsGenerateSchema) { QString schemaFilePath(QString(path + "TAEval.sql")); qDebug() << "Generating Schema..." << schemaFilePath; QFile* file = new QFile(schemaFilePath); runSqlScript(file); delete file; qDebug() << "Generated DB " << dbName; } } }
bool MainWindow::dbAvailable() { if (m_db.isOpen()) { if (dbVersionOk()) { return true; } } QFileInfo fi(dbFilePath()); if (!fi.exists()) { logMsg("Creating sqlite database " + m_dbPath, 0); } //qDebug() << "Instantiating mysql connection, lib paths:" << QCoreApplication::libraryPaths(); m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(m_dbPath); //qDebug() << "Attempting to open db"; if (!m_db.open()) { return false; } if (!dbVersionOk()) { // Reopen database which was closed in dbReset(), called from dbVersionOk() in the failing case logMsg("Updating database", 0); bool openOK = m_db.open(); if (!openOK) return false; // Create missing tables dbAssertTables(); // Insert current version if (!dbInsertVersion()) logMsg("Failed to update db version", 0); return openOK; } // Opened successfully and version check passed return true; }