Skip to content

Commit

Permalink
Merge pull request #2125 from joto/refactor-db-connection
Browse files Browse the repository at this point in the history
Refactor db connection: Use const ref instead of ptr
  • Loading branch information
lonvia authored Jan 29, 2024
2 parents 3e79e6e + 0a9e7de commit 229d832
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/flex-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ void table_connection_t::connect(std::string const &conninfo)
m_db_connection->exec("SET synchronous_commit = off");
}

static void
enable_check_trigger(pg_conn_t *db_connection, flex_table_t const &table)
static void enable_check_trigger(pg_conn_t const &db_connection,
flex_table_t const &table)
{
std::string checks;

Expand Down Expand Up @@ -291,7 +291,7 @@ void table_connection_t::start(bool append)
: flex_table_t::table_type::permanent,
table().full_name()));

enable_check_trigger(m_db_connection.get(), table());
enable_check_trigger(*m_db_connection, table());
}

prepare();
Expand All @@ -310,7 +310,7 @@ void table_connection_t::stop(bool updateable, bool append)

if (table().cluster_by_geom()) {
if (table().geom_column().needs_isvalid()) {
drop_geom_check_trigger(m_db_connection.get(), table().schema(),
drop_geom_check_trigger(*m_db_connection, table().schema(),
table().name());
}

Expand Down Expand Up @@ -357,7 +357,7 @@ void table_connection_t::stop(bool updateable, bool append)
m_id_index_created = false;

if (updateable) {
enable_check_trigger(m_db_connection.get(), table());
enable_check_trigger(*m_db_connection, table());
}
}

Expand Down
45 changes: 22 additions & 23 deletions src/pgsql-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,45 @@ idlist_t get_ids_from_result(pg_result_t const &result) {
return ids;
}

void create_geom_check_trigger(pg_conn_t *db_connection,
void create_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table,
std::string const &condition)
{
std::string const func_name =
qualified_name(schema, table + "_osm2pgsql_valid");

db_connection->exec("CREATE OR REPLACE FUNCTION {}()\n"
"RETURNS TRIGGER AS $$\n"
"BEGIN\n"
" IF {} THEN \n"
" RETURN NEW;\n"
" END IF;\n"
" RETURN NULL;\n"
"END;"
"$$ LANGUAGE plpgsql",
func_name, condition);
db_connection.exec("CREATE OR REPLACE FUNCTION {}()\n"
"RETURNS TRIGGER AS $$\n"
"BEGIN\n"
" IF {} THEN \n"
" RETURN NEW;\n"
" END IF;\n"
" RETURN NULL;\n"
"END;"
"$$ LANGUAGE plpgsql",
func_name, condition);

db_connection->exec("CREATE TRIGGER \"{}\""
" BEFORE INSERT OR UPDATE"
" ON {}"
" FOR EACH ROW EXECUTE PROCEDURE"
" {}()",
table + "_osm2pgsql_valid",
qualified_name(schema, table), func_name);
db_connection.exec("CREATE TRIGGER \"{}\""
" BEFORE INSERT OR UPDATE"
" ON {}"
" FOR EACH ROW EXECUTE PROCEDURE"
" {}()",
table + "_osm2pgsql_valid",
qualified_name(schema, table), func_name);
}

void drop_geom_check_trigger(pg_conn_t *db_connection,
void drop_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table)
{
std::string const func_name =
qualified_name(schema, table + "_osm2pgsql_valid");

db_connection->exec(R"(DROP TRIGGER "{}" ON {})",
table + "_osm2pgsql_valid",
qualified_name(schema, table));
db_connection.exec(R"(DROP TRIGGER "{}" ON {})", table + "_osm2pgsql_valid",
qualified_name(schema, table));

db_connection->exec("DROP FUNCTION IF EXISTS {} ()", func_name);
db_connection.exec("DROP FUNCTION IF EXISTS {} ()", func_name);
}

void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
Expand Down
4 changes: 2 additions & 2 deletions src/pgsql-helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class pg_result_t;
*/
idlist_t get_ids_from_result(pg_result_t const &result);

void create_geom_check_trigger(pg_conn_t *db_connection,
void create_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table,
std::string const &condition);

void drop_geom_check_trigger(pg_conn_t *db_connection,
void drop_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table);

Expand Down
6 changes: 3 additions & 3 deletions src/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
m_sql_conn->exec(sql);

if (m_srid != "4326") {
create_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
create_geom_check_trigger(*m_sql_conn, m_target->schema(),
m_target->name(), "ST_IsValid(NEW.way)");
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,

if (!m_append) {
if (m_srid != "4326") {
drop_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
drop_geom_check_trigger(*m_sql_conn, m_target->schema(),
m_target->name());
}

Expand Down Expand Up @@ -237,7 +237,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
m_sql_conn->exec("CREATE INDEX ON {} USING BTREE (osm_id) {}",
qual_name, tablespace_clause(table_space_index));
if (m_srid != "4326") {
create_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
create_geom_check_trigger(*m_sql_conn, m_target->schema(),
m_target->name(),
"ST_IsValid(NEW.way)");
}
Expand Down

0 comments on commit 229d832

Please sign in to comment.