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

Refactor db connection: Use const ref instead of ptr #2125

Merged
merged 2 commits into from
Jan 29, 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
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