CellModules
PluginSQL.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <chrono>
5#include "Connection.hpp"
6#include "SchemeSQL.hpp"
7
8#define LOG std::cout << "PluginSQL: "
9#define ERR std::cerr << "PluginSQL: "
10
11namespace PluginSQL {
12 template<typename cell_t>
13 class PluginSQL {
14 std::string host;
15 int port;
16 std::string user;
17 std::string password;
18 std::string database;
19
22
23 public:
24
25 PluginSQL(nlohmann::json const &config, bool dropTables = false) :
26 host(config["sql"]["host"]),
27 port(config["sql"]["port"]),
28 user(config["sql"]["user"]),
29 password(config["sql"]["password"]),
30 database(config["sql"]["database"]),
31 scheme(config["id"].get<std::string>()),
32 conn(user, password, host, port, database){
33 CreateTables(dropTables);
34 }
35
36 template<typename world_t>
37 void endUpdate(world_t *w) {
38 conn.Execute("BEGIN;");
39 conn.Execute("SET LOCAL synchronous_commit = OFF;");
40 pushCellsCopy(w);
41 pushWorldQuery(w);
42 conn.Execute("COMMIT;");
43 }
44
45 protected:
46
47 template<typename world_t>
48 void pushCellsCopy(world_t *w) {
49 if (w->cells.size() > 0) {
50 conn.CopyBinary(scheme.CellTable, scheme.CellTableDataLayout, [&]() {
51 conn.headerCopy(w->cells.size(),Scheme::CellRowSize);
52 for (auto* c : w->cells) {
53 Scheme::CellValueBinary(conn,c, w);
54 }
55 conn.footerCopy();
56 }
57 );
58 }
59 }
60
61 template<typename world_t>
62 void pushWorldCopy(world_t *w) {
63 conn.CopyBinary(scheme.WorldTable, scheme.WorldTableDataLayout, [&]() {
64 conn.headerCopy(1,Scheme::WorldRowSize);
65 Scheme::WorldValueBinary(conn, w);
66 conn.footerCopy();
67 }
68 );
69 }
70
71 template<typename world_t>
72 void pushCellsQuery(world_t *w) {
73 if (w->newCells.size() > 0) {
74 std::stringstream cellInsertStatement;
75 cellInsertStatement << "INSERT INTO " << scheme.CellTable << " " << scheme.CellTableDataLayout
76 << " VALUES ";
77 for (size_t i = 0; i < std::max(int(w->newCells.size() - 1),0); i++) {
78 scheme.CellValueStatement(cellInsertStatement,w->newCells[i], w);
79 cellInsertStatement << ", ";
80 }
81 if (w->newCells.size() > 0)
82 scheme.CellValueStatement(cellInsertStatement,w->newCells[w->newCells.size() - 1], w);
83 cellInsertStatement << ";";
84 conn.Execute(cellInsertStatement.str());
85 }
86 }
87
88 template<typename world_t>
89 void pushWorldQuery(world_t *w) {
90 std::stringstream worldInsertStatement;
91 worldInsertStatement << "INSERT INTO " << scheme.WorldTable << " " << scheme.WorldTableDataLayout
92 << " VALUES " << scheme.WorldValueStatement(w) << ";";
93 conn.Execute(worldInsertStatement.str());
94 }
95
96 void CreateTables(bool dropTablesIfExist) {
97 if (dropTablesIfExist) {
98 std::string dropCellTable = "DROP TABLE IF EXISTS " + scheme.CellTable + ";";
99 conn.Execute(dropCellTable);
100 }
101 conn.Execute(scheme.CellTableCreationStatement+" CREATE INDEX ON " + scheme.CellTable + " (step);");
102
103 if (dropTablesIfExist) {
104 std::string dropWorldTable = "DROP TABLE IF EXISTS " + scheme.WorldTable + ";";
105 conn.Execute(dropWorldTable);
106 }
107 conn.Execute(scheme.WorldTableCreationStatement+" CREATE INDEX ON " + scheme.WorldTable + " (step);");
108 }
109 };
110}
111
void CopyBinary(const std::string &table, const std::string &columns, const std::function< void()> &writeRows)
Definition: Connection.hpp:54
void Execute(const std::string &sql)
Definition: Connection.hpp:43
void pushCellsCopy(world_t *w)
Definition: PluginSQL.hpp:48
std::string password
Definition: PluginSQL.hpp:17
std::string user
Definition: PluginSQL.hpp:16
void endUpdate(world_t *w)
Definition: PluginSQL.hpp:37
void pushWorldQuery(world_t *w)
Definition: PluginSQL.hpp:89
void pushCellsQuery(world_t *w)
Definition: PluginSQL.hpp:72
std::string host
Definition: PluginSQL.hpp:14
PluginSQL(nlohmann::json const &config, bool dropTables=false)
Definition: PluginSQL.hpp:25
void pushWorldCopy(world_t *w)
Definition: PluginSQL.hpp:62
void CreateTables(bool dropTablesIfExist)
Definition: PluginSQL.hpp:96
std::string database
Definition: PluginSQL.hpp:18
a class to store JSON values
Definition: json.hpp:12931
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.hpp:1787
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
double max(double a, double b)
Computes the maximum of two numbers.
Definition: std.hpp:280
const std::string CellTable
Definition: SchemeSQL.hpp:48
const std::string CellTableDataLayout
Definition: SchemeSQL.hpp:54