Skip to content

Commit

Permalink
fix: error when a function returns RECORD or SETOF RECORD
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla authored Aug 4, 2023
1 parent 0fce7ca commit aa53623
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #2862, Fix null filtering on embedded resource when using a column name equal to the relation name - @steve-chavez
- #1586, Fix function parameters of type character and bit not ignoring length - @laurenceisla
+ Fixes the error "value too long for type character(1)" when the char length of the parameter was bigger than one.
- #2881, Fix error when a function returns `RECORD` or `SET OF RECORD` - @laurenceisla

## [11.1.0] - 2023-06-07

Expand Down
3 changes: 2 additions & 1 deletion src/PostgREST/Query/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mutatePlanToQuery (Delete mainQi logicForest range ordts returnings)

callPlanToQuery :: CallPlan -> PgVersion -> SQL.Snippet
callPlanToQuery (FunctionCall qi params args returnsScalar returnsSetOfScalar returnsCompositeAlias returnings) pgVer =
"SELECT " <> (if returnsScalar || returnsSetOfScalar then "pgrst_call AS pgrst_scalar " else returnedColumns) <> " " <>
"SELECT " <> (if returnsScalar || returnsSetOfScalar then "pgrst_call.pgrst_scalar" else returnedColumns) <> " " <>
fromCall
where
fromCall = case params of
Expand All @@ -176,6 +176,7 @@ callPlanToQuery (FunctionCall qi params args returnsScalar returnsSetOfScalar re

callIt :: SQL.Snippet -> SQL.Snippet
callIt argument | pgVer < pgVersion130 && pgVer >= pgVersion110 && returnsCompositeAlias = "(SELECT (" <> fromQi qi <> "(" <> argument <> ")).*) pgrst_call"
| returnsScalar || returnsSetOfScalar = "(SELECT " <> fromQi qi <> "(" <> argument <> ") pgrst_scalar) pgrst_call"
| otherwise = fromQi qi <> "(" <> argument <> ") pgrst_call"

fmtParams :: [RoutineParam] -> SQL.Snippet
Expand Down
21 changes: 21 additions & 0 deletions test/spec/Feature/Query/RpcSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,27 @@ spec actualPgVersion =
`shouldRespondWith`
[json|null|]

when (actualPgVersion >= pgVersion110) $ do
it "returns a record type" $ do
post "/rpc/returns_record"
""
`shouldRespondWith`
[json|{"id":1,"name":"Windows 7","client_id":1}|]
post "/rpc/returns_record_params"
[json|{"id":1, "name": "Windows%"}|]
`shouldRespondWith`
[json|{"id":1,"name":"Windows 7","client_id":1}|]

it "returns a setof record type" $ do
post "/rpc/returns_setof_record"
""
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]|]
post "/rpc/returns_setof_record_params"
[json|{"id":1,"name":"Windows%"}|]
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]|]

context "different types when overloaded" $ do
it "returns composite type" $
post "/rpc/ret_point_overloaded"
Expand Down
16 changes: 16 additions & 0 deletions test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3352,3 +3352,19 @@ create or replace function bit_param_insert(bit_ bit(4), bit_arr bit(4)[])
returns void as $$
insert into bitchar_with_length(bit, bit_arr) values($1, $2);
$$ language sql;

create function returns_record() returns record as $$
select * from projects limit 1;
$$ language sql;

create function returns_record_params(id int, name text) returns record as $$
select * from projects p where p.id = $1 and p.name like $2;
$$ language sql;

create function returns_setof_record() returns setof record as $$
select * from projects limit 2;
$$ language sql;

create function returns_setof_record_params(id int, name text) returns setof record as $$
select * from projects p where p.id >= $1 and p.name like $2;
$$ language sql;

0 comments on commit aa53623

Please sign in to comment.