Skip to content

Commit 46c5cb7

Browse files
authored
Merge pull request #1978 from joto/properties-fixes
Properties fixes
2 parents 397201b + 0ee008f commit 46c5cb7

2 files changed

Lines changed: 19 additions & 27 deletions

File tree

src/properties.cpp

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,42 +79,36 @@ bool properties_t::get_bool(std::string const &property,
7979
void properties_t::set_string(std::string property, std::string value,
8080
bool update_database)
8181
{
82-
m_properties.insert_or_assign(std::move(property), std::move(value));
83-
if (update_database) {
84-
update(property);
82+
auto const r =
83+
m_properties.insert_or_assign(std::move(property), std::move(value));
84+
85+
if (!update_database) {
86+
return;
8587
}
88+
89+
auto const &inserted = *(r.first);
90+
log_debug(" Storing {}='{}'", inserted.first, inserted.second);
91+
92+
pg_conn_t const db_connection{m_conninfo};
93+
db_connection.exec(
94+
"PREPARE set_property(text, text) AS"
95+
" INSERT INTO {} (property, value) VALUES ($1, $2)"
96+
" ON CONFLICT (property) DO UPDATE SET value = EXCLUDED.value",
97+
table_name());
98+
db_connection.exec_prepared("set_property", inserted.first,
99+
inserted.second);
86100
}
87101

88102
void properties_t::set_int(std::string property, int64_t value,
89103
bool update_database)
90104
{
91-
m_properties.insert_or_assign(std::move(property), std::to_string(value));
92-
if (update_database) {
93-
update(property);
94-
}
105+
set_string(std::move(property), std::to_string(value), update_database);
95106
}
96107

97108
void properties_t::set_bool(std::string property, bool value,
98109
bool update_database)
99110
{
100-
m_properties.insert_or_assign(std::move(property),
101-
value ? "true" : "false");
102-
if (update_database) {
103-
update(property);
104-
}
105-
}
106-
107-
void properties_t::update(std::string const &property) const
108-
{
109-
pg_conn_t const db_connection{m_conninfo};
110-
111-
db_connection.exec("PREPARE set_property(text, text) AS"
112-
" UPDATE {} SET value = $2 WHERE property = $1",
113-
table_name());
114-
115-
auto const value = m_properties.at(property);
116-
log_debug(" Storing {}='{}'", property, value);
117-
db_connection.exec_prepared("set_property", property, value);
111+
set_string(std::move(property), value ? "true" : "false", update_database);
118112
}
119113

120114
void properties_t::store()

src/properties.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ class properties_t
9494
private:
9595
std::string table_name() const;
9696

97-
void update(std::string const &property) const;
98-
9997
std::map<std::string, std::string> m_properties;
10098
std::string m_conninfo;
10199
std::string m_schema;

0 commit comments

Comments
 (0)