Commit 959653f5 authored by Alice Salsé's avatar Alice Salsé
Browse files

set default value

parent e676d80f
......@@ -15,8 +15,9 @@ do
done
echo Creation des tables réseau
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -v schemaname=${schemaname} -f sql/network/create_table.sql
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -c "TRUNCATE ${schemaname}.pipe_default_node RESTART IDENTITY;"
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -c "TRUNCATE ${schemaname}.pipe_default_node RESTART IDENTITY; TRUNCATE ${schemaname}.default_value RESTART IDENTITY;"
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -c "\copy ${schemaname}.pipe_default_node FROM '../config/csv/${schemaname}/pipe_default_node.csv' WITH DELIMITER ';' ESCAPE'\' CSV HEADER;"
psql -h ${host} -d ${dbname} -U ${user} -p ${port} -b -q -c "\copy ${schemaname}.default_value FROM '../config/csv/${schemaname}/default_value.csv' WITH DELIMITER ';' ESCAPE'\' CSV HEADER;"
echo Creation des tables
python python/create_table.py $schemaname
echo Installation des configurations spécifiques
......
\encoding UTF8
set search_path to utils, public;
CREATE OR REPLACE FUNCTION fnc_set_default_value(
IN i_regclass regclass,
IN i_new record,
OUT o_new_json jsonb
)
RETURNS jsonb AS $BODY$
DECLARE
_def_value record;
_select_values text;
_calc_values record;
BEGIN
o_new_json := row_to_json(i_new.*)::jsonb;
for _def_value in EXECUTE FORMAT ('SELECT * FROM %1$I.default_value
where (tablename=%2$L or tablename=''_all'')
and (%3$L::jsonb @> objectdef::jsonb or objectdef is null);'
,(parse_ident(i_regclass::text))[1], (parse_ident(i_regclass::text))[2], o_new_json::text) LOOP
if _def_value.defvalue is not null then
o_new_json := o_new_json || _def_value.defvalue;
end if;
if _def_value.calcvalue is not null then
select string_agg(value||' '||key ::text,',') INTO _select_values from jsonb_each_text(_def_value.calcvalue);
EXECUTE FORMAT ('SELECT %1$s FROM (SELECT (a).* FROM (SELECT $1 a) b) c', _select_values) USING i_new INTO _calc_values;
o_new_json := o_new_json || row_to_json(_calc_values.*)::jsonb;
end if;
END LOOP;
RETURN;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
......@@ -34,6 +34,13 @@ CREATE TABLE IF NOT EXISTS config.pipe_default_node (
UNIQUE(schemaname,level)
);
CREATE TABLE IF NOT EXISTS default_value (
tablename varchar(20)
, objectdef jsonb
, defvalue jsonb
, calcvalue jsonb
);
INSERT INTO config.pipe_default_node
SELECT :'schemaname', code, True FROM config.vl_level
ON CONFLICT DO NOTHING;
......
......@@ -15,6 +15,9 @@ DECLARE
_def_node record;
_def_node_new jsonb;
_def_node_new_geom geometry;
_def_value record;
_select_values text;
_calc_values record;
begin
--==== INSTEAD OF INSERT ON VIEW ====
-- >>> Get tables <<<
......@@ -36,6 +39,20 @@ begin
INTO _geom_id;
-- >>> New row to jsonb <<<
_new := row_to_json(new.*)::jsonb;
-- ++ Calculate defaults values ++
for _def_value in EXECUTE FORMAT ('SELECT * FROM %1$I.default_value
where (tablename=%2$L or tablename=''_all'')
and (%3$L::jsonb @> objectdef::jsonb or objectdef is null);'
,(parse_ident(i_regclass::text))[1], (parse_ident(i_regclass::text))[2], _new::text) LOOP
if _def_value.defvalue is not null then
_new := _new || _def_value.defvalue;
end if;
if _def_value.calcvalue is not null then
select string_agg(value||' '||key ::text,',') INTO _select_values from jsonb_each_text(_def_value.calcvalue);
EXECUTE FORMAT ('SELECT %1$s FROM (SELECT (a).* FROM (SELECT $1 a) b) c', _select_values) USING new INTO _calc_values;
_new := _new || row_to_json(_calc_values.*)::jsonb;
end if;
END LOOP;
_new := _new -'id' || jsonb_build_object('level', _level);
-- >>> Insert geom if not exist <<<
IF _geom_id is null then
......@@ -47,9 +64,8 @@ begin
for _def_node in EXECUTE FORMAT ('SELECT * FROM %1$I.pipe_default_node
where pipetable=%2$L and (%3$L::jsonb @> pipedef::jsonb or pipedef is null);'
,TG_TABLE_SCHEMA, _table, _new::text) LOOP
EXECUTE FORMAT ('SELECT %1$s
FROM (SELECT %2$L geom) b;'
, _def_node.calcgeom, new.geom) INTO _def_node_new_geom;
EXECUTE FORMAT ('SELECT %1$s FROM (SELECT (a).* FROM (SELECT $1 a) b) c'
, _def_node.calcgeom) USING new INTO _def_node_new_geom;
_def_node_new := _new || _def_node.defvalue || jsonb_build_object('geom', _def_node_new_geom);
PERFORM utils.fnc_insert_from_jsonb(TG_TABLE_SCHEMA||'.vw_'||_def_node.nodetable, _def_node_new);
END LOOP;
......
......@@ -12,6 +12,9 @@ DECLARE
_level int;
_new jsonb;
_old jsonb;
_def_value record;
_select_values text;
_calc_values record;
begin
--==== INSTEAD OF UPDATE ON VIEW ====
-- >>> Get tables <<<
......@@ -56,6 +59,20 @@ begin
-- >>> Update attributes <<<
_old:=row_to_json(old.*)::jsonb;
_new := row_to_json(new.*)::jsonb;
-- ++ Calculate defaults values ++
for _def_value in EXECUTE FORMAT ('SELECT * FROM %1$I.default_value
where (tablename=%2$L or tablename=''_all'')
and (%3$L::jsonb @> objectdef::jsonb or objectdef is null);'
,(parse_ident(i_regclass::text))[1], (parse_ident(i_regclass::text))[2], _new::text) LOOP
if _def_value.defvalue is not null then
_new := _new || _def_value.defvalue;
end if;
if _def_value.calcvalue is not null then
select string_agg(value||' '||key ::text,',') INTO _select_values from jsonb_each_text(_def_value.calcvalue);
EXECUTE FORMAT ('SELECT %1$s FROM (SELECT (a).* FROM (SELECT $1 a) b) c', _select_values) USING new INTO _calc_values;
_new := _new || row_to_json(_calc_values.*)::jsonb;
end if;
END LOOP;
_new :=_new - 'geom';
_new :=_new || jsonb_build_object('id', coalesce(_geom_id,new.id));
PERFORM utils.fnc_update_from_jsonb(TG_TABLE_SCHEMA||'.'||_nw_table, _new, _old);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment