pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: stan <stanb@panix.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: A question about trigger fucntion syntax
Date: Sun, 11 Aug 2019 17:31:13 -0400
Message-ID: <20190811213113.GB3980@panix.com> (raw)
In-Reply-To: <20190811125613.GA7408@panix.com>
References: <20190811125613.GA7408@panix.com>

I trimmed this thread quite a bit.

Thanks to the help I have received, I am making progress. I have looked a the
recommended documentation, and I believe I am close. I have utilized the
"debugging printf" capability here. Here is where I am. I have the following
function create.


DROP TRIGGER validate_task_trig ON task_instance ;

DROP FUNCTION check_permission() ;


CREATE FUNCTION check_permission()
RETURNS trigger AS $$
DECLARE _permit boolean;
BEGIN
        SELECT
                permit INTO _permit
        FROM
                permitted_work
        WHERE
                NEW.employee_key = OLD.employee_key
        AND
                NEW.work_type_key = OLD.work_type_key;
        RAISE NOTICE 'New employee_id % NEW.work_type_key % _permit = %', 
			NEW.employee_key ,
			NEW.work_type_key ,
			_permit ;
        if NOT _permit THEN
                RAISE NOTICE 'No permission record';
                RAISE EXCEPTION 'No permission record';
	ELSE
                RAISE NOTICE 'Found Permission Record';
        END IF;
        if _permit = FALSE THEN
                RAISE NOTICE 'Permission Denied';
	ELSE
                RAISE NOTICE 'Permission Granted';
        END IF;

        return NEW;
END;
$$ 
LANGUAGE PLPGSQL;

CREATE TRIGGER validate_task_trig BEFORE INSERT OR UPDATE ON task_instance
    FOR EACH ROW EXECUTE FUNCTION check_permission();


Now the issues, currently seems to be that nothing is getting assigned to
_permit. Here is the output of a run with 0 records in the permitted_work
table.

NOTICE:  New employee_id 1 NEW.work_type_key 8 _permit = <NULL>
NOTICE:  Found Permission Record
NOTICE:  Permission Granted
INSERT 0 1

so it appears that nothing is getting assigned to _permit. Also should I be
checking for _permit as NOT NULL in the first if clause?

Thanks for all the had holding on this. Brand new application for me.

-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





view thread (5+ messages)  latest in thread

Message-ID: <20190811213113.GB3980@panix.com>
Permalink:  ../20190811213113.GB3980@panix.com/
Also on:    postgresql.org/message-id/20190811213113.GB3980@panix.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: stanb@panix.com
  Subject: Re: A question about trigger fucntion syntax
  In-Reply-To: <20190811213113.GB3980@panix.com>
  References: <20190811125613.GA7408@panix.com> <20190811213113.GB3980@panix.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox