-
Notifications
You must be signed in to change notification settings - Fork 3
/
jbridge.c
562 lines (464 loc) · 16.1 KB
/
jbridge.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <pg_jinx.h>
static jclass bridgeClass;
static jmethodID bridgeInit;
extern void bridge_initialize(void);
static void throwJavaException(const char* funcName) {
jobject ex;
PG_TRY(); {
jarray jstrs;
jintArray jints;
ErrorData* errorData = CopyErrorData();
int eints[] = {0,0,0,0,0,0,0,0,0};
jints = (*env)->NewIntArray(env, 9);
eints[0]=errorData->elevel;
eints[1]=errorData->saved_errno;
eints[2]=errorData->sqlerrcode;
eints[3]=errorData->lineno;
eints[4]=errorData->cursorpos;
eints[5]=errorData->internalpos;
eints[6]=errorData->output_to_server;
eints[7]=errorData->output_to_client;
eints[8]=errorData->show_funcname;
(*env)->SetIntArrayRegion(env, jints, 0, 9, eints);
jstrs = (*env)->NewObjectArray(env, 8, stringClass, NULL);
(*env)->SetObjectArrayElement(env, jstrs, 0, makeJavaString(errorData->message) );
(*env)->SetObjectArrayElement(env, jstrs, 1, makeJavaString(errorData->filename));
(*env)->SetObjectArrayElement(env, jstrs, 2, makeJavaString(errorData->funcname));
(*env)->SetObjectArrayElement(env, jstrs, 3, makeJavaString(errorData->detail));
(*env)->SetObjectArrayElement(env, jstrs, 4, makeJavaString(errorData->hint));
(*env)->SetObjectArrayElement(env, jstrs, 5, makeJavaString(errorData->context));
(*env)->SetObjectArrayElement(env, jstrs, 6, makeJavaString(errorData->internalquery));
(*env)->SetObjectArrayElement(env, jstrs, 7, makeJavaString(funcName));
ex = (*env)->NewObject(env,serverExceptionClass, serverExceptionConstructor, jints, jstrs );
elog(DEBUG1, "Exception in function %s", funcName);
FlushErrorState();
FreeErrorData(errorData);
(*env)->Throw(env,ex);
}
PG_CATCH(); { elog(WARNING, "Exception while generating exception"); }
PG_END_TRY();
}
static void Exception_throw(int errCode, const char* errMessage, ...) {
char buf[1024];
va_list args;
jstring message;
jstring sqlState;
jobject ex;
int idx;
va_start(args, errMessage);
vsnprintf(buf, sizeof(buf), errMessage, args);
ereport(DEBUG3, (errcode(errCode), errmsg("%s", buf)));
PG_TRY(); {
message = makeJavaString(buf);
// unpack MAKE_SQLSTATE code
for (idx = 0; idx < 5; ++idx) {
buf[idx] = PGUNSIXBIT(errCode);
errCode >>= 6;
}
buf[idx] = 0;
sqlState = makeJavaString(buf);
ex = (*env)->NewObject(env, sqlException, sqlExceptionConstructor, message, sqlState);
(*env)->DeleteLocalRef(env,message);
(*env)->DeleteLocalRef(env,sqlState);
(*env)->Throw(env,ex);
}
PG_CATCH(); { ereport(WARNING, (errcode(errCode), errmsg("Exception while generating exception: %s", buf))); }
PG_END_TRY();
va_end(args);
}
static void Exception_throwSPI(const char* function, int errCode) {
Exception_throw(ERRCODE_INTERNAL_ERROR,
"SPI function SPI_%s failed with error %s", function,
SPI_result_code_string(errCode));
}
static JNIEXPORT void JNICALL pglog(JNIEnv* env, jclass cls, jint logLevel, jstring jstr) {
char* str = fromJavaString(jstr);
if(str != 0) {
// elog uses printf formatting but the logger does not so we must escape all '%' in the string.
char c;
const char* cp;
int percentCount = 0;
for(cp = str; (c = *cp) != 0; ++cp) {
if(c == '%') ++percentCount;
}
if(percentCount > 0) {
// Make room to expand all "%" to "%%"
char* str2 = palloc((cp - str) + percentCount + 1);
char* cp2 = str2;
/* Expand... */
for(cp = str; (c = *cp) != 0; ++cp) {
if(c == '%') *cp2++ = c;
*cp2++ = c;
}
*cp2 = 0;
pfree(str);
str = str2;
}
PG_TRY(); { elog(logLevel, "%s", str); pfree(str); }
PG_CATCH(); { throwJavaException("ereport"); }
PG_END_TRY();
}
}
static JNIEXPORT jlong JNICALL _modifyTuple(JNIEnv* env, jclass clazz, jlong _this, jlong _tuple, jintArray _indexes, jobjectArray _values) {
Relation self = (Relation)(_this);
jlong result = 0;
if(self != 0 && _tuple != 0) {
HeapTuple tuple = (HeapTuple)_tuple;
PG_TRY(); {
jint idx;
TupleDesc tupleDesc = self->rd_att;
jint count = (*env)->GetArrayLength(env, _indexes);
Datum* values = (Datum*)palloc(count * sizeof(Datum));
char* nulls = 0;
jint* javaIdxs = (*env)->GetIntArrayElements(env, _indexes, 0);
int* indexes;
if(sizeof(int) == sizeof(jint)) /* compiler will optimize this */
indexes = (int*)javaIdxs;
else indexes = (int*)palloc(count * sizeof(int));
for(idx = 0; idx < count; ++idx) {
int attIndex;
Oid typeId;
jobject value;
if(sizeof(int) == sizeof(jint)) /* compiler will optimize this */
attIndex = indexes[idx];
else {
attIndex = (int)javaIdxs[idx];
indexes[idx] = attIndex;
}
typeId = SPI_gettypeid(tupleDesc, attIndex);
if(!OidIsValid(typeId)) {
Exception_throw(ERRCODE_INVALID_DESCRIPTOR_INDEX,
"Invalid attribute index \"%d\"", attIndex);
return 0L; /* Exception */
}
value = (*env)->GetObjectArrayElement(env,_values, idx);
if(value != 0) values[idx] = objectToDatum(typeId, value);
else {
if(nulls == 0) {
nulls = (char*)palloc(count+1);
memset(nulls, ' ', count); /* all values non-null initially */
nulls[count] = 0;
}
nulls[idx] = 'n';
values[idx] = 0;
}
}
tuple = SPI_modifytuple(self, tuple, count, indexes, values, nulls);
if(tuple == 0)
Exception_throwSPI("modifytuple", SPI_result);
(*env)->ReleaseIntArrayElements(env, _indexes, javaIdxs, JNI_ABORT);
if(sizeof(int) != sizeof(jint)) /* compiler will optimize this */
pfree(indexes);
pfree(values);
if(nulls != 0)
pfree(nulls);
}
PG_CATCH();
{
tuple = 0;
throwJavaException("SPI_gettypeid");
}
PG_END_TRY();
if(tuple != 0)
result = (jlong)tuple;
}
return result;
}
static JNIEXPORT jarray JNICALL getUser(JNIEnv *env, jclass clazz) {
jarray result = (*env)->NewObjectArray(env, 2, stringClass, NULL);
CHECK_EXCEPTION("%s\nallocating string array","unused");
char * u = CStringGetDatum(GetUserNameFromId(GetUserId()));
(*env)->SetObjectArrayElement(env, result, 0, makeJavaString(u));
CHECK_EXCEPTION("%s\nstoring user", NULL);
u = CStringGetDatum(GetUserNameFromId(GetSessionUserId()));
(*env)->SetObjectArrayElement(env, result, 1, makeJavaString(u));
CHECK_EXCEPTION("%s\nstoring current user",NULL );
return result;
}
static JNIEXPORT jint JNICALL exec(JNIEnv* env, jclass cls, jlong threadId, jstring cmd, jint count) {
jint result = 0;
char* command = fromJavaString(cmd);
if(command != 0) {
PG_TRY();
{
// TODO: can I make sure I'm connected?
// Invocation_assertConnect();
result = (jint)SPI_exec(command, (int)count);
if(result < 0)
Exception_throwSPI("exec", result);
}
PG_CATCH();
{
throwJavaException("SPI_exec");
}
PG_END_TRY();
pfree(command);
}
return result;
}
static JNIEXPORT jobject JNICALL formTuple(JNIEnv* env, jclass cls, jlong _this, jobjectArray jvalues) {
jobject result = 0;
PG_TRY(); {
jint idx;
HeapTuple tuple;
MemoryContext curr;
TupleDesc self = (TupleDesc)_this;
int count = self->natts;
Datum* values = (Datum*)palloc(count * sizeof(Datum));
char* nulls = palloc(count);
// TODO: this is totally broken
memset(values, 0, count * sizeof(Datum));
memset(nulls, 'n', count); /* all values null initially */
for(idx = 0; idx < count; ++idx) {
jobject value = (*env)->GetObjectArrayElement(env,jvalues, idx);
if(value != 0) {
values[idx] = objectToDatum(SPI_gettypeid(self, idx+1), value);
nulls[idx] = ' ';
}
}
tuple = heap_formtuple(self, values, nulls);
// FIXME:
// result = Tuple_internalCreate(tuple, false);
pfree(values);
pfree(nulls);
}
PG_CATCH(); { throwJavaException("heap_formtuple"); }
PG_END_TRY();
return result;
}
// ExecutionPlan
#define ERRCODE_PARAMETER_COUNT_MISMATCH MAKE_SQLSTATE('0','7', '0','0','1')
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 != (*env)->GetArrayLength(env,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;
values = (Datum*)palloc(count * sizeof(Datum));
for(idx = 0; idx < count; ++idx)
{
Oid typeId = SPI_getargtypeid(ePlan, idx);
jobject value = (*env)->GetObjectArrayElement(env,jvalues, idx);
if(value != 0) {
values[idx] = objectToDatum(typeId, value);
(*env)->DeleteLocalRef(env,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;
}
static JNIEXPORT jlong JNICALL _cursorOpen(JNIEnv* env, jclass clazz, jlong _this, jlong threadId, jstring cursorName, jobjectArray jvalues) {
jlong jportal = 0;
if(_this != 0) {
PG_TRY(); {
Datum* values = 0;
char* nulls = 0;
if(coerceObjects( (void *)_this, jvalues, &values, &nulls)) {
Portal portal;
char* name = 0;
if(cursorName != 0) name = fromJavaString(cursorName);
// TODO: make sure the SPI is open
// false -- was currentReadOnly
// Invocation_assertConnect();
portal = SPI_cursor_open( name, (void *)_this, values, nulls, false);
if(name != 0) pfree(name);
if(values != 0) pfree(values);
if(nulls != 0) pfree(nulls);
jportal = (jlong)portal;
}
}
PG_CATCH(); { throwJavaException("SPI_cursor_open"); }
PG_END_TRY();
}
return jportal;
}
// result = (jboolean)SPI_is_cursor_plan( (void *)_this );
// result = (jint)SPI_execute_plan( (void *)_this, values, nulls, false , (int)count);
static JNIEXPORT jlong JNICALL _prepare(JNIEnv* env, jclass clazz, jlong threadId, jstring jcmd, jlongArray paramTypes) {
jlong result = 0;
PG_TRY(); {
char* cmd;
void* ePlan;
int paramCount = (*env)->GetArrayLength(env,paramTypes);
jOid* paramOids = (jOid *)(*env)->GetIntArrayElements(env,paramTypes, false);
cmd = fromJavaString(jcmd);
// Invocation_assertConnect();
ePlan = SPI_prepare(cmd, paramCount, (Oid *)paramOids);
pfree(cmd);
if(ePlan == 0) Exception_throwSPI("prepare", SPI_result);
else {
result = (jlong) SPI_saveplan(ePlan);
SPI_freeplan(ePlan); /* Get rid of the original, nobody can see it anymore */
}
}
PG_CATCH(); { throwJavaException("SPI_prepare"); }
PG_END_TRY();
return result;
}
static JNIEXPORT jint JNICALL _fetch(JNIEnv* env, jclass clazz, jlong portal, jlong threadId, jboolean forward, jint count) {
jint result = 0;
if(portal != 0) {
PG_TRY(); {
SPI_cursor_fetch((Portal)portal, forward == JNI_TRUE, (int)count);
result = (jint)SPI_processed;
} PG_CATCH(); { throwJavaException("SPI_cursor_fetch"); }
PG_END_TRY();
}
return result;
}
static JNIEXPORT jint JNICALL _move(JNIEnv* env, jclass clazz, jlong portal, jlong threadId, jboolean forward, jint count) {
jint result = 0;
if(portal != 0) {
PG_TRY(); {
SPI_cursor_move((Portal)portal, forward == JNI_TRUE, (int)count);
result = (jint)SPI_processed;
}
PG_CATCH(); { throwJavaException("SPI_cursor_move"); }
PG_END_TRY();
}
return result;
}
static JNIEXPORT jint JNICALL sp_start(JNIEnv* env, jclass cls) {
PG_TRY(); { BeginInternalSubTransaction(NULL); }
PG_CATCH(); { throwJavaException("BeginInternalSubTransaction"); }
PG_END_TRY();
return GetCurrentSubTransactionId();
}
static JNIEXPORT void JNICALL sp_commit(JNIEnv* env, jclass clazz) {
PG_TRY(); { ReleaseCurrentSubTransaction(); }
PG_CATCH(); { throwJavaException("ReleaseCurrentSubTransaction"); }
PG_END_TRY();
SPI_restore_connection();
}
static JNIEXPORT void JNICALL sp_rollback(JNIEnv* env, jclass clazz) {
PG_TRY(); { RollbackAndReleaseCurrentSubTransaction(); }
PG_CATCH(); { throwJavaException("RollbackAndReleaseCurrentSubTransaction"); }
PG_END_TRY();
SPI_restore_connection();
}
#define MAX_PARMS 20
static JNIEXPORT jarray JNICALL _execute(JNIEnv *env, jclass clazz, jint num, jobject cmd, jarray args) {
char *strcmd = fromJavaString(cmd);
Oid *argtyps;
Datum *values;
char *nulls;
int parmcount = objectArrayToDatumArray(args, &argtyps, &values, &nulls );
int ret;
if ((ret = SPI_connect()) < 0) {
throwJavaException("SPI_connect");
return NULL;
}
PG_TRY(); { ret = SPI_execute_with_args(strcmd, parmcount, argtyps, values, nulls, false, num); }
PG_CATCH(); { throwJavaException("SPI_execute_args"); }
PG_END_TRY();
pfree(argtyps);
pfree(values);
pfree(nulls);
pfree(strcmd);
int rows = SPI_processed;
if (ret == SPI_OK_SELECT || (ret == SPI_OK_UTILITY && rows > 0 && SPI_tuptable != NULL) );
else {
SPI_finish();
// I should probably contrive to return a number here?
return (*env)->NewObject(env, intClass, intConstructor, rows);
}
SPITupleTable *spi_tuptable = SPI_tuptable;
TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
AttInMetadata *attinmeta = TupleDescGetAttInMetadata(spi_tupdesc);
jarray result = (*env)->NewObjectArray(env, rows, objClass, NULL);
CHECK_EXCEPTION("%s\nallocating resultset array","unused");
for (int cntr = 0; cntr < rows; cntr++) {
HeapTuple spi_tuple = spi_tuptable->vals[cntr];
jobject row = tupleToObject(spi_tupdesc, spi_tuple);
(*env)->SetObjectArrayElement(env, result, cntr, row);
CHECK_EXCEPTION("%s\nstoring row %d ", cntr);
}
SPI_finish();
return result;
}
/*
static JNIEXPORT jarray JNICALL _execute(JNIEnv *env, jclass clazz, jint num, jobject cmd, jarray args) {
int ret;
if ((ret = SPI_connect()) < 0) {
throwJavaException("SPI_connect");
return NULL;
}
// the true/false (read-only) setting and the #-of-rows setting should be parameters
PG_TRY(); { ret = SPI_execute(fromJavaString(cmd), false, 0); }
PG_CATCH(); { throwJavaException("SPI_execute"); }
PG_END_TRY();
int rows = SPI_processed;
if (ret == SPI_OK_SELECT || (ret == SPI_OK_UTILITY && rows > 0 && SPI_tuptable != NULL) );
else {
SPI_finish();
return NULL;
}
SPITupleTable *spi_tuptable = SPI_tuptable;
TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
AttInMetadata *attinmeta = TupleDescGetAttInMetadata(spi_tupdesc);
jarray result = (*env)->NewObjectArray(env, rows, objClass, NULL);
CHECK_EXCEPTION("%s\nallocating resultset array","unused");
for (int cntr = 0; cntr < rows; cntr++) {
HeapTuple spi_tuple = spi_tuptable->vals[cntr];
jobject row = tupleToObject(spi_tupdesc, spi_tuple);
(*env)->SetObjectArrayElement(env, result, cntr, row);
CHECK_EXCEPTION("%s\nstoring row %d ", cntr);
}
SPI_finish();
return result;
}
*/
void bridge_initialize(void) {
char *cls = "org.sourcewave.jinx.PostgresBridge";
jint nMethods = 0;
JNINativeMethod methods[] = {
{ "log", "(ILjava/lang/String;)V", pglog },
{ "getUser", "()[Ljava/lang/String;", getUser },
{ "_modifyTuple", "(JJ[I[Ljava/lang/Object;)J", _modifyTuple },
// TupleDesc
{ "_formTuple", "(J[Ljava/lang/Object;)J", formTuple },
// ExecutionPlan
{ "_prepare", "(JLjava/lang/String;[I)J", _prepare },
// portal
{ "_fetch", "(JJZI)I", _fetch },
{ "_move", "(JJZI)I", _move },
// savepoint
{ "sp_start", "()I", sp_start },
{ "sp_commit", "()V", sp_commit },
{ "sp_rollback", "()V", sp_rollback },
// execute
{ "_execute", "(ILjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", _execute },
{ NULL, NULL, NULL }};
CACHE_CLASS(bridgeClass, cls);
CACHE_STATIC_METHOD(bridgeInit, bridgeClass, "init", "()V");
(*env)->CallStaticObjectMethod(env, bridgeClass, bridgeInit);
CHECK_EXCEPTION("%s\nbridge init","unused");
for(nMethods = 0; methods[nMethods].name != NULL; nMethods++);
if((*env)->RegisterNatives(env, bridgeClass, methods, nMethods) != JNI_OK) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
ereport(ERROR, ( errmsg("Unable to register native methods")));
}
}