Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
database-postgresql.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <string>
8#include <libpq-fe.h>
9#include "database.h"
10
11// Template class for PostgreSQL based data storage
13{
14public:
15 Database_PostgreSQL(const std::string &connect_string, const char *type);
17
18 void beginSave() override;
19 void endSave() override;
20 void rollback();
21
22 bool initialized() const override;
23
24 void verifyDatabase() override;
25
26protected:
27 // Conversion helpers
28 inline int pg_to_int(PGresult *res, int row, int col)
29 {
30 return atoi(PQgetvalue(res, row, col));
31 }
32
33 inline u32 pg_to_uint(PGresult *res, int row, int col)
34 {
35 return (u32) atoi(PQgetvalue(res, row, col));
36 }
37
38 inline float pg_to_float(PGresult *res, int row, int col)
39 {
40 return (float) atof(PQgetvalue(res, row, col));
41 }
42
43 inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
44 {
45 return v3s16(
46 pg_to_int(res, row, col),
47 pg_to_int(res, row, col + 1),
48 pg_to_int(res, row, col + 2)
49 );
50 }
51
52 inline std::string pg_to_string(PGresult *res, int row, int col)
53 {
54 return std::string(PQgetvalue(res, row, col), PQgetlength(res, row, col));
55 }
56
57 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
58 const void **params,
59 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
60 bool clear = true, bool nobinary = true)
61 {
62 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
63 (const char* const*) params, paramsLengths, paramsFormats,
64 nobinary ? 1 : 0), clear);
65 }
66
67 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
68 const char **params, bool clear = true, bool nobinary = true)
69 {
70 return execPrepared(stmtName, paramsNumber,
71 (const void **)params, NULL, NULL, clear, nobinary);
72 }
73
74 void createTableIfNotExists(const std::string &table_name, const std::string &definition);
75
76 // Database initialization
78 virtual void createDatabase() = 0;
79 virtual void initStatements() = 0;
80 inline void prepareStatement(const std::string &name, const std::string &sql)
81 {
82 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
83 }
84
85 int getPGVersion() const { return m_pgversion; }
86
87private:
88 // Database connectivity checks
89 void ping();
90
91 // Database usage
92 PGresult *checkResults(PGresult *res, bool clear = true);
93
94 // Attributes
95 std::string m_connect_string;
96 PGconn *m_conn = nullptr;
97 int m_pgversion = 0;
98};
99
100// Not sure why why we have to do this. can't C++ figure it out on its own?
101#define PARENT_CLASS_FUNCS \
102 void beginSave() { Database_PostgreSQL::beginSave(); } \
103 void endSave() { Database_PostgreSQL::endSave(); } \
104 void verifyDatabase() { Database_PostgreSQL::verifyDatabase(); }
105
107{
108public:
109 MapDatabasePostgreSQL(const std::string &connect_string);
110 virtual ~MapDatabasePostgreSQL() = default;
111
112 bool saveBlock(const v3s16 &pos, std::string_view data);
113 void loadBlock(const v3s16 &pos, std::string *block);
114 bool deleteBlock(const v3s16 &pos);
115 void listAllLoadableBlocks(std::vector<v3s16> &dst);
116
118
119protected:
120 virtual void createDatabase();
121 virtual void initStatements();
122};
123
125{
126public:
127 PlayerDatabasePostgreSQL(const std::string &connect_string);
128 virtual ~PlayerDatabasePostgreSQL() = default;
129
131 bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
132 bool removePlayer(const std::string &name);
133 void listPlayers(std::vector<std::string> &res);
134
136
137protected:
138 virtual void createDatabase();
139 virtual void initStatements();
140
141private:
142 bool playerDataExists(const std::string &playername);
143};
144
146{
147public:
148 AuthDatabasePostgreSQL(const std::string &connect_string);
149 virtual ~AuthDatabasePostgreSQL() = default;
150
151 virtual bool getAuth(const std::string &name, AuthEntry &res);
152 virtual bool saveAuth(const AuthEntry &authEntry);
153 virtual bool createAuth(AuthEntry &authEntry);
154 virtual bool deleteAuth(const std::string &name);
155 virtual void listNames(std::vector<std::string> &res);
156 virtual void reload();
157
159
160protected:
161 virtual void createDatabase();
162 virtual void initStatements();
163
164private:
165 virtual void writePrivileges(const AuthEntry &authEntry);
166};
167
169{
170public:
171 ModStorageDatabasePostgreSQL(const std::string &connect_string);
173
174 void getModEntries(const std::string &modname, StringMap *storage);
175 void getModKeys(const std::string &modname, std::vector<std::string> *storage);
176 bool getModEntry(const std::string &modname, const std::string &key, std::string *value);
177 bool hasModEntry(const std::string &modname, const std::string &key);
178 bool setModEntry(const std::string &modname,
179 const std::string &key, std::string_view value);
180 bool removeModEntry(const std::string &modname, const std::string &key);
181 bool removeModEntries(const std::string &modname);
182 void listMods(std::vector<std::string> *res);
183
185
186protected:
187 virtual void createDatabase();
188 virtual void initStatements();
189};
190
191#undef PARENT_CLASS_FUNCS
Definition database-postgresql.h:146
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 ~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:66
Definition database-postgresql.h:13
v3s16 pg_to_v3s16(PGresult *res, int row, int col)
Definition database-postgresql.h:43
void beginSave() override
std::string pg_to_string(PGresult *res, int row, int col)
Definition database-postgresql.h:52
bool initialized() const override
std::string m_connect_string
Definition database-postgresql.h:95
u32 pg_to_uint(PGresult *res, int row, int col)
Definition database-postgresql.h:33
int m_pgversion
Definition database-postgresql.h:97
int getPGVersion() const
Definition database-postgresql.h:85
float pg_to_float(PGresult *res, int row, int col)
Definition database-postgresql.h:38
void endSave() override
void createTableIfNotExists(const std::string &table_name, const std::string &definition)
PGconn * m_conn
Definition database-postgresql.h:96
PGresult * execPrepared(const char *stmtName, const int paramsNumber, const char **params, bool clear=true, bool nobinary=true)
Definition database-postgresql.h:67
void verifyDatabase() override
Open and initialize the database if needed.
void prepareStatement(const std::string &name, const std::string &sql)
Definition database-postgresql.h:80
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:28
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:57
virtual void initStatements()=0
virtual void createDatabase()=0
Definition database.h:15
Definition database-postgresql.h:107
void listAllLoadableBlocks(std::vector< v3s16 > &dst)
virtual void createDatabase()
bool saveBlock(const v3s16 &pos, std::string_view data)
MapDatabasePostgreSQL(const std::string &connect_string)
virtual void initStatements()
bool deleteBlock(const v3s16 &pos)
virtual ~MapDatabasePostgreSQL()=default
void loadBlock(const v3s16 &pos, std::string *block)
Definition database.h:28
Definition database-postgresql.h:169
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 getModKeys(const std::string &modname, std::vector< std::string > *storage)
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:79
Definition database-postgresql.h:125
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:46
Definition player_sao.h:58
Definition remoteplayer.h:27
#define PARENT_CLASS_FUNCS
Definition database-postgresql.h:101
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
std::unordered_map< std::string, std::string > StringMap
Definition string.h:66
Definition database.h:57