db/schema/0012_newtype.sql
Orion Kindel 68cd9c5bb4
Some checks failed
gen-migrations / gen-migrations (push) Failing after 7s
feat: add casts to+from text for text newtypes
2023-07-20 12:30:42 -05:00

27 lines
1.0 KiB
PL/PgSQL

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);\';'
);
execute concat( 'create cast '
, ' (' || qualified_name || ' as text)'
, ' with function ' || qualified_name || E'_to_string(' || qualified_name || ')'
, ' as assignment;'
);
execute concat( 'create cast '
, ' (text as ' || qualified_name || ')'
, ' with function ' || qualified_name || E'_of_string(text)'
, ' as assignment;'
);
end;
$$;