Skip to content

Commit a910b86

Browse files
authored
Merge pull request #1856 from joto/pgsql-cleanup-again
Pgsql cleanup again
2 parents 3a4b676 + 760ee27 commit a910b86

File tree

10 files changed

+67
-81
lines changed

10 files changed

+67
-81
lines changed

src/db-copy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void db_copy_thread_t::thread_t::write_to_db(db_cmd_copy_t *buffer)
184184
start_copy(buffer->target);
185185
}
186186

187-
m_conn->copy_data(buffer->buffer, buffer->target->name);
187+
m_conn->copy_send(buffer->buffer, buffer->target->name);
188188
}
189189

190190
void db_copy_thread_t::thread_t::start_copy(
@@ -205,15 +205,15 @@ void db_copy_thread_t::thread_t::start_copy(
205205
}
206206

207207
sql.push_back('\0');
208-
m_conn->query(PGRES_COPY_IN, sql.data());
208+
m_conn->copy_start(sql.data());
209209

210210
m_inflight = target;
211211
}
212212

213213
void db_copy_thread_t::thread_t::finish_copy()
214214
{
215215
if (m_inflight) {
216-
m_conn->end_copy(m_inflight->name);
216+
m_conn->copy_end(m_inflight->name);
217217
m_inflight.reset();
218218
}
219219
}

