From d74f8fc584e12c7f9e39042963341345af3bdab6 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 25 Jan 2023 19:27:20 +0400 Subject: [PATCH] Add some functions --- 20230125142000_functions.down.sql | 4 ++++ 20230125142000_functions.up.sql | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 20230125142000_functions.down.sql create mode 100644 20230125142000_functions.up.sql diff --git a/20230125142000_functions.down.sql b/20230125142000_functions.down.sql new file mode 100644 index 0000000..a15c7a7 --- /dev/null +++ b/20230125142000_functions.down.sql @@ -0,0 +1,4 @@ +BEGIN; +DROP FUNCTION is_name; +DROP FUNCTION is_language_code; +COMMIT; diff --git a/20230125142000_functions.up.sql b/20230125142000_functions.up.sql new file mode 100644 index 0000000..aa3238c --- /dev/null +++ b/20230125142000_functions.up.sql @@ -0,0 +1,16 @@ +BEGIN; + +CREATE FUNCTION is_name(value TEXT) RETURNS BOOLEAN LANGUAGE plpgsql AS $$ +BEGIN + RETURN value = btrim(value) AND length(value) > 0; +END; +$$; + +CREATE FUNCTION is_language_code(value VARCHAR) RETURNS BOOLEAN LANGUAGE plpgsql +AS $$ +BEGIN + RETURN value ~ '^[a-z]{2}$'; +END; +$$; + +COMMIT;