Thread: SPI_prepare for semi-unknown types

SPI_prepare for semi-unknown types

From
Alex Pilosov
Date:
Is there a way to call SPI_prepare in case the precise datatype is not
known? 

Example, having a statement like 'select count(*) from foo where
fieldname=$1' where I know that $1 is a stringish type and it _should_ be
convertable using xxx_in (CString-to-datum conversion functions), however,
I do not know the precise type (could be name or varchar or text).

I understand that SPI_execute uses pg_parse_and_rewrite to do the actual
parsing and binding of function OIDs to the argument types. I'm looking at
transformExpr in parser/parse_expr.c, it seems to be the only place that
actually does it. 

There, I'm not sure how would it handle the case where paramtype is
specified as 'char', but it actually may need to be cast to 'text' for
execution. I guess I can find out by just trying it, just wanted to ask
first. :)

Ideally, I would like to be able to say "I don't know what this parameter
is like, treat it like it would be treated coming in a fully-formed query
(i.e. using CSTRING conversions)", but I'm not sure if its possible.

Thanks to anyone who can offer some help.

-alex



Re: SPI_prepare for semi-unknown types

From
Tom Lane
Date:
Alex Pilosov <alex@pilosoft.com> writes:
> Example, having a statement like 'select count(*) from foo where
> fieldname=$1' where I know that $1 is a stringish type and it _should_ be
> convertable using xxx_in (CString-to-datum conversion functions), however,
> I do not know the precise type (could be name or varchar or text).

Declare the parameter as text, and then coerce what you are given to
text before you execute the statement.  You don't get any free
adaptation to new datatypes in an already-completed plan.
        regards, tom lane