Re: Session variables (how do I pass session data to a function) - Mailing list pgsql-general

From Harald Armin Massa
Subject Re: Session variables (how do I pass session data to a function)
Date
Msg-id 7be3f35d0810200415s1b8f5a02u3003585fce5b5f05@mail.gmail.com
Whole thread Raw
In response to Session variables (how do I pass session data to a function)  ("Philip W. Dalrymple" <pwd@mdtsoft.com>)
List 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!

pgsql-general by date:

Previous
From: "Andrus"
Date:
Subject: Re: IS NULL seems to return false, even when parameter is NULL
Next
From: pere roca
Date:
Subject: removing a portion of text