tps/functions/srce_edit.pgsql

66 lines
1.4 KiB
Plaintext
Raw Normal View History

2018-02-08 17:40:23 -05:00
CREATE OR REPLACE FUNCTION tps.srce_set(_name text, _defn jsonb) RETURNS jsonb
AS $f$
2018-02-15 22:20:24 -05:00
DECLARE
_cnt int;
_conflict BOOLEAN;
_message jsonb;
2018-02-08 17:40:23 -05:00
BEGIN
/*
1. determine if insert or update
2. if update, determine if conflicts exists
3. do merge
*/
2018-02-15 22:20:24 -05:00
-------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
2018-02-23 15:11:04 -05:00
_message =
2018-02-15 22:20:24 -05:00
$$
{
"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
2018-02-23 15:11:04 -05:00
ON CONFLICT ON CONSTRAINT srce_pkey DO UPDATE
2018-02-15 22:20:24 -05:00
SET
defn = _defn;
2018-02-23 15:11:04 -05:00
_message =
$$
{
"message":"definition set"
,"status":"success"
}
$$::jsonb;
return _message;
2018-02-08 17:40:23 -05:00
END;
$f$
LANGUAGE plpgsql