From b726c60503704995024a2784a90cdc67b4e95fa6 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 15 Feb 2018 22:20:24 -0500 Subject: [PATCH] convert o creat function --- functions/srce_edit.pgsql | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/functions/srce_edit.pgsql b/functions/srce_edit.pgsql index a5d05e0..d4926bf 100644 --- a/functions/srce_edit.pgsql +++ b/functions/srce_edit.pgsql @@ -1,6 +1,12 @@ CREATE OR REPLACE FUNCTION tps.srce_set(_name text, _defn jsonb) RETURNS jsonb AS $f$ + +DECLARE +_cnt int; +_conflict BOOLEAN; +_message jsonb; + BEGIN /* @@ -9,6 +15,42 @@ BEGIN 3. do merge */ + -------check for transctions already existing under this source----------- + SELECT + COUNT(*) + INTO + _cnt + FROM + tps.trans + WHERE + srce = _name; + + -------set a message------------------------------------------------------ + IF _cnt > 0 THEN + _conflict = TRUE; + --get out of the function somehow + _message: = + $$ + { + "message":"transactions already exist under source profile, cannot change the definition" + ,"status":"error" + } + $$::jsonb; + return _message; + END IF; + + /*-----------------schema validation--------------------- + yeah dont feel like it right now + ---------------------------------------------------------*/ + + INSERT INTO + tps.srce + SELECT + _name, _defn + ON CONFLICT DO UPDATE + SET + defn = _defn; + END; $f$