Skip to content

Commit 43d0c56

Browse files
committed
middle-pgsql: add wrapper for simple prepared queries
1 parent 7db43b2 commit 43d0c56

File tree

2 files changed

+39
-76
lines changed

2 files changed

+39
-76
lines changed

middle-pgsql.cpp

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ using namespace std;
3838
#include "options.hpp"
3939
#include "osmtypes.hpp"
4040
#include "output-pgsql.hpp"
41-
#include "pgsql.hpp"
4241
#include "util.hpp"
4342

4443
/**
@@ -219,6 +218,13 @@ void middle_pgsql_t::table_desc::end_copy()
219218
}
220219
}
221220

221+
pg_result_t
222+
middle_pgsql_t::table_desc::exec_prepared(char const *stmt, char const *param,
223+
ExecStatusType expect) const
224+
{
225+
return pgsql_execPrepared(sql_conn, stmt, 1, &param, expect);
226+
}
227+
222228
void middle_pgsql_t::table_desc::stop(bool droptemp, bool build_indexes)
223229
{
224230
time_t start, end;
@@ -489,12 +495,8 @@ size_t middle_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
489495
// Nodes must have been written back at this point.
490496
assert(tables[NODE_TABLE].copyMode == 0);
491497

492-
PGconn *sql_conn = tables[NODE_TABLE].sql_conn;
493-
494-
char const *paramValues[1];
495-
paramValues[0] = buffer.c_str();
496-
auto res = pgsql_execPrepared(sql_conn, "get_node_list", 1, paramValues,
497-
PGRES_TUPLES_OK);
498+
auto res =
499+
tables[NODE_TABLE].exec_prepared("get_node_list", buffer.c_str());
498500
auto countPG = PQntuples(res.get());
499501

500502
std::unordered_map<osmid_t, osmium::Location> locs;
@@ -538,15 +540,12 @@ size_t middle_pgsql_t::nodes_get_list(osmium::WayNodeList *nodes) const
538540

539541
void middle_pgsql_t::local_nodes_delete(osmid_t osm_id)
540542
{
541-
char const *paramValues[1];
542-
char buffer[64];
543543
// Make sure we're out of copy mode */
544544
tables[NODE_TABLE].end_copy();
545545

546-
sprintf( buffer, "%" PRIdOSMID, osm_id );
547-
paramValues[0] = buffer;
548-
pgsql_execPrepared(tables[NODE_TABLE].sql_conn, "delete_node", 1,
549-
paramValues, PGRES_COMMAND_OK);
546+
char buffer[64];
547+
sprintf(buffer, "%" PRIdOSMID, osm_id);
548+
tables[NODE_TABLE].exec_prepared("delete_node", buffer, PGRES_COMMAND_OK);
550549
}
551550

552551
void middle_pgsql_t::nodes_delete(osmid_t osm_id)
@@ -564,29 +563,24 @@ void middle_pgsql_t::node_changed(osmid_t osm_id)
564563
return;
565564
}
566565

567-
char const *paramValues[1];
568-
char buffer[64];
569566
// Make sure we're out of copy mode */
570567
tables[WAY_TABLE].end_copy();
571568
tables[REL_TABLE].end_copy();
572569

570+
char buffer[64];
573571
sprintf( buffer, "%" PRIdOSMID, osm_id );
574-
paramValues[0] = buffer;
575572

576573
//keep track of whatever ways and rels these nodes intersect
577574
//TODO: dont need to stop the copy above since we are only reading?
578-
auto res =
579-
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "mark_ways_by_node", 1,
580-
paramValues, PGRES_TUPLES_OK);
575+
auto res = tables[WAY_TABLE].exec_prepared("mark_ways_by_node", buffer);
581576
for (int i = 0; i < PQntuples(res.get()); ++i) {
582577
char *end;
583578
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
584579
ways_pending_tracker->mark(marked);
585580
}
586581

