Skip to content

Commit 64a2e1f

Browse files
committed
Apply quote_literal to the start_with argument of connectby. Fixes problem
reported by David Garamond when working with bytea parent and child keys.
1 parent 92bec9a commit 64a2e1f

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

contrib/tablefunc/tablefunc.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
7979
MemoryContext per_query_ctx,
8080
AttInMetadata *attinmeta,
8181
Tuplestorestate *tupstore);
82+
static char *quote_literal_cstr(char *rawstr);
8283

8384
typedef struct
8485
{
@@ -1319,23 +1320,23 @@ build_tuplestore_recursively(char *key_fld,
13191320
/* Build initial sql statement */
13201321
if (!show_serial)
13211322
{
1322-
appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = '%s' AND %s IS NOT NULL AND %s <> %s",
1323+
appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
13231324
key_fld,
13241325
parent_key_fld,
13251326
relname,
13261327
parent_key_fld,
1327-
start_with,
1328+
quote_literal_cstr(start_with),
13281329
key_fld, key_fld, parent_key_fld);
13291330
serial_column = 0;
13301331
}
13311332
else
13321333
{
1333-
appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = '%s' AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1334+
appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
13341335
key_fld,
13351336
parent_key_fld,
13361337
relname,
13371338
parent_key_fld,
1338-
start_with,
1339+
quote_literal_cstr(start_with),
13391340
key_fld, key_fld, parent_key_fld,
13401341
orderby_fld);
13411342
serial_column = 1;
@@ -1691,3 +1692,21 @@ make_crosstab_tupledesc(TupleDesc spi_tupdesc, int num_categories)
16911692

16921693
return tupdesc;
16931694
}
1695+
1696+
/*
1697+
* Return a properly quoted literal value.
1698+
* Uses quote_literal in quote.c
1699+
*/
1700+
static char *
1701+
quote_literal_cstr(char *rawstr)
1702+
{
1703+
text *rawstr_text;
1704+
text *result_text;
1705+
char *result;
1706+
1707+
rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
1708+
result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text)));
1709+
result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));
1710+
1711+
return result;
1712+
}

0 commit comments

Comments
 (0)