Skip to content

Commit 9a53848

Browse files
committed
Add PostgreSQL-version agnostic type lookup
1 parent ce1240c commit 9a53848

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

test/src/test_with_clause_parser.c

+22-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <utils/elog.h>
1717
#include <utils/lsyscache.h>
1818
#include <utils/memutils.h>
19+
#include <utils/regproc.h>
1920

2021
#include "annotations.h"
2122
#include "export.h"
@@ -245,21 +246,30 @@ TS_TEST_FN(ts_test_with_clause_parse)
245246
HeapTuple tuple;
246247
WithClauseValue *result;
247248

248-
/* Look up any missing type ids before using it below to allow
249-
* user-defined types. Note that this will not look up types we have found
250-
* in previous calls of this function. */
249+
/*
250+
* Look up any missing type ids before using it below to allow
251+
* user-defined types.
252+
*
253+
* Note that this will not look up types we have found in previous calls
254+
* of this function.
255+
*
256+
* We use the slightly more complicated way of calling to_regtype since
257+
* that exists on all versions of PostgreSQL. We cannot use regtypein
258+
* since that can generate errors and we do not want to deal with that.
259+
*/
251260
for (unsigned int i = 0; i < TS_ARRAY_LEN(test_args); ++i)
252261
{
262+
LOCAL_FCINFO(fcinfo_in, 1);
253263
Datum result;
254-
ErrorSaveContext escontext = { T_ErrorSaveContext };
255-
if (test_args[i].type_id == InvalidOid &&
256-
DirectInputFunctionCallSafe(regtypein,
257-
(char *) test_args[i].arg_names[0],
258-
InvalidOid,
259-
-1,
260-
(Node *) &escontext,
261-
&result))
262-
test_args[i].type_id = DatumGetObjectId(result);
264+
if (test_args[i].type_id == InvalidOid)
265+
{
266+
InitFunctionCallInfoData(*fcinfo_in, NULL, 1, InvalidOid, NULL, NULL);
267+
fcinfo_in->args[0].value = CStringGetTextDatum(test_args[i].arg_names[0]);
268+
fcinfo_in->args[0].isnull = false;
269+
result = to_regtype(fcinfo_in);
270+
if (!fcinfo_in->isnull)
271+
test_args[i].type_id = DatumGetObjectId(result);
272+
}
263273
}
264274

265275
if (SRF_IS_FIRSTCALL())

tsl/test/shared/expected/with_clause_parser.out

+3
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,6 @@ SELECT * FROM test_with_clause_parse('{
402402
sqlstate_raise | | | | | |
403403
(7 rows)
404404

405+
\c :TEST_DBNAME :ROLE_SUPERUSER
406+
DROP TYPE sqlstate_raise CASCADE;
407+
NOTICE: drop cascades to 2 other objects

tsl/test/shared/sql/with_clause_parser.sql

+3
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,6 @@ SELECT * FROM test_with_clause_parse('{
139139
{"c", "name", "bar"},
140140
{"d", "regclass", "pg_type"}
141141
}');
142+
143+
\c :TEST_DBNAME :ROLE_SUPERUSER
144+
DROP TYPE sqlstate_raise CASCADE;

0 commit comments

Comments
 (0)