Skip to content

Commit 82ebd7a

Browse files
committed
Move all exec_prepared() helper functions into pg_conn_t class
1 parent 0372425 commit 82ebd7a

File tree

7 files changed

+40
-47
lines changed

7 files changed

+40
-47
lines changed

src/flex-table.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ pg_result_t table_connection_t::get_geom_by_id(osmium::item_type type,
312312
char const *param_values[] = {type_to_char(type), id_str.c_str()};
313313
return m_db_connection->exec_prepared("get_wkb", 2, param_values);
314314
}
315-
char const *param_values[] = {id_str.c_str()};
316-
return m_db_connection->exec_prepared("get_wkb", 1, param_values);
315+
return m_db_connection->exec_prepared("get_wkb", id_str);
317316
}
318317

319318
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Helper functions for the postgresql connections */
22
#include "format.hpp"
33
#include "pgsql.hpp"
4+
#include "util.hpp"
45

56
#include <cstdarg>
67
#include <cstdio>
@@ -120,6 +121,23 @@ 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 *param) const
125+
{
126+
return exec_prepared(stmt, 1, &param);
127+
}
128+
129+
pg_result_t pg_conn_t::exec_prepared(char const *stmt,
130+
std::string const &param) const
131+
{
132+
return exec_prepared(stmt, param.c_str());
133+
}
134+
135+
pg_result_t pg_conn_t::exec_prepared(char const *stmt, osmid_t id) const
136+
{
137+
util::integer_to_buffer buffer{id};
138+
return exec_prepared(stmt, buffer.c_str());
139+
}
140+
123141
std::string tablespace_clause(std::string const &name)
124142
{
125143
std::string sql;

src/pgsql.hpp

Lines changed: 8 additions & 0 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>
@@ -103,6 +105,12 @@ class pg_conn_t
103105
char const *const *param_values,
104106
ExecStatusType expect = PGRES_TUPLES_OK) const;
105107

108+
pg_result_t exec_prepared(char const *stmt, char const *param) const;
109+
110+
pg_result_t exec_prepared(char const *stmt, std::string const &param) const;
111+
112+
pg_result_t exec_prepared(char const *stmt, osmid_t id) const;
113+
106114
pg_result_t query(ExecStatusType expect, char const *sql) const;
107115

108116
pg_result_t query(ExecStatusType expect, std::string const &sql) const;

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)