Created
January 19, 2023 14:52
-
-
Save wess/15a017d6a665261787397bf094d8321d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE EXTENSION IF NOT EXISTS plpgsql; | |
CREATE TABLE doc_store ( | |
key text PRIMARY KEY, | |
value json | |
); | |
CREATE OR REPLACE FUNCTION insert_key_value(p_key text, p_value json) | |
RETURNS void AS $$ | |
BEGIN | |
INSERT INTO doc_store (key, value) | |
VALUES (p_key, p_value); | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION update_key_value(p_key text, p_value json) | |
RETURNS void AS $$ | |
BEGIN | |
UPDATE doc_store | |
SET value = p_value | |
WHERE key = p_key; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION get_value(p_key text) | |
RETURNS json AS $$ | |
DECLARE | |
result json; | |
BEGIN | |
SELECT value INTO result | |
FROM doc_store | |
WHERE key = p_key; | |
RETURN result; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION delete_key_value(p_key text) | |
RETURNS void AS $$ | |
BEGIN | |
DELETE FROM doc_store | |
WHERE key = p_key; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION key_exists(p_key text) | |
RETURNS boolean AS $$ | |
BEGIN | |
RETURN (SELECT COUNT(*) FROM doc_store WHERE key = p_key) > 0; | |
END; | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment