Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use helper function for DROP TABLE #2163

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/flex-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ static void enable_check_trigger(pg_conn_t const &db_connection,
void table_connection_t::start(pg_conn_t const &db_connection, bool append)
{
if (!append) {
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE",
table().full_name());
drop_table_if_exists(db_connection, table().schema(), table().name());
}

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

if (!append) {
db_connection.exec(table().build_sql_create_table(
Expand Down
8 changes: 2 additions & 6 deletions src/middle-pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ void middle_pgsql_t::table_desc::drop_table(
util::timer_t timer;

log_info("Dropping table '{}'", name());

auto const qual_name = qualified_name(schema(), name());
db_connection.exec("DROP TABLE IF EXISTS {}", qual_name);

drop_table_if_exists(db_connection, schema(), name());
log_info("Table '{}' dropped in {}", name(),
util::human_readable_duration(timer.stop()));
}
Expand Down Expand Up @@ -1318,8 +1315,7 @@ static void table_setup(pg_conn_t const &db_connection,
middle_pgsql_t::table_desc const &table)
{
log_debug("Setting up table '{}'", table.name());
auto const qual_name = qualified_name(table.schema(), table.name());
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
drop_table_if_exists(db_connection, table.schema(), table.name());
table.create_table(db_connection);
}

Expand Down
11 changes: 10 additions & 1 deletion src/pgsql-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* For a full list of authors see the git log.
*/

#include "pgsql-helper.hpp"

#include "format.hpp"
#include "pgsql.hpp"
#include "pgsql-helper.hpp"
#include "pgsql-capabilities.hpp"

#include <cassert>

Expand Down Expand Up @@ -73,3 +75,10 @@ void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
auto const qual_name = qualified_name(schema, name);
db_connection.exec("ANALYZE {}", qual_name);
}

void drop_table_if_exists(pg_conn_t const &db_connection,
std::string const &schema, std::string const &name)
{
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE",
qualified_name(schema, name));
}
3 changes: 3 additions & 0 deletions src/pgsql-helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ void drop_geom_check_trigger(pg_conn_t const &db_connection,
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
std::string const &name);

void drop_table_if_exists(pg_conn_t const &db_connection,
std::string const &schema, std::string const &name);

#endif // OSM2PGSQL_PGSQL_HELPER_HPP
11 changes: 6 additions & 5 deletions src/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,22 @@ void table_t::start(connection_params_t const &connection_params,

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

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

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

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

Expand Down