ORM C++
Loading...
Searching...
No Matches
database.hpp
1#pragma once
2
3#include "database/BackendType.hpp"
4#include "database/binding/Binding.hpp"
5#include "database/CommandGeneratorFactory.hpp"
6#include "query.hpp"
7#include "soci/soci.h"
8
9namespace orm
10{
17{
18public:
19 template <typename T>
20 using Payload = db::binding::BindingPayload<T>;
21
26
32 auto connect(const std::string& connectionString) -> void;
33
37 auto disconnect() -> void;
38
46 template <typename T>
47 auto select(Query<T>& query) -> std::vector<T>
48 {
49 std::vector<T> result;
50
51 auto command = commandGeneratorFactory.getCommandGenerator(backendType).select(query.getData());
52
53 Payload<T>::bindingInfo.joinedValues = query.getData().shouldJoin;
54
55 soci::rowset<Payload<T>> preparedRowSet = (sql.prepare << command);
56
57 for (auto& payload : preparedRowSet)
58 {
59 result.push_back(std::move(payload.value));
60 }
61
62 return result;
63 }
64
71 template <typename T>
72 auto insert(const std::vector<T>& objects) -> void
73 {
74 for (const auto& object : objects)
75 {
76 insert(object);
77 }
78 }
79
86 template <typename T>
87 auto insert(T object) -> void
88 {
89 static auto command = commandGeneratorFactory.getCommandGenerator(backendType).insert(Model<T>::getModelInfo());
90
91 const Payload<T> payload{};
92
93 payload.value = std::move(object);
94
95 sql << command, soci::use(payload);
96 }
97
103 template <typename T>
104 auto createTable() -> void
105 {
106 static auto modelInfo = Model<T>::getModelInfo();
107
108 static auto command = commandGeneratorFactory.getCommandGenerator(backendType).createTable(modelInfo);
109
110 sql << command;
111 }
112
118 template <typename T>
119 auto deleteTable() -> void
120 {
121 static Model<T> model;
122
123 static auto command = commandGeneratorFactory.getCommandGenerator(backendType).dropTable(model.getModelInfo());
124
125 sql << command;
126 }
127
133 auto getBackendType() -> db::BackendType;
134
138 auto beginTransaction() -> void;
139
143 auto commitTransaction() -> void;
144
148 auto rollbackTransaction() -> void;
149
150private:
151 soci::session sql;
152 std::unique_ptr<soci::transaction> transaction;
153 db::BackendType backendType;
154 db::CommandGeneratorFactory commandGeneratorFactory;
155};
156} // namespace orm
A class representing a database in the ORM framework.
Definition database.hpp:17
Database()
Constructs a new Database object.
auto beginTransaction() -> void
Starts a transaction.
auto createTable() -> void
Execute a create table query for a model.
Definition database.hpp:104
auto connect(const std::string &connectionString) -> void
Connects to a database.
auto commitTransaction() -> void
Commits a transaction.
auto select(Query< T > &query) -> std::vector< T >
Executes a select query and returns the result.
Definition database.hpp:47
auto insert(const std::vector< T > &objects) -> void
Executes a insert query for multiple objects.
Definition database.hpp:72
auto insert(T object) -> void
Executes a insert query for a single object.
Definition database.hpp:87
auto rollbackTransaction() -> void
Rollbacks a transaction.
auto deleteTable() -> void
Execute a delete table query for a model.
Definition database.hpp:119
auto disconnect() -> void
Disconnects from the database.
auto getBackendType() -> db::BackendType
Get the backend type of the database.
A template class representing a model in the ORM framework.
Definition model.hpp:17
static auto getModelInfo(bool force=false) -> model::ModelInfo &
Get the model info for the model.
Definition model.hpp:24
A template class representing a select query in the ORM framework.
Definition query.hpp:20