587582
//do the rels too
588-
res = pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels_by_node", 1,
589-
paramValues, PGRES_TUPLES_OK);
583+
res = tables[REL_TABLE].exec_prepared("mark_rels_by_node", buffer);
590584
for (int i = 0; i < PQntuples(res.get()); ++i) {
591585
char *end;
592586
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -638,19 +632,13 @@ void middle_pgsql_t::ways_set(osmium::Way const &way)
638632

639633
bool middle_pgsql_t::ways_get(osmid_t id, osmium::memory::Buffer &buffer) const
640634
{
641-
char const *paramValues[1];
642-
PGconn *sql_conn = tables[WAY_TABLE].sql_conn;
643-
644635
// Make sure we're out of copy mode
645636
assert(tables[WAY_TABLE].copyMode == 0);
646637

647638
char tmp[16];
648639
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
649-
paramValues[0] = tmp;
650-
651-
auto res = pgsql_execPrepared(sql_conn, "get_way", 1, paramValues,
652-
PGRES_TUPLES_OK);
653640

641+
auto res = tables[WAY_TABLE].exec_prepared("get_way", tmp);
654642
if (PQntuples(res.get()) != 1) {
655643
return false;
656644
}
@@ -673,7 +661,6 @@ size_t middle_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
673661
osmium::memory::Buffer &buffer) const
674662
{
675663
char tmp[16];
676-
char const *paramValues[1];
677664

678665
// create a list of ids in tmp2 to query the database
679666
std::string tmp2("{");
@@ -693,11 +680,7 @@ size_t middle_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
693680
// Make sures all ways have been written back.
694681
assert(tables[WAY_TABLE].copyMode == 0);
695682

696-
PGconn *sql_conn = tables[WAY_TABLE].sql_conn;
697-
698-
paramValues[0] = tmp2.c_str();
699-
auto res = pgsql_execPrepared(sql_conn, "get_way_list", 1, paramValues,
700-
PGRES_TUPLES_OK);
683+
auto res = tables[WAY_TABLE].exec_prepared("get_way_list", tmp2.c_str());
701684
int countPG = PQntuples(res.get());
702685

703686
idlist_t wayidspg;
@@ -742,15 +725,12 @@ size_t middle_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
742725

743726
void middle_pgsql_t::ways_delete(osmid_t osm_id)
744727
{
745-
char const *paramValues[1];
746-
char buffer[64];
747728
// Make sure we're out of copy mode */
748729
tables[WAY_TABLE].end_copy();
749730

731+
char buffer[64];
750732
sprintf( buffer, "%" PRIdOSMID, osm_id );
751-
paramValues[0] = buffer;
752-
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "delete_way", 1, paramValues,
753-
PGRES_COMMAND_OK);
733+
tables[WAY_TABLE].exec_prepared("delete_way", buffer, PGRES_COMMAND_OK);
754734
}
755735

756736
void middle_pgsql_t::iterate_ways(middle_t::pending_processor& pf)
@@ -774,19 +754,15 @@ void middle_pgsql_t::iterate_ways(middle_t::pending_processor& pf)
774754

775755
void middle_pgsql_t::way_changed(osmid_t osm_id)
776756
{
777-
char const *paramValues[1];
778-
char buffer[64];
779757
// Make sure we're out of copy mode */
780758
tables[REL_TABLE].end_copy();
781759

782-
sprintf( buffer, "%" PRIdOSMID, osm_id );
783-
paramValues[0] = buffer;
760+
char buffer[64];
761+
sprintf(buffer, "%" PRIdOSMID, osm_id);
784762

785763
//keep track of whatever rels this way intersects
786764
//TODO: dont need to stop the copy above since we are only reading?
787-
auto res =
788-
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels_by_way", 1,
789-
paramValues, PGRES_TUPLES_OK);
765+
auto res = tables[REL_TABLE].exec_prepared("mark_rels_by_way", buffer);
790766
for (int i = 0; i < PQntuples(res.get()); ++i) {
791767
char *end;
792768
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -873,19 +849,13 @@ void middle_pgsql_t::relations_set(osmium::Relation const &rel)
873849

874850
bool middle_pgsql_t::relations_get(osmid_t id, osmium::memory::Buffer &buffer) const
875851
{
876-
char tmp[16];
877-
char const *paramValues[1];
878-
PGconn *sql_conn = tables[REL_TABLE].sql_conn;
879-
taglist_t member_temp;
880-
881852
// Make sure we're out of copy mode
882853
assert(tables[REL_TABLE].copyMode == 0);
883854

855+
char tmp[16];
884856
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
885-
paramValues[0] = tmp;
886857

887-
auto res = pgsql_execPrepared(sql_conn, "get_rel", 1, paramValues,
888-
PGRES_TUPLES_OK);
858+
auto res = tables[REL_TABLE].exec_prepared("get_rel", tmp);
889859
// Fields are: members, tags, member_count */
890860

891861
if (PQntuples(res.get()) != 1) {
@@ -907,22 +877,17 @@ bool middle_pgsql_t::relations_get(osmid_t id, osmium::memory::Buffer &buffer) c
907877

908878
void middle_pgsql_t::relations_delete(osmid_t osm_id)
909879
{
910-
char const *paramValues[1];
911-
char buffer[64];
912880
// Make sure we're out of copy mode */
913881
tables[WAY_TABLE].end_copy();
914882
tables[REL_TABLE].end_copy();
915883

884+
char buffer[64];
916885
sprintf( buffer, "%" PRIdOSMID, osm_id );
917-
paramValues[0] = buffer;
918-
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "delete_rel", 1, paramValues,
919-
PGRES_COMMAND_OK);
886+
tables[REL_TABLE].exec_prepared("delete_rel", buffer, PGRES_COMMAND_OK);
920887

921888
//keep track of whatever ways this relation interesects
922889
//TODO: dont need to stop the copy above since we are only reading?
923-
auto res =
924-
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "mark_ways_by_rel", 1,
925-
paramValues, PGRES_TUPLES_OK);
890+
auto res = tables[WAY_TABLE].exec_prepared("mark_ways_by_rel", buffer);
926891
for (int i = 0; i < PQntuples(res.get()); ++i) {
927892
char *end;
928893
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -950,19 +915,16 @@ void middle_pgsql_t::iterate_relations(pending_processor& pf)
950915

951916
void middle_pgsql_t::relation_changed(osmid_t osm_id)
952917
{
953-
char const *paramValues[1];
954-
char buffer[64];
955918
// Make sure we're out of copy mode */
956919
tables[REL_TABLE].end_copy();
957920

921+
char buffer[64];
958922
sprintf( buffer, "%" PRIdOSMID, osm_id );
959-
paramValues[0] = buffer;
960923

961924
//keep track of whatever ways and rels these nodes intersect
962925
//TODO: dont need to stop the copy above since we are only reading?
963926
//TODO: can we just mark the id without querying? the where clause seems intersect reltable.parts with the id
964-
auto res = pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels", 1,
965-
paramValues, PGRES_TUPLES_OK);
927+
auto res = tables[REL_TABLE].exec_prepared("mark_rels", buffer);
966928
for (int i = 0; i < PQntuples(res.get()); ++i) {
967929
char *end;
968930
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -972,17 +934,13 @@ void middle_pgsql_t::relation_changed(osmid_t osm_id)
972934

973935
idlist_t middle_pgsql_t::relations_using_way(osmid_t way_id) const
974936
{
975-
char const *paramValues[1];
976-
char buffer[64];
977937
// Make sure we're out of copy mode */
978938
assert(tables[REL_TABLE].copyMode == 0);
979939

940+
char buffer[64];
980941
sprintf(buffer, "%" PRIdOSMID, way_id);
981-
paramValues[0] = buffer;
982942

983-
auto result =
984-
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "rels_using_way", 1,
985-
paramValues, PGRES_TUPLES_OK);
943+
auto result = tables[REL_TABLE].exec_prepared("rels_using_way", buffer);
986944
const int ntuples = PQntuples(result.get());
987945
idlist_t rel_ids;
988946
rel_ids.resize((size_t) ntuples);

middle-pgsql.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#ifndef MIDDLE_PGSQL_H
1010
#define MIDDLE_PGSQL_H
1111

12+
#include <memory>
13+
14+
#include "id-tracker.hpp"
1215
#include "middle.hpp"
13-
#include "node-ram-cache.hpp"
1416
#include "node-persistent-cache.hpp"
15-
#include "id-tracker.hpp"
16-
#include <memory>
17+
#include "node-ram-cache.hpp"
18+
#include "pgsql.hpp"
1719

1820
struct middle_pgsql_t : public slim_middle_t {
1921
middle_pgsql_t();
@@ -72,6 +74,9 @@ struct middle_pgsql_t : public slim_middle_t {
7274
void create();
7375
void begin_copy();
7476
void end_copy();
77+
pg_result_t
78+
exec_prepared(char const *stmt, char const *param,
79+
ExecStatusType expect = PGRES_TUPLES_OK) const;
7580
void stop(bool droptemp, bool build_indexes);
7681
void commit();
7782

0 commit comments

Comments
 (0)