-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathpgsql-capabilities.cpp
More file actions
226 lines (186 loc) · 6.57 KB
/
pgsql-capabilities.cpp
File metadata and controls
226 lines (186 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "format.hpp"
#include "logging.hpp"
#include "pgsql-capabilities.hpp"
#include "pgsql-capabilities-int.hpp"
#include "pgsql.hpp"
#include "version.hpp"
#include <stdexcept>
namespace {
database_capabilities_t &capabilities() noexcept
{
static database_capabilities_t capabilities;
return capabilities;
}
} // anonymous namespace
database_capabilities_t &database_capabilities_for_testing() noexcept
{
return capabilities();
}
namespace {
void init_set_from_query(std::set<std::string> *set,
pg_conn_t const &db_connection, char const *table,
char const *column, char const *condition = "true")
{
set->clear(); // Clear existing in case this is called multiple times
auto const res = db_connection.exec("SELECT {} FROM {} WHERE {}", column,
table, condition);
for (int i = 0; i < res.num_tuples(); ++i) {
set->emplace(res.get(i, 0));
}
}
/// Get all config settings from the database.
void init_settings(pg_conn_t const &db_connection)
{
capabilities().settings.clear(); // In case this is called multiple times
auto const res =
db_connection.exec("SELECT name, setting FROM pg_catalog.pg_settings");
for (int i = 0; i < res.num_tuples(); ++i) {
capabilities().settings.emplace(res.get(i, 0), res.get(i, 1));
}
}
void init_database_name(pg_conn_t const &db_connection)
{
auto const res = db_connection.exec("SELECT current_catalog");
if (res.num_tuples() != 1) {
throw std::runtime_error{
"Database error: Can not access database name."};
}
capabilities().database_name = res.get(0, 0);
}
void init_geometry_oid(pg_conn_t const &db_connection)
{
auto const res =
db_connection.exec("SELECT oid FROM pg_type WHERE typname='geometry'");
if (res.num_tuples() != 1) {
throw std::runtime_error{"Database error: Can not get geometry type. "
"Is PostGIS extension loaded?"};
}
capabilities().geometry_type_oid = std::stoul(std::string{res.get(0, 0)});
}
void init_postgis_version(pg_conn_t const &db_connection)
{
auto const res = db_connection.exec(
"SELECT regexp_split_to_table(extversion, '\\.') FROM"
" pg_catalog.pg_extension WHERE extname='postgis'");
if (res.num_tuples() == 0) {
throw fmt_error(
"The postgis extension is not enabled on the database '{}'."
" Are you using the correct database?"
" Enable with 'CREATE EXTENSION postgis;'",
capabilities().database_name);
}
try {
if (res.num_tuples() >= 2) {
capabilities().postgis = {std::stoi(std::string{res.get(0, 0)}),
std::stoi(std::string{res.get(1, 0)})};
}
} catch (...) { // NOLINT(bugprone-empty-catch)
// Fall through if std::stoi() fails in which case the version
// is reported as 0.
}
}
} // anonymous namespace
void init_database_capabilities(pg_conn_t const &db_connection)
{
init_settings(db_connection);
init_database_name(db_connection);
init_postgis_version(db_connection);
init_geometry_oid(db_connection);
try {
log_info("Database version: {}",
capabilities().settings.at("server_version"));
if (capabilities().postgis.major == 0) {
log_info("PostGIS version: could not be detected");
} else {
log_info("PostGIS version: {}.{}", capabilities().postgis.major,
capabilities().postgis.minor);
}
auto const version_str =
capabilities().settings.at("server_version_num");
capabilities().database_version =
std::strtoul(version_str.c_str(), nullptr, 10);
if (capabilities().database_version <
get_minimum_postgresql_server_version_num()) {
throw fmt_error(
"Your database version is too old (need at least {}).",
get_minimum_postgresql_server_version());
}
if (capabilities().settings.at("server_encoding") != "UTF8") {
throw std::runtime_error{"Database is not using UTF8 encoding."};
}
} catch (std::out_of_range const &) {
// Thrown by the settings.at() if the named setting isn't found
throw std::runtime_error{"Can't access database setting."};
}
init_set_from_query(&capabilities().extensions, db_connection,
"pg_catalog.pg_extension", "extname");
init_set_from_query(
&capabilities().schemas, db_connection, "pg_catalog.pg_namespace",
"nspname", "nspname !~ '^pg_' AND nspname <> 'information_schema'");
init_set_from_query(&capabilities().tablespaces, db_connection,
"pg_catalog.pg_tablespace", "spcname",
"spcname != 'pg_global'");
init_set_from_query(&capabilities().index_methods, db_connection,
"pg_catalog.pg_am", "amname", "amtype = 'i'");
init_set_from_query(
&capabilities().tables, db_connection, "pg_catalog.pg_tables",
"schemaname || '.' || tablename",
"schemaname NOT IN ('pg_catalog', 'information_schema')");
}
bool has_extension(std::string const &value)
{
return capabilities().extensions.count(value);
}
bool has_schema(std::string const &value)
{
if (value.empty()) {
return true;
}
return capabilities().schemas.count(value);
}
bool has_tablespace(std::string const &value)
{
if (value.empty()) {
return true;
}
return capabilities().tablespaces.count(value);
}
bool has_index_method(std::string const &value)
{
return capabilities().index_methods.count(value);
}
bool has_table(std::string schema, std::string const &name)
{
schema += '.';
schema += name;
return capabilities().tables.count(schema);
}
bool is_geometry_type(unsigned int oid)
{
return capabilities().geometry_type_oid == oid;
}
void check_schema(std::string const &schema)
{
if (has_schema(schema)) {
return;
}
throw fmt_error("Schema '{0}' not available."
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
schema);
}
uint32_t get_database_version() noexcept
{
return capabilities().database_version;
}
postgis_version get_postgis_version() noexcept
{
return capabilities().postgis;
}