-
Notifications
You must be signed in to change notification settings - Fork 0
/
completeScript.sql
86 lines (72 loc) · 2.4 KB
/
completeScript.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--Operador e função;
create or replace function func_jsonvalidartipo(pDoc text , pTipo text)
returns boolean as
$function$
begin
case pTipo when 'object' then
return (select true from json_each(pDoc::json) limit 1);
when 'array' then
return (select true from (select json_each(json_array_elements(pDoc::json)) limit 1 ) a );
else
return false ;
end case;
exception when others then
return false ;
end;
$function$
IMMUTABLE
parallel safe
language plpgsql
cost 10;
create operator == (
leftarg = text,
rightarg = text,
commutator = ==,
function = func_jsonvalidartipo
);
---- Conversor de objeto
CREATE OR REPLACE FUNCTION func_jsonconverterobjeto ( doc text )
RETURNS TABLE ( chave text, valor text, sequencia bigint ) AS
$funcao$
declare
jDoc json ;
rJson record ;
iCount bigint := 0 ;
begin
jDoc:=doc::json;
if doc == 'array' then
for rJson in select json_array_elements_text( jDoc )::json arrayelement
loop
iCount := iCount+1 ;
return query select key::text , value::text , iCount from json_each( rJson.arrayelement );
end loop;
else
return query select key::text , value::text , iCount from json_each( jDoc );
end if ;
end ;
$funcao$
LANGUAGE 'plpgsql'
IMMUTABLE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
PARALLEL SAFE
COST 10 ROWS 5;
-- Função de utilidade.
create or replace function func_jsonmapeamento(pchavePrimaria bigint , pJson text)
returns table ( chaveprimaria bigint ,chave text , valor text ,pai text , sequencia bigint ) as
$body$
begin
return query with recursive json_repartido (valor , chave , pai, sequencia ) as (
select jsoninicial.valor , jsoninicial.chave , null , jsoninicial.sequencia
from func_jsonconverterobjeto( pJson) jsoninicial
union all
select jsoncomarray.valor as valor ,jsoncomarray.chave as chave , j.chave as pai ,jsoncomarray.sequencia sequencia
from json_repartido j
join func_jsonconverterobjeto(j.valor::text) jsoncomarray on true
where j.valor=='object' or j.valor=='array'
) select pchavePrimaria, j.chave , j.valor , j.pai , j.sequencia from json_repartido j where not j.valor=='object' and not j.valor=='array';
end;
$body$
IMMUTABLE
rows 30
language plpgsql ;