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 std::stringstream cellInsertStatement;
39 std::stringstream worldInsertStatement;
40 try {
41 //auto t1 = std::chrono::high_resolution_clock::now();
42
43
44 // Cell Data Update
45 //TODO: use COPY
46 cellInsertStatement << "INSERT INTO " << scheme.CellTable << " " << scheme.CellTableDataLayout
47 << " VALUES ";
48
49 auto &cells = w->cells;
50 for (size_t i = 0; i < std::max(int(cells.size() - 1),0); i++) {
51 scheme.CellValueStatement(cellInsertStatement,cells[i], w);
52 cellInsertStatement << ", ";
53 }
54
55 if (cells.size() > 0)
56 scheme.CellValueStatement(cellInsertStatement,cells[cells.size() - 1], w);
57
58 cellInsertStatement << ";";
59
60 // Execute Statement
61 if(cells.size() > 0) conn.Execute(cellInsertStatement.str());
62
63
64 // World Data Update
65 worldInsertStatement << "INSERT INTO " << scheme.WorldTable << " " << scheme.WorldTableDataLayout
66 << " VALUES " << scheme.WorldValueStatement(w) << ";";
67
68 // Execute Statement
69 conn.Execute(worldInsertStatement.str());
70
71 /*auto t2 = std::chrono::high_resolution_clock::now();
72 ERR << "Database update took "
73 << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
74 << " milliseconds\n";*/
75 } catch (const std::exception &e) {
76 LOG << cellInsertStatement.str() << std::endl;
77 LOG << worldInsertStatement.str() << std::endl;
78 ERR << "Error: " << e.what() << std::endl;
79 throw;
80 }
81 }
82
83 protected:
84 void CreateTables(bool dropTablesIfExist) {
85 if (dropTablesIfExist) {
86 LOG << "Dropping table '" << scheme.CellTable << "'" << std::endl;
87 std::string dropCellTable = "DROP TABLE IF EXISTS " + scheme.CellTable + ";";
88 conn.Execute(dropCellTable);
89 }
90
91 LOG << "Creating table '" << scheme.CellTable << "'" << std::endl;
93
94 if (dropTablesIfExist) {
95 LOG << "Dropping table '" << scheme.WorldTable << "'" << std::endl;
96 std::string dropWorldTable = "DROP TABLE IF EXISTS " + scheme.WorldTable + ";";
97 conn.Execute(dropWorldTable);
98 }
99
100 LOG << "Creating table '" << scheme.WorldTable << "'" << std::endl;
102 }
103 };
104}
105
#define LOG
Definition: PluginSQL.hpp:8
#define ERR
Definition: PluginSQL.hpp:9
void Execute(std::string query)
Definition: Connection.hpp:40
std::string password
Definition: PluginSQL.hpp:17
std::string user
Definition: PluginSQL.hpp:16
void endUpdate(world_t *w)
Definition: PluginSQL.hpp:37
std::string host
Definition: PluginSQL.hpp:14
PluginSQL(nlohmann::json const &config, bool dropTables=false)
Definition: PluginSQL.hpp:25
void CreateTables(bool dropTablesIfExist)
Definition: PluginSQL.hpp:84
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
iostream endl
End-of-line manipulator.
Definition: std.hpp:274
void CellValueStatement(std::stringstream &ss, cell_t *c, world_t *w)
Definition: SchemeSQL.hpp:67
const std::string WorldTable
Definition: SchemeSQL.hpp:28
const std::string WorldTableDataLayout
Definition: SchemeSQL.hpp:34
const std::string CellTable
Definition: SchemeSQL.hpp:29
const std::string CellTableDataLayout
Definition: SchemeSQL.hpp:35
std::string WorldTableCreationStatement
Definition: SchemeSQL.hpp:31
std::string CellTableCreationStatement
Definition: SchemeSQL.hpp:32
std::string WorldValueStatement(world_t *w)
Definition: SchemeSQL.hpp:85