-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sqlite code reorganized * Reorganize postgres code
- Loading branch information
Showing
30 changed files
with
729 additions
and
663 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
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,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 |
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,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 |
This file was deleted.
Oops, something went wrong.
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,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 |
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,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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 |
Oops, something went wrong.