Skip to content

Commit 35a266c

Browse files
authored
Merge pull request #1025 from joto/refactor-tests-pg-code
Refactor tests pg code
2 parents 3b22178 + 695337a commit 35a266c

5 files changed

Lines changed: 132 additions & 134 deletions

File tree

src/middle-pgsql.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,16 @@ middle_query_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
345345
buffer[buffer.size() - 1] = '}';
346346

347347
// Nodes must have been written back at this point.
348-
auto res = exec_prepared("get_node_list", buffer.c_str());
349-
auto countPG = PQntuples(res.get());
348+
auto const res = exec_prepared("get_node_list", buffer.c_str());
349+
auto const countPG = res.num_tuples();
350350

351351
std::unordered_map<osmid_t, osmium::Location> locs;
352352
for (int i = 0; i < countPG; ++i) {
353353
locs.emplace(
354-
strtoosmid(PQgetvalue(res.get(), i, 0), nullptr, 10),
354+
strtoosmid(res.get_value(i, 0), nullptr, 10),
355355
osmium::Location(
356-
(int)strtol(PQgetvalue(res.get(), i, 2), nullptr, 10),
357-
(int)strtol(PQgetvalue(res.get(), i, 1), nullptr, 10)));
356+
(int)strtol(res.get_value(i, 2), nullptr, 10),
357+
(int)strtol(res.get_value(i, 1), nullptr, 10)));
358358
}
359359

