create or replace function public.loja_editar_envio(p_descricao text, p_mundo boolean, p_paises text[], p_meios text[]) returns void language plpgsql security definer set search_path = public as $$ declare t_mu text; t_pa text; t_me text; n int; begin if auth.uid() is null then raise exception 'Entre na sua conta.'; end if; select data_type into t_mu from information_schema.columns where table_schema='public' and table_name='lojas' and column_name='envia_mundo'; select data_type into t_pa from information_schema.columns where table_schema='public' and table_name='lojas' and column_name='paises_entrega'; select data_type into t_me from information_schema.columns where table_schema='public' and table_name='lojas' and column_name='meios_envio'; execute format('update public.lojas set descricao = $1, envia_mundo = %s, paises_entrega = %s, meios_envio = %s where dono_id::text = $5', case when t_mu = 'boolean' then '$2' when t_mu = 'jsonb' then 'to_jsonb($2)' when t_mu = 'json' then 'to_json($2)' else '$2::text' end, case when t_pa = 'ARRAY' then '$3' when t_pa = 'jsonb' then 'to_jsonb($3)' when t_pa = 'json' then 'to_json($3)' else 'array_to_string($3, '','')' end, case when t_me = 'ARRAY' then '$4' when t_me = 'jsonb' then 'to_jsonb($4)' when t_me = 'json' then 'to_json($4)' else 'array_to_string($4, '','')' end) using p_descricao, p_mundo, coalesce(p_paises, '{}'), coalesce(p_meios, '{}'), auth.uid()::text; get diagnostics n = row_count; if n = 0 then raise exception 'Loja não encontrada.'; end if; end $$; revoke all on function public.loja_editar_envio(text, boolean, text[], text[]) from public; grant execute on function public.loja_editar_envio(text, boolean, text[], text[]) to authenticated;