Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
database-postgresql.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <string>
23#include <libpq-fe.h>
24#include "database.h"
25#include "util/basic_macros.h"
26
27class Settings;
28
30{
31public:
32 Database_PostgreSQL(const std::string &connect_string, const char *type);
34
35 void beginSave();
36 void endSave();
37 void rollback();
38
39 bool initialized() const;
40
41
42protected:
43 // Conversion helpers
44 inline int pg_to_int(PGresult *res, int row, int col)
45 {
46 return atoi(PQgetvalue(res, row, col));
47 }
48
49 inline u32 pg_to_uint(PGresult *res, int row, int col)
50 {
51 return (u32) atoi(PQgetvalue(res, row, col));
52 }
53
54 inline float pg_to_float(PGresult *res, int row, int col)
55 {
56 return (float) atof(PQgetvalue(res, row, col));
57 }
58
59 inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
60 {
61 return v3s16(
62 pg_to_int(res, row, col),
63 pg_to_int(res, row, col + 1),
64 pg_to_int(res, row, col + 2)
65 );
66 }
67
68 inline std::string pg_to_string(PGresult *res, int row, int col)
69 {
70 return std::string(PQgetvalue(res, row, col), PQgetlength(res, row, col));
71 }
72
73 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
74 const void **params,
75 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
76 bool clear = true, bool nobinary = true)
77 {
78 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
79 (const char* const*) params, paramsLengths, paramsFormats,
80 nobinary ? 1 : 0), clear);
81 }
82
83 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
84 const char **params, bool clear = true, bool nobinary = true)
85 {
86 return execPrepared(stmtName, paramsNumber,
87 (const void **)params, NULL, NULL, clear, nobinary);
88 }
89
90 void createTableIfNotExists(const std::string &table_name, const std::string &definition);
92
93 // Database initialization
95 virtual void createDatabase() = 0;
96 virtual void initStatements() = 0;
97 inline void prepareStatement(const std::string &name, const std::string &sql)
98 {
99 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
100 }
101
102 int getPGVersion() const { return m_pgversion; }
103
104private:
105 // Database connectivity checks
106 void ping();
107
108 // Database usage
109 PGresult *checkResults(PGresult *res, bool clear = true);
110
111 // Attributes
112 std::string m_connect_string;
113 PGconn *m_conn = nullptr;
114 int m_pgversion = 0;
115};
116
118{
119public:
120 MapDatabasePostgreSQL(const std::string &connect_string);
121 virtual ~MapDatabasePostgreSQL() = default;
122
123 bool saveBlock(const v3s16 &pos, std::string_view data);
124 void loadBlock(const v3s16 &pos, std::string *block);
125 bool deleteBlock(const v3s16 &pos);
126 void listAllLoadableBlocks(std::vector<v3s16> &dst);
127
130
131protected:
132 virtual void createDatabase();
133 virtual void initStatements();
134};
135
137{
138public:
139 PlayerDatabasePostgreSQL(const std::string &connect_string);
140 virtual ~PlayerDatabasePostgreSQL() = default;
141
143 bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
144 bool removePlayer(const std::string &name);
145 void listPlayers(std::vector<std::string> &res);
146
147protected:
148 virtual void createDatabase();
149 virtual void initStatements();
150
151private:
152 bool playerDataExists(const std::string &playername);
153};
154
156{
157public:
158 AuthDatabasePostgreSQL(const std::string &connect_string);
159 virtual ~AuthDatabasePostgreSQL() = default;
160
162
163 virtual bool getAuth(const std::string &name, AuthEntry &res);
164 virtual bool saveAuth(const AuthEntry &authEntry);
165 virtual bool createAuth(AuthEntry &authEntry);
166 virtual bool deleteAuth(const std::string &name);
167 virtual void listNames(std::vector<std::string> &res);
168 virtual void reload();
169
170protected:
171 virtual void createDatabase();
172 virtual void initStatements();
173
174private:
175 virtual void writePrivileges(const AuthEntry &authEntry);
176};
177
179{
180public:
181 ModStorageDatabasePostgreSQL(const std::string &connect_string);
183
184 void getModEntries(const std::string &modname, StringMap *storage);
185 void getModKeys(const std::string &modname, std::vector<std::string> *storage);
186 bool getModEntry(const std::string &modname, const std::string &key, std::string *value);
187 bool hasModEntry(const std::string &modname, const std::string &key);
188 bool setModEntry(const std::string &modname,
189 const std::string &key, std::string_view value);
190 bool removeModEntry(const std::string &modname, const std::string &key);
191 bool removeModEntries(const std::string &modname);
192 void listMods(std::vector<std::string> *res);
193
196
197protected:
198 virtual void createDatabase();
199 virtual void initStatements();
200};
Definition: database-postgresql.h:156
virtual void writePrivileges(const AuthEntry &authEntry)
virtual bool saveAuth(const AuthEntry &authEntry)
virtual bool getAuth(const std::string &name, AuthEntry &res)
virtual void createDatabase()
virtual void verifyDatabase()
Definition: database-postgresql.h:161
virtual ~AuthDatabasePostgreSQL()=default
virtual bool deleteAuth(const std::string &name)
virtual bool createAuth(AuthEntry &authEntry)
virtual void listNames(std::vector< std::string > &res)
virtual void initStatements()
virtual void reload()
AuthDatabasePostgreSQL(const std::string &connect_string)
Definition: database.h:76
Definition: database-postgresql.h:30
v3s16 pg_to_v3s16(PGresult *res, int row, int col)
Definition: database-postgresql.h:59
std::string pg_to_string(PGresult *res, int row, int col)
Definition: database-postgresql.h:68
bool initialized() const
std::string m_connect_string
Definition: database-postgresql.h:112
u32 pg_to_uint(PGresult *res, int row, int col)
Definition: database-postgresql.h:49
int m_pgversion
Definition: database-postgresql.h:114
int getPGVersion() const
Definition: database-postgresql.h:102
float pg_to_float(PGresult *res, int row, int col)
Definition: database-postgresql.h:54
void createTableIfNotExists(const std::string &table_name, const std::string &definition)
PGconn * m_conn
Definition: database-postgresql.h:113
PGresult * execPrepared(const char *stmtName, const int paramsNumber, const char **params, bool clear=true, bool nobinary=true)
Definition: database-postgresql.h:83
void prepareStatement(const std::string &name, const std::string &sql)
Definition: database-postgresql.h:97
PGresult * checkResults(PGresult *res, bool clear=true)
Database_PostgreSQL(const std::string &connect_string, const char *type)
int pg_to_int(PGresult *res, int row, int col)
Definition: database-postgresql.h:44
PGresult * execPrepared(const char *stmtName, const int paramsNumber, const void **params, const int *paramsLengths=NULL, const int *paramsFormats=NULL, bool clear=true, bool nobinary=true)
Definition: database-postgresql.h:73
virtual void initStatements()=0
virtual void createDatabase()=0
Definition: database.h:30
Definition: database-postgresql.h:118
void listAllLoadableBlocks(std::vector< v3s16 > &dst)
virtual void createDatabase()
bool saveBlock(const v3s16 &pos, std::string_view data)
MapDatabasePostgreSQL(const std::string &connect_string)
void endSave()
Definition: database-postgresql.h:129
virtual void initStatements()
void beginSave()
Definition: database-postgresql.h:128
bool deleteBlock(const v3s16 &pos)
virtual ~MapDatabasePostgreSQL()=default
void loadBlock(const v3s16 &pos, std::string *block)
Definition: database.h:38
Definition: database-postgresql.h:179
bool setModEntry(const std::string &modname, const std::string &key, std::string_view value)
bool removeModEntry(const std::string &modname, const std::string &key)
bool hasModEntry(const std::string &modname, const std::string &key)
bool removeModEntries(const std::string &modname)
ModStorageDatabasePostgreSQL(const std::string &connect_string)
void beginSave()
Definition: database-postgresql.h:194
void getModKeys(const std::string &modname, std::vector< std::string > *storage)
void endSave()
Definition: database-postgresql.h:195
bool getModEntry(const std::string &modname, const std::string &key, std::string *value)
void getModEntries(const std::string &modname, StringMap *storage)
void listMods(std::vector< std::string > *res)
Definition: database.h:89
Definition: database-postgresql.h:137
virtual void initStatements()
void savePlayer(RemotePlayer *player)
void listPlayers(std::vector< std::string > &res)
PlayerDatabasePostgreSQL(const std::string &connect_string)
bool playerDataExists(const std::string &playername)
virtual void createDatabase()
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao)
bool removePlayer(const std::string &name)
virtual ~PlayerDatabasePostgreSQL()=default
Definition: database.h:56
Definition: player_sao.h:71
Definition: remoteplayer.h:40
Definition: settings.h:124
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
static LightingParams params
Definition: light.cpp:40
std::unordered_map< std::string, std::string > StringMap
Definition: string.h:78
Definition: database.h:67