|
| 1 | +#include <stdexcept> |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +#include <arpa/inet.h> |
| 5 | +#include "db-postgresql.h" |
| 6 | +#include "util.h" |
| 7 | +#include "types.h" |
| 8 | + |
| 9 | +#define ARRLEN(x) (sizeof(x) / sizeof((x)[0])) |
| 10 | + |
| 11 | +DBPostgreSQL::DBPostgreSQL(const std::string &mapdir) |
| 12 | +{ |
| 13 | + std::ifstream ifs((mapdir + "/world.mt").c_str()); |
| 14 | + if(!ifs.good()) |
| 15 | + throw std::runtime_error("Failed to read world.mt"); |
| 16 | + std::string const connect_string = get_setting("pgsql_connection", ifs); |
| 17 | + ifs.close(); |
| 18 | + db = PQconnectdb(connect_string.c_str()); |
| 19 | + |
| 20 | + if (PQstatus(db) != CONNECTION_OK) { |
| 21 | + throw std::runtime_error(std::string( |
| 22 | + "PostgreSQL database error: ") + |
| 23 | + PQerrorMessage(db) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + prepareStatement( |
| 28 | + "get_block_pos", |
| 29 | + "SELECT posX, posY, posZ FROM blocks" |
| 30 | + ); |
| 31 | + prepareStatement( |
| 32 | + "get_blocks_z", |
| 33 | + "SELECT posX, posY, data FROM blocks WHERE posZ = $1::int4" |
| 34 | + ); |
| 35 | + |
| 36 | + checkResults(PQexec(db, "START TRANSACTION;")); |
| 37 | + checkResults(PQexec(db, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;")); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +DBPostgreSQL::~DBPostgreSQL() |
| 42 | +{ |
| 43 | + try { |
| 44 | + checkResults(PQexec(db, "COMMIT;")); |
| 45 | + } catch (std::exception& caught) { |
| 46 | + std::cerr << "could not finalize: " << caught.what() << std::endl; |
| 47 | + } |
| 48 | + PQfinish(db); |
| 49 | +} |
| 50 | + |
| 51 | +std::vector<BlockPos> DBPostgreSQL::getBlockPos() |
| 52 | +{ |
| 53 | + std::vector<BlockPos> positions; |
| 54 | + |
| 55 | + PGresult *results = execPrepared( |
| 56 | + "get_block_pos", 0, |
| 57 | + NULL, NULL, NULL, false, false |
| 58 | + ); |
| 59 | + |
| 60 | + int numrows = PQntuples(results); |
| 61 | + |
| 62 | + for (int row = 0; row < numrows; ++row) |
| 63 | + positions.push_back(pg_to_blockpos(results, row, 0)); |
| 64 | + |
| 65 | + PQclear(results); |
| 66 | + |
| 67 | + return positions; |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +void DBPostgreSQL::getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos) |
| 72 | +{ |
| 73 | + int32_t const z = htonl(zPos); |
| 74 | + |
| 75 | + const void *args[] = { &z }; |
| 76 | + const int argLen[] = { sizeof(z) }; |
| 77 | + const int argFmt[] = { 1 }; |
| 78 | + |
| 79 | + PGresult *results = execPrepared( |
| 80 | + "get_blocks_z", ARRLEN(args), args, |
| 81 | + argLen, argFmt, false |
| 82 | + ); |
| 83 | + |
| 84 | + int numrows = PQntuples(results); |
| 85 | + |
| 86 | + for (int row = 0; row < numrows; ++row) { |
| 87 | + BlockPos position; |
| 88 | + position.x = pg_binary_to_int(results, row, 0); |
| 89 | + position.y = pg_binary_to_int(results, row, 1); |
| 90 | + position.z = zPos; |
| 91 | + Block const b( |
| 92 | + position, |
| 93 | + ustring( |
| 94 | + reinterpret_cast<unsigned char*>( |
| 95 | + PQgetvalue(results, row, 2) |
| 96 | + ), |
| 97 | + PQgetlength(results, row, 2) |
| 98 | + ) |
| 99 | + ); |
| 100 | + blocks[position.x].push_back(b); |
| 101 | + } |
| 102 | + |
| 103 | + PQclear(results); |
| 104 | +} |
| 105 | + |
| 106 | +PGresult *DBPostgreSQL::checkResults(PGresult *res, bool clear) |
| 107 | +{ |
| 108 | + ExecStatusType statusType = PQresultStatus(res); |
| 109 | + |
| 110 | + switch (statusType) { |
| 111 | + case PGRES_COMMAND_OK: |
| 112 | + case PGRES_TUPLES_OK: |
| 113 | + break; |
| 114 | + case PGRES_FATAL_ERROR: |
| 115 | + throw std::runtime_error( |
| 116 | + std::string("PostgreSQL database error: ") + |
| 117 | + PQresultErrorMessage(res) |
| 118 | + ); |
| 119 | + default: |
| 120 | + throw std::runtime_error( |
| 121 | + "Unhandled PostgreSQL result code" |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (clear) |
| 126 | + PQclear(res); |
| 127 | + |
| 128 | + return res; |
| 129 | +} |
| 130 | + |
| 131 | +void DBPostgreSQL::prepareStatement(const std::string &name, const std::string &sql) |
| 132 | +{ |
| 133 | + checkResults(PQprepare(db, name.c_str(), sql.c_str(), 0, NULL)); |
| 134 | +} |
| 135 | + |
| 136 | +PGresult *DBPostgreSQL::execPrepared( |
| 137 | + const char *stmtName, const int paramsNumber, |
| 138 | + const void **params, |
| 139 | + const int *paramsLengths, const int *paramsFormats, |
| 140 | + bool clear, bool nobinary |
| 141 | +) |
| 142 | +{ |
| 143 | + return checkResults(PQexecPrepared(db, stmtName, paramsNumber, |
| 144 | + (const char* const*) params, paramsLengths, paramsFormats, |
| 145 | + nobinary ? 1 : 0), clear |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +int DBPostgreSQL::pg_to_int(PGresult *res, int row, int col) |
| 150 | +{ |
| 151 | + return atoi(PQgetvalue(res, row, col)); |
| 152 | +} |
| 153 | + |
| 154 | +int DBPostgreSQL::pg_binary_to_int(PGresult *res, int row, int col) |
| 155 | +{ |
| 156 | + int32_t* raw = reinterpret_cast<int32_t*>(PQgetvalue(res, row, col)); |
| 157 | + return ntohl(*raw); |
| 158 | +} |
| 159 | + |
| 160 | +BlockPos DBPostgreSQL::pg_to_blockpos(PGresult *res, int row, int col) |
| 161 | +{ |
| 162 | + BlockPos result; |
| 163 | + result.x = pg_to_int(res, row, col); |
| 164 | + result.y = pg_to_int(res, row, col + 1); |
| 165 | + result.z = pg_to_int(res, row, col + 2); |
| 166 | + return result; |
| 167 | +} |
0 commit comments