forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionPlan.c
More file actions
330 lines (311 loc) · 7.5 KB
/
Copy pathExecutionPlan.c
File metadata and controls
330 lines (311 loc) · 7.5 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
* Distributed under the terms shown in the file COPYRIGHT
* found in the root folder of this project or at
* http://eng.tada.se/osprojects/COPYRIGHT.html
*
* @author Thomas Hallgren
*/
#include <postgres.h>
#include <executor/tuptable.h>
#include <utils/guc.h>
#include "org_postgresql_pljava_internal_ExecutionPlan.h"
#include "pljava/Invocation.h"
#include "pljava/Exception.h"
#include "pljava/Function.h"
#include "pljava/SPI.h"
#include "pljava/type/Oid.h"
#include "pljava/type/Portal.h"
#include "pljava/type/String.h"
/* Class 07 - Dynamic SQL Error */
#define ERRCODE_PARAMETER_COUNT_MISMATCH MAKE_SQLSTATE('0','7', '0','0','1')
/* Make this datatype available to the postgres system.
*/
extern void ExecutionPlan_initialize(void);
void ExecutionPlan_initialize(void)
{
JNINativeMethod methods[] =
{
{
"_cursorOpen",
"(JJLjava/lang/String;[Ljava/lang/Object;)Lorg/postgresql/pljava/internal/Portal;",
Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen
},
{
"_isCursorPlan",
"(J)Z",
Java_org_postgresql_pljava_internal_ExecutionPlan__1isCursorPlan
},
{
"_execute",
"(JJ[Ljava/lang/Object;I)I",
Java_org_postgresql_pljava_internal_ExecutionPlan__1execute
},
{
"_prepare",
"(JLjava/lang/String;[Lorg/postgresql/pljava/internal/Oid;)J",
Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare
},
{
"_invalidate",
"(J)V",
Java_org_postgresql_pljava_internal_ExecutionPlan__1invalidate
},
{ 0, 0, 0 }
};
PgObject_registerNatives("org/postgresql/pljava/internal/ExecutionPlan", methods);
}
static bool coerceObjects(void* ePlan, jobjectArray jvalues, Datum** valuesPtr, char** nullsPtr)
{
char* nulls = 0;
Datum* values = 0;
int count = SPI_getargcount(ePlan);
if((jvalues == 0 && count != 0)
|| (jvalues != 0 && count != JNI_getArrayLength(jvalues)))
{
Exception_throw(ERRCODE_PARAMETER_COUNT_MISMATCH,
"Number of values does not match number of arguments for prepared plan");
return false;
}
if(count > 0)
{
int idx;
jobject typeMap = Invocation_getTypeMap();
values = (Datum*)palloc(count * sizeof(Datum));
for(idx = 0; idx < count; ++idx)
{
Oid typeId = SPI_getargtypeid(ePlan, idx);
Type type = Type_fromOid(typeId, typeMap);
jobject value = JNI_getObjectArrayElement(jvalues, idx);
if(value != 0)
{
values[idx] = Type_coerceObject(type, value);
JNI_deleteLocalRef(value);
}
else
{
values[idx] = 0;
if(nulls == 0)
{
nulls = (char*)palloc(count+1);
memset(nulls, ' ', count); /* all values non-null initially */
nulls[count] = 0;
*nullsPtr = nulls;
}
nulls[idx] = 'n';
}
}
}
*valuesPtr = values;
*nullsPtr = nulls;
return true;
}
/****************************************
* JNI methods
****************************************/
/*
* Class: org_postgresql_pljava_internal_ExecutionPlan
* Method: _cursorOpen
* Signature: (JJLjava/lang/String;[Ljava/lang/Object;)Lorg/postgresql/pljava/internal/Portal;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_internal_ExecutionPlan__1cursorOpen(JNIEnv* env, jclass clazz, jlong _this, jlong threadId, jstring cursorName, jobjectArray jvalues)
{
jobject jportal = 0;
if(_this != 0)
{
BEGIN_NATIVE
STACK_BASE_VARS
STACK_BASE_PUSH(threadId)
PG_TRY();
{
Ptr2Long p2l;
Datum* values = 0;
char* nulls = 0;
p2l.longVal = _this;
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
{
Portal portal;
char* name = 0;
if(cursorName != 0)
name = String_createNTS(cursorName);
Invocation_assertConnect();
portal = SPI_cursor_open(
name, p2l.ptrVal, values, nulls, Function_isCurrentReadOnly());
if(name != 0)
pfree(name);
if(values != 0)
pfree(values);
if(nulls != 0)
pfree(nulls);
jportal = Portal_create(portal);
}
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_cursor_open");
}
PG_END_TRY();
STACK_BASE_POP()
END_NATIVE
}
return jportal;
}
/*
* Class: org_postgresql_pljava_internal_ExecutionPlan
* Method: _isCursorPlan
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL
Java_org_postgresql_pljava_internal_ExecutionPlan__1isCursorPlan(JNIEnv* env, jclass clazz, jlong _this)
{
jboolean result = JNI_FALSE;
if(_this != 0)
{
BEGIN_NATIVE
PG_TRY();
{
Ptr2Long p2l;
p2l.longVal = _this;
Invocation_assertConnect();
result = (jboolean)SPI_is_cursor_plan(p2l.ptrVal);
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_is_cursor_plan");
}
PG_END_TRY();
END_NATIVE
}
return result;
}
/*
* Class: org_postgresql_pljava_internal_ExecutionPlan
* Method: _execute
* Signature: (JJ[Ljava/lang/Object;I)V
*/
JNIEXPORT jint JNICALL
Java_org_postgresql_pljava_internal_ExecutionPlan__1execute(JNIEnv* env, jclass clazz, jlong _this, jlong threadId, jobjectArray jvalues, jint count)
{
jint result = 0;
if(_this != 0)
{
BEGIN_NATIVE
STACK_BASE_VARS
STACK_BASE_PUSH(threadId)
PG_TRY();
{
Ptr2Long p2l;
Datum* values = 0;
char* nulls = 0;
p2l.longVal = _this;
if(coerceObjects(p2l.ptrVal, jvalues, &values, &nulls))
{
Invocation_assertConnect();
result = (jint)SPI_execute_plan(
p2l.ptrVal, values, nulls, Function_isCurrentReadOnly(), (int)count);
if(result < 0)
Exception_throwSPI("execute_plan", result);
if(values != 0)
pfree(values);
if(nulls != 0)
pfree(nulls);
}
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_execute_plan");
}
PG_END_TRY();
STACK_BASE_POP()
END_NATIVE
}
return result;
}
/*
* Class: org_postgresql_pljava_internal_ExecutionPlan
* Method: _prepare
* Signature: (JLjava/lang/String;[Lorg/postgresql/pljava/internal/Oid;)J;
*/
JNIEXPORT jlong JNICALL
Java_org_postgresql_pljava_internal_ExecutionPlan__1prepare(JNIEnv* env, jclass clazz, jlong threadId, jstring jcmd, jobjectArray paramTypes)
{
jlong result = 0;
BEGIN_NATIVE
STACK_BASE_VARS
STACK_BASE_PUSH(threadId)
PG_TRY();
{
char* cmd;
void* ePlan;
int paramCount = 0;
Oid* paramOids = 0;
if(paramTypes != 0)
{
paramCount = JNI_getArrayLength(paramTypes);
if(paramCount > 0)
{
int idx;
paramOids = (Oid*)palloc(paramCount * sizeof(Oid));
for(idx = 0; idx < paramCount; ++idx)
{
jobject joid = JNI_getObjectArrayElement(paramTypes, idx);
paramOids[idx] = Oid_getOid(joid);
JNI_deleteLocalRef(joid);
}
}
}
cmd = String_createNTS(jcmd);
Invocation_assertConnect();
ePlan = SPI_prepare(cmd, paramCount, paramOids);
pfree(cmd);
if(ePlan == 0)
Exception_throwSPI("prepare", SPI_result);
else
{
Ptr2Long p2l;
/* Make the plan durable
*/
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
p2l.ptrVal = SPI_saveplan(ePlan);
result = p2l.longVal;
SPI_freeplan(ePlan); /* Get rid of the original, nobody can see it anymore */
}
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_prepare");
}
PG_END_TRY();
STACK_BASE_POP()
END_NATIVE
return result;
}
/*
* Class: org_postgresql_pljava_internal_ExecutionPlan
* Method: _invalidate
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_postgresql_pljava_internal_ExecutionPlan__1invalidate(JNIEnv* env, jclass clazz, jlong _this)
{
/* The plan is not cached as a normal JavaHandle since its made
* persistent.
*/
if(_this != 0)
{
BEGIN_NATIVE_NO_ERRCHECK
PG_TRY();
{
Ptr2Long p2l;
p2l.longVal = _this;
SPI_freeplan(p2l.ptrVal);
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_freeplan");
}
PG_END_TRY();
END_NATIVE
}
}