@@ -73,15 +73,14 @@ flex_table_column_t &flex_table_t::add_column(std::string const &name,
7373 (m_columns.size () == 1 &&
7474 m_columns[0 ].type () == table_column_type::id_type));
7575
76- m_columns.emplace_back (name, type, sql_type);
77- auto &column = m_columns.back ();
76+ auto &column = m_columns.emplace_back (name, type, sql_type);
7877
7978 if (column.is_geometry_column ()) {
80- if (m_geom_column != std::numeric_limits<std::size_t >::max ()) {
79+ if (m_geom_column == std::numeric_limits<std::size_t >::max ()) {
80+ m_geom_column = m_columns.size () - 1 ;
81+ } else {
8182 m_has_multiple_geom_columns = true ;
8283 }
83- m_geom_column = m_columns.size () - 1 ;
84- column.set_not_null ();
8584 }
8685
8786 return column;
@@ -162,6 +161,30 @@ void table_connection_t::connect(std::string const &conninfo)
162161 m_db_connection->exec (" SET synchronous_commit = off" );
163162}
164163
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+
165188void table_connection_t::start (bool append)
166189{
167190 assert (m_db_connection);
@@ -184,12 +207,7 @@ void table_connection_t::start(bool append)
184207 : flex_table_t ::table_type::permanent,
185208 table ().full_name ()));
186209
187- if (table ().has_geom_column () &&
188- table ().geom_column ().needs_isvalid ()) {
189- create_geom_check_trigger (m_db_connection.get (), table ().schema (),
190- table ().name (),
191- table ().geom_column ().name ());
192- }
210+ enable_check_trigger (m_db_connection.get (), table ());
193211 }
194212
195213 prepare ();
@@ -254,10 +272,8 @@ void table_connection_t::stop(bool updateable, bool append)
254272 table ().full_tmp_name (), table ().name ()));
255273 m_id_index_created = false ;
256274
257- if (updateable && table ().geom_column ().needs_isvalid ()) {
258- create_geom_check_trigger (m_db_connection.get (), table ().schema (),
259- table ().name (),
260- table ().geom_column ().name ());
275+ if (updateable) {
276+ enable_check_trigger (m_db_connection.get (), table ());
261277 }
262278 }
263279
0 commit comments