Skip to content

Commit

Permalink
feat(getlayerstofilter): returns are the layers that have the columns…
Browse files Browse the repository at this point in the history
… muni_id and sector_id (p. ex) in their primitive tables
  • Loading branch information
nperez-bgeo committed Jan 31, 2025
1 parent 10bad17 commit 5f3fc68
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions updates/40/40000/utils/dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
72 changes: 72 additions & 0 deletions utils/fct/gw_fct_getlayerstofilter.sql
Original file line number Diff line number Diff line change
@@ -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$
;

0 comments on commit 5f3fc68

Please sign in to comment.