Skip to content

Commit 5e661d6

Browse files
committed
Extend trigger creation code so it works with multiple geom columns
1 parent 4a0928b commit 5e661d6

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/flex-table.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ void table_connection_t::connect(std::string const &conninfo)
161161
m_db_connection->exec("SET synchronous_commit = off");
162162
}
163163

164+
static void
165+
enable_check_trigger(pg_conn_t *db_connection, flex_table_t const &table)
166+
{
167+
std::string checks;
168+
169+
for (auto const &column : table) {
170+
if (column.is_geometry_column() && column.needs_isvalid()) {
171+
checks.append(
172+
R"((NEW."{0}" IS NULL OR ST_IsValid(NEW."{0}")) AND )"_format(
173+
column.name()));
174+
}
175+
}
176+
177+
if (checks.empty()) {
178+
return;
179+
}
180+
181+
// remove last " AND "
182+
checks.resize(checks.size() - 5);
183+
184+
create_geom_check_trigger(db_connection, table.schema(), table.name(),
185+
checks);
186+
}
187+
164188
void table_connection_t::start(bool append)
165189
{
166190
assert(m_db_connection);
@@ -183,12 +207,7 @@ void table_connection_t::start(bool append)
183207
: flex_table_t::table_type::permanent,
184208
table().full_name()));
185209

186-
if (table().has_geom_column() &&
187-
table().geom_column().needs_isvalid()) {
188-
create_geom_check_trigger(m_db_connection.get(), table().schema(),
189-
table().name(),
190-
table().geom_column().name());
191-
}
210+
enable_check_trigger(m_db_connection.get(), table());
192211
}
193212

194213
prepare();
@@ -253,10 +272,8 @@ void table_connection_t::stop(bool updateable, bool append)
253272
table().full_tmp_name(), table().name()));
254273
m_id_index_created = false;
255274

256-
if (updateable && table().geom_column().needs_isvalid()) {
257-
create_geom_check_trigger(m_db_connection.get(), table().schema(),
258-
table().name(),
259-
table().geom_column().name());
275+
if (updateable) {
276+
enable_check_trigger(m_db_connection.get(), table());
260277
}
261278
}
262279

src/pgsql-helper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt,
3535
void create_geom_check_trigger(pg_conn_t *db_connection,
3636
std::string const &schema,
3737
std::string const &table,
38-
std::string const &geom_column)
38+
std::string const &condition)
3939
{
4040
std::string func_name = qualified_name(schema, table + "_osm2pgsql_valid");
4141

4242
db_connection->exec(
4343
"CREATE OR REPLACE FUNCTION {}()\n"
4444
"RETURNS TRIGGER AS $$\n"
4545
"BEGIN\n"
46-
" IF ST_IsValid(NEW.{}) THEN \n"
46+
" IF {} THEN \n"
4747
" RETURN NEW;\n"
4848
" END IF;\n"
4949
" RETURN NULL;\n"
5050
"END;"
51-
"$$ LANGUAGE plpgsql;"_format(func_name, geom_column));
51+
"$$ LANGUAGE plpgsql;"_format(func_name, condition));
5252

5353
db_connection->exec(
5454
"CREATE TRIGGER \"{}\""

src/pgsql-helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ idlist_t get_ids_from_db(pg_conn_t const *db_connection, char const *stmt,
3333
void create_geom_check_trigger(pg_conn_t *db_connection,
3434
std::string const &schema,
3535
std::string const &table,
36-
std::string const &geom_column);
36+
std::string const &condition);
3737

3838
void drop_geom_check_trigger(pg_conn_t *db_connection,
3939
std::string const &schema,

src/table.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
134134

135135
if (m_srid != "4326") {
136136
create_geom_check_trigger(m_sql_conn.get(), m_target->schema,
137-
m_target->name, "way");
137+
m_target->name, "ST_IsValid(NEW.way)");
138138
}
139139
}
140140

@@ -243,7 +243,8 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
243243
qual_name, tablespace_clause(table_space_index)));
244244
if (m_srid != "4326") {
245245
create_geom_check_trigger(m_sql_conn.get(), m_target->schema,
246-
m_target->name, "way");
246+
m_target->name,
247+
"ST_IsValid(NEW.way)");
247248
}
248249
}
249250

0 commit comments

Comments
 (0)