@@ -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+
164188void 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
0 commit comments