Luanti 5.10.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#include "util/basic_macros.h"
11
12class Settings;
13
15{
16public:
17 Database_PostgreSQL(const std::string &connect_string, const char *type);
19
20 void beginSave();
21 void endSave();
22 void rollback();
23
24 bool initialized() const;
25
26
27protected:
28 // Conversion helpers
29 inline int pg_to_int(PGresult *res, int row, int col)
30 {
31 return atoi(PQgetvalue(res, row, col));
32 }
33
34 inline u32 pg_to_uint(PGresult *res, int row, int col)
35 {
36 return (u32) atoi(PQgetvalue(res, row, col));
37 }
38
39 inline float pg_to_float(PGresult *res, int row, int col)
40 {
41 return (float) atof(PQgetvalue(res, row, col));
42 }
43
44 inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
45 {
46 return v3s16(
47 pg_to_int(res, row, col),
48 pg_to_int(res, row, col + 1),
49 pg_to_int(res, row, col + 2)
50 );
51 }
52
53 inline std::string pg_to_string(PGresult *res, int row, int col)
54 {
55 return std::string(PQgetvalue(res, row, col), PQgetlength(res, row, col));
56 }
57
58 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
59 const void **params,
60 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
61 bool clear = true, bool nobinary = true)
62 {
63 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
64 (const char* const*) params, paramsLengths, paramsFormats,
65 nobinary ? 1 : 0), clear);
66 }
67
68 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
69 const char **params, bool clear = true, bool nobinary = true)
70 {
71 return execPrepared(stmtName, paramsNumber,
72 (const void **)params, NULL, NULL, clear, nobinary);
73 }
74
75 void createTableIfNotExists(const std::string &table_name, const std::string &definition);
77
78 // Database initialization
80 virtual void createDatabase() = 0;
81 virtual void initStatements() = 0;
82 inline void prepareStatement(const std::string &name, const std::string &sql)
83 {
84 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
85 }
86
87 int getPGVersion() const { return m_pgversion; }
88
89private:
90 // Database connectivity checks
91 void ping();
92
93 // Database usage
94 PGresult *checkResults(PGresult *res, bool clear = true);
95
96 // Attributes
97 std::string m_connect_string;
98 PGconn *m_conn = nullptr;
99 int m_pgversion = 0;
100};
101
103{
104public:
105 MapDatabasePostgreSQL(const std::string &connect_string);
106 virtual ~MapDatabasePostgreSQL() = default;
107
108 bool saveBlock(const v3s16 &pos, std::string_view data);
109 void loadBlock(const v3s16 &pos, std::string *block);
110 bool deleteBlock(const v3s16 &pos);
111 void listAllLoadableBlocks(std::vector<v3s16> &dst);
112
115
116protected:
117 virtual void createDatabase();
118 virtual void initStatements();
119};
120
122{
123public:
124 PlayerDatabasePostgreSQL(const std::string &connect_string);
125 virtual ~PlayerDatabasePostgreSQL() = default;
126
128 bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
129 bool removePlayer(const std::string &name);
130 void listPlayers(std::vector<std::string> &res);
131
132protected:
133 virtual void createDatabase();
134 virtual void initStatements();
135
136private:
137 bool playerDataExists(const std::string &playername);
138};
139
141{
142public:
143 AuthDatabasePostgreSQL(const std::string &connect_string);
144 virtual ~AuthDatabasePostgreSQL() = default;
145
147
148 virtual bool getAuth(const std::string &name, AuthEntry &res);
149 virtual bool saveAuth(const AuthEntry &authEntry);
150 virtual bool createAuth(AuthEntry &authEntry);
151 virtual bool deleteAuth(const std::string &name);
152 virtual void listNames(std::vector<std::string> &res);
153 virtual void reload();
154
155protected:
156 virtual void createDatabase();
157 virtual void initStatements();
158
159private:
160 virtual void writePrivileges(const AuthEntry &authEntry);
161};
162
164{
165public:
166 ModStorageDatabasePostgreSQL(const std::string &connect_string);
168
169 void getModEntries(const std::string &modname, StringMap *storage);
170 void getModKeys(const std::string &modname, std::vector<std::string> *storage);
171 bool getModEntry(const std::string &modname, const std::string &key, std::string *value);
172 bool hasModEntry(const std::string &modname, const std::string &key);
173 bool setModEntry(const std::string &modname,
174 const std::string &key, std::string_view value);
175 bool removeModEntry(const std::string &modname, const std::string &key);
176 bool removeModEntries(const std::string &modname);
177 void listMods(std::vector<std::string> *res);
178
181
182protected:
183 virtual void createDatabase();
184 virtual void initStatements();
185};
Definition database-postgresql.h:141
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:146
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:61
Definition database-postgresql.h:15
v3s16 pg_to_v3s16(PGresult *res, int row, int col)
Definition database-postgresql.h:44
std::string pg_to_string(PGresult *res, int row, int col)
Definition database-postgresql.h:53
bool initialized() const
std::string m_connect_string
Definition database-postgresql.h:97
u32 pg_to_uint(PGresult *res, int row, int col)
Definition database-postgresql.h:34
int m_pgversion
Definition database-postgresql.h:99
int getPGVersion() const
Definition database-postgresql.h:87
float pg_to_float(PGresult *res, int row, int col)
Definition database-postgresql.h:39
void createTableIfNotExists(const std::string &table_name, const std::string &definition)
PGconn * m_conn
Definition database-postgresql.h:98
PGresult * execPrepared(const char *stmtName, const int paramsNumber, const char **params, bool clear=true, bool nobinary=true)
Definition database-postgresql.h:68
void prepareStatement(const std::string &name, const std::string &sql)
Definition database-postgresql.h:82
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:29
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:58
virtual void initStatements()=0
virtual void createDatabase()=0
Definition database.h:15
Definition database-postgresql.h:103
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:114
virtual void initStatements()
void beginSave()
Definition database-postgresql.h:113
bool deleteBlock(const v3s16 &pos)
virtual ~MapDatabasePostgreSQL()=default
void loadBlock(const v3s16 &pos, std::string *block)
Definition database.h:23
Definition database-postgresql.h:164
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:179
void getModKeys(const std::string &modname, std::vector< std::string > *storage)
void endSave()
Definition database-postgresql.h:180
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:74
Definition database-postgresql.h:122
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:41
Definition player_sao.h:56
Definition remoteplayer.h:26
Definition settings.h:109
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
std::unordered_map< std::string, std::string > StringMap
Definition string.h:65
Definition database.h:52