-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype storing project file to database
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters