feat: create_newtype_text

This commit is contained in:
Orion Kindel 2023-06-10 12:04:03 -05:00
parent 047a51b45d
commit 49bed63d07
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 17 additions and 1 deletions

16
schema/000_newtype.sql Normal file
View File

@ -0,0 +1,16 @@
create function create_newtype_text(qualified_name text)
returns void
language plpgsql
as $$
begin
execute concat('create type ', qualified_name, ' as (str text);');
execute concat( 'create function '
, qualified_name || '_to_string(val ' || qualified_name || ')'
, E' returns text language sql as \'select (val.str);\';'
);
execute concat( 'create function '
, qualified_name || '_of_string(val text)'
, ' returns ' || qualified_name || E' language sql as \'select row(val);\';'
);
end;
$$;

View File

@ -1,4 +1,4 @@
create type public.usr_username as (username text);
select create_newtype_text('public.usr_username');
create table public.usr_audit
( usr int not null