|
9 | 9 |
|
10 | 10 | #define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
|
11 | 11 |
|
| 12 | +/* PostgreSQLBase */ |
| 13 | + |
| 14 | +PostgreSQLBase::~PostgreSQLBase() |
| 15 | +{ |
| 16 | + if (db) |
| 17 | + PQfinish(db); |
| 18 | +} |
| 19 | + |
| 20 | +void PostgreSQLBase::openDatabase(const char *connect_string) |
| 21 | +{ |
| 22 | + if (db) |
| 23 | + throw std::logic_error("Database already open"); |
| 24 | + |
| 25 | + db = PQconnectdb(connect_string); |
| 26 | + if (PQstatus(db) != CONNECTION_OK) { |
| 27 | + throw std::runtime_error(std::string("PostgreSQL database error: ") + |
| 28 | + PQerrorMessage(db) |
| 29 | + ); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +PGresult *PostgreSQLBase::checkResults(PGresult *res, bool clear) |
| 34 | +{ |
| 35 | + ExecStatusType statusType = PQresultStatus(res); |
| 36 | + |
| 37 | + switch (statusType) { |
| 38 | + case PGRES_COMMAND_OK: |
| 39 | + case PGRES_TUPLES_OK: |
| 40 | + break; |
| 41 | + case PGRES_FATAL_ERROR: |
| 42 | + throw std::runtime_error( |
| 43 | + std::string("PostgreSQL database error: ") + |
| 44 | + PQresultErrorMessage(res) |
| 45 | + ); |
| 46 | + default: |
| 47 | + throw std::runtime_error( |
| 48 | + std::string("Unhandled PostgreSQL result code ") + |
| 49 | + std::to_string(statusType) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (clear) |
| 54 | + PQclear(res); |
| 55 | + return res; |
| 56 | +} |
| 57 | + |
| 58 | +PGresult *PostgreSQLBase::execPrepared( |
| 59 | + const char *stmtName, const int paramsNumber, |
| 60 | + const void **params, |
| 61 | + const int *paramsLengths, const int *paramsFormats, |
| 62 | + bool clear) |
| 63 | +{ |
| 64 | + return checkResults(PQexecPrepared(db, stmtName, paramsNumber, |
| 65 | + (const char* const*) params, paramsLengths, paramsFormats, |
| 66 | + 1 /* binary output */), clear |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +/* DBPostgreSQL */ |
| 71 | + |
12 | 72 | DBPostgreSQL::DBPostgreSQL(const std::string &mapdir)
|
13 | 73 | {
|
14 | 74 | std::ifstream ifs(mapdir + "world.mt");
|
15 | 75 | if (!ifs.good())
|
16 | 76 | throw std::runtime_error("Failed to read world.mt");
|
17 | 77 | std::string connect_string = read_setting("pgsql_connection", ifs);
|
18 | 78 | ifs.close();
|
19 |
| - db = PQconnectdb(connect_string.c_str()); |
20 | 79 |
|
21 |
| - if (PQstatus(db) != CONNECTION_OK) { |
22 |
| - throw std::runtime_error(std::string( |
23 |
| - "PostgreSQL database error: ") + |
24 |
| - PQerrorMessage(db) |
25 |
| - ); |
26 |
| - } |
| 80 | + openDatabase(connect_string.c_str()); |
27 | 81 |
|
28 | 82 | prepareStatement(
|
29 | 83 | "get_block_pos",
|
@@ -56,7 +110,6 @@ DBPostgreSQL::~DBPostgreSQL()
|
56 | 110 | } catch (const std::exception& caught) {
|
57 | 111 | std::cerr << "could not finalize: " << caught.what() << std::endl;
|
58 | 112 | }
|
59 |
| - PQfinish(db); |
60 | 113 | }
|
61 | 114 |
|
62 | 115 |
|
@@ -169,50 +222,6 @@ void DBPostgreSQL::getBlocksByPos(BlockList &blocks,
|
169 | 222 | }
|
170 | 223 | }
|
171 | 224 |
|
172 |
| - |
173 |
| -PGresult *DBPostgreSQL::checkResults(PGresult *res, bool clear) |
174 |
| -{ |
175 |
| - ExecStatusType statusType = PQresultStatus(res); |
176 |
| - |
177 |
| - switch (statusType) { |
178 |
| - case PGRES_COMMAND_OK: |
179 |
| - case PGRES_TUPLES_OK: |
180 |
| - break; |
181 |
| - case PGRES_FATAL_ERROR: |
182 |
| - throw std::runtime_error( |
183 |
| - std::string("PostgreSQL database error: ") + |
184 |
| - PQresultErrorMessage(res) |
185 |
| - ); |
186 |
| - default: |
187 |
| - throw std::runtime_error( |
188 |
| - "Unhandled PostgreSQL result code" |
189 |
| - ); |
190 |
| - } |
191 |
| - |
192 |
| - if (clear) |
193 |
| - PQclear(res); |
194 |
| - |
195 |
| - return res; |
196 |
| -} |
197 |
| - |
198 |
| -void DBPostgreSQL::prepareStatement(const std::string &name, const std::string &sql) |
199 |
| -{ |
200 |
| - checkResults(PQprepare(db, name.c_str(), sql.c_str(), 0, NULL)); |
201 |
| -} |
202 |
| - |
203 |
| -PGresult *DBPostgreSQL::execPrepared( |
204 |
| - const char *stmtName, const int paramsNumber, |
205 |
| - const void **params, |
206 |
| - const int *paramsLengths, const int *paramsFormats, |
207 |
| - bool clear |
208 |
| -) |
209 |
| -{ |
210 |
| - return checkResults(PQexecPrepared(db, stmtName, paramsNumber, |
211 |
| - (const char* const*) params, paramsLengths, paramsFormats, |
212 |
| - 1 /* binary output */), clear |
213 |
| - ); |
214 |
| -} |
215 |
| - |
216 | 225 | int DBPostgreSQL::pg_binary_to_int(PGresult *res, int row, int col)
|
217 | 226 | {
|
218 | 227 | int32_t* raw = reinterpret_cast<int32_t*>(PQgetvalue(res, row, col));
|
|
0 commit comments