Skip to content

Commit 911eb9b

Browse files
committed
Database connection in middle-pgsql doesn't need to be unique_ptr
1 parent 13b448e commit 911eb9b

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/middle-pgsql.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ pg_result_t middle_query_pgsql_t::exec_prepared(char const *stmt,
7373
pg_result_t middle_pgsql_t::exec_prepared(char const *stmt,
7474
osmid_t osm_id) const
7575
{
76-
assert(m_query_conn);
7776
util::integer_to_buffer buffer{osm_id};
7877
char const *const bptr = buffer.c_str();
79-
return m_query_conn->exec_prepared(stmt, 1, &bptr);
78+
return m_db_connection.exec_prepared(stmt, 1, &bptr);
8079
}
8180

8281
void middle_query_pgsql_t::exec_sql(std::string const &sql_cmd) const
@@ -587,9 +586,8 @@ idlist_t middle_query_pgsql_t::relations_using_way(osmid_t way_id) const
587586

588587
void middle_pgsql_t::analyze()
589588
{
590-
assert(m_query_conn);
591589
for (auto const &table : m_tables) {
592-
m_query_conn->exec("ANALYZE {}"_format(table.name()));
590+
m_db_connection.exec("ANALYZE {}"_format(table.name()));
593591
}
594592
}
595593

@@ -615,28 +613,25 @@ void middle_pgsql_t::start()
615613
m_mark_pending = false;
616614
}
617615

618-
m_query_conn.reset(
619-
new pg_conn_t{m_out_options->database_options.conninfo()});
620-
621616
if (m_append) {
622617
// Prepare queries for updating dependent objects
623618
for (auto &table : m_tables) {
624619
if (!table.m_prepare_intarray.empty()) {
625-
m_query_conn->exec(table.m_prepare_intarray);
620+
m_db_connection.exec(table.m_prepare_intarray);
626621
}
627622
}
628623
} else {
629624
// (Re)create tables.
630-
m_query_conn->exec("SET client_min_messages = WARNING");
625+
m_db_connection.exec("SET client_min_messages = WARNING");
631626
for (auto &table : m_tables) {
632627
fmt::print(stderr, "Setting up table: {}\n", table.name());
633-
m_query_conn->exec(
628+
m_db_connection.exec(
634629
"DROP TABLE IF EXISTS {} CASCADE"_format(table.name()));
635-
m_query_conn->exec(table.m_create);
630+
m_db_connection.exec(table.m_create);
636631
}
637632

638633
// The extra query connection is only needed in append mode, so close.
639-
m_query_conn.reset();
634+
m_db_connection.close();
640635
}
641636
}
642637

@@ -646,7 +641,7 @@ void middle_pgsql_t::commit()
646641
// release the copy thread and its query connection
647642
m_copy_thread->finish();
648643

649-
m_query_conn.reset();
644+
m_db_connection.close();
650645
}
651646

652647
void middle_pgsql_t::flush() { m_db_copy.sync(); }
@@ -775,6 +770,7 @@ middle_pgsql_t::middle_pgsql_t(options_t const *options)
775770
: m_append(options->append), m_mark_pending(true), m_out_options(options),
776771
m_cache(new node_ram_cache{options->alloc_chunkwise | ALLOC_LOSSY,
777772
options->cache}),
773+
m_db_connection(m_out_options->database_options.conninfo()),
778774
m_copy_thread(
779775
std::make_shared<db_copy_thread_t>(options->database_options.conninfo())),
780776
m_db_copy(m_copy_thread)

src/middle-pgsql.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ struct middle_pgsql_t : public slim_middle_t
130130

131131
std::shared_ptr<id_tracker> m_ways_pending_tracker, m_rels_pending_tracker;
132132

133-
std::unique_ptr<pg_conn_t> m_query_conn;
133+
pg_conn_t m_db_connection;
134+
134135
// middle keeps its own thread for writing to the database.
135136
std::shared_ptr<db_copy_thread_t> m_copy_thread;
136137
db_copy_mgr_t<db_deleter_by_id_t> m_db_copy;

src/pgsql.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class pg_result_t
9191
*
9292
* Wraps the PGconn object of the libpq library.
9393
*
94-
* The connection is automatically closed when the object is destroyed.
94+
* The connection is automatically closed when the object is destroyed or
95+
* you can close it explicitly by calling close().
9596
*/
9697
class pg_conn_t
9798
{
@@ -116,6 +117,9 @@ class pg_conn_t
116117

117118
char const *error_msg() const noexcept;
118119

120+
/// Close database connection.
121+
void close() noexcept { m_conn.reset(); }
122+
119123
private:
120124
struct pg_conn_deleter_t
121125
{

0 commit comments

Comments
 (0)