Skip to content

Commit eaeb407

Browse files
committed
Keep all Consts as expressions while deparsing.
($n placeholders will be filled with evaluated value when sending the query). This allows to prepare 'generic' (with placeholders) queries on the remote side and later reuse them.
1 parent 9728fb2 commit eaeb407

File tree

1 file changed

+19
-87
lines changed

1 file changed

+19
-87
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 19 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,99 +2247,31 @@ deparseVar(Var *node, deparse_expr_cxt *context)
22472247
static void
22482248
deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
22492249
{
2250-
StringInfo buf = context->buf;
2251-
Oid typoutput;
2252-
bool typIsVarlena;
2253-
char *extval;
2254-
bool isfloat = false;
2255-
bool needlabel;
2256-
2257-
if (node->constisnull)
2250+
if (context->params_list)
22582251
{
2259-
appendStringInfoString(buf, "NULL");
2260-
if (showtype >= 0)
2261-
appendStringInfo(buf, "::%s",
2262-
deparse_type_name(node->consttype,
2263-
node->consttypmod));
2264-
return;
2265-
}
2252+
int pindex = 0;
2253+
ListCell *lc;
22662254

2267-
getTypeOutputInfo(node->consttype,
2268-
&typoutput, &typIsVarlena);
2269-
extval = OidOutputFunctionCall(typoutput, node->constvalue);
2255+
/* find its index in params_list */
2256+
foreach(lc, *context->params_list)
2257+
{
2258+
pindex++;
2259+
if (equal(node, (Node *) lfirst(lc)))
2260+
break;
2261+
}
2262+
if (lc == NULL)
2263+
{
2264+
/* not in list, so add it */
2265+
pindex++;
2266+
*context->params_list = lappend(*context->params_list, node);
2267+
}
22702268

2271-
switch (node->consttype)
2272-
{
2273-
case INT2OID:
2274-
case INT4OID:
2275-
case INT8OID:
2276-
case OIDOID:
2277-
case FLOAT4OID:
2278-
case FLOAT8OID:
2279-
case NUMERICOID:
2280-
{
2281-
/*
2282-
* No need to quote unless it's a special value such as 'NaN'.
2283-
* See comments in get_const_expr().
2284-
*/
2285-
if (strspn(extval, "0123456789+-eE.") == strlen(extval))
2286-
{
2287-
if (extval[0] == '+' || extval[0] == '-')
2288-
appendStringInfo(buf, "(%s)", extval);
2289-
else
2290-
appendStringInfoString(buf, extval);
2291-
if (strcspn(extval, "eE.") != strlen(extval))
2292-
isfloat = true; /* it looks like a float */
2293-
}
2294-
else
2295-
appendStringInfo(buf, "'%s'", extval);
2296-
}
2297-
break;
2298-
case BITOID:
2299-
case VARBITOID:
2300-
appendStringInfo(buf, "B'%s'", extval);
2301-
break;
2302-
case BOOLOID:
2303-
if (strcmp(extval, "t") == 0)
2304-
appendStringInfoString(buf, "true");
2305-
else
2306-
appendStringInfoString(buf, "false");
2307-
break;
2308-
default:
2309-
deparseStringLiteral(buf, extval);
2310-
break;
2269+
printRemoteParam(pindex, node->consttype, node->consttypmod, context);
23112270
}
2312-
2313-
pfree(extval);
2314-
2315-
if (showtype < 0)
2316-
return;
2317-
2318-
/*
2319-
* For showtype == 0, append ::typename unless the constant will be
2320-
* implicitly typed as the right type when it is read in.
2321-
*
2322-
* XXX this code has to be kept in sync with the behavior of the parser,
2323-
* especially make_const.
2324-
*/
2325-
switch (node->consttype)
2271+
else
23262272
{
2327-
case BOOLOID:
2328-
case INT4OID:
2329-
case UNKNOWNOID:
2330-
needlabel = false;
2331-
break;
2332-
case NUMERICOID:
2333-
needlabel = !isfloat || (node->consttypmod >= 0);
2334-
break;
2335-
default:
2336-
needlabel = true;
2337-
break;
2273+
printRemotePlaceholder(node->consttype, node->consttypmod, context);
23382274
}
2339-
if (needlabel || showtype > 0)
2340-
appendStringInfo(buf, "::%s",
2341-
deparse_type_name(node->consttype,
2342-
node->consttypmod));
23432275
}
23442276

23452277
/*

0 commit comments

Comments
 (0)