Algunas veces es necesario recuperar el codigo de una funcion de la base de datos, o un grupo de funciones bajo un patron, por ejemplo quiero todas las funciones que empiecen con "act", en
POSTGRES SQL
create or replace function ldj_listar_funciones(_nombre_funcion text) returns setof record as $$
-------------------------------
-- FUNDACION AUTAPO
-- Autor: Luis A. Jordan P.
-- email: jordanluis@yahoo.com
-------------------------------
declare
_bucle record;
begin
for _bucle in
SELECT ('CREATE OR REPLACE FUNCTION '|| p.proname ||'(' ||
CASE WHEN proallargtypes IS NOT NULL THEN
pg_catalog.array_to_string(
ARRAY(
SELECT
CASE
WHEN p.proargmodes[s.i] = 'i' THEN ''
WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '
WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '
END ||
CASE
WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''
ELSE p.proargnames[s.i] || ' '
END ||
pg_catalog.format_type(p.proallargtypes[s.i], NULL)
FROM pg_catalog.generate_series(
1, pg_catalog.array_upper(p.proallargtypes, 1)
) AS s(i)
), ', '
)
ELSE
pg_catalog.array_to_string(
ARRAY(
SELECT
CASE
WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''
ELSE p.proargnames[s.i+1] || ' '
END ||
pg_catalog.format_type(p.proargtypes[s.i], NULL)
FROM
pg_catalog.generate_series(
0, pg_catalog.array_upper(p.proargtypes, 1)
) AS s(i)
), ', '
)
END ||
CASE WHEN p.proretset THEN ') returns setof ' ELSE '' END ||
pg_catalog.format_type(p.prorettype, NULL) || ' as \$\$ ' || p.prosrc
|| '\$\$ language '||lanname||';')::text as funcion
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
JOIN pg_catalog.pg_roles r ON r.oid = p.proowner
WHERE p.proname ilike _nombre_funcion
AND NOT p.proisagg
AND pg_catalog.pg_function_is_visible(p.oid)
loop
return next _bucle;
end loop;
return;
end;
$$ language plpgsql;
SELECT * FROM ldj_listar_funciones('act_%') as (funcion text);
miércoles, 27 de mayo de 2015
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario