I just simple add the command in the Schema modeler and inspect the parameter, still not call them from my app…
I use postgres 9 from the enterpriseDb website.
You can use this:
This one not have the return value
CREATE OR REPLACE FUNCTION random_string(length integer)
RETURNS TEXT
AS $$
DECLARE
chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
result text := '';
i integer := 0;
BEGIN
IF length < 0 THEN
raise exception 'Given length cannot be less than 0';
END IF;
FOR i IN 1..length LOOP
result := result || chars[1+random()*(array_length(chars, 1)-1)];
END LOOP;
RETURN result;
END;
$$
LANGUAGE plpgsql;
-- I try with the most simplistic function I have:
CREATE OR REPLACE FUNCTION public.db_version()
RETURNS INTEGER
AS $$
BEGIN
RETURN 1;
END;
$$
LANGUAGE plpgsql;
This one not show the input parameters, or the custom table return
-- Django compatible login.
CREATE OR REPLACE FUNCTION login_user (user_name text, pass text, client_version text)
RETURNS TABLE(isOk boolean, company_name TEXT, company_id INTEGER, server_version INTEGER)
AS $$
DECLARE
isOk bool=false;
company_name TEXT;
schema TEXT;
company_id INTEGER;
user_id INTEGER;
BEGIN
IF EXISTS (SELECT 1 FROM public.auth_user WHERE username=user_name) THEN
SELECT INTO isOk,company_name,company_id, schema, user_id
encode(libs.digest(split_part(password, '$', 2) || pass, 'sha1'), 'hex') =
split_part(password, '$', 3),
Company.name,
Company.id,
Company.username,
public.auth_user.id
FROM public.auth_user INNER JOIN public.Company ON public.auth_user.IdCompany = public.Company.Id
WHERE public.auth_user.username=user_name;
IF isOk THEN
UPDATE public.auth_user
SET
last_login= NOW()
WHERE user_id=user_id;
PERFORM public.set_schema(schema);
PERFORM public.login_device(user_id,client_version);
RETURN QUERY SELECT isOk, company_name, company_id, public.db_version();
END IF;
END IF;
IF not isOk THEN
RETURN QUERY SELECT False, null::TEXT, null::INTEGER, public.db_version();
END IF;
END;
$$
LANGUAGE 'plpgsql';
This is one with a special datatype , show the input but not the return
-- Soporte KEY => Value
CREATE EXTENSION hstore
SCHEMA libs;
CREATE OR REPLACE FUNCTION public.text_to_record(text_data TEXT)
RETURNS SETOF hstore
AS $$
DECLARE
line RECORD;
BEGIN
FOR line in SELECT regexp_split_to_table(text_data,E'\n') AS keys LOOP
RETURN NEXT line.keys::hstore;
END LOOP;
END
$$
LANGUAGE plpgsql;