Skip to content

Commit

Permalink
Code reorganization (#25)
Browse files Browse the repository at this point in the history
* sqlite code reorganized

* Reorganize postgres code
  • Loading branch information
bgs99 authored Jun 8, 2024
1 parent d9a73cf commit 61be781
Show file tree
Hide file tree
Showing 30 changed files with 729 additions and 663 deletions.
29 changes: 13 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,30 @@ project(podrm LANGUAGES C CXX)
add_subdirectory(vendor)

find_package(fmt REQUIRED)
find_package(PostgreSQL REQUIRED)
find_package(SQLite3 REQUIRED)

add_library(podrm STATIC)
target_compile_features(podrm PUBLIC cxx_std_20)
target_sources(podrm PRIVATE lib/sqlite/utils.cpp lib/sqlite/operations.cpp
lib/postgres/utils.cpp lib/postgres/operations.cpp)
target_include_directories(podrm SYSTEM PUBLIC include)
target_link_libraries(
podrm
PUBLIC Boost::pfr
PRIVATE PostgreSQL::PostgreSQL SQLite::SQLite3 fmt::fmt)

add_library(podrm-reflection INTERFACE)

target_compile_features(podrm-reflection INTERFACE cxx_std_20)
target_include_directories(podrm-reflection SYSTEM INTERFACE include)
target_link_libraries(podrm-reflection INTERFACE Boost::pfr)

add_subdirectory(lib/postgres)
add_subdirectory(lib/sqlite)

option(PFR_ORM_USE_GSL_SPAN
"Use Microsoft.GSL for span implementation instead of std::span" OFF)
if(PFR_ORM_USE_GSL_SPAN)
target_compile_definitions(podrm PUBLIC PFR_ORM_USE_GSL_SPAN)
target_compile_definitions(podrm-reflection INTERFACE PFR_ORM_USE_GSL_SPAN)
find_package(Microsoft.GSL REQUIRED)
target_link_libraries(podrm PUBLIC Microsoft.GSL::GSL)
target_link_libraries(podrm-reflection INTERFACE Microsoft.GSL::GSL)
endif()

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
option(PFR_ORM_ASAN "Build podrm with address sanitizer" OFF)

if(PFR_ORM_ASAN)
target_compile_options(podrm PRIVATE -fsanitize=address)
target_link_options(podrm PRIVATE -fsanitize=address)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()

include(CTest)
Expand Down
32 changes: 32 additions & 0 deletions include/podrm/postgres/database.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <podrm/api.hpp>
#include <podrm/postgres/detail/connection.hpp>
#include <podrm/reflection.hpp>

#include <string>
#include <utility>

namespace podrm::postgres {

class Database {
public:
Database(const std::string &connectionStr)
: Database(detail::Connection{connectionStr}) {}

template <DatabaseEntity T> void createTable() {
return this->connection.createTable(DatabaseEntityDescription<T>);
}

template <DatabaseEntity T> bool exists() {
return this->connection.exists(DatabaseEntityDescription<T>);
}

private:
detail::Connection connection;

explicit Database(detail::Connection &&connection)
: connection(std::move(connection)) {}
};

} // namespace podrm::postgres
38 changes: 38 additions & 0 deletions include/podrm/postgres/detail/connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <podrm/postgres/detail/result.hpp>
#include <podrm/postgres/detail/str.hpp>
#include <podrm/reflection.hpp>

#include <string>
#include <string_view>

struct pg_conn;

namespace podrm::postgres::detail {

class Connection {
public:
Connection(const std::string &connectionStr);
~Connection();

void createTable(const EntityDescription &entity);

bool exists(const EntityDescription &entity);

Connection(const Connection &) = delete;
Connection(Connection &&) noexcept;
Connection &operator=(const Connection &) = delete;
Connection &operator=(Connection &&) = delete;

[[nodiscard]] Str escapeIdentifier(std::string_view identifier) const;

private:
pg_conn *connection;

Result execute(const std::string &statement);

Result query(const std::string &statement);
};

} // namespace podrm::postgres::detail
12 changes: 0 additions & 12 deletions include/podrm/postgres/detail/operations.hpp

This file was deleted.

26 changes: 26 additions & 0 deletions include/podrm/postgres/detail/result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <string_view>

struct pg_result;

namespace podrm::postgres::detail {

class Result {
public:
Result(pg_result *result) : result(result) {}
~Result();

[[nodiscard]] int status() const;
[[nodiscard]] std::string_view value(int row, int column) const;

Result(const Result &) = delete;
Result(Result &&) noexcept;
Result &operator=(const Result &) = delete;
Result &operator=(Result &&) = delete;

private:
pg_result *result;
};

} // namespace podrm::postgres::detail
24 changes: 24 additions & 0 deletions include/podrm/postgres/detail/str.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string_view>

namespace podrm::postgres::detail {

class Str {
public:
Str(char *str) : str(str) {}
~Str();

[[nodiscard]] std::string_view view() const { return str; }
operator std::string_view() const { return str; }

Str(const Str &) = delete;
Str(Str &&) = delete;
Str &operator=(const Str &) = delete;
Str &operator=(Str &&) = delete;

private:
char *str;
};

} // namespace podrm::postgres::detail
17 changes: 0 additions & 17 deletions include/podrm/postgres/operations.hpp

This file was deleted.

96 changes: 0 additions & 96 deletions include/podrm/postgres/utils.hpp

This file was deleted.

71 changes: 71 additions & 0 deletions include/podrm/sqlite/database.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <podrm/api.hpp>
#include <podrm/primary_key.hpp>
#include <podrm/reflection.hpp>
#include <podrm/sqlite/detail/connection.hpp>

#include <filesystem>
#include <optional>
#include <utility>

namespace podrm::sqlite {

class Database {
public:
//---------------- Constructors ------------------//

static Database fromRaw(sqlite3 &connection) {
return Database{detail::Connection::fromRaw(connection)};
}

static Database inMemory(const char *name) {
return Database{detail::Connection::inMemory(name)};
}

static Database inFile(const std::filesystem::path &path) {
return Database{detail::Connection::inFile(path)};
}

//---------------- Operations ------------------//

template <DatabaseEntity T> void createTable() {
return this->connection.createTable(DatabaseEntityDescription<T>);
}

template <typename T> bool exists() {
return this->connection.exists(DatabaseEntityDescription<T>);
}

template <DatabaseEntity Entity> void persist(Entity &entity) {
return this->connection.persist(DatabaseEntityDescription<Entity>, &entity);
}

template <DatabaseEntity Entity>
std::optional<Entity> find(const PrimaryKeyType<Entity> &key) {
Entity result;
if (!this->connection.find(DatabaseEntityDescription<Entity>, key,
&result)) {
return std::nullopt;
}

return result;
}

template <DatabaseEntity Entity>
void erase(const PrimaryKeyType<Entity> &key) {
this->connection.erase(DatabaseEntityDescription<Entity>, key);
}

template <DatabaseEntity Entity> void update(const Entity &entity) {
this->connection.update(DatabaseEntityDescription<Entity>, &entity);
}

private:
detail::Connection connection;

explicit Database(detail::Connection &&connection)
: connection(std::move(connection)) {}
};

} // namespace podrm::sqlite
Loading

0 comments on commit 61be781

Please sign in to comment.