Re: Session variables (how do I pass session data to a function)

From: "Harald Armin Massa" <haraldarminmassa(at)gmail(dot)com>
To: "Philip W(dot) Dalrymple III" <pwd(at)mdtsoft(dot)com>
Cc: "General Postgres Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Session variables (how do I pass session data to a function)
Date: 2008-10-20 11:15:46
Message-ID: 7be3f35d0810200415s1b8f5a02u3003585fce5b5f05@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Philip,

"session variables" is the perfect name for this kind of use; and
googling it up some times ago lead me to

a) a temp table implementation
b) some shared memory implementation

of these I can present you with a), written to store an integer
user-ID; you can adjust it accordingly:

CREATE OR REPLACE FUNCTION set_user(myid_user integer)
RETURNS integer AS
$BODY$
BEGIN
perform relname from pg_class
where relname = 'icke_tmp'
and case when has_schema_privilege(relnamespace, 'USAGE')
then pg_table_is_visible(oid) else false end;
if not found then
create temporary table icke_tmp (
id_user integer
);
else
delete from icke_tmp;
end if;

insert into icke_tmp values (myid_user);
RETURN 0;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;

CREATE OR REPLACE FUNCTION get_user()
RETURNS integer AS
$BODY$
declare
ergebnis int4;
BEGIN
perform relname from pg_class
where relname = 'icke_tmp'
and case when has_schema_privilege(relnamespace, 'USAGE')
then pg_table_is_visible(oid) else false end;
if not found then
return 0;
else
select id_user from icke_tmp into ergebnis;
end if;
if not found then
ergebnis:=0;
end if;
RETURN ergebnis;
END;
$BODY$
LANGUAGE 'plpgsql' STABLE
COST 100;

You can adjust that "0" to NULL or whatever should be the default for
your application. In mine I default to 0; with 0 being something like
"testuser".

best wishes,

Harald
--
GHUM Harald Massa
persuadere et programmare
Harald Armin Massa
Spielberger Straße 49
70435 Stuttgart
0173/9409607
no fx, no carrier pigeon
-
EuroPython 2009 will take place in Birmingham - Stay tuned!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message pere roca 2008-10-20 11:21:31 removing a portion of text
Previous Message Andrus 2008-10-20 10:26:57 Re: IS NULL seems to return false, even when parameter is NULL