Skip to content

Commit

Permalink
Prototype storing project file to database
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Apr 28, 2024
1 parent 55c0e81 commit b2ab312
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
89 changes: 89 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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>

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 )
{
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( databaseFilePath );
if ( !db.open() )
{
QString txt = "Error opening database:" + db.lastError().text();
RiaLogging::error( txt );
return false;
}

if ( !createTableIfNeeded() ) return false;
if ( !insertContent( content ) ) return false;

return true;
}

} // namespace RiaProjectBackupTools
29 changes: 29 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Original file line number Diff line number Diff line change
@@ -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 );
}
2 changes: 2 additions & 0 deletions ApplicationLibCode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
Expand All @@ -49,6 +50,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)
Expand Down
16 changes: 16 additions & 0 deletions ApplicationLibCode/ProjectDataModel/RimProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "RiaFieldHandleTools.h"
#include "RiaFilePathTools.h"
#include "RiaGuiApplication.h"
#include "RiaProjectBackupTools.h"
#include "RiaProjectFileVersionTools.h"
#include "RiaTextStringTools.h"
#include "RiaVersionInfo.h"
Expand Down Expand Up @@ -405,7 +406,22 @@ RimMainPlotCollection* RimProject::mainPlotCollection() const
bool RimProject::writeProjectFile()
{
transferPathsToGlobalPathList();

bool couldOpenFile = writeFile();

bool createBackup = true;
if ( createBackup )
{
QFile xmlFile( fileName );
if ( xmlFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QString content = xmlFile.readAll();

QString backupFilename = fileName + "db";
RiaProjectBackupTools::appendTextToDatabase( backupFilename, content );
}
}

distributePathsFromGlobalPathList();

return couldOpenFile;
Expand Down

0 comments on commit b2ab312

Please sign in to comment.