|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * |
| 4 | + * This file is part of osm2pgsql (https://osm2pgsql.org/). |
| 5 | + * |
| 6 | + * Copyright (C) 2006-2022 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "format.hpp" |
| 11 | +#include "pgsql.hpp" |
| 12 | +#include "pgsql-helper.hpp" |
| 13 | + |
| 14 | +#include <set> |
| 15 | +#include <string> |
| 16 | + |
| 17 | +static std::set<std::string> init_set_from_table(pg_conn_t const &db_connection, |
| 18 | + char const *table, |
| 19 | + char const *column, |
| 20 | + char const *condition) |
| 21 | +{ |
| 22 | + std::set<std::string> values; |
| 23 | + |
| 24 | + auto const res = db_connection.query( |
| 25 | + PGRES_TUPLES_OK, |
| 26 | + "SELECT {} FROM {} WHERE {}"_format(column, table, condition)); |
| 27 | + for (int i = 0; i < res.num_tuples(); ++i) { |
| 28 | + values.insert(res.get_value_as_string(i, 0)); |
| 29 | + } |
| 30 | + |
| 31 | + return values; |
| 32 | +} |
| 33 | + |
| 34 | +bool has_extension(pg_conn_t const &db_connection, std::string const &value) |
| 35 | +{ |
| 36 | + static const std::set<std::string> values = init_set_from_table( |
| 37 | + db_connection, "pg_catalog.pg_extension", "extname", "true"); |
| 38 | + |
| 39 | + return values.count(value); |
| 40 | +} |
| 41 | + |
| 42 | +bool has_schema(pg_conn_t const &db_connection, std::string const &value) |
| 43 | +{ |
| 44 | + static const std::set<std::string> values = init_set_from_table( |
| 45 | + db_connection, "pg_catalog.pg_namespace", "nspname", |
| 46 | + "nspname !~ '^pg_' AND nspname <> 'information_schema'"); |
| 47 | + |
| 48 | + if (value.empty()) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return values.count(value); |
| 53 | +} |
| 54 | + |
| 55 | +bool has_tablespace(pg_conn_t const &db_connection, std::string const &value) |
| 56 | +{ |
| 57 | + static const std::set<std::string> values = |
| 58 | + init_set_from_table(db_connection, "pg_catalog.pg_tablespace", |
| 59 | + "spcname", "spcname != 'pg_global'"); |
| 60 | + |
| 61 | + if (value.empty()) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + return values.count(value); |
| 66 | +} |
0 commit comments