Skip to content

Commit f011782

Browse files
committed
Add a variant of the database exec() command with fmt parameters
Makes lots of code simpler.
1 parent 7660648 commit f011782

File tree

8 files changed

+76
-64
lines changed

8 files changed

+76
-64
lines changed

src/flex-table.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,12 @@ void table_connection_t::start(bool append)
287287
m_db_connection->exec("SET client_min_messages = WARNING");
288288

289289
if (!append) {
290-
m_db_connection->exec(
291-
"DROP TABLE IF EXISTS {} CASCADE"_format(table().full_name()));
290+
m_db_connection->exec("DROP TABLE IF EXISTS {} CASCADE",
291+
table().full_name());
292292
}
293293

294294
// These _tmp tables can be left behind if we run out of disk space.
295-
m_db_connection->exec(
296-
"DROP TABLE IF EXISTS {}"_format(table().full_tmp_name()));
295+
m_db_connection->exec("DROP TABLE IF EXISTS {}", table().full_tmp_name());
297296
m_db_connection->exec("RESET client_min_messages");
298297

299298
if (!append) {
@@ -362,9 +361,9 @@ void table_connection_t::stop(bool updateable, bool append)
362361

363362
m_db_connection->exec(sql);
364363

365-
m_db_connection->exec("DROP TABLE {}"_format(table().full_name()));
366-
m_db_connection->exec(R"(ALTER TABLE {} RENAME TO "{}")"_format(
367-
table().full_tmp_name(), table().name()));
364+
m_db_connection->exec("DROP TABLE {}", table().full_name());
365+
m_db_connection->exec(R"(ALTER TABLE {} RENAME TO "{}")",
366+
table().full_tmp_name(), table().name());
368367
m_id_index_created = false;
369368

370369
if (updateable) {

src/middle-pgsql.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void middle_pgsql_t::table_desc::drop_table(
8585
log_info("Dropping table '{}'", name());
8686

8787
auto const qual_name = qualified_name(schema(), name());
88-
db_connection.exec("DROP TABLE IF EXISTS {}"_format(qual_name));
88+
db_connection.exec("DROP TABLE IF EXISTS {}", qual_name);
8989

9090
log_info("Table '{}' dropped in {}", name(),
9191
util::human_readable_duration(timer.stop()));
@@ -668,8 +668,7 @@ void middle_pgsql_t::start()
668668
for (auto const &table : m_tables) {
669669
log_debug("Setting up table '{}'", table.name());
670670
auto const qual_name = qualified_name(table.schema(), table.name());
671-
m_db_connection.exec(
672-
"DROP TABLE IF EXISTS {} CASCADE"_format(qual_name));
671+
m_db_connection.exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
673672
if (!table.m_create_table.empty()) {
674673
m_db_connection.exec(table.m_create_table);
675674
}
@@ -826,9 +825,11 @@ static table_sql sql_for_relations() noexcept
826825
static bool check_bucket_index(pg_conn_t *db_connection,
827826
std::string const &prefix)
828827
{
829-
auto const res = db_connection->exec(
830-
"SELECT relname FROM pg_class WHERE relkind='i' AND"
831-
" relname = '{}_ways_nodes_bucket_idx';"_format(prefix));
828+
auto const res =
829+
db_connection->exec("SELECT relname FROM pg_class"
830+
" WHERE relkind='i'"
831+
" AND relname = '{}_ways_nodes_bucket_idx'",
832+
prefix);
832833
return res.num_tuples() > 0;
833834
}
834835

src/pgsql-helper.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,24 @@ void create_geom_check_trigger(pg_conn_t *db_connection,
3939
{
4040
std::string func_name = qualified_name(schema, table + "_osm2pgsql_valid");
4141

42-
db_connection->exec(
43-
"CREATE OR REPLACE FUNCTION {}()\n"
44-
"RETURNS TRIGGER AS $$\n"
45-
"BEGIN\n"
46-
" IF {} THEN \n"
47-
" RETURN NEW;\n"
48-
" END IF;\n"
49-
" RETURN NULL;\n"
50-
"END;"
51-
"$$ LANGUAGE plpgsql;"_format(func_name, condition));
42+
db_connection->exec("CREATE OR REPLACE FUNCTION {}()\n"
43+
"RETURNS TRIGGER AS $$\n"
44+
"BEGIN\n"
45+
" IF {} THEN \n"
46+
" RETURN NEW;\n"
47+
" END IF;\n"
48+
" RETURN NULL;\n"
49+
"END;"
50+
"$$ LANGUAGE plpgsql",
51+
func_name, condition);
5252

53-
db_connection->exec(
54-
"CREATE TRIGGER \"{}\""
55-
" BEFORE INSERT OR UPDATE"
56-
" ON {}"
57-
" FOR EACH ROW EXECUTE PROCEDURE"
58-
" {}();"_format(table + "_osm2pgsql_valid",
59-
qualified_name(schema, table), func_name));
53+
db_connection->exec("CREATE TRIGGER \"{}\""
54+
" BEFORE INSERT OR UPDATE"
55+
" ON {}"
56+
" FOR EACH ROW EXECUTE PROCEDURE"
57+
" {}()",
58+
table + "_osm2pgsql_valid",
59+
qualified_name(schema, table), func_name);
6060
}
6161

6262
void drop_geom_check_trigger(pg_conn_t *db_connection,
@@ -65,17 +65,18 @@ void drop_geom_check_trigger(pg_conn_t *db_connection,
6565
{
6666
std::string func_name = qualified_name(schema, table + "_osm2pgsql_valid");
6767

68-
db_connection->exec(R"(DROP TRIGGER "{}" ON {};)"_format(
69-
table + "_osm2pgsql_valid", qualified_name(schema, table)));
68+
db_connection->exec(R"(DROP TRIGGER "{}" ON {})",
69+
table + "_osm2pgsql_valid",
70+
qualified_name(schema, table));
7071

71-
db_connection->exec("DROP FUNCTION IF EXISTS {} ();"_format(func_name));
72+
db_connection->exec("DROP FUNCTION IF EXISTS {} ()", func_name);
7273
}
7374

7475
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
7576
std::string const &name)
7677
{
7778
auto const qual_name = qualified_name(schema, name);
78-
db_connection.exec("ANALYZE {}"_format(qual_name));
79+
db_connection.exec("ANALYZE {}", qual_name);
7980
}
8081

8182
bool has_table(pg_conn_t const &db_connection, std::string const &schema,

src/pgsql.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ void pg_conn_t::set_config(char const *setting, char const *value) const
4242
// Update pg_settings instead of using SET because it does not yield
4343
// errors on older versions of PostgreSQL where the settings are not
4444
// implemented.
45-
exec("UPDATE pg_settings SET setting = '{}' WHERE name = '{}'"_format(
46-
value, setting));
45+
exec("UPDATE pg_settings SET setting = '{}' WHERE name = '{}'", value,
46+
setting);
4747
}
4848

4949
pg_result_t pg_conn_t::exec(char const *sql) const

src/pgsql.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ class pg_conn_t
189189
pg_result_t exec(char const *sql) const;
190190
pg_result_t exec(std::string const &sql) const;
191191

192+
/**
193+
* Run the specified SQL command.
194+
*
195+
* \param sql The SQL command using fmt lib patterns.
196+
* \param params Any number of arguments for the fmt lib.
197+
* \throws std::runtime_exception If the command failed (didn't return
198+
* status code PGRES_COMMAND_OK or PGRES_TUPLES_OK).
199+
*/
200+
template <typename... TArgs>
201+
pg_result_t exec(char const *sql, TArgs... params) const
202+
{
203+
return exec(fmt::format(sql, std::forward<TArgs>(params)...));
204+
}
205+
192206
/**
193207
* Run the named prepared SQL statement and return the results.
194208
*

src/table.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
9292

9393
// we are making a new table
9494
if (!m_append) {
95-
m_sql_conn->exec(
96-
"DROP TABLE IF EXISTS {} CASCADE"_format(qual_name));
95+
m_sql_conn->exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
9796
}
9897

9998
// These _tmp tables can be left behind if we run out of disk space.
100-
m_sql_conn->exec("DROP TABLE IF EXISTS {}"_format(qual_tmp_name));
99+
m_sql_conn->exec("DROP TABLE IF EXISTS {}", qual_tmp_name);
101100
m_sql_conn->exec("RESET client_min_messages");
102101

103102
//making a new table
@@ -149,9 +148,9 @@ void table_t::prepare()
149148
{
150149
//let postgres cache this query as it will presumably happen a lot
151150
auto const qual_name = qualified_name(m_target->schema, m_target->name);
152-
m_sql_conn->exec(
153-
"PREPARE get_wkb(int8) AS SELECT way FROM {} WHERE osm_id = $1"_format(
154-
qual_name));
151+
m_sql_conn->exec("PREPARE get_wkb(int8) AS"
152+
" SELECT way FROM {} WHERE osm_id = $1",
153+
qual_name);
155154
}
156155

157156
void table_t::generate_copy_column_list()
@@ -229,23 +228,22 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
229228

230229
m_sql_conn->exec(sql);
231230

232-
m_sql_conn->exec("DROP TABLE {}"_format(qual_name));
233-
m_sql_conn->exec(R"(ALTER TABLE {} RENAME TO "{}")"_format(
234-
qual_tmp_name, m_target->name));
231+
m_sql_conn->exec("DROP TABLE {}", qual_name);
232+
m_sql_conn->exec(R"(ALTER TABLE {} RENAME TO "{}")", qual_tmp_name,
233+
m_target->name);
235234

236235
log_info("Creating geometry index on table '{}'...", m_target->name);
237236

238237
// Use fillfactor 100 for un-updatable imports
239-
m_sql_conn->exec("CREATE INDEX ON {} USING GIST (way) {} {}"_format(
240-
qual_name, (updateable ? "" : "WITH (fillfactor = 100)"),
241-
tablespace_clause(table_space_index)));
238+
m_sql_conn->exec("CREATE INDEX ON {} USING GIST (way) {} {}", qual_name,
239+
(updateable ? "" : "WITH (fillfactor = 100)"),
240+
tablespace_clause(table_space_index));
242241

243242
/* slim mode needs this to be able to apply diffs */
244243
if (updateable) {
245244
log_info("Creating osm_id index on table '{}'...", m_target->name);
246-
m_sql_conn->exec(
247-
"CREATE INDEX ON {} USING BTREE (osm_id) {}"_format(
248-
qual_name, tablespace_clause(table_space_index)));
245+
m_sql_conn->exec("CREATE INDEX ON {} USING BTREE (osm_id) {}",
246+
qual_name, tablespace_clause(table_space_index));
249247
if (m_srid != "4326") {
250248
create_geom_check_trigger(m_sql_conn.get(), m_target->schema,
251249
m_target->name,
@@ -258,15 +256,14 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
258256
log_info("Creating hstore indexes on table '{}'...",
259257
m_target->name);
260258
if (m_hstore_mode != hstore_column::none) {
261-
m_sql_conn->exec(
262-
"CREATE INDEX ON {} USING GIN (tags) {}"_format(
263-
qual_name, tablespace_clause(table_space_index)));
259+
m_sql_conn->exec("CREATE INDEX ON {} USING GIN (tags) {}",
260+
qual_name,
261+
tablespace_clause(table_space_index));
264262
}
265263
for (auto const &hcolumn : m_hstore_columns) {
266-
m_sql_conn->exec(
267-
R"(CREATE INDEX ON {} USING GIN ("{}") {})"_format(
268-
qual_name, hcolumn,
269-
tablespace_clause(table_space_index)));
264+
m_sql_conn->exec(R"(CREATE INDEX ON {} USING GIN ("{}") {})",
265+
qual_name, hcolumn,
266+
tablespace_clause(table_space_index));
270267
}
271268
}
272269
log_info("Analyzing table '{}'...", m_target->name);

tests/common-pg.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ class tempdb_t
101101
conn_t conn{"dbname=postgres"};
102102

103103
m_db_name = "osm2pgsql-test-{}-{}"_format(getpid(), time(nullptr));
104-
conn.exec("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
105-
conn.exec("CREATE DATABASE \"{}\" WITH ENCODING 'UTF8'"_format(
106-
m_db_name));
104+
conn.exec(R"(DROP DATABASE IF EXISTS "{}")", m_db_name);
105+
conn.exec(R"(CREATE DATABASE "{}" WITH ENCODING 'UTF8')",
106+
m_db_name);
107107

108108
conn_t local = connect();
109109
local.exec("CREATE EXTENSION postgis");
@@ -136,7 +136,7 @@ class tempdb_t
136136
}
137137
try {
138138
conn_t conn{"dbname=postgres"};
139-
conn.exec("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
139+
conn.exec(R"(DROP DATABASE IF EXISTS "{}")", m_db_name);
140140
} catch (...) {
141141
fprintf(stderr, "DROP DATABASE failed. Ignored.\n");
142142
}

tests/test-db-copy-mgr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ static std::shared_ptr<db_target_descr_t> setup_table(std::string const &cols)
2424
{
2525
auto const conn = db.connect();
2626
conn.exec("DROP TABLE IF EXISTS test_copy_mgr");
27-
conn.exec("CREATE TABLE test_copy_mgr (id int8{}{})"_format(
28-
cols.empty() ? "" : ",", cols));
27+
conn.exec("CREATE TABLE test_copy_mgr (id int8{}{})",
28+
cols.empty() ? "" : ",", cols);
2929

3030
auto table = std::make_shared<db_target_descr_t>();
3131
table->name = "test_copy_mgr";

0 commit comments

Comments
 (0)