forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.cpp
More file actions
163 lines (131 loc) · 4.31 KB
/
properties.cpp
File metadata and controls
163 lines (131 loc) · 4.31 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
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "properties.hpp"
#include "format.hpp"
#include "logging.hpp"
#include "pgsql-capabilities.hpp"
#include "pgsql.hpp"
#include <cassert>
#include <cstdlib>
namespace {
constexpr char const *const PROPERTIES_TABLE = "osm2pgsql_properties";
} // anonymous namespace
properties_t::properties_t(connection_params_t connection_params,
std::string schema)
: m_connection_params(std::move(connection_params)),
m_schema(std::move(schema)),
m_has_properties_table(has_table(m_schema, PROPERTIES_TABLE))
{
assert(!m_schema.empty());
log_debug("Found properties table '{}': {}.", PROPERTIES_TABLE,
m_has_properties_table);
}
std::string properties_t::get_string(std::string const &property,
std::string const &default_value) const
{
auto const it = m_properties.find(property);
if (it == m_properties.end()) {
return default_value;
}
return it->second;
}
int64_t properties_t::get_int(std::string const &property,
int64_t default_value) const
{
auto const it = m_properties.find(property);
if (it == m_properties.end()) {
return default_value;
}
char *end = nullptr;
errno = 0;
int64_t const val = std::strtol(it->second.data(), &end, 10);
if (errno || *end != '\0') {
throw fmt_error("Corruption in properties: '{}' must be an integer.",
property);
}
return val;
}
bool properties_t::get_bool(std::string const &property,
bool default_value) const
{
auto const it = m_properties.find(property);
if (it == m_properties.end()) {
return default_value;
}
if (it->second == "true") {
return true;
}
if (it->second == "false") {
return false;
}
throw fmt_error("Corruption in properties: '{}' must be 'true' or 'false'.",
property);
}
void properties_t::set_string(std::string const &property,
std::string const &value)
{
m_properties[property] = value;
m_to_update[property] = value;
}
void properties_t::set_int(std::string const &property, int64_t value)
{
set_string(property, std::to_string(value));
}
void properties_t::set_bool(std::string const &property, bool value)
{
set_string(property, value ? "true" : "false");
}
void properties_t::init_table()
{
auto const table = table_name();
log_info("Initializing properties table '{}'.", table);
pg_conn_t const db_connection{m_connection_params, "prop.store"};
db_connection.exec("CREATE TABLE IF NOT EXISTS {} ("
" property TEXT NOT NULL PRIMARY KEY,"
" value TEXT NOT NULL)",
table);
db_connection.exec("TRUNCATE {}", table);
m_has_properties_table = true;
}
void properties_t::store()
{
auto const table = table_name();
log_info("Storing properties to table '{}'.", table);
pg_conn_t const db_connection{m_connection_params, "prop.store"};
db_connection.prepare(
"set_property",
"INSERT INTO {} (property, value) VALUES ($1::text, $2::text)"
" ON CONFLICT (property) DO UPDATE SET value = EXCLUDED.value",
table);
for (auto const &[k, v] : m_to_update) {
log_debug(" Storing {}='{}'", k, v);
db_connection.exec_prepared("set_property", k, v);
}
m_to_update.clear();
}
bool properties_t::load()
{
if (!m_has_properties_table) {
log_info("No properties found in database from previous import.");
return false;
}
m_properties.clear();
auto const table = table_name();
log_info("Loading properties from table '{}'.", table);
pg_conn_t const db_connection{m_connection_params, "prop.load"};
auto const result = db_connection.exec("SELECT * FROM {}", table);
for (int i = 0; i < result.num_tuples(); ++i) {
m_properties.insert_or_assign(result.get_value(i, 0), result.get(i, 1));
}
return true;
}
std::string properties_t::table_name() const
{
return qualified_name(m_schema, PROPERTIES_TABLE);
}