src/middle-pgsql.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,9 @@ void middle_pgsql_t::start()
670670
auto const qual_name = qualified_name(table.schema(), table.name());
671671
m_db_connection.exec(
672672
"DROP TABLE IF EXISTS {} CASCADE"_format(qual_name));
673-
m_db_connection.exec(table.m_create_table);
673+
if (!table.m_create_table.empty()) {
674+
m_db_connection.exec(table.m_create_table);
675+
}
674676
}
675677
}
676678
}
@@ -824,8 +826,7 @@ static table_sql sql_for_relations() noexcept
824826
static bool check_bucket_index(pg_conn_t *db_connection,
825827
std::string const &prefix)
826828
{
827-
auto const res = db_connection->query(
828-
PGRES_TUPLES_OK,
829+
auto const res = db_connection->exec(
829830
"SELECT relname FROM pg_class WHERE relkind='i' AND"
830831
" relname = '{}_ways_nodes_bucket_idx';"_format(prefix));
831832
return res.num_tuples() > 0;
@@ -873,7 +874,9 @@ middle_pgsql_t::get_query_instance()
873874

874875
// We use a connection per table to enable the use of COPY
875876
for (auto &table : m_tables) {
876-
mid->exec_sql(table.m_prepare_query);
877+
if (!table.m_prepare_query.empty()) {
878+
mid->exec_sql(table.m_prepare_query);
879+
}
877880
}
878881

879882
return std::shared_ptr<middle_query_t>(mid.release());

src/pgsql-capabilities.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ static void init_set_from_query(std::set<std::string> *set,
3232
char const *table, char const *column,
3333
char const *condition = "true")
3434
{
35-
auto const res = db_connection.query(
36-
PGRES_TUPLES_OK,
35+
auto const res = db_connection.exec(
3736
"SELECT {} FROM {} WHERE {}"_format(column, table, condition));
3837
for (int i = 0; i < res.num_tuples(); ++i) {
3938
set->emplace(res.get(i, 0));
@@ -43,8 +42,8 @@ static void init_set_from_query(std::set<std::string> *set,
4342
/// Get all config settings from the database.
4443
static void init_settings(pg_conn_t const &db_connection)
4544
{
46-
auto const res = db_connection.query(
47-
PGRES_TUPLES_OK, "SELECT name, setting FROM pg_settings");
45+
auto const res =
46+
db_connection.exec("SELECT name, setting FROM pg_settings");
4847

4948
for (int i = 0; i < res.num_tuples(); ++i) {
5049
capabilities().settings.emplace(res.get(i, 0), res.get(i, 1));
@@ -53,8 +52,7 @@ static void init_settings(pg_conn_t const &db_connection)
5352

5453
static void init_database_name(pg_conn_t const &db_connection)
5554
{
56-
auto const res =
57-
db_connection.query(PGRES_TUPLES_OK, "SELECT current_catalog");
55+
auto const res = db_connection.exec("SELECT current_catalog");
5856

5957
if (res.num_tuples() != 1) {
6058
throw std::runtime_error{
@@ -66,9 +64,9 @@ static void init_database_name(pg_conn_t const &db_connection)
6664

6765
static void init_postgis_version(pg_conn_t const &db_connection)
6866
{
69-
auto const res = db_connection.query(
70-
PGRES_TUPLES_OK, "SELECT regexp_split_to_table(extversion, '\\.') FROM"
71-
" pg_extension WHERE extname='postgis'");
67+
auto const res = db_connection.exec(
68+
"SELECT regexp_split_to_table(extversion, '\\.') FROM"
69+
" pg_extension WHERE extname='postgis'");
7270

7371
if (res.num_tuples() == 0) {
7472
throw std::runtime_error{

src/pgsql-helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool has_table(pg_conn_t const &db_connection, std::string const &schema,
8484
auto const sql = "SELECT count(*) FROM pg_tables"
8585
" WHERE schemaname='{}' AND tablename='{}'"_format(
8686
schema.empty() ? "public" : schema, table);
87-
auto const res = db_connection.query(PGRES_TUPLES_OK, sql);
87+
auto const res = db_connection.exec(sql);
8888
char const *const num = res.get_value(0, 0);
8989

9090
return num[0] == '1' && num[1] == '\0';

src/pgsql.cpp

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,51 @@ char const *pg_conn_t::error_msg() const noexcept
3737
return PQerrorMessage(m_conn.get());
3838
}
3939

40-
pg_result_t pg_conn_t::query(ExecStatusType expect, char const *sql) const
40+
void pg_conn_t::set_config(char const *setting, char const *value) const
41+
{
42+
// Update pg_settings instead of using SET because it does not yield
43+
// errors on older versions of PostgreSQL where the settings are not
44+
// implemented.
45+
exec("UPDATE pg_settings SET setting = '{}' WHERE name = '{}'"_format(
46+
value, setting));
47+
}
48+
49+
pg_result_t pg_conn_t::exec(char const *sql) const
4150
{
4251
assert(m_conn);
4352

4453
log_sql("{}", sql);
4554
pg_result_t res{PQexec(m_conn.get(), sql)};
46-
if (res.status() != expect) {
55+
if (res.status() != PGRES_COMMAND_OK && res.status() != PGRES_TUPLES_OK) {
4756
throw std::runtime_error{"Database error: {}"_format(error_msg())};
4857
}
4958
return res;
5059
}
5160

52-
pg_result_t pg_conn_t::query(ExecStatusType expect,
53-
std::string const &sql) const
54-
{
55-
return query(expect, sql.c_str());
56-
}
57-
58-
void pg_conn_t::set_config(char const *setting, char const *value) const
61+
pg_result_t pg_conn_t::exec(std::string const &sql) const
5962
{
60-
// Update pg_settings instead of using SET because it does not yield
61-
// errors on older versions of PostgreSQL where the settings are not
62-
// implemented.
63-
auto const sql =
64-
"UPDATE pg_settings SET setting = '{}' WHERE name = '{}'"_format(
65-
value, setting);
66-
query(PGRES_TUPLES_OK, sql);
63+
return exec(sql.c_str());
6764
}
6865

69-
void pg_conn_t::exec(char const *sql) const
66+
void pg_conn_t::copy_start(char const *sql) const
7067
{
71-
if (sql && sql[0] != '\0') {
72-
query(PGRES_COMMAND_OK, sql);
73-
}
74-
}
68+
assert(m_conn);
7569

76-
void pg_conn_t::exec(std::string const &sql) const
77-
{
78-
if (!sql.empty()) {
79-
query(PGRES_COMMAND_OK, sql.c_str());
70+
log_sql("{}", sql);
71+
pg_result_t const res{PQexec(m_conn.get(), sql)};
72+
if (res.status() != PGRES_COPY_IN) {
73+
throw std::runtime_error{
74+
"Database error on COPY: {}"_format(error_msg())};
8075
}
8176
}
8277

83-
void pg_conn_t::copy_data(std::string const &sql,
78+
void pg_conn_t::copy_send(std::string const &data,
8479
std::string const &context) const
8580
{
8681
assert(m_conn);
8782

88-
log_sql_data("Copy data to '{}':\n{}", context, sql);
89-
int const r = PQputCopyData(m_conn.get(), sql.c_str(), (int)sql.size());
83+
log_sql_data("Copy data to '{}':\n{}", context, data);
84+
int const r = PQputCopyData(m_conn.get(), data.c_str(), (int)data.size());
9085

9186
switch (r) {
9287
case 0: // need to wait for write ready
@@ -101,17 +96,17 @@ void pg_conn_t::copy_data(std::string const &sql,
10196
break;
10297
}
10398

104-
if (sql.size() < 1100) {
105-
log_error("Data: {}", sql);
99+
if (data.size() < 1100) {
100+
log_error("Data: {}", data);
106101
} else {
107-
log_error("Data: {}\n...\n{}", std::string(sql, 0, 500),
108-
std::string(sql, sql.size() - 500));
102+
log_error("Data: {}\n...\n{}", std::string(data, 0, 500),
103+
std::string(data, data.size() - 500));
109104
}
110105

111106
throw std::runtime_error{"COPYing data to Postgresql."};
112107
}
113108

114-
void pg_conn_t::end_copy(std::string const &context) const
109+
void pg_conn_t::copy_end(std::string const &context) const
115110
{
116111
assert(m_conn);
117112

src/pgsql.hpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ class pg_conn_t
179179
public:
180180
explicit pg_conn_t(std::string const &conninfo);
181181

182+
/**
183+
* Run the specified SQL command.
184+
*
185+
* \param sql The SQL command. If this is empty, nothing is done and a
186+
* default constructed pg_result_t is returned.
187+
* \throws std::runtime_exception If the command failed (didn't return
188+
* status code PGRES_COMMAND_OK or PGRES_TUPLES_OK).
189+
*/
190+
pg_result_t exec(char const *sql) const;
191+
pg_result_t exec(std::string const &sql) const;
192+
182193
/**
183194
* Run the named prepared SQL statement and return the results.
184195
*
@@ -211,35 +222,15 @@ class pg_conn_t
211222
param_ptrs.data());
212223
}
213224

214-
/**
215-
* Run the specified SQL query and return the results.
216-
*
217-
* \param expect The expected status code of the SQL command.
218-
* \param sql The SQL command.
219-
* \throws exception if the result is not as expected.
220-
*/
221-
pg_result_t query(ExecStatusType expect, char const *sql) const;
222-
pg_result_t query(ExecStatusType expect, std::string const &sql) const;
223-
224225
/**
225226
* Update a PostgreSQL setting (like with the SET command). Will silently
226227
* ignore settings that are not available or any other errors.
227228
*/
228229
void set_config(char const *setting, char const *value) const;
229230

230-
/**
231-
* Run the specified SQL query. This can only be used for commands that
232-
* have no output and return status code PGRES_COMMAND_OK.
233-
*
234-
* \param sql The SQL command.
235-
* \throws exception if the command failed.
236-
*/
237-
void exec(char const *sql) const;
238-
void exec(std::string const &sql) const;
239-
240-
void copy_data(std::string const &sql, std::string const &context) const;
241-
242-
void end_copy(std::string const &context) const;
231+
void copy_start(char const *sql) const;
232+
void copy_send(std::string const &data, std::string const &context) const;
233+
void copy_end(std::string const &context) const;
243234

244235
/// Return the latest generated error message on this connection.
245236
char const *error_msg() const noexcept;

tests/common-pg.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class conn_t : public pg_conn_t
4141

4242
std::string result_as_string(std::string const &cmd) const
4343
{
44-
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
44+
pg_result_t const res = exec(cmd);
4545
REQUIRE(res.num_tuples() == 1);
4646
return std::string{res.get(0, 0)};
4747
}
@@ -63,14 +63,14 @@ class conn_t : public pg_conn_t
6363

6464
void assert_null(std::string const &cmd) const
6565
{
66-
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
66+
pg_result_t const res = exec(cmd);
6767
REQUIRE(res.num_tuples() == 1);
6868
REQUIRE(res.is_null(0, 0));
6969
}
7070

7171
pg_result_t require_row(std::string const &cmd) const
7272
{
73-
pg_result_t res = query(PGRES_TUPLES_OK, cmd);
73+
pg_result_t res = exec(cmd);
7474
REQUIRE(res.num_tuples() == 1);
7575

7676
return res;

tests/test-db-copy-mgr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ TEST_CASE("copy_mgr_t")
232232
mgr.sync();
233233

234234
auto const conn = db.connect();
235-
auto const res = conn.query(PGRES_TUPLES_OK,
236-
"SELECT t FROM test_copy_mgr ORDER BY id");
235+
auto const res = conn.exec("SELECT t FROM test_copy_mgr ORDER BY id");
237236
CHECK(res.num_tuples() == 2);
238237
CHECK(res.get(0, 0) == "good");
239238
CHECK(res.get(1, 0) == "better");

tests/test-output-gazetteer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class gazetteer_fixture_t
260260
"SELECT skeys({}), svals({}) FROM place"
261261
" WHERE osm_type = '{}' AND osm_id = {}"
262262
" AND class = '{}'"_format(column, column, tchar, id, cls);
263-
auto const res = conn.query(PGRES_TUPLES_OK, sql);
263+
auto const res = conn.exec(sql);
264264

265265
hstore_list actual;
266266
for (int i = 0; i < res.num_tuples(); ++i) {

tests/test-pgsql.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ TEST_CASE("Table name with schema")
3737
TEST_CASE("query with SELECT should work")
3838
{
3939
auto conn = db.db().connect();
40-
auto const result = conn.query(PGRES_TUPLES_OK, "SELECT 42");
40+
auto const result = conn.exec("SELECT 42");
4141
REQUIRE(result.status() == PGRES_TUPLES_OK);
4242
REQUIRE(result.num_fields() == 1);
4343
REQUIRE(result.num_tuples() == 1);
@@ -47,7 +47,7 @@ TEST_CASE("query with SELECT should work")
4747
TEST_CASE("query with invalid SQL should fail")
4848
{
4949
auto conn = db.db().connect();
50-
REQUIRE_THROWS(conn.query(PGRES_TUPLES_OK, "NOT-VALID-SQL"));
50+
REQUIRE_THROWS(conn.exec("NOT-VALID-SQL"));
5151
}
5252

5353
TEST_CASE("exec with invalid SQL should fail")

0 commit comments

Comments
 (0)