@@ -14,7 +14,8 @@ drop schema if exists
1414 network_types,
1515 named_query_builder,
1616 enum_tables,
17- geometry
17+ geometry,
18+ function_returning_enum
1819cascade;
1920drop extension if exists tablefunc;
2021drop extension if exists intarray;
@@ -1238,3 +1239,125 @@ create table geometry.geom (
12381239 polygon polygon ,
12391240 circle circle
12401241);
1242+
1243+ -- ------------------------------------------------------------------------------
1244+
1245+ create schema function_returning_enum ;
1246+
1247+ create table function_returning_enum .stage_options (
1248+ type text primary key
1249+ );
1250+ comment on table function_returning_enum.stage_options is E' @enum' ;
1251+ insert into function_returning_enum .stage_options (type) values (' pending' ), (' round 1' ), (' round 2' ), (' rejected' ), (' hired' );
1252+
1253+ create type function_returning_enum .animal_type as enum (
1254+ ' CAT' ,
1255+ ' DOG' ,
1256+ ' FISH'
1257+ );
1258+
1259+
1260+ create table function_returning_enum .enum_table (
1261+ transportation text not null constraint transportation_enum unique
1262+ );
1263+
1264+ comment on constraint transportation_enum on function_returning_enum.enum_table is E' @enum' ;
1265+ insert into function_returning_enum .enum_table (transportation) values (' CAR' ), (' BIKE' ), (' SUBWAY' );
1266+
1267+ create table function_returning_enum .applicants (
1268+ id int ,
1269+ first_name text ,
1270+ last_name text ,
1271+ favorite_pet function_returning_enum .animal_type ,
1272+ stage text references function_returning_enum .stage_options (type),
1273+ transportation text references function_returning_enum .enum_table (transportation)
1274+ );
1275+
1276+ -- - follow the convention of [enum_name]_enum_domain
1277+ create domain function_returning_enum .stage_options_enum_domain as text ;
1278+
1279+ create function function_returning_enum .applicants_next_stage(
1280+ a function_returning_enum .applicants
1281+ ) returns function_returning_enum .stage_options_enum_domain
1282+ as $$
1283+ select (case when a .stage = ' round 2' then ' hired'
1284+ else ' rejected' end)::function_returning_enum .stage_options_enum_domain ;
1285+ $$ language sql stable;
1286+ comment on function function_returning_enum.applicants_next_stage is E' @filterable' ;
1287+
1288+ create table function_returning_enum .length_status (
1289+ type text primary key
1290+ );
1291+ comment on table function_returning_enum.length_status is E' @enum' ;
1292+ insert into function_returning_enum .length_status (type) values (' ok' ), (' too_short' );
1293+
1294+ -- - use an @enum tag this time
1295+ create domain function_returning_enum .length as text ;
1296+ comment on domain function_returning_enum.length is E' @enum length_status' ;
1297+
1298+ create function function_returning_enum .text_length(
1299+ text text ,
1300+ min_length int
1301+ ) returns function_returning_enum .length
1302+ as $$
1303+ select (case when length(text ) < min_length then ' too_short'
1304+ else ' ok' end)::function_returning_enum .length ;
1305+ $$ language sql stable;
1306+
1307+ create function function_returning_enum .applicants_name_length(
1308+ a function_returning_enum .applicants
1309+ ) returns function_returning_enum .length
1310+ as $$
1311+ select function_returning_enum .text_length (a .last_name , 4 )::function_returning_enum .length ;
1312+ $$ language sql stable;
1313+ comment on function function_returning_enum.applicants_name_length is E' @filterable' ;
1314+
1315+ create function function_returning_enum .applicants_by_stage(
1316+ wanted_stage function_returning_enum .stage_options_enum_domain
1317+ ) returns setof function_returning_enum .applicants
1318+ as $$
1319+ select * from function_returning_enum .applicants a where a .stage = wanted_stage;
1320+ $$ language sql stable;
1321+
1322+ create function function_returning_enum .applicants_by_favorite_pet(
1323+ pet function_returning_enum .animal_type
1324+ ) returns setof function_returning_enum .applicants
1325+ as $$
1326+ select * from function_returning_enum .applicants a where a .favorite_pet = pet;
1327+ $$ language sql stable;
1328+
1329+ create function function_returning_enum .applicants_pet_food(
1330+ a function_returning_enum .applicants
1331+ ) returns function_returning_enum .animal_type
1332+ as $$
1333+ select (case
1334+ when a .favorite_pet = ' FISH' then null
1335+ when a .favorite_pet = ' CAT' then ' FISH'
1336+ when a .favorite_pet = ' DOG' then ' CAT'
1337+ else null
1338+ end)::function_returning_enum .animal_type ;
1339+ $$ language sql stable;
1340+ comment on function function_returning_enum.applicants_pet_food is E' @filterable' ;
1341+
1342+ create domain function_returning_enum .transportation as text ;
1343+ comment on domain function_returning_enum.transportation is E' @enum enum_table_transportation_enum' ;
1344+
1345+ create function function_returning_enum .applicants_by_transportation(
1346+ transportation function_returning_enum .transportation
1347+ ) returns setof function_returning_enum .applicants
1348+ as $$
1349+ select * from function_returning_enum .applicants a where a .transportation = applicants_by_transportation .transportation ;
1350+ $$ language sql stable;
1351+
1352+ create function function_returning_enum .applicants_favorite_pet_transportation(
1353+ a function_returning_enum .applicants
1354+ ) returns function_returning_enum .transportation
1355+ as $$
1356+ select (case
1357+ when a .favorite_pet = ' FISH' then ' SUBWAY'
1358+ when a .favorite_pet = ' CAT' then ' CAR'
1359+ when a .favorite_pet = ' DOG' then ' BIKE'
1360+ else null
1361+ end)::function_returning_enum .transportation ;
1362+ $$ language sql stable;
1363+ comment on function function_returning_enum.applicants_favorite_pet_transportation is E' @filterable' ;
0 commit comments