3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 #include "util/basic_macros.h"
29 class Database_PostgreSQL: public Database
32 Database_PostgreSQL(const std::string &connect_string);
33 ~Database_PostgreSQL();
38 bool initialized() const;
43 inline int pg_to_int(PGresult *res, int row, int col)
45 return atoi(PQgetvalue(res, row, col));
48 inline u32 pg_to_uint(PGresult *res, int row, int col)
50 return (u32) atoi(PQgetvalue(res, row, col));
53 inline float pg_to_float(PGresult *res, int row, int col)
55 return (float) atof(PQgetvalue(res, row, col));
58 inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
61 pg_to_int(res, row, col),
62 pg_to_int(res, row, col + 1),
63 pg_to_int(res, row, col + 2)
67 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
69 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
70 bool clear = true, bool nobinary = true)
72 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
73 (const char* const*) params, paramsLengths, paramsFormats,
74 nobinary ? 1 : 0), clear);
77 inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
78 const char **params, bool clear = true, bool nobinary = true)
80 return execPrepared(stmtName, paramsNumber,
81 (const void **)params, NULL, NULL, clear, nobinary);
84 void createTableIfNotExists(const std::string &table_name, const std::string &definition);
85 void verifyDatabase();
87 // Database initialization
88 void connectToDatabase();
89 virtual void createDatabase() = 0;
90 virtual void initStatements() = 0;
91 inline void prepareStatement(const std::string &name, const std::string &sql)
93 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
96 const int getPGVersion() const { return m_pgversion; }
98 // Database connectivity checks
102 PGresult *checkResults(PGresult *res, bool clear = true);
105 std::string m_connect_string;
106 PGconn *m_conn = nullptr;
110 class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
113 MapDatabasePostgreSQL(const std::string &connect_string);
114 virtual ~MapDatabasePostgreSQL() = default;
116 bool saveBlock(const v3s16 &pos, const std::string &data);
117 void loadBlock(const v3s16 &pos, std::string *block);
118 bool deleteBlock(const v3s16 &pos);
119 void listAllLoadableBlocks(std::vector<v3s16> &dst);
121 void beginSave() { Database_PostgreSQL::beginSave(); }
122 void endSave() { Database_PostgreSQL::endSave(); }
125 virtual void createDatabase();
126 virtual void initStatements();
129 class PlayerDatabasePostgreSQL : private Database_PostgreSQL, public PlayerDatabase
132 PlayerDatabasePostgreSQL(const std::string &connect_string);
133 virtual ~PlayerDatabasePostgreSQL() = default;
135 void savePlayer(RemotePlayer *player);
136 bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
137 bool removePlayer(const std::string &name);
138 void listPlayers(std::vector<std::string> &res);
141 virtual void createDatabase();
142 virtual void initStatements();
145 bool playerDataExists(const std::string &playername);