8#define LOG std::cout << "PluginSQL: "
9#define ERR std::cerr << "PluginSQL: "
12 template<
typename cell_t>
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);
36 template<
typename world_t>
39 conn.
Execute(
"SET LOCAL synchronous_commit = OFF;");
47 template<
typename world_t>
49 if (w->cells.size() > 0) {
51 conn.headerCopy(w->cells.size(),Scheme::CellRowSize);
52 for (auto* c : w->cells) {
53 Scheme::CellValueBinary(conn,c, w);
61 template<
typename world_t>
63 conn.CopyBinary(scheme.WorldTable, scheme.WorldTableDataLayout, [&]() {
64 conn.headerCopy(1,Scheme::WorldRowSize);
65 Scheme::WorldValueBinary(conn, w);
71 template<
typename world_t>
73 if (w->newCells.size() > 0) {
74 std::stringstream cellInsertStatement;
75 cellInsertStatement <<
"INSERT INTO " << scheme.CellTable <<
" " << scheme.CellTableDataLayout
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 <<
", ";
81 if (w->newCells.size() > 0)
82 scheme.CellValueStatement(cellInsertStatement,w->newCells[w->newCells.size() - 1], w);
83 cellInsertStatement <<
";";
84 conn.Execute(cellInsertStatement.str());
88 template<
typename world_t>
90 std::stringstream worldInsertStatement;
91 worldInsertStatement <<
"INSERT INTO " << scheme.WorldTable <<
" " << scheme.WorldTableDataLayout
92 <<
" VALUES " << scheme.WorldValueStatement(w) <<
";";
93 conn.Execute(worldInsertStatement.str());
97 if (dropTablesIfExist) {
98 std::string dropCellTable =
"DROP TABLE IF EXISTS " + scheme.CellTable +
";";
99 conn.Execute(dropCellTable);
101 conn.Execute(scheme.CellTableCreationStatement+
" CREATE INDEX ON " + scheme.CellTable +
" (step);");
103 if (dropTablesIfExist) {
104 std::string dropWorldTable =
"DROP TABLE IF EXISTS " + scheme.WorldTable +
";";
105 conn.Execute(dropWorldTable);
107 conn.Execute(scheme.WorldTableCreationStatement+
" CREATE INDEX ON " + scheme.WorldTable +
" (step);");
void CopyBinary(const std::string &table, const std::string &columns, const std::function< void()> &writeRows)
void Execute(const std::string &sql)
void pushCellsCopy(world_t *w)
void endUpdate(world_t *w)
void pushWorldQuery(world_t *w)
void pushCellsQuery(world_t *w)
PluginSQL(nlohmann::json const &config, bool dropTables=false)
void pushWorldCopy(world_t *w)
void CreateTables(bool dropTablesIfExist)
a class to store JSON values
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Provides common mathematical functions and vector operations.
double max(double a, double b)
Computes the maximum of two numbers.
const std::string CellTable
const std::string CellTableDataLayout