Sanity check on view - Mailing list pgsql-general

From Philippe Lang
Subject Sanity check on view
Date
Msg-id 8AE5A725DAF7364F97FF75D99E45B65759955958@SBS1.attiksystem.local
Whole thread Raw
List pgsql-general
Hi,

I've got a view, which is supposed to be called with a WHERE clause, like:

------------------------------------------------------------
SELECT * FROM data_view WHERE od_id = '1234';
------------------------------------------------------------

I'd like to make sure it is called correctly: not all "od_id" values should be permitted.

I came up with this solution:

------------------------------------------------------------
--  FUNCTION: get_data_view
------------------------------------------------------------
CREATE OR REPLACE FUNCTION get_data_view(integer)
  RETURNS SETOF data_view AS
'
  DECLARE

    data    RECORD;

  BEGIN

    FOR data IN SELECT * FROM data_view WHERE od_id = $1 LOOP

      IF data.foo != ''bar'' THEN
        RAISE EXCEPTION ''blablabla'';
      END IF;

      RETURN NEXT data;
    END LOOP;

    RETURN;

  END;
'
  LANGUAGE 'plpgsql' VOLATILE;
------------------------------------------------------------

Is there maybe a better alternative?

I tried to raise an exception from sql directly, in a "case... when...", but it didn’t work...

Thanks!

-------------------------------------------------------------
Attik System              web  : http://www.attiksystem.ch
Philippe Lang             phone: +41 26 422 13 75
rte de la Fonderie 2      gsm  : +41 79 351 49 94
1700 Fribourg             pgp  : http://keyserver.pgp.com



Attachment

pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: On insert duplicate row, return existing key
Next
From: AI Rumman
Date:
Subject: column depends on sequence