Skip to content

Commit 835b092

Browse files
authored
Merge pull request #1158 from joto/refactor-exec-prepared
Refactor exec_prepared
2 parents a329211 + cc3a355 commit 835b092

File tree

7 files changed

+61
-57
lines changed

7 files changed

+61
-57
lines changed

src/flex-table.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,10 @@ pg_result_t table_connection_t::get_geom_by_id(osmium::item_type type,
314314
assert(m_db_connection);
315315
std::string const id_str = fmt::to_string(id);
316316
if (table().has_multicolumn_id_index()) {
317-
char const *param_values[] = {type_to_char(type), id_str.c_str()};
318-
return m_db_connection->exec_prepared("get_wkb", 2, param_values);
317+
return m_db_connection->exec_prepared(
318+
"get_wkb", type_to_char(type), id_str.c_str());
319319
}
320-
char const *param_values[] = {id_str.c_str()};
321-
return m_db_connection->exec_prepared("get_wkb", 1, param_values);
320+
return m_db_connection->exec_prepared("get_wkb", id_str);
322321
}
323322

324323
void table_connection_t::delete_rows_with(osmium::item_type type, osmid_t id)

src/middle-pgsql.cpp

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,6 @@ middle_pgsql_t::table_desc::table_desc(options_t const &options,
5757
m_copy_target->id = "id"; // XXX hardcoded column name
5858
}
5959

