forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plvsubst.c
277 lines (224 loc) · 5.71 KB
/
plvsubst.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
This code implements one part of functonality of
free available library PL/Vision. Please look www.quest.com
Original author: Steven Feuerstein, 1996 - 2002
PostgreSQL implementation author: Pavel Stehule, 2006-2018
This module is under BSD Licence
History:
1.0. first public version 22. September 2006
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "string.h"
#include "stdlib.h"
#include "utils/pg_locale.h"
#include "mb/pg_wchar.h"
#include "lib/stringinfo.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/memutils.h"
#include "utils/lsyscache.h"
#include "access/tupmacs.h"
#include "orafce.h"
#include "builtins.h"
PG_FUNCTION_INFO_V1(plvsubst_string_array);
PG_FUNCTION_INFO_V1(plvsubst_string_string);
PG_FUNCTION_INFO_V1(plvsubst_setsubst);
PG_FUNCTION_INFO_V1(plvsubst_setsubst_default);
PG_FUNCTION_INFO_V1(plvsubst_subst);
#define C_SUBST "%s"
text *c_subst = NULL;
static void
init_c_subst()
{
if (!c_subst)
{
MemoryContext oldctx;
oldctx = MemoryContextSwitchTo(TopMemoryContext);
c_subst = cstring_to_text(C_SUBST);
MemoryContextSwitchTo(oldctx);
}
}
static void
set_c_subst(text *sc)
{
MemoryContext oldctx;
if (c_subst)
pfree(c_subst);
oldctx = MemoryContextSwitchTo(TopMemoryContext);
c_subst = sc ? TextPCopy(sc) : cstring_to_text(C_SUBST);
MemoryContextSwitchTo(oldctx);
}
static text*
plvsubst_string(text *template_in, ArrayType *vals_in, text *c_subst, FunctionCallInfo fcinfo)
{
ArrayType *v = vals_in;
int nitems,
*dims,
ndims;
char *p;
int16 typlen;
bool typbyval;
char typalign;
char typdelim;
Oid typelem;
Oid typiofunc;
FmgrInfo proc;
int i = 0, items = 0;
StringInfo sinfo;
const char *template_str;
int template_len;
char *sizes;
int *positions;
int subst_mb_len;
int subst_len;
const bits8 *bitmap;
int bitmask;
if (v != NULL && (ndims = ARR_NDIM(v)) > 0)
{
if (ndims != 1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid parameter"),
errdetail("Array of arguments has wrong dimension: %d", ndims)));
p = (char *) ARR_DATA_PTR(v);
dims = ARR_DIMS(v);
nitems = ArrayGetNItems(ndims, dims);
bitmap = ARR_NULLBITMAP(v);
get_type_io_data(ARR_ELEMTYPE(v), IOFunc_output,
&typlen, &typbyval,
&typalign, &typdelim,
&typelem, &typiofunc);
fmgr_info_cxt(typiofunc, &proc, fcinfo->flinfo->fn_mcxt);
}
else
{
nitems = 0;
p = NULL;
bitmap = NULL;
}
template_str = VARDATA(template_in);
template_len = ora_mb_strlen(template_in, &sizes, &positions);
subst_mb_len = ora_mb_strlen1(c_subst);
subst_len = VARSIZE_ANY_EXHDR(c_subst);
sinfo = makeStringInfo();
bitmask = 1;
for (i = 0; i < template_len; i++)
{
if (strncmp(&template_str[positions[i]], VARDATA(c_subst), subst_len) == 0)
{
Datum itemvalue;
char *value;
if (items++ < nitems)
{
if (bitmap && (*bitmap & bitmask) == 0)
value = pstrdup("NULL");
else
{
itemvalue = fetch_att(p, typbyval, typlen);
value = DatumGetCString(FunctionCall3(&proc,
itemvalue,
ObjectIdGetDatum(typelem),
Int32GetDatum(-1)));
p = att_addlength_pointer(p, typlen, p);
p = (char *) att_align_nominal(p, typalign);
}
appendStringInfoString(sinfo, value);
pfree(value);
if (bitmap)
{
bitmask <<= 1;
if (bitmask == 0x100)
{
bitmap++;
bitmask = 1;
}
}
}
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("too few parameters specified for template string")));
i += subst_mb_len - 1;
}
else
appendBinaryStringInfo(sinfo, &template_str[positions[i]], sizes[i]);
}
return cstring_to_text(sinfo->data);
}
Datum
plvsubst_string_array(PG_FUNCTION_ARGS)
{
init_c_subst();
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
PG_RETURN_NULL();
PG_RETURN_TEXT_P(plvsubst_string(PG_GETARG_TEXT_P(0),
PG_GETARG_ARRAYTYPE_P(1),
PG_ARGISNULL(2) ? c_subst : PG_GETARG_TEXT_P(2),
fcinfo));
}
Datum
plvsubst_string_string(PG_FUNCTION_ARGS)
{
Datum r;
ArrayType *array;
#if PG_VERSION_NUM >= 120000
LOCAL_FCINFO(locfcinfo, 2);
#else
FunctionCallInfoData locfcinfo_data;
FunctionCallInfo locfcinfo = &locfcinfo_data;
#endif
Oid collation = PG_GET_COLLATION();
init_c_subst();
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
PG_RETURN_NULL();
/*
* I can't use DirectFunctionCall2
*/
InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 2, collation, NULL, NULL);
#if PG_VERSION_NUM >= 120000
locfcinfo->args[0].value = PG_GETARG_DATUM(1);
locfcinfo->args[1].value = PG_GETARG_IF_EXISTS(2, DATUM, CStringGetTextDatum(","));
locfcinfo->args[0].isnull = false;
locfcinfo->args[1].isnull = false;
#else
locfcinfo->arg[0] = PG_GETARG_DATUM(1);
locfcinfo->arg[1] = PG_GETARG_IF_EXISTS(2, DATUM, CStringGetTextDatum(","));
locfcinfo->argnull[0] = false;
locfcinfo->argnull[1] = false;
#endif
r = text_to_array(locfcinfo);
if (locfcinfo->isnull || r == (Datum) 0)
array = NULL;
else
array = DatumGetArrayTypeP(r);
PG_RETURN_TEXT_P(plvsubst_string(PG_GETARG_TEXT_P(0),
array,
PG_GETARG_IF_EXISTS(3, TEXT_P, c_subst),
fcinfo));
}
Datum
plvsubst_setsubst(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("substition is NULL"),
errdetail("Substitution keyword may not be NULL.")));
set_c_subst(PG_GETARG_TEXT_P(0));
PG_RETURN_VOID();
}
Datum
plvsubst_setsubst_default(PG_FUNCTION_ARGS)
{
set_c_subst(NULL);
PG_RETURN_VOID();
}
Datum
plvsubst_subst(PG_FUNCTION_ARGS)
{
init_c_subst();
PG_RETURN_TEXT_P(TextPCopy(c_subst));
}