From b8947d865532394a104344084485a9c92d599bef Mon Sep 17 00:00:00 2001 From: boutinb Date: Wed, 18 Dec 2024 13:26:59 +0100 Subject: [PATCH] Set inMemory as property of DatabaseInterface --- CommonData/databaseinterface.cpp | 31 ++++++++++++++++--------------- CommonData/databaseinterface.h | 7 ++++--- Engine/enginebase.cpp | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/CommonData/databaseinterface.cpp b/CommonData/databaseinterface.cpp index a261336abf..3cb6174560 100644 --- a/CommonData/databaseinterface.cpp +++ b/CommonData/databaseinterface.cpp @@ -54,12 +54,13 @@ void DatabaseInterface::upgradeDBFromVersion(Version originalVersion) } DatabaseInterface::DatabaseInterface(bool createDb, bool inMemory) + : _inMemory{inMemory} { assert(!_singleton); _singleton = this; - if(createDb) create(inMemory); - else load(inMemory); + if(createDb) create(); + else load(); } DatabaseInterface::~DatabaseInterface() @@ -1353,14 +1354,14 @@ void DatabaseInterface::labelsWrite(Column *column) transactionWriteEnd(); } -std::string DatabaseInterface::dbFile(bool onlyName, bool inMemory) const +std::string DatabaseInterface::dbFile(bool onlyName) const { JASPTIMER_SCOPE(DatabaseInterface::dbFile); static std::string fileName = "internal.sqlite"; static std::string memoryName = ":memory:"; - if (inMemory) + if (_inMemory) return memoryName; return onlyName ? fileName : Utils::osPath(TempFiles::sessionDirName() + "/" + fileName).string(); @@ -1609,18 +1610,18 @@ void DatabaseInterface::_runStatementsRepeatedly(const std::string & statements, } } -void DatabaseInterface::create(bool inMemory) +void DatabaseInterface::create() { JASPTIMER_SCOPE(DatabaseInterface::create); assert(!_db); - if(!inMemory && std::filesystem::exists(dbFile(false, inMemory))) + if(!_inMemory && std::filesystem::exists(dbFile())) { - Log::log() << "DatabaseInterface::create: Removing existing sqlite internal db at " << dbFile(false, inMemory) << std::endl; - std::filesystem::remove(dbFile(false, inMemory)); + Log::log() << "DatabaseInterface::create: Removing existing sqlite internal db at " << dbFile() << std::endl; + std::filesystem::remove(dbFile()); } - int ret = sqlite3_open_v2(dbFile(false, inMemory).c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL); + int ret = sqlite3_open_v2(dbFile().c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL); if(ret != SQLITE_OK) { @@ -1628,22 +1629,22 @@ void DatabaseInterface::create(bool inMemory) throw std::runtime_error("JASP cannot run without an internal database and it cannot be created. Contact the JASP team for help."); } else - Log::log() << "Opened internal sqlite database for creation at '" << dbFile(false, inMemory) << "'." << std::endl; + Log::log() << "Opened internal sqlite database for creation at '" << dbFile() << "'." << std::endl; transactionWriteBegin(); runStatements(_dbConstructionSql); transactionWriteEnd(); } -void DatabaseInterface::load(bool inMemory) +void DatabaseInterface::load() { JASPTIMER_SCOPE(DatabaseInterface::load); assert(!_db); - if(!std::filesystem::exists(dbFile(false, inMemory))) - throw std::runtime_error("Trying to load '" + dbFile(false, inMemory) + "' but it doesn't exist!"); + if(!std::filesystem::exists(dbFile())) + throw std::runtime_error("Trying to load '" + dbFile() + "' but it doesn't exist!"); - int ret = sqlite3_open_v2(dbFile(false, inMemory).c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL); + int ret = sqlite3_open_v2(dbFile().c_str(), &_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL); if(ret != SQLITE_OK) { @@ -1651,7 +1652,7 @@ void DatabaseInterface::load(bool inMemory) throw std::runtime_error("JASP cannot run without an internal database and it cannot be created. Contact the JASP team for help."); } else - Log::log() << "Opened internal sqlite database for loading at '" << dbFile(false, inMemory) << "'." << std::endl; + Log::log() << "Opened internal sqlite database for loading at '" << dbFile() << "'." << std::endl; } diff --git a/CommonData/databaseinterface.h b/CommonData/databaseinterface.h index f5eeccd640..26d366f93e 100644 --- a/CommonData/databaseinterface.h +++ b/CommonData/databaseinterface.h @@ -58,7 +58,7 @@ class DatabaseInterface DatabaseInterface(bool create = false, bool inMemory = false); ///< Creates or loads a sqlite database based on the argument ~DatabaseInterface(); - std::string dbFile(bool onlyPostfix, bool inMemory = false) const; ///< Convenience function for getting the filename where sqlite db should be + std::string dbFile(bool onlyPostfix = false) const; ///< Convenience function for getting the filename where sqlite db should be static DatabaseInterface * singleton() { return _singleton; } ///< There can be only one! https://www.youtube.com/watch?v=sqcLjcSloXs @@ -166,8 +166,8 @@ class DatabaseInterface void _runStatements( const std::string & statements, std::function * bindParameters = nullptr, std::function * processRow = nullptr); ///< Runs several sql statements without looking at the results. Unless processRow is not NULL, then this is called for each row. void _runStatementsRepeatedly( const std::string & statements, std::function ** bindParameters, size_t row)> bindParameterFactory, std::function * processRow = nullptr); - void create(bool inMemory = false); ///< Creates a new sqlite database in sessiondir and loads it - void load(bool inMemory = false); ///< Loads a sqlite database from sessiondir (after loading a jaspfile) + void create(); ///< Creates a new sqlite database in sessiondir and loads it + void load(); ///< Loads a sqlite database from sessiondir (after loading a jaspfile) void close(); ///< Closes the loaded database and disconnects bool tableHasColumn(const std::string & tableName, const std::string & columnName); @@ -175,6 +175,7 @@ class DatabaseInterface _transactionReadDepth = 0; sqlite3 * _db = nullptr; + bool _inMemory = false; static std::string _wrap_sqlite3_column_text(sqlite3_stmt * stmt, int iCol); static const std::string _dbConstructionSql; diff --git a/Engine/enginebase.cpp b/Engine/enginebase.cpp index a6d63665fd..3b3b3f6032 100644 --- a/Engine/enginebase.cpp +++ b/Engine/enginebase.cpp @@ -29,7 +29,7 @@ EngineBase::EngineBase(unsigned long sessionID, bool useMemory) JASPTIMER_STOP(TempFiles Attach); if(sessionID != 0) //Otherwise we are just running to fix R packages - _db = new DatabaseInterface(useMemory); + _db = new DatabaseInterface(false, useMemory); } void EngineBase::provideStateFileName(std::string & root, std::string & relativePath)