Skip to content

Commit 9e2b3b0

Browse files
committed
Refactor out common function to get PostGIS version
1 parent 8606e96 commit 9e2b3b0

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

src/flex-table.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,10 @@ void table_connection_t::stop(bool updateable, bool append)
223223
table().geom_column().name());
224224
}
225225

226-
auto const res = m_db_connection->query(
227-
PGRES_TUPLES_OK,
228-
"SELECT regexp_split_to_table(postgis_lib_version(), '\\.')");
229-
auto const postgis_major = std::stoi(res.get_value_as_string(0, 0));
230-
auto const postgis_minor = std::stoi(res.get_value_as_string(1, 0));
226+
auto const postgis_version = get_postgis_version(*m_db_connection);
231227

232228
sql += " ORDER BY ";
233-
if (postgis_major == 2 && postgis_minor < 4) {
229+
if (postgis_version.major == 2 && postgis_version.minor < 4) {
234230
fmt::print(stderr, "Using GeoHash for clustering\n");
235231
if (table().srid() == 4326) {
236232
sql += "ST_GeoHash({},10)"_format(table().geom_column().name());

src/pgsql.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,12 @@ std::string qualified_name(std::string const &schema, std::string const &name)
174174

175175
return result;
176176
}
177+
178+
postgis_version get_postgis_version(pg_conn_t const &db_connection) {
179+
auto const res = db_connection.query(
180+
PGRES_TUPLES_OK,
181+
"SELECT regexp_split_to_table(postgis_lib_version(), '\\.')");
182+
183+
return {std::stoi(res.get_value_as_string(0, 0)),
184+
std::stoi(res.get_value_as_string(1, 0))};
185+
}

src/pgsql.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#ifndef OSM2PGSQL_PGSQL_HPP
22
#define OSM2PGSQL_PGSQL_HPP
33

4-
/* Helper functions for PostgreSQL access */
4+
/**
5+
* \file
6+
*
7+
* This file is part of osm2pgsql (https://github.com/openstreetmap/osm2pgsql).
8+
*
9+
* Helper classes and functions for PostgreSQL access.
10+
*/
511

612
#include "osmtypes.hpp"
713

@@ -154,4 +160,13 @@ std::string tablespace_clause(std::string const &name);
154160
*/
155161
std::string qualified_name(std::string const &schema, std::string const &name);
156162

163+
struct postgis_version
164+
{
165+
int major;
166+
int minor;
167+
};
168+
169+
/// Get PostGIS major and minor version.
170+
postgis_version get_postgis_version(pg_conn_t const &db_connection);
171+
157172
#endif // OSM2PGSQL_PGSQL_HPP

src/table.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,10 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
198198
sql += " WHERE ST_IsValid(way)";
199199
}
200200

201-
auto res = m_sql_conn->query(
202-
PGRES_TUPLES_OK,
203-
"SELECT regexp_split_to_table(postgis_lib_version(), '\\.')");
204-
auto const postgis_major = std::stoi(res.get_value_as_string(0, 0));
205-
auto const postgis_minor = std::stoi(res.get_value_as_string(1, 0));
201+
auto const postgis_version = get_postgis_version(*m_sql_conn);
206202

207203
sql += " ORDER BY ";
208-
if (postgis_major == 2 && postgis_minor < 4) {
204+
if (postgis_version.major == 2 && postgis_version.minor < 4) {
209205
fmt::print(stderr, "Using GeoHash for clustering\n");
210206
if (m_srid == "4326") {
211207
sql += "ST_GeoHash(way,10)";

tests/test-pgsql.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <catch.hpp>
22

3+
#include "common-import.hpp"
34
#include "pgsql.hpp"
45

6+
static testing::db::import_t db;
7+
58
TEST_CASE("Tablespace clause with no tablespace")
69
{
710
REQUIRE(tablespace_clause("") == "");
@@ -21,3 +24,10 @@ TEST_CASE("Table name with schema")
2124
{
2225
REQUIRE(qualified_name("osm", "foo") == R"("osm"."foo")");
2326
}
27+
28+
TEST_CASE("PostGIS version")
29+
{
30+
auto conn = db.db().connect();
31+
auto const postgis_version = get_postgis_version(conn);
32+
REQUIRE(postgis_version.major >= 2);
33+
}

0 commit comments

Comments
 (0)