mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
11402 Save copy of project file to a backup database when saving project file
This commit is contained in:
@@ -54,6 +54,7 @@ set(SOURCE_GROUP_HEADER_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.h
|
||||
)
|
||||
|
||||
set(SOURCE_GROUP_SOURCE_FILES
|
||||
@@ -105,6 +106,7 @@ set(SOURCE_GROUP_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})
|
||||
|
||||
109
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Normal file
109
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2024 Equinor ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RiaProjectBackupTools.h"
|
||||
#include "RiaLogging.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QVariant>
|
||||
|
||||
namespace RiaProjectBackupTools
|
||||
{
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool createTableIfNeeded()
|
||||
{
|
||||
QSqlQuery query;
|
||||
if ( !query.exec( "CREATE TABLE IF NOT EXISTS file_versions ("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"timestamp DATETIME,"
|
||||
"content TEXT)" ) )
|
||||
{
|
||||
QString txt = "Error creating table:" + query.lastError().text();
|
||||
RiaLogging::error( txt );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool insertContent( const QString& content )
|
||||
{
|
||||
QSqlQuery query;
|
||||
query.prepare( "INSERT INTO file_versions (timestamp, content) "
|
||||
"VALUES (:timestamp, :content)" );
|
||||
query.bindValue( ":timestamp", QDateTime::currentDateTime() );
|
||||
query.bindValue( ":content", content );
|
||||
if ( !query.exec() )
|
||||
{
|
||||
QString txt = "Error saving file content to database:" + query.lastError().text();
|
||||
RiaLogging::error( txt );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content )
|
||||
{
|
||||
const QString databaseType = "QSQLITE";
|
||||
|
||||
if ( !QSqlDatabase::isDriverAvailable( databaseType ) )
|
||||
{
|
||||
RiaLogging::error( "sqlite database is not available." );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to open the SQLITE database
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
if ( !db.isValid() || !db.open() )
|
||||
{
|
||||
RiaLogging::info( "Adding database" );
|
||||
|
||||
// Add the SQLITE database, and it it required to do this once per session. The database will be available during the lifetime of
|
||||
// the application, and can be accessed using QSqlDatabase::database()
|
||||
db = QSqlDatabase::addDatabase( databaseType );
|
||||
}
|
||||
if ( !db.open() )
|
||||
{
|
||||
QString txt = "Error opening database:" + db.lastError().text();
|
||||
RiaLogging::error( txt );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the file name for the database. The database will be created if it does not exist.
|
||||
db.setDatabaseName( databaseFilePath );
|
||||
|
||||
if ( !createTableIfNeeded() ) return false;
|
||||
if ( !insertContent( content ) ) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace RiaProjectBackupTools
|
||||
29
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Normal file
29
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2024 Equinor ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//==================================================================================================
|
||||
namespace RiaProjectBackupTools
|
||||
{
|
||||
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content );
|
||||
}
|
||||
Reference in New Issue
Block a user