Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwwqq-0005CU-Tc for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 23:02:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hwwqp-0006vU-G9 for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 23:02:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwwqp-0006nZ-4w for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 23:02:11 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwwqg-0002Vx-Qa for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 23:02:09 +0000 Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mailbackend.panix.com (Postfix) with ESMTP id 466Dxy3k3Jz1pn6 for ; Sun, 11 Aug 2019 19:01:58 -0400 (EDT) Received: by panix2.panix.com (Postfix, from userid 17622) id 466Dxy2Sfhz1ZVM; Sun, 11 Aug 2019 19:01:58 -0400 (EDT) Date: Sun, 11 Aug 2019 19:01:58 -0400 From: stan To: pgsql-general@lists.postgresql.org Subject: [SOLVED] Re: A question about trigger fucntion syntax Message-ID: <20190811230158.GA26930@panix.com> References: <20190811125613.GA7408@panix.com> <20190811213113.GB3980@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190811213113.GB3980@panix.com> X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Aug 11, 2019 at 05:31:13PM -0400, stan wrote: > 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 = > 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. For the archive. I have this working, Here is the function that I woulnd up with. 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 = permitted_work.employee_key AND NEW.work_type_key = permitted_work.work_type_key; if _permit IS NULL THEN RAISE EXCEPTION 'No permission record'; ELSE END IF; if _permit = FALSE THEN RAISE EXCEPTION 'Permisson Denied'; 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(); Thanks to all the people that were instrumental in helping me learn triggers and functions. -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin