-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_map.c
190 lines (153 loc) · 4.1 KB
/
pg_map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "postgres.h"
#include "funcapi.h"
#include "utils/lsyscache.h"
#include "catalog/pg_type.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_proc.h"
#include "access/htup_details.h"
#include "utils/syscache.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/tqual.h"
#include "utils/fmgroids.h"
#include <string.h>
#include <stdio.h>
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_oid_map);
PG_FUNCTION_INFO_V1(pg_procname_map);
static AnyArrayType *anyarray_map(Oid procId, AnyArrayType *array, int reclimit);
static Oid get_cast_proc(Oid src, Oid dst);
static Oid get_proc_arg_oid(Oid procId);
static bool contains(text *str, text *s);
Datum
pg_oid_map(PG_FUNCTION_ARGS)
{
Oid procId = PG_GETARG_OID(0);
AnyArrayType *array = PG_GETARG_ANY_ARRAY(1);
AnyArrayType *result;
result = anyarray_map(procId, array, 1);
PG_RETURN_ARRAYTYPE_P(result);
}
Datum
pg_procname_map(PG_FUNCTION_ARGS)
{
MemoryContext oldcontext = CurrentMemoryContext;
text *pro_name = PG_GETARG_TEXT_PP(0);
AnyArrayType *array = PG_GETARG_ANY_ARRAY(1);
Oid procId = InvalidOid;
AnyArrayType *result;
FmgrInfo finfo;
if (contains(pro_name, cstring_to_text("(")) ||
contains(pro_name, cstring_to_text(")")))
{
fmgr_info(F_TO_REGPROCEDURE, &finfo);
}
else
fmgr_info(F_TO_REGPROC, &finfo);
PG_TRY();
{
procId = DatumGetObjectId(FunctionCall1(&finfo,
PointerGetDatum(pro_name)));
}
PG_CATCH();
{
MemoryContextSwitchTo(oldcontext);
elog(ERROR,
"can't find function \"%s\"",
text_to_cstring(pro_name));
}
PG_END_TRY();
Assert(procId != InvalidOid);
result = anyarray_map(procId, array, 1);
PG_RETURN_ARRAYTYPE_P(result);
}
static AnyArrayType *
anyarray_map(Oid procId, AnyArrayType *array, int reclimit)
{
FmgrInfo funcinfo;
FunctionCallInfoData locfcinfo;
ArrayMapState *amstate;
Oid elemtype = AARR_ELEMTYPE(array);
Oid argtype = get_proc_arg_oid(procId);
if (reclimit < 0)
elog(ERROR, "unlimited recursion");
if (elemtype != argtype)
{
AnyArrayType *newarray = anyarray_map(get_cast_proc(elemtype,
argtype),
array,
reclimit - 1);
pfree(array);
array = newarray;
}
fmgr_info(procId, &funcinfo);
amstate = (ArrayMapState *) palloc0(sizeof(ArrayMapState));
InitFunctionCallInfoData(locfcinfo, &funcinfo, 1,
InvalidOid, NULL, NULL);
locfcinfo.arg[0] = PointerGetDatum(array);
locfcinfo.argnull[0] = false;
return DatumGetAnyArray(array_map(&locfcinfo,
get_func_rettype(procId),
amstate));
}
static Oid
get_cast_proc(Oid src, Oid dst)
{
Relation cast_heap;
HeapScanDesc heapScan;
Oid castproc = InvalidOid;
HeapTuple tuple;
ScanKeyData keys[2];
ScanKeyInit(&keys[0],
Anum_pg_cast_castsource,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(src));
ScanKeyInit(&keys[1],
Anum_pg_cast_casttarget,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(dst));
cast_heap = heap_open(CastRelationId, AccessShareLock);
heapScan = heap_beginscan(cast_heap, SnapshotSelf, 2, keys);
tuple = heap_getnext(heapScan, ForwardScanDirection);
if (HeapTupleIsValid(tuple))
{
Datum value;
bool isnull = true;
value = heap_getattr(tuple, Anum_pg_cast_castfunc,
RelationGetDescr(cast_heap), &isnull);
castproc = DatumGetObjectId(value);
}
heap_endscan(heapScan);
heap_close(cast_heap, AccessShareLock);
if (castproc != InvalidOid)
return castproc;
else
elog(ERROR, "cast from %d to %d not found", src, dst);
}
static Oid
get_proc_arg_oid(Oid procId)
{
HeapTuple htup;
Oid arg = InvalidOid;
htup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procId));
if (HeapTupleIsValid(htup))
{
Form_pg_proc proctup = (Form_pg_proc) GETSTRUCT(htup);
if (proctup->proargtypes.dim1 > 0)
arg = proctup->proargtypes.values[0];
ReleaseSysCache(htup);
}
if (arg == InvalidOid)
elog(ERROR, "can't get argument type");
return arg;
}
static bool
contains(text *str, text *s)
{
Datum result;
result = DirectFunctionCall2(textpos,
PointerGetDatum(str),
PointerGetDatum(s));
return DatumGetInt32(result) != 0;
}