Skip to content

Commit 61af154

Browse files
authored
Merge pull request #2163 from joto/helper-drop-table
Use helper function for DROP TABLE
2 parents 8fd3324 + e752fde commit 61af154

5 files changed

+24
-15
lines changed

src/flex-table.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ static void enable_check_trigger(pg_conn_t const &db_connection,
281281
void table_connection_t::start(pg_conn_t const &db_connection, bool append)
282282
{
283283
if (!append) {
284-
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE",
285-
table().full_name());
284+
drop_table_if_exists(db_connection, table().schema(), table().name());
286285
}
287286

288287
// These _tmp tables can be left behind if we run out of disk space.
289-
db_connection.exec("DROP TABLE IF EXISTS {}", table().full_tmp_name());
288+
drop_table_if_exists(db_connection, table().schema(),
289+
table().name() + "_tmp");
290290

291291
if (!append) {
292292
db_connection.exec(table().build_sql_create_table(

src/middle-pgsql.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ void middle_pgsql_t::table_desc::drop_table(
148148
util::timer_t timer;
149149

150150
log_info("Dropping table '{}'", name());
151-
152-
auto const qual_name = qualified_name(schema(), name());
153-
db_connection.exec("DROP TABLE IF EXISTS {}", qual_name);
154-
151+
drop_table_if_exists(db_connection, schema(), name());
155152
log_info("Table '{}' dropped in {}", name(),
156153
util::human_readable_duration(timer.stop()));
157154
}
@@ -1318,8 +1315,7 @@ static void table_setup(pg_conn_t const &db_connection,
13181315
middle_pgsql_t::table_desc const &table)
13191316
{
13201317
log_debug("Setting up table '{}'", table.name());
1321-
auto const qual_name = qualified_name(table.schema(), table.name());
1322-
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
1318+
drop_table_if_exists(db_connection, table.schema(), table.name());
13231319
table.create_table(db_connection);
13241320
}
13251321

src/pgsql-helper.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
* For a full list of authors see the git log.
88
*/
99

10+
#include "pgsql-helper.hpp"
11+
1012
#include "format.hpp"
1113
#include "pgsql.hpp"
12-
#include "pgsql-helper.hpp"
14+
#include "pgsql-capabilities.hpp"
1315

1416
#include <cassert>
1517

@@ -73,3 +75,10 @@ void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
7375
auto const qual_name = qualified_name(schema, name);
7476
db_connection.exec("ANALYZE {}", qual_name);
7577
}
78+
79+
void drop_table_if_exists(pg_conn_t const &db_connection,
80+
std::string const &schema, std::string const &name)
81+
{
82+
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE",
83+
qualified_name(schema, name));
84+
}

src/pgsql-helper.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ void drop_geom_check_trigger(pg_conn_t const &db_connection,
4040
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
4141
std::string const &name);
4242

43+
void drop_table_if_exists(pg_conn_t const &db_connection,
44+
std::string const &schema, std::string const &name);
45+
4346
#endif // OSM2PGSQL_PGSQL_HELPER_HPP

src/table.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,22 @@ void table_t::start(connection_params_t const &connection_params,
8383

8484
connect();
8585
log_info("Setting up table '{}'", m_target->name());
86-
auto const qual_name = qualified_name(m_target->schema(), m_target->name());
87-
auto const qual_tmp_name =
88-
qualified_name(m_target->schema(), m_target->name() + "_tmp");
8986

9087
// we are making a new table
9188
if (!m_append) {
92-
m_db_connection->exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
89+
drop_table_if_exists(*m_db_connection, m_target->schema(),
90+
m_target->name());
9391
}
9492

9593
// These _tmp tables can be left behind if we run out of disk space.
96-
m_db_connection->exec("DROP TABLE IF EXISTS {}", qual_tmp_name);
94+
drop_table_if_exists(*m_db_connection, m_target->schema(),
95+
m_target->name() + "_tmp");
9796

9897
//making a new table
9998
if (!m_append) {
10099
//define the new table
100+
auto const qual_name =
101+
qualified_name(m_target->schema(), m_target->name());
101102
auto sql =
102103
fmt::format("CREATE UNLOGGED TABLE {} (osm_id int8,", qual_name);
103104

0 commit comments

Comments
 (0)