60-
pg_result_t middle_query_pgsql_t::exec_prepared(char const *stmt,
61-
char const *param) const
62-
{
63-
return m_sql_conn.exec_prepared(stmt, 1, &param);
64-
}
65-
66-
pg_result_t middle_query_pgsql_t::exec_prepared(char const *stmt,
67-
osmid_t osm_id) const
68-
{
69-
util::integer_to_buffer buffer{osm_id};
70-
return exec_prepared(stmt, buffer.c_str());
71-
}
72-
73-
pg_result_t middle_pgsql_t::exec_prepared(char const *stmt,
74-
osmid_t osm_id) const
75-
{
76-
util::integer_to_buffer buffer{osm_id};
77-
char const *const bptr = buffer.c_str();
78-
return m_db_connection.exec_prepared(stmt, 1, &bptr);
79-
}
80-
8160
void middle_query_pgsql_t::exec_sql(std::string const &sql_cmd) const
8261
{
8362
m_sql_conn.exec(sql_cmd);
@@ -259,7 +238,7 @@ middle_query_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
259238
buffer[buffer.size() - 1] = '}';
260239

261240
// Nodes must have been written back at this point.
262-
auto const res = exec_prepared("get_node_list", buffer.c_str());
241+
auto const res = m_sql_conn.exec_prepared("get_node_list", buffer);
263242
std::unordered_map<osmid_t, osmium::Location> locs;
264243
for (int i = 0; i < res.num_tuples(); ++i) {
265244
locs.emplace(
@@ -321,15 +300,15 @@ void middle_pgsql_t::node_changed(osmid_t osm_id)
321300
}
322301

323302
// Find all ways referencing this node and mark them as pending.
324-
auto res = exec_prepared("mark_ways_by_node", osm_id);
303+
auto res = m_db_connection.exec_prepared("mark_ways_by_node", osm_id);
325304
for (int i = 0; i < res.num_tuples(); ++i) {
326305
osmid_t const marked = osmium::string_to_object_id(res.get_value(i, 0));
327306
way_changed(marked);
328307
m_ways_pending_tracker->mark(marked);
329308
}
330309

331310
// Find all relations referencing this node and mark them as pending.
332-
res = exec_prepared("mark_rels_by_node", osm_id);
311+
res = m_db_connection.exec_prepared("mark_rels_by_node", osm_id);
333312
for (int i = 0; i < res.num_tuples(); ++i) {
334313
osmid_t const marked = osmium::string_to_object_id(res.get_value(i, 0));
335314
m_rels_pending_tracker->mark(marked);
@@ -357,7 +336,7 @@ void middle_pgsql_t::way_set(osmium::Way const &way)
357336
bool middle_query_pgsql_t::way_get(osmid_t id,
358337
osmium::memory::Buffer &buffer) const
359338
{
360-
auto const res = exec_prepared("get_way", id);
339+
auto const res = m_sql_conn.exec_prepared("get_way", id);
361340

362341
if (res.num_tuples() != 1) {
363342
return false;
@@ -395,7 +374,7 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
395374
// replace last , with } to complete list of ids
396375
id_list.back() = '}';
397376

398-
auto const res = exec_prepared("get_way_list", id_list.c_str());
377+
auto const res = m_sql_conn.exec_prepared("get_way_list", id_list);
399378
idlist_t wayidspg;
400379
for (int i = 0; i < res.num_tuples(); ++i) {
401380
wayidspg.push_back(osmium::string_to_object_id(res.get_value(i, 0)));
@@ -459,7 +438,7 @@ void middle_pgsql_t::way_changed(osmid_t osm_id)
459438
}
460439

461440
//keep track of whatever rels this way intersects
462-
auto const res = exec_prepared("mark_rels_by_way", osm_id);
441+
auto const res = m_db_connection.exec_prepared("mark_rels_by_way", osm_id);
463442
for (int i = 0; i < res.num_tuples(); ++i) {
464443
osmid_t const marked = osmium::string_to_object_id(res.get_value(i, 0));
465444
m_rels_pending_tracker->mark(marked);
@@ -512,7 +491,7 @@ void middle_pgsql_t::relation_set(osmium::Relation const &rel)
512491
bool middle_query_pgsql_t::relation_get(osmid_t id,
513492
osmium::memory::Buffer &buffer) const
514493
{
515-
auto const res = exec_prepared("get_rel", id);
494+
auto const res = m_sql_conn.exec_prepared("get_rel", id);
516495
// Fields are: members, tags, member_count */
517496
//
518497
if (res.num_tuples() != 1) {
@@ -536,7 +515,7 @@ void middle_pgsql_t::relation_delete(osmid_t osm_id)
536515
{
537516
assert(m_append);
538517
//keep track of whatever ways this relation interesects
539-
auto const res = exec_prepared("mark_ways_by_rel", osm_id);
518+
auto const res = m_db_connection.exec_prepared("mark_ways_by_rel", osm_id);
540519
for (int i = 0; i < res.num_tuples(); ++i) {
541520
osmid_t const marked = osmium::string_to_object_id(res.get_value(i, 0));
542521
m_ways_pending_tracker->mark(marked);
@@ -564,7 +543,7 @@ void middle_pgsql_t::relation_changed(osmid_t osm_id)
564543
//keep track of whatever ways and rels these nodes intersect
565544
//TODO: dont need to stop the copy above since we are only reading?
566545
//TODO: can we just mark the id without querying? the where clause seems intersect reltable.parts with the id
567-
auto const res = exec_prepared("mark_rels", osm_id);
546+
auto const res = m_db_connection.exec_prepared("mark_rels", osm_id);
568547
for (int i = 0; i < res.num_tuples(); ++i) {
569548
osmid_t const marked = osmium::string_to_object_id(res.get_value(i, 0));
570549
m_rels_pending_tracker->mark(marked);
@@ -573,7 +552,7 @@ void middle_pgsql_t::relation_changed(osmid_t osm_id)
573552

574553
idlist_t middle_query_pgsql_t::relations_using_way(osmid_t way_id) const
575554
{
576-
auto const result = exec_prepared("rels_using_way", way_id);
555+
auto const result = m_sql_conn.exec_prepared("rels_using_way", way_id);
577556
int const ntuples = result.num_tuples();
578557
idlist_t rel_ids;
579558
rel_ids.resize((size_t)ntuples);

src/middle-pgsql.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ class middle_query_pgsql_t : public middle_query_t
4141
private:
4242
size_t local_nodes_get_list(osmium::WayNodeList *nodes) const;
4343

44-
pg_result_t exec_prepared(char const *stmt, char const *param) const;
45-
pg_result_t exec_prepared(char const *stmt, osmid_t osm_id) const;
46-
4744
pg_conn_t m_sql_conn;
4845
std::shared_ptr<node_ram_cache> m_cache;
4946
std::shared_ptr<node_persistent_cache> m_persistent_cache;
@@ -117,7 +114,6 @@ struct middle_pgsql_t : public slim_middle_t
117114
};
118115

119116
void buffer_store_tags(osmium::OSMObject const &obj, bool attrs);
120-
pg_result_t exec_prepared(char const *stmt, osmid_t osm_id) const;
121117

122118
table_desc m_tables[NUM_TABLES];
123119

src/pgsql.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* Helper functions for the postgresql connections */
22
#include "format.hpp"
33
#include "pgsql.hpp"
4+
#include "util.hpp"
45

6+
#include <array>
57
#include <cstdarg>
68
#include <cstdio>
79

@@ -93,17 +95,16 @@ void pg_conn_t::end_copy(std::string const &context) const
9395
}
9496
}
9597

96-
pg_result_t pg_conn_t::exec_prepared(char const *stmt, int num_params,
97-
char const *const *param_values,
98-
const ExecStatusType expect) const
98+
pg_result_t
99+
pg_conn_t::exec_prepared_internal(char const *stmt, int num_params,
100+
char const *const *param_values) const
99101
{
100102
#ifdef DEBUG_PGSQL
101103
fmt::print(stderr, "ExecPrepared: {}\n", stmt);
102104
#endif
103-
//run the prepared statement
104105
pg_result_t res{PQexecPrepared(m_conn.get(), stmt, num_params, param_values,
105106
nullptr, nullptr, 0)};
106-
if (PQresultStatus(res.get()) != expect) {
107+
if (PQresultStatus(res.get()) != PGRES_TUPLES_OK) {
107108
fmt::print(stderr, "Prepared statement failed with: {} ({})\n",
108109
error_msg(), PQresultStatus(res.get()));
109110
fmt::print(stderr, "Query: {}\n", stmt);
@@ -120,6 +121,29 @@ pg_result_t pg_conn_t::exec_prepared(char const *stmt, int num_params,
120121
return res;
121122
}
122123

124+
pg_result_t pg_conn_t::exec_prepared(char const *stmt, char const *p1, char const *p2) const
125+
{
126+
std::array<const char *, 2> params{{p1, p2}};
127+
return exec_prepared_internal(stmt, params.size(), params.data());
128+
}
129+
130+
pg_result_t pg_conn_t::exec_prepared(char const *stmt, char const *param) const
131+
{
132+
return exec_prepared_internal(stmt, 1, &param);
133+
}
134+
135+
pg_result_t pg_conn_t::exec_prepared(char const *stmt,
136+
std::string const &param) const
137+
{
138+
return exec_prepared(stmt, param.c_str());
139+
}
140+
141+
pg_result_t pg_conn_t::exec_prepared(char const *stmt, osmid_t id) const
142+
{
143+
util::integer_to_buffer buffer{id};
144+
return exec_prepared(stmt, buffer.c_str());
145+
}
146+
123147
std::string tablespace_clause(std::string const &name)
124148
{
125149
std::string sql;

src/pgsql.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
/* Helper functions for PostgreSQL access */
55

6+
#include "osmtypes.hpp"
7+
68
#include <libpq-fe.h>
79

810
#include <cassert>
@@ -99,9 +101,17 @@ class pg_conn_t
99101
public:
100102
explicit pg_conn_t(std::string const &conninfo);
101103

102-
pg_result_t exec_prepared(char const *stmt, int num_params,
103-
char const *const *param_values,
104-
ExecStatusType expect = PGRES_TUPLES_OK) const;
104+
/// Execute a prepared statement with one parameter.
105+
pg_result_t exec_prepared(char const *stmt, char const *param) const;
106+
107+
/// Execute a prepared statement with two parameters.
108+
pg_result_t exec_prepared(char const *stmt, char const *p1, char const *p2) const;
109+
110+
/// Execute a prepared statement with one string parameter.
111+
pg_result_t exec_prepared(char const *stmt, std::string const &param) const;
112+
113+
/// Execute a prepared statement with one integer parameter.
114+
pg_result_t exec_prepared(char const *stmt, osmid_t id) const;
105115

106116
pg_result_t query(ExecStatusType expect, char const *sql) const;
107117

@@ -121,6 +131,9 @@ class pg_conn_t
121131
void close() noexcept { m_conn.reset(); }
122132

123133
private:
134+
pg_result_t exec_prepared_internal(char const *stmt, int num_params,
135+
char const *const *param_values) const;
136+
124137
struct pg_conn_deleter_t
125138
{
126139
void operator()(PGconn *p) const noexcept { PQfinish(p); }

src/table.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,8 @@ void table_t::escape_type(std::string const &value, ColumnType flags)
456456
}
457457
}
458458

459-
table_t::wkb_reader table_t::get_wkb_reader(osmid_t const id)
459+
table_t::wkb_reader table_t::get_wkb_reader(osmid_t id)
460460
{
461-
util::integer_to_buffer tmp{id};
462-
char const *param_values[] = {tmp.c_str()};
463-
464-
// the prepared statement get_wkb will behave differently depending on the
465-
// sql_conn
466-
// each table has its own sql_connection with the get_way referring to the
467-
// appropriate table
468-
auto res = m_sql_conn->exec_prepared("get_wkb", 1, param_values);
461+
auto res = m_sql_conn->exec_prepared("get_wkb", id);
469462
return wkb_reader{std::move(res)};
470463
}

src/table.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class table_t
5959
int m_count;
6060
int m_current;
6161
};
62-
wkb_reader get_wkb_reader(osmid_t const id);
62+
wkb_reader get_wkb_reader(osmid_t id);
6363

6464
protected:
6565
void connect();

0 commit comments

Comments
 (0)