Skip to content

Commit 020fac9

Browse files
committed
Refactor: Use template rendering and explicit prepare function
for preparing queries in the middle
1 parent 10fb133 commit 020fac9

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

src/middle-pgsql.cpp

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ std::vector<std::string> build_sql(options_t const &options,
130130

131131
middle_pgsql_t::table_desc::table_desc(options_t const &options,
132132
table_sql const &ts)
133-
: m_prepare_queries(build_sql(options, ts.prepare_queries)),
134-
m_copy_target(std::make_shared<db_target_descr_t>(
133+
: m_copy_target(std::make_shared<db_target_descr_t>(
135134
options.middle_dbschema, build_sql(options, ts.name), "id"))
136135
{
137136
m_create_fw_dep_indexes = build_sql(options, ts.create_fw_dep_indexes);
@@ -149,9 +148,10 @@ void middle_pgsql_t::dbexec(std::string_view templ) const
149148
m_db_connection.exec(render_template(templ));
150149
}
151150

152-
void middle_query_pgsql_t::exec_sql(std::string const &sql_cmd) const
151+
void middle_query_pgsql_t::prepare(std::string_view stmt,
152+
std::string const &sql_cmd) const
153153
{
154-
m_db_connection.exec(sql_cmd);
154+
m_db_connection.prepare(stmt, fmt::runtime(sql_cmd));
155155
}
156156

157157
void middle_pgsql_t::table_desc::drop_table(
@@ -1203,16 +1203,6 @@ table_sql sql_for_nodes(middle_pgsql_options const &options)
12031203

12041204
sql.name = "{prefix}_nodes";
12051205

1206-
if (options.nodes) {
1207-
sql.prepare_queries = {
1208-
"PREPARE get_node_list(int8[]) AS"
1209-
" SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
1210-
" WHERE id = ANY($1::int8[])",
1211-
"PREPARE get_node(int8) AS"
1212-
" SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
1213-
" WHERE id = $1"};
1214-
}
1215-
12161206
return sql;
12171207
}
12181208

@@ -1222,17 +1212,6 @@ table_sql sql_for_ways()
12221212

12231213
sql.name = "{prefix}_ways";
12241214

1225-
sql.prepare_queries = {"PREPARE get_way(int8) AS"
1226-
" SELECT nodes, tags{attribute_columns_use}"
1227-
" FROM {schema}\"{prefix}_ways\" o"
1228-
" {users_table_access}"
1229-
" WHERE o.id = $1",
1230-
"PREPARE get_way_list(int8[]) AS"
1231-
" SELECT o.id, nodes, tags{attribute_columns_use}"
1232-
" FROM {schema}\"{prefix}_ways\" o"
1233-
" {users_table_access}"
1234-
" WHERE o.id = ANY($1::int8[])"};
1235-
12361215
sql.create_fw_dep_indexes = {
12371216
"CREATE OR REPLACE FUNCTION"
12381217
" {schema}\"{prefix}_index_bucket\"(int8[])"
@@ -1254,12 +1233,6 @@ table_sql sql_for_relations()
12541233

12551234
sql.name = "{prefix}_rels";
12561235

1257-
sql.prepare_queries = {"PREPARE get_rel(int8) AS"
1258-
" SELECT members, tags{attribute_columns_use}"
1259-
" FROM {schema}\"{prefix}_rels\" o"
1260-
" {users_table_access}"
1261-
" WHERE o.id = $1"};
1262-
12631236
sql.create_fw_dep_indexes = {
12641237
"CREATE OR REPLACE FUNCTION"
12651238
" {schema}\"{prefix}_member_ids\"(jsonb, char)"
@@ -1373,12 +1346,36 @@ middle_pgsql_t::get_query_instance()
13731346
m_options->connection_params, m_cache, m_persistent_cache,
13741347
m_store_options);
13751348

1376-
// We use a connection per table to enable the use of COPY
1377-
for (auto &table : m_tables) {
1378-
for (auto const &query : table.prepare_queries()) {
1379-
mid->exec_sql(query);
1380-
}
1381-
}
1349+
if (m_store_options.nodes) {
1350+
mid->prepare("get_node",
1351+
render_template(
1352+
"SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
1353+
" WHERE id = $1::int8"));
1354+
1355+
mid->prepare("get_node_list",
1356+
render_template(
1357+
"SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
1358+
" WHERE id = ANY($1::int8[])"));
1359+
}
1360+
1361+
mid->prepare("get_way",
1362+
render_template("SELECT nodes, tags{attribute_columns_use}"
1363+
" FROM {schema}\"{prefix}_ways\" o"
1364+
" {users_table_access}"
1365+
" WHERE o.id = $1::int8"));
1366+
1367+
mid->prepare(
1368+
"get_way_list",
1369+
render_template("SELECT o.id, nodes, tags{attribute_columns_use}"
1370+
" FROM {schema}\"{prefix}_ways\" o"
1371+
" {users_table_access}"
1372+
" WHERE o.id = ANY($1::int8[])"));
1373+
1374+
mid->prepare("get_rel",
1375+
render_template("SELECT members, tags{attribute_columns_use}"
1376+
" FROM {schema}\"{prefix}_rels\" o"
1377+
" {users_table_access}"
1378+
" WHERE o.id = $1::int8"));
13821379

13831380
return std::shared_ptr<middle_query_t>(mid.release());
13841381
}

src/middle-pgsql.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class middle_query_pgsql_t : public middle_query_t
7171
bool relation_get(osmid_t id,
7272
osmium::memory::Buffer *buffer) const override;
7373

74-
void exec_sql(std::string const &sql_cmd) const;
74+
void prepare(std::string_view stmt, std::string const &sql_cmd) const;
7575

7676
private:
7777
osmium::Location get_node_location_flatnodes(osmid_t id) const;
@@ -89,7 +89,6 @@ class middle_query_pgsql_t : public middle_query_t
8989
struct table_sql
9090
{
9191
std::string name;
92-
std::vector<std::string> prepare_queries;
9392
std::vector<std::string> create_fw_dep_indexes;
9493
};
9594

@@ -155,14 +154,8 @@ struct middle_pgsql_t : public middle_t
155154

156155
osmid_t max_id() const noexcept { return m_max_id; }
157156

158-
std::vector<std::string> const &prepare_queries() const noexcept
159-
{
160-
return m_prepare_queries;
161-
}
162-
163157
private:
164158
std::vector<std::string> m_create_fw_dep_indexes;
165-
std::vector<std::string> m_prepare_queries;
166159
std::shared_ptr<db_target_descr_t> m_copy_target;
167160
task_result_t m_task_result;
168161

0 commit comments

Comments
 (0)