示例#1
0
void ManageBDD::copyDatabaseToWritableDirectory(const QString &dbName)
{
    if (this->databasePath.isEmpty())
    {
        QDir dbDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
        this->databasePath = dbDir.absolutePath();
        qDebug() << this->databasePath;
        if (!dbDir.exists())
            dbDir.mkpath(dbDir.absolutePath());
        qDebug() << "Data dir exists : " << dbDir.exists();
    }
    QFile dbFile(this->databasePath + "/" + dbName);
    qDebug() << dbFile.fileName();
    if (!dbFile.exists())
    {
        qDebug() << "File doesn't exist";
        QFile dbTemplate(this->applicationPath + "/databases/" + dbName);
        qDebug() << "Trying to copy " << dbTemplate.fileName();
        if (dbTemplate.exists())
            dbTemplate.copy(dbFile.fileName());

    }
    else
        qDebug() << "File already exists";
    dbFile.setPermissions(QFile::ReadOwner|QFile::WriteOwner);
    // OPENED DB PATH IS SAVED IN HASH
    this->nameToPathHash[dbName] = dbFile.fileName();
    this->localDBName = this->nameToPathHash[dbName];
}
示例#2
0
bool PersistentStore::init()
{
    QMutexLocker locker(&_mutex);

    // Get application data directory and database filename.
    QSettings settings;
    QString   appdata = settings.value("app/app_data").toString();
    if(appdata.isEmpty())
    {
        DEBUG << "Error creating the PersistentStore: app.appdata not set.";
        return false;
    }
    bool      create = false;
    QString   dbUrl(appdata + QDir::separator() + DEFAULT_DBNAME);
    QFileInfo dbFileInfo(dbUrl);

    // Initialize database object.
    _db = _db.addDatabase("QSQLITE", "tarsnap");
    _db.setConnectOptions("QSQLITE_OPEN_URI");
    _db.setDatabaseName(dbUrl);

    // Determine whether to try to open the database.
    if(!dbFileInfo.exists())
    {
        create = true;
    }
    else if(!dbFileInfo.isFile() || !dbFileInfo.isReadable())
    {
        DEBUG
            << "Error creating the PersistentStore: DB file is not accessible "
            << dbUrl;
        return false;
    } // Database file exists and is readable; attempt to open.
    else if(!_db.open())
    {
        DEBUG << "Error opening the PersistentStore DB: "
              << _db.lastError().text();
        return false;
    }
    else
    {
        // Successfully opened database.
        QStringList tables = _db.tables();
        // Is the database valid?
        if(!tables.contains("archives", Qt::CaseInsensitive))
        {
            _db.close();
            DEBUG << "Invalid PersistentStore DB found. Attempting to recover.";
            QString newName(dbUrl + "." +
                            QString::number(QDateTime::currentMSecsSinceEpoch()));
            if(!QFile::rename(dbUrl, newName))
            {
                DEBUG << "Failed to rename current invalid PersistentStore DB. "
                         "Please manually clean up the DB directory "
                      << appdata;
                return false;
            }
            create = true;
        }
        else
        {
            // Check the database version, and upgrade if necessary.
            if(!tables.contains("version", Qt::CaseInsensitive))
            {
                if(!upgradeVersion0())
                {
                    DEBUG << "Failed to upgrade PersistentStore DB. It's best "
                             "to start from scratch by purging the existing DB "
                             "in "
                          << appdata;
                    return false;
                }
            }
            int       version = -1;
            QSqlQuery query(_db);
            if(query.exec("SELECT version FROM version"))
            {
                query.next();
                version = query.value(0).toInt();
            }
            else
            {
                DEBUG << "Failed to get current DB version: "
                      << query.lastError().text();
                return false;
            }
            if((version == 0) && upgradeVersion1())
            {
                DEBUG << "DB upgraded to version 1.";
                version = 1;
            }
            if((version == 1) && upgradeVersion2())
            {
                DEBUG << "DB upgraded to version 2.";
                version = 2;
            }
            if((version == 2) && upgradeVersion3())
            {
                DEBUG << "DB upgraded to version 3.";
                version = 3;
            }
            if((version == 3) && upgradeVersion4())
            {
                DEBUG << "DB upgraded to version 4.";
                version = 4;
            }
        }
    }

    // Create new database (if needed).
    if(create)
    {
        QFile dbTemplate(":/dbtemplate.db");
        if(!dbTemplate.copy(dbUrl))
        {
            DEBUG << "Failed to create the PersistentStore DB.";
            return false;
        }
        // Work around the fact that QFile::copy from the resource system does
        // not set u+w on the resulted file
        QFile dbFile(dbUrl);
        dbFile.setPermissions(dbFile.permissions() | QFileDevice::WriteOwner);
        dbFile.close();
        if(!_db.open())
        {
            DEBUG << "Error opening the PersistentStore DB: "
                  << _db.lastError().text();
            return false;
        }
    }
    return _initialized = true;
}