360360
for (auto &n : *nodes) {
@@ -408,17 +408,17 @@ void middle_pgsql_t::node_changed(osmid_t osm_id)
408408

409409
//keep track of whatever ways and rels these nodes intersect
410410
auto res = exec_prepared("mark_ways_by_node", osm_id);
411-
for (int i = 0; i < PQntuples(res.get()); ++i) {
411+
for (int i = 0; i < res.num_tuples(); ++i) {
412412
char *end;
413-
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
413+
osmid_t marked = strtoosmid(res.get_value(i, 0), &end, 10);
414414
ways_pending_tracker->mark(marked);
415415
}
416416

417417
//do the rels too
418418
res = exec_prepared("mark_rels_by_node", osm_id);
419-
for (int i = 0; i < PQntuples(res.get()); ++i) {
419+
for (int i = 0; i < res.num_tuples(); ++i) {
420420
char *end;
421-
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
421+
osmid_t marked = strtoosmid(res.get_value(i, 0), &end, 10);
422422
rels_pending_tracker->mark(marked);
423423
}
424424
}
@@ -444,18 +444,18 @@ void middle_pgsql_t::ways_set(osmium::Way const &way)
444444
bool middle_query_pgsql_t::ways_get(osmid_t id,
445445
osmium::memory::Buffer &buffer) const
446446
{
447-
auto res = exec_prepared("get_way", id);
447+
auto const res = exec_prepared("get_way", id);
448448

449-
if (PQntuples(res.get()) != 1) {
449+
if (res.num_tuples() != 1) {
450450
return false;
451451
}
452452

453453
{
454454
osmium::builder::WayBuilder builder(buffer);
455455
builder.set_id(id);
456456

457-
pgsql_parse_nodes(PQgetvalue(res.get(), 0, 0), buffer, builder);
458-
pgsql_parse_tags(PQgetvalue(res.get(), 0, 1), buffer, builder);
457+
pgsql_parse_nodes(res.get_value(0, 0), buffer, builder);
458+
pgsql_parse_tags(res.get_value(0, 1), buffer, builder);
459459
}
460460

461461
buffer.commit();
@@ -486,14 +486,14 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
486486
tmp2[tmp2.length() - 1] = '}';
487487

488488
// Make sures all ways have been written back.
489-
auto res = exec_prepared("get_way_list", tmp2.c_str());
490-
int countPG = PQntuples(res.get());
489+
auto const res = exec_prepared("get_way_list", tmp2.c_str());
490+
int const countPG = res.num_tuples();
491491

492492
idlist_t wayidspg;
493493

494494
for (int i = 0; i < countPG; i++) {
495495
wayidspg.push_back(
496-
strtoosmid(PQgetvalue(res.get(), i, 0), nullptr, 10));
496+
strtoosmid(res.get_value(i, 0), nullptr, 10));
497497
}
498498

499499
// Match the list of ways coming from postgres in a different order
@@ -509,9 +509,9 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
509509
osmium::builder::WayBuilder builder(buffer);
510510
builder.set_id(m.ref());
511511

512-
pgsql_parse_nodes(PQgetvalue(res.get(), j, 1), buffer,
512+
pgsql_parse_nodes(res.get_value(j, 1), buffer,
513513
builder);
514-
pgsql_parse_tags(PQgetvalue(res.get(), j, 2), buffer,
514+
pgsql_parse_tags(res.get_value(j, 2), buffer,
515515
builder);
516516
}
517517

@@ -551,10 +551,10 @@ void middle_pgsql_t::iterate_ways(middle_t::pending_processor &pf)
551551
void middle_pgsql_t::way_changed(osmid_t osm_id)
552552
{
553553
//keep track of whatever rels this way intersects
554-
auto res = exec_prepared("mark_rels_by_way", osm_id);
555-
for (int i = 0; i < PQntuples(res.get()); ++i) {
554+
auto const res = exec_prepared("mark_rels_by_way", osm_id);
555+
for (int i = 0; i < res.num_tuples(); ++i) {
556556
char *end;
557-
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
557+
osmid_t marked = strtoosmid(res.get_value(i, 0), &end, 10);
558558
rels_pending_tracker->mark(marked);
559559
}
560560
}
@@ -605,19 +605,19 @@ void middle_pgsql_t::relations_set(osmium::Relation const &rel)
605605
bool middle_query_pgsql_t::relations_get(osmid_t id,
606606
osmium::memory::Buffer &buffer) const
607607
{
608-
auto res = exec_prepared("get_rel", id);
608+
auto const res = exec_prepared("get_rel", id);
609609
// Fields are: members, tags, member_count */
610610
//
611-
if (PQntuples(res.get()) != 1) {
611+
if (res.num_tuples() != 1) {
612612
return false;
613613
}
614614

615615
{
616616
osmium::builder::RelationBuilder builder(buffer);
617617
builder.set_id(id);
618618

619-
pgsql_parse_members(PQgetvalue(res.get(), 0, 0), buffer, builder);
620-
pgsql_parse_tags(PQgetvalue(res.get(), 0, 1), buffer, builder);
619+
pgsql_parse_members(res.get_value(0, 0), buffer, builder);
620+
pgsql_parse_tags(res.get_value(0, 1), buffer, builder);
621621
}
622622

623623
buffer.commit();
@@ -629,9 +629,9 @@ void middle_pgsql_t::relations_delete(osmid_t osm_id)
629629
{
630630
//keep track of whatever ways this relation interesects
631631
auto const res = exec_prepared("mark_ways_by_rel", osm_id);
632-
for (int i = 0; i < PQntuples(res.get()); ++i) {
632+
for (int i = 0; i < res.num_tuples(); ++i) {
633633
char *end;
634-
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
634+
osmid_t marked = strtoosmid(res.get_value(i, 0), &end, 10);
635635
ways_pending_tracker->mark(marked);
636636
}
637637

@@ -658,22 +658,22 @@ void middle_pgsql_t::relation_changed(osmid_t osm_id)
658658
//keep track of whatever ways and rels these nodes intersect
659659
//TODO: dont need to stop the copy above since we are only reading?
660660
//TODO: can we just mark the id without querying? the where clause seems intersect reltable.parts with the id
661-
auto res = exec_prepared("mark_rels", osm_id);
662-
for (int i = 0; i < PQntuples(res.get()); ++i) {
661+
auto const res = exec_prepared("mark_rels", osm_id);
662+
for (int i = 0; i < res.num_tuples(); ++i) {
663663
char *end;
664-
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
664+
osmid_t marked = strtoosmid(res.get_value(i, 0), &end, 10);
665665
rels_pending_tracker->mark(marked);
666666
}
667667
}
668668

669669
idlist_t middle_query_pgsql_t::relations_using_way(osmid_t way_id) const
670670
{
671-
auto result = exec_prepared("rels_using_way", way_id);
672-
const int ntuples = PQntuples(result.get());
671+
auto const result = exec_prepared("rels_using_way", way_id);
672+
int const ntuples = result.num_tuples();
673673
idlist_t rel_ids;
674674
rel_ids.resize((size_t)ntuples);
675675
for (int i = 0; i < ntuples; ++i) {
676-
rel_ids[i] = strtoosmid(PQgetvalue(result.get(), i, 0), nullptr, 10);
676+
rel_ids[i] = strtoosmid(result.get_value(i, 0), nullptr, 10);
677677
}
678678

679679
return rel_ids;

src/pgsql.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <cstdarg>
55
#include <cstdio>
66

7-
pg_conn_t::pg_conn_t(std::string const &connection)
8-
: m_conn(PQconnectdb(connection.c_str()))
7+
pg_conn_t::pg_conn_t(std::string const &conninfo)
8+
: m_conn(PQconnectdb(conninfo.c_str()))
99
{
1010
if (PQstatus(m_conn.get()) != CONNECTION_OK) {
1111
fprintf(stderr, "Connection to database failed: %s\n", error_msg());

src/pgsql.hpp

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,93 @@
55

66
#include <libpq-fe.h>
77

8+
#include <cassert>
89
#include <memory>
910
#include <string>
1011

11-
struct pg_result_deleter_t
12+
/**
13+
* PostgreSQL query result.
14+
*
15+
* Wraps the PGresult object of the libpq library.
16+
*/
17+
class pg_result_t
1218
{
13-
void operator()(PGresult *p) const noexcept { PQclear(p); }
14-
};
19+
public:
20+
explicit pg_result_t(PGresult *result) noexcept : m_result(result) {}
21+
22+
/// Get a pointer to the underlying PGresult object.
23+
PGresult *get() const noexcept { return m_result.get(); }
24+
25+
/// Get the status of this result.
26+
ExecStatusType status() const noexcept
27+
{
28+
return PQresultStatus(m_result.get());
29+
}
30+
31+
/// The number of fields (columns) in this result.
32+
int num_fields() const noexcept { return PQnfields(m_result.get()); }
33+
34+
/// The number of tuples (rows) in this result.
35+
int num_tuples() const noexcept { return PQntuples(m_result.get()); }
36+
37+
/// Does the field at (row, col) has the NULL value?
38+
bool is_null(int row, int col) const noexcept
39+
{
40+
assert(row < num_tuples() && col < num_fields());
41+
return PQgetisnull(m_result.get(), row, col) != 0;
42+
}
43+
44+
/// The length of the field at (row, col) in bytes.
45+
int get_length(int row, int col) const noexcept
46+
{
47+
assert(row < num_tuples() && col < num_fields());
48+
return PQgetlength(m_result.get(), row, col);
49+
}
50+
51+
/**
52+
* Get value of the field at (row, col) as char pointer. The string is
53+
* null-terminated. Only valid as long as the pg_result_t is in scope.
54+
*/
55+
char const *get_value(int row, int col) const noexcept
56+
{
57+
assert(row < num_tuples() && col < num_fields());
58+
return PQgetvalue(m_result.get(), row, col);
59+
}
60+
61+
/**
62+
* Create a std::string with the value of the field at (row, col). This
63+
* does the correct thing for binary data.
64+
*/
65+
std::string get_value_as_string(int row, int col) const noexcept
66+
{
67+
return std::string(get_value(row, col), get_length(row, col));
68+
}
1569

16-
using pg_result_t = std::unique_ptr<PGresult, pg_result_deleter_t>;
70+
private:
71+
struct pg_result_deleter_t
72+
{
73+
void operator()(PGresult *p) const noexcept { PQclear(p); }
74+
};
1775

18-
struct pg_conn_deleter_t
19-
{
20-
void operator()(PGconn *p) const noexcept { PQfinish(p); }
76+
std::unique_ptr<PGresult, pg_result_deleter_t> m_result;
2177
};
2278

23-
using pg_conn_wrapper_t = std::unique_ptr<PGconn, pg_conn_deleter_t>;
24-
2579
/**
26-
* Simple postgres connection.
80+
* PostgreSQL connection.
81+
*
82+
* Wraps the PGconn object of the libpq library.
2783
*
2884
* The connection is automatically closed when the object is destroyed.
2985
*/
3086
class pg_conn_t
3187
{
3288
public:
33-
pg_conn_t(std::string const &connection);
89+
explicit pg_conn_t(std::string const &conninfo);
3490

3591
pg_result_t exec_prepared(char const *stmt, int num_params,
3692
const char *const *param_values,
3793
ExecStatusType expect = PGRES_TUPLES_OK) const;
3894

39-
void copy_data(std::string const &sql, std::string const &context) const;
40-
void end_copy(std::string const &context) const;
41-
4295
pg_result_t query(ExecStatusType expect, char const *sql) const;
4396

4497
pg_result_t query(ExecStatusType expect, std::string const &sql) const;
@@ -47,10 +100,19 @@ class pg_conn_t
47100

48101
void exec(std::string const &sql) const;
49102

103+
void copy_data(std::string const &sql, std::string const &context) const;
104+
105+
void end_copy(std::string const &context) const;
106+
50107
char const *error_msg() const noexcept;
51108

52109
private:
53-
pg_conn_wrapper_t m_conn;
110+
struct pg_conn_deleter_t
111+
{
112+
void operator()(PGconn *p) const noexcept { PQfinish(p); }
113+
};
114+
115+
std::unique_ptr<PGconn, pg_conn_deleter_t> m_conn;
54116
};
55117

56118
#endif // OSM2PGSQL_PGSQL_HPP

src/table.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,16 @@ class table_t
4545
const char *get_next()
4646
{
4747
if (m_current < m_count) {
48-
return PQgetvalue(m_result.get(), m_current++, 0);
48+
return m_result.get_value(m_current++, 0);
4949
}
5050
return nullptr;
5151
}
5252

5353
int get_count() const { return m_count; }
54-
void reset()
55-
{
56-
//NOTE: PQgetvalue doc doesn't say if you can call it
57-
// multiple times with the same row col
58-
m_current = 0;
59-
}
6054

6155
private:
6256
wkb_reader(pg_result_t &&result)
63-
: m_result(std::move(result)), m_count(PQntuples(m_result.get())),
57+
: m_result(std::move(result)), m_count(m_result.num_tuples()),
6458
m_current(0)
6559
{}
6660

0 commit comments

Comments
 (0)