From 5f3fc681108f813f8af0c84869e130d338ce1f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20P=C3=A9rez?= Date: Fri, 31 Jan 2025 10:34:06 +0100 Subject: [PATCH] feat(getlayerstofilter): returns are the layers that have the columns muni_id and sector_id (p. ex) in their primitive tables --- updates/40/40000/utils/dml.sql | 6 +++ utils/fct/gw_fct_getlayerstofilter.sql | 72 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 utils/fct/gw_fct_getlayerstofilter.sql diff --git a/updates/40/40000/utils/dml.sql b/updates/40/40000/utils/dml.sql index 43fff0ac3..f46dca18b 100644 --- a/updates/40/40000/utils/dml.sql +++ b/updates/40/40000/utils/dml.sql @@ -1140,3 +1140,9 @@ INSERT INTO config_form_fields (formname, formtype, tabname, columnname, layoutn INSERT INTO config_form_fields (formname, formtype, tabname, columnname, layoutname, layoutorder, "datatype", widgettype, "label", tooltip, placeholder, ismandatory, isparent, iseditable, isautoupdate, isfilter, dv_querytext, dv_orderby_id, dv_isnullvalue, dv_parent_id, dv_querytext_filterc, stylesheet, widgetcontrols, widgetfunction, linkedobject, hidden, web_layoutorder) VALUES('generic', 'check_project', 'tab_data', 'admin_check', 'lyt_data_1', 4, NULL, 'check', 'Check admin data:', NULL, NULL, false, false, true, false, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '{"minRole": "role_admin"}'::json, NULL, NULL, false, 1); UPDATE config_form_fields SET columnname='conneccat_id' WHERE formname='v_edit_link' AND formtype='form_feature' AND columnname='connecat_id' AND tabname='tab_none'; + + +-- 31/01/2025 +INSERT INTO sys_function +(id, function_name, project_type, function_type, input_params, return_type, descript, sys_role, sample_query, "source") +VALUES(3380, 'gw_fct_getlayerstofilter', 'utils', 'function', 'json', 'json', 'Function to filter WMS layers with imported columns', 'role_basic', NULL, 'core'); diff --git a/utils/fct/gw_fct_getlayerstofilter.sql b/utils/fct/gw_fct_getlayerstofilter.sql new file mode 100644 index 000000000..f6ebe85e1 --- /dev/null +++ b/utils/fct/gw_fct_getlayerstofilter.sql @@ -0,0 +1,72 @@ +/* +This file is part of Giswater 3 +The program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +This version of Giswater is provided by Giswater Association +*/ + +--FUNCTION CODE: 3380 + +DROP FUNCTION SCHEMA_NAME.gw_fct_getlayerstofilter(json); + +CREATE OR REPLACE FUNCTION SCHEMA_NAME.gw_fct_getlayerstofilter(p_data json) + RETURNS json + LANGUAGE plpgsql +AS $function$ + +/* +SELECT SCHEMA_NAME.gw_fct_getselectorsfiltered($${"client":{"device": 5, "lang": "es_ES", "cur_user": "bgeo", "tiled": "False", "infoType": 1}, "form":{}, "feature":{}, +"data":{"filterFields":{}, "pageInfo":{}, "filter": ["muni_id", "sector_id"],"layers": ["Node","Connec"]}}$$); +*/ + +DECLARE + +v_layer text; +v_msgerr json; +v_version json; +v_errcontext text; +v_device integer; +v_layers jsonb; +v_cur_user text; +v_layersFiltered text[] := ARRAY[]::text[]; +v_columns jsonb; +v_schemaname text; +v_filter jsonb; + +BEGIN + + -- Set search path to local schema + SET search_path = "SCHEMA_NAME", public; + v_schemaname = "SCHEMA_NAME"; + + -- get api version + EXECUTE 'SELECT row_to_json(row) FROM (SELECT value FROM config_param_system WHERE parameter=''admin_version'') row' INTO v_version; + + -- Get input parameters: + v_cur_user := (p_data ->> 'client')::json->> 'cur_user'; + v_device := (p_data ->> 'client')::json->> 'device'; + v_filter := (p_data ->> 'data')::jsonb->> 'filter'; + v_layers := (p_data ->> 'data')::jsonb->> 'layers'; + + FOR v_layer IN SELECT jsonb_array_elements_text(v_layers) + LOOP + SELECT json_agg(column_name) INTO v_columns FROM information_schema.columns WHERE table_schema = v_schemaname AND table_name = lower(v_layer); + -- Check if v_columns contain the columns filter + IF v_columns @> v_filter THEN + v_layersFiltered := array_append(v_layersFiltered,v_layer); + END IF; + END LOOP; + + RETURN gw_fct_json_create_return(json_build_object( + 'status', 'Accepted', + 'version', v_version, + 'body', json_build_object('layers', v_layersFiltered) + ),3380, null, null, NULL); + + -- Exception handling + EXCEPTION WHEN OTHERS THEN + GET STACKED DIAGNOSTICS v_errcontext = pg_exception_context; + RETURN json_build_object('status', 'Failed','NOSQLERR', SQLERRM, 'version', v_version, 'SQLSTATE', SQLSTATE, 'MSGERR', (v_msgerr::json ->> 'MSGERR'))::json; + +END; +$function$ +;