@@ -199,7 +199,7 @@ BEGIN
199199END;
200200$BODY$
201201 LANGUAGE plpgsql VOLATILE;
202-
202+
203203/* *
204204 * Function: import_from_s3_trigger_for_non_empty_layer
205205 * (for tasked import into non-empty layers)
@@ -232,74 +232,102 @@ $BODY$
232232 * Returns:
233233 * - The enriched NEW row (trigger return).
234234 */
235- -- TODO: Remove code-duplication of the following trigger functions!!
236235CREATE OR REPLACE FUNCTION import_from_s3_trigger_for_non_empty_layer () RETURNS trigger AS
237236$BODY$
238237DECLARE
239- author TEXT := TG_ARGV[0 ];
240- currentVersion BIGINT := TG_ARGV[1 ];
241- isPartial BOOLEAN := TG_ARGV[2 ];
242- onExists TEXT := TG_ARGV[3 ];
243- onNotExists TEXT := TG_ARGV[4 ];
244- onVersionConflict TEXT := TG_ARGV[5 ];
245- onMergeConflict TEXT := TG_ARGV[6 ];
246- historyEnabled BOOLEAN := TG_ARGV[7 ]::BOOLEAN ;
247- spaceContext TEXT := TG_ARGV[8 ];
248- tables TEXT := TG_ARGV[9 ];
249- format TEXT := TG_ARGV[10 ];
250- entityPerLine TEXT := TG_ARGV[11 ];
251- featureCount INT : = 0 ;
252- input TEXT ;
253- inputType TEXT ;
238+ author text := TG_ARGV[0 ];
239+ currentVersion bigint := TG_ARGV[1 ];
240+ isPartial boolean := TG_ARGV[2 ];
241+ onExists text := TG_ARGV[3 ];
242+ onNotExists text := TG_ARGV[4 ];
243+ onVersionConflict text := TG_ARGV[5 ];
244+ onMergeConflict text := TG_ARGV[6 ];
245+ historyEnabled boolean := TG_ARGV[7 ]::boolean ;
246+ spaceContext text := TG_ARGV[8 ];
247+ tables text := TG_ARGV[9 ];
248+ format text := TG_ARGV[10 ];
249+ entityPerLine text := TG_ARGV[11 ];
250+
251+ feature_collection text ;
252+ featureCount int : = 0 ;
254253BEGIN
255254 -- TODO: Remove the following workaround once the caller-side was fixed
256255 onExists = CASE WHEN onExists = ' null' THEN NULL ELSE onExists END;
257256 onNotExists = CASE WHEN onNotExists = ' null' THEN NULL ELSE onNotExists END;
258257 onVersionConflict = CASE WHEN onVersionConflict = ' null' THEN NULL ELSE onVersionConflict END;
259258 onMergeConflict = CASE WHEN onMergeConflict = ' null' THEN NULL ELSE onMergeConflict END;
260259
261- -- TODO: remove support for CSV_JSON_WKB and CSV_GEOJSON if all import process are tasked based
262- IF format = ' CSV_JSON_WKB' AND NEW .geo IS NOT NULL THEN
263- -- TODO: Extend feature_writer with possibility to provide geometry (as JSONB manipulations are quite slow)
264- -- TODO: Remove unnecessary xyz_reduce_precision call, because the FeatureWriter will do it anyways
265- NEW .jsondata := jsonb_set(NEW .jsondata ::JSONB, ' {geometry}' , xyz_reduce_precision(ST_ASGeojson(ST_Force3D(NEW .geo )), false)::JSONB);
266- input = NEW .jsondata ::TEXT ;
267- inputType = ' Feature' ;
268- END IF;
269-
270- IF format = ' GEOJSON' OR format = ' CSV_GEOJSON' THEN
271- IF entityPerLine = ' Feature' THEN
272- input = NEW .jsondata ::TEXT ;
273- inputType = ' Feature' ;
274- ELSE
275- -- TODO: Shouldn't the input be a FeatureCollection here? Seems to be a list of Features
276- input = (NEW .jsondata ::JSONB- > ' features' )::TEXT ;
277- inputType = ' Features' ;
278- END IF;
279- END IF;
280-
281- -- TODO: check how to use asyncify instead
282260 PERFORM context(
283261 jsonb_build_object(
284- ' stepId' , get_stepid_from_work_table(TG_TABLE_NAME::REGCLASS) ,
262+ ' stepId' , get_stepid_from_work_table(TG_TABLE_NAME::regclass) ,
285263 ' schema' , TG_TABLE_SCHEMA,
286264 ' tables' , string_to_array(tables, ' ,' ),
287265 ' historyEnabled' , historyEnabled,
288266 ' context' , CASE WHEN spaceContext = ' null' THEN null ELSE spaceContext END,
289- ' batchMode' , inputType != ' Feature '
267+ ' batchMode' , true
290268 )
291269 );
292270
293- SELECT write_features(
294- input, inputType, author, false, currentVersion,
295- onExists, onNotExists, onVersionConflict, onMergeConflict, isPartial
296- )::JSONB- > ' count' INTO featureCount;
271+ IF format = ' CSV_JSON_WKB' THEN
272+ SELECT jsonb_build_object(
273+ ' type' , ' FeatureCollection' ,
274+ ' features' ,
275+ coalesce(
276+ jsonb_agg(
277+ jsonb_set(
278+ n .jsondata ::jsonb,
279+ ' {geometry}' ,
280+ xyz_reduce_precision(ST_AsGeoJSON(ST_Force3D(n .geo )), false)::JSONB
281+ )
282+ ),
283+ ' []' ::jsonb
284+ )
285+ )::text
286+ INTO feature_collection
287+ FROM new_rows n
288+ WHERE n .geo IS NOT NULL ;
289+ END IF;
297290
298- NEW .jsondata = NULL ;
299- NEW .geo = NULL ;
300- NEW .count = featureCount;
291+ IF format IN (' GEOJSON' , ' CSV_GEOJSON' ) THEN
292+ IF entityPerLine = ' Feature' THEN
293+ SELECT jsonb_build_object(
294+ ' type' , ' FeatureCollection' ,
295+ ' features' ,
296+ COALESCE(jsonb_agg(n .jsondata ::jsonb), ' []' ::jsonb)
297+ )::text
298+ INTO feature_collection
299+ FROM new_rows n;
300+ ELSE
301+ SELECT jsonb_build_object(
302+ ' type' , ' FeatureCollection' ,
303+ ' features' ,
304+ COALESCE(jsonb_agg(f .feature ), ' []' ::jsonb)
305+ )::text
306+ INTO feature_collection
307+ FROM new_rows n
308+ CROSS JOIN LATERAL jsonb_array_elements(n .jsondata ::jsonb - > ' features' ) AS f(feature);
309+ END IF;
310+ ELSE
311+ RAISE EXCEPTION ' Unsupported format: %' , format;
312+ END IF;
301313
302- RETURN NEW;
314+ IF feature_collection IS NOT NULL THEN
315+ SELECT (write_features(
316+ feature_collection,
317+ ' FeatureCollection' ,
318+ author,
319+ false,
320+ currentVersion,
321+ onExists,
322+ onNotExists,
323+ onVersionConflict,
324+ onMergeConflict,
325+ isPartial
326+ )::jsonb - >> ' count' )::int
327+ INTO featureCount;
328+ END IF;
329+
330+ RETURN null ;
303331END;
304332$BODY$
305333LANGUAGE plpgsql VOLATILE;
@@ -1329,4 +1357,4 @@ BEGIN
13291357 RETURN NEW;
13301358END;
13311359$BODY$
1332- LANGUAGE plpgsql VOLATILE;
1360+ LANGUAGE plpgsql VOLATILE;
0 commit comments