I implemented a Postgres function that takes the table name, column name, and column id as parameters.
It is dynamic because the result varies according to the table name.
This works very well when tested in sql.
postgresql function
create or replace function test(layer_name anyelement, field_name text, object_id text)
returns setof anyelement
language plpgsql
as $function$
begin
return query execute format('
select
*
from
%s
where
%s = cast($1 as int4)'
, pg_typeof(layer_name), field_name)
using object_id;
end;
$function$
;
call
select * from test(null::table_name,'field_name','2');
But I call the function using mybatis, and it fails without receiving results.
<select id="test" parameterType="map" resultType="map">
select * from test(null::#{layer_name}, #{field_name}, #{object_id})
</select>
or
<select id="test" parameterType="map" resultType="map">
select * from test(cast(null as #{layer_name}), #{field_name}, #{object_id})
</select>
What am I missing and what do I need to try to make it work?
Error Code 1
### The error occurred while setting parameters
### SQL: select * from test(null::?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
Error Code 2
### The error occurred while setting parameters
### SQL: select * from test(cast(null as ?), ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"