Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Fixes for query execution, relation refs and raw test
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed May 23, 2024
1 parent cfc1cdc commit 4f7f9e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/data/pq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,15 @@ Pq::query(const std::string &query)
{
std::scoped_lock write_lock{pqxx_mutex};
pqxx::work worker(*sdb);
pqxx::result result;
try {
auto result = worker.exec(query);
result = worker.exec(query);
worker.commit();
} catch (std::exception &e) {
log_error("ERROR executing query %1%", e.what());
// Return an empty result so higher level code can handle the error
pqxx::result result;
return result;
}

return result;
}

Expand Down
11 changes: 2 additions & 9 deletions src/raw/queryraw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,6 @@ QueryRaw::applyChange(const OsmRelation &relation) const
query.append(fmt.str());
queries->push_back(query);

// Refresh all refs stored into the rel_refs table
queries->push_back("DELETE FROM rel_refs WHERE rel_id=" + std::to_string(relation.id) + ";");

for (auto mit = relation.members.begin(); mit != relation.members.end(); ++mit) {
queries->push_back("INSERT INTO rel_refs (rel_id, way_id) VALUES (" + std::to_string(relation.id) + "," + std::to_string(mit->ref) + ");");
}

} else {

// Update only the Relation's geometry. This is the case when a Relation was indirectly
Expand Down Expand Up @@ -518,7 +511,7 @@ QueryRaw::getRelationsByWaysRefs(std::string &wayIds) const
std::list<std::shared_ptr<osmobjects::OsmRelation>> rels;

// Query for getting Relations
std::string relsQuery = "SELECT distinct(osm_id), refs, version, tags, uid, changeset from rel_refs join relations r on r.osm_id = rel_id where way_id = any(ARRAY[" + wayIds + "])";
std::string relsQuery = "SELECT distinct(osm_id), refs, version, tags, uid, changeset FROM relations WHERE EXISTS (SELECT 1 FROM jsonb_array_elements(refs) AS ref WHERE (ref->>'ref')::bigint IN (" + wayIds + "));";
auto rels_result = dbconn->query(relsQuery);

// Fill vector with OsmRelation objects
Expand Down Expand Up @@ -871,7 +864,7 @@ QueryRaw::getWaysByNodesRefs(std::string &nodeIds) const
// std::string waysQuery = "SELECT distinct(osm_id), refs, version, tags, uid, changeset from way_refs join ways_poly wp on wp.osm_id = way_id where node_id = any(ARRAY[" + nodeIds + "])";
queries.push_back("SELECT distinct(osm_id), refs, version, tags, uid, changeset from ways_poly where refs @> '{" + nodeIds + "}'");

queries.push_back("SELECT distinct(osm_id), refs, version, tags, uid, changeset from ways_lines where refs @> '{" + nodeIds + "}'");
queries.push_back("SELECT distinct(osm_id), refs, version, tags, uid, changeset from ways_line where refs @> '{" + nodeIds + "}'");
// waysQuery += " UNION SELECT distinct(osm_id), refs, version, tags, uid, changeset from way_refs join ways_line wl on wl.osm_id = way_id where node_id = any(ARRAY[" + nodeIds + "]);";

for (auto it = queries.begin(); it != queries.end(); ++it) {
Expand Down
22 changes: 17 additions & 5 deletions src/testsuite/libunderpass.all/raw-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,39 @@ bool processFile(const std::string &filename, std::shared_ptr<Pq> &db) {
osmchanges->readChanges(destdir_base + "/testsuite/testdata/raw/" + filename);
queryraw->buildGeometries(osmchanges, poly);
osmchanges->areaFilter(poly);
std::string rawquery;
auto rawquery = std::make_shared<std::vector<std::string>>();

for (auto it = std::begin(osmchanges->changes); it != std::end(osmchanges->changes); ++it) {
osmchange::OsmChange *change = it->get();
// Nodes
for (auto nit = std::begin(change->nodes); nit != std::end(change->nodes); ++nit) {
osmobjects::OsmNode *node = nit->get();
rawquery += queryraw->applyChange(*node);
auto changes = *queryraw->applyChange(*node);
for (auto it = changes.begin(); it != changes.end(); ++it) {
rawquery->push_back(*it);
}
}
// Ways
for (auto wit = std::begin(change->ways); wit != std::end(change->ways); ++wit) {
osmobjects::OsmWay *way = wit->get();
rawquery += queryraw->applyChange(*way);
auto changes = *queryraw->applyChange(*way);
for (auto it = changes.begin(); it != changes.end(); ++it) {
rawquery->push_back(*it);
}
}
// Relations
for (auto rit = std::begin(change->relations); rit != std::end(change->relations); ++rit) {
osmobjects::OsmRelation *relation = rit->get();
rawquery += queryraw->applyChange(*relation);
auto changes = *queryraw->applyChange(*relation);
for (auto it = changes.begin(); it != changes.end(); ++it) {
rawquery->push_back(*it);
}
}
}
db->query(rawquery);

for (auto rit = std::begin(*rawquery); rit != std::end(*rawquery); ++rit) {
db->query(*rit);
}
}

const std::vector<std::string> expectedGeometries = {
Expand Down

0 comments on commit 4f7f9e6

Please sign in to comment.