]> git.lizzy.rs Git - dragonfireclient.git/blob - src/database-postgresql.h
Implement a PostgreSQL backend
[dragonfireclient.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 Settings &conf);
34         ~Database_PostgreSQL();
35
36         void beginSave();
37         void endSave();
38
39         bool saveBlock(const v3s16 &pos, const std::string &data);
40         void loadBlock(const v3s16 &pos, std::string *block);
41         bool deleteBlock(const v3s16 &pos);
42         void listAllLoadableBlocks(std::vector<v3s16> &dst);
43         bool initialized() const;
44
45 private:
46         // Database initialization
47         void connectToDatabase();
48         void initStatements();
49         void createDatabase();
50
51         inline void prepareStatement(const std::string &name, const std::string &sql)
52         {
53                 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
54         }
55
56         // Database connectivity checks
57         void ping();
58         void verifyDatabase();
59
60         // Database usage
61         PGresult *checkResults(PGresult *res, bool clear = true);
62
63         inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
64                         const void **params,
65                         const int *paramsLengths = NULL, const int *paramsFormats = NULL,
66                         bool clear = true, bool nobinary = true)
67         {
68                 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
69                         (const char* const*) params, paramsLengths, paramsFormats,
70                         nobinary ? 1 : 0), clear);
71         }
72
73         // Conversion helpers
74         inline int pg_to_int(PGresult *res, int row, int col)
75         {
76                 return atoi(PQgetvalue(res, row, col));
77         }
78
79         inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
80         {
81                 return v3s16(
82                         pg_to_int(res, row, col),
83                         pg_to_int(res, row, col + 1),
84                         pg_to_int(res, row, col + 2)
85                 );
86         }
87
88         // Attributes
89         std::string m_connect_string;
90         PGconn *m_conn;
91         int m_pgversion;
92 };
93
94 #endif
95