]> git.lizzy.rs Git - minetest.git/blob - src/database-postgresql.h
Player collisionbox: Make settable
[minetest.git] / src / database-postgresql.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef DATABASE_POSTGRESQL_HEADER
21 #define DATABASE_POSTGRESQL_HEADER
22
23 #include <string>
24 #include <libpq-fe.h>
25 #include "database.h"
26 #include "util/basic_macros.h"
27
28 class Settings;
29
30 class Database_PostgreSQL: public Database
31 {
32 public:
33         Database_PostgreSQL(const std::string &connect_string);
34         ~Database_PostgreSQL();
35
36         void beginSave();
37         void endSave();
38
39         bool initialized() const;
40
41
42 protected:
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 PGresult *execPrepared(const char *stmtName, const int paramsNumber,
69                 const void **params,
70                 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
71                 bool clear = true, bool nobinary = true)
72         {
73                 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
74                         (const char* const*) params, paramsLengths, paramsFormats,
75                         nobinary ? 1 : 0), clear);
76         }
77
78         inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
79                 const char **params, bool clear = true, bool nobinary = true)
80         {
81                 return execPrepared(stmtName, paramsNumber,
82                         (const void **)params, NULL, NULL, clear, nobinary);
83         }
84
85         void createTableIfNotExists(const std::string &table_name, const std::string &definition);
86         void verifyDatabase();
87
88         // Database initialization
89         void connectToDatabase();
90         virtual void createDatabase() = 0;
91         virtual void initStatements() = 0;
92         inline void prepareStatement(const std::string &name, const std::string &sql)
93         {
94                 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
95         }
96
97         const int getPGVersion() const { return m_pgversion; }
98 private:
99         // Database connectivity checks
100         void ping();
101
102         // Database usage
103         PGresult *checkResults(PGresult *res, bool clear = true);
104
105         // Attributes
106         std::string m_connect_string;
107         PGconn *m_conn = nullptr;
108         int m_pgversion = 0;
109 };
110
111 class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
112 {
113 public:
114         MapDatabasePostgreSQL(const std::string &connect_string);
115         virtual ~MapDatabasePostgreSQL() {}
116
117         bool saveBlock(const v3s16 &pos, const std::string &data);
118         void loadBlock(const v3s16 &pos, std::string *block);
119         bool deleteBlock(const v3s16 &pos);
120         void listAllLoadableBlocks(std::vector<v3s16> &dst);
121
122         void beginSave() { Database_PostgreSQL::beginSave(); }
123         void endSave() { Database_PostgreSQL::endSave(); }
124
125 protected:
126         virtual void createDatabase();
127         virtual void initStatements();
128 };
129
130 class PlayerDatabasePostgreSQL : private Database_PostgreSQL, public PlayerDatabase
131 {
132 public:
133         PlayerDatabasePostgreSQL(const std::string &connect_string);
134         virtual ~PlayerDatabasePostgreSQL() {}
135
136         void savePlayer(RemotePlayer *player);
137         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
138         bool removePlayer(const std::string &name);
139         void listPlayers(std::vector<std::string> &res);
140
141 protected:
142         virtual void createDatabase();
143         virtual void initStatements();
144
145 private:
146         bool playerDataExists(const std::string &playername);
147 };
148
149 #endif
150