Clean project structure.
This commit is contained in:
parent
78a4fa99ff
commit
947bf937fd
496 changed files with 206 additions and 137 deletions
28
src/base/database/CMakeLists.txt
Normal file
28
src/base/database/CMakeLists.txt
Normal file
|
@ -0,0 +1,28 @@
|
|||
set(MODULE_NAME database)
|
||||
|
||||
set(SQLite3_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/src/third_party/sqlite3")
|
||||
set(SQLite3_SOURCE_FILE "${CMAKE_SOURCE_DIR}/src/third_party/sqlite3/sqlite3.c")
|
||||
|
||||
list(APPEND HEADERS
|
||||
Database.h
|
||||
DatabaseManager.h
|
||||
database_interfaces/SqliteInterface.h
|
||||
${SQLite3_SOURCE_FILE})
|
||||
|
||||
list(APPEND SOURCES
|
||||
Database.cpp
|
||||
DatabaseManager.cpp
|
||||
database_interfaces/SqliteInterface.cpp
|
||||
${SQLite3_SOURCE_FILE})
|
||||
|
||||
add_library(${MODULE_NAME} SHARED ${SOURCES} ${HEADERS})
|
||||
|
||||
target_include_directories(${MODULE_NAME} PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/database_interfaces
|
||||
${SQLite3_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(${MODULE_NAME} core)
|
||||
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src/base)
|
||||
set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
|
27
src/base/database/Database.cpp
Normal file
27
src/base/database/Database.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include "Database.h"
|
||||
|
||||
Database::Database()
|
||||
: mPath()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Database::~Database()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<Database> Database::Create()
|
||||
{
|
||||
return std::make_unique<Database>();
|
||||
}
|
||||
|
||||
void Database::setPath(const Path& path)
|
||||
{
|
||||
mPath = path;
|
||||
}
|
||||
|
||||
const Path& Database::getPath() const
|
||||
{
|
||||
return mPath;
|
||||
}
|
26
src/base/database/Database.h
Normal file
26
src/base/database/Database.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
using Path = std::filesystem::path;
|
||||
|
||||
class Database
|
||||
{
|
||||
public:
|
||||
Database();
|
||||
|
||||
~Database();
|
||||
|
||||
static std::unique_ptr<Database> Create();
|
||||
|
||||
const Path& getPath() const;
|
||||
|
||||
void setPath(const Path& path);
|
||||
|
||||
private:
|
||||
Path mPath;
|
||||
};
|
||||
|
||||
using DatabasePtr = std::unique_ptr<Database>;
|
41
src/base/database/DatabaseManager.cpp
Normal file
41
src/base/database/DatabaseManager.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "DatabaseManager.h"
|
||||
|
||||
DatabaseManager::DatabaseManager()
|
||||
: mDatabase(),
|
||||
mDatabaseInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DatabaseManager::~DatabaseManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<DatabaseManager> DatabaseManager::Create()
|
||||
{
|
||||
return std::make_unique<DatabaseManager>();
|
||||
}
|
||||
|
||||
|
||||
void DatabaseManager::openDatabase(const Path& path)
|
||||
{
|
||||
mDatabase = Database::Create();
|
||||
mDatabase->setPath(path);
|
||||
|
||||
mDatabaseInterface = SqliteInterface::Create();
|
||||
mDatabaseInterface->open(mDatabase.get());
|
||||
}
|
||||
|
||||
void DatabaseManager::run(const std::string& statement)
|
||||
{
|
||||
mDatabaseInterface->run(statement);
|
||||
}
|
||||
|
||||
void DatabaseManager::onShutDown()
|
||||
{
|
||||
if(mDatabaseInterface)
|
||||
{
|
||||
mDatabaseInterface->close();
|
||||
}
|
||||
}
|
31
src/base/database/DatabaseManager.h
Normal file
31
src/base/database/DatabaseManager.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "SqliteInterface.h"
|
||||
#include "Database.h"
|
||||
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
|
||||
using Path = std::filesystem::path;
|
||||
|
||||
class DatabaseManager
|
||||
{
|
||||
public:
|
||||
DatabaseManager();
|
||||
|
||||
~DatabaseManager();
|
||||
|
||||
static std::unique_ptr<DatabaseManager> Create();
|
||||
|
||||
void openDatabase(const Path& path);
|
||||
|
||||
void run(const std::string& statement);
|
||||
|
||||
void onShutDown();
|
||||
|
||||
private:
|
||||
DatabasePtr mDatabase;
|
||||
SqliteInterfacePtr mDatabaseInterface;
|
||||
};
|
||||
|
||||
using DatabaseManagerPtr = std::unique_ptr<DatabaseManager>;
|
57
src/base/database/database_interfaces/SqliteInterface.cpp
Normal file
57
src/base/database/database_interfaces/SqliteInterface.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include "SqliteInterface.h"
|
||||
|
||||
#include "FileLogger.h"
|
||||
|
||||
SqliteInterface::SqliteInterface()
|
||||
: mSqliteDb(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SqliteInterface::~SqliteInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<SqliteInterface> SqliteInterface::Create()
|
||||
{
|
||||
return std::make_unique<SqliteInterface>();
|
||||
}
|
||||
|
||||
void SqliteInterface::open(Database* db)
|
||||
{
|
||||
const auto path = db->getPath().string();
|
||||
int rc = sqlite3_open(path.c_str(), &mSqliteDb);
|
||||
if( rc )
|
||||
{
|
||||
MLOG_ERROR("Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb));
|
||||
sqlite3_close(mSqliteDb);
|
||||
}
|
||||
}
|
||||
|
||||
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
|
||||
{
|
||||
for(int i=0; i<argc; i++)
|
||||
{
|
||||
//std::cout << "%s = %s\n" << azColName[i] << argv[i] ? argv[i] : "NULL" << std::endl;
|
||||
|
||||
MLOG_ERROR("NULL");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SqliteInterface::run(const std::string& statement)
|
||||
{
|
||||
char *zErrMsg = 0;
|
||||
int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg);
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
MLOG_ERROR("SQL error: %s\n" << zErrMsg);
|
||||
sqlite3_free(zErrMsg);
|
||||
}
|
||||
}
|
||||
|
||||
void SqliteInterface::close()
|
||||
{
|
||||
sqlite3_close(mSqliteDb);
|
||||
}
|
26
src/base/database/database_interfaces/SqliteInterface.h
Normal file
26
src/base/database/database_interfaces/SqliteInterface.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "Database.h"
|
||||
|
||||
class SqliteInterface
|
||||
{
|
||||
public:
|
||||
SqliteInterface();
|
||||
|
||||
~SqliteInterface();
|
||||
|
||||
static std::unique_ptr<SqliteInterface> Create();
|
||||
|
||||
void open(Database* db);
|
||||
|
||||
void close();
|
||||
|
||||
void run(const std::string& statement);
|
||||
|
||||
private:
|
||||
sqlite3* mSqliteDb;
|
||||
};
|
||||
|
||||
using SqliteInterfacePtr = std::unique_ptr<SqliteInterface>;
|
Loading…
Add table
Add a link
Reference in a new issue