forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelation.c
More file actions
266 lines (244 loc) · 6.28 KB
/
Copy pathRelation.c
File metadata and controls
266 lines (244 loc) · 6.28 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
/*
* 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/spi.h>
#include "org_postgresql_pljava_internal_Relation.h"
#include "pljava/Exception.h"
#include "pljava/Invocation.h"
#include "pljava/SPI.h"
#include "pljava/type/Type_priv.h"
#include "pljava/type/String.h"
#include "pljava/type/TupleDesc.h"
#include "pljava/type/Tuple.h"
#include "pljava/type/Relation.h"
static jclass s_Relation_class;
static jmethodID s_Relation_init;
/*
* org.postgresql.pljava.Relation type.
*/
jobject Relation_create(Relation td)
{
return (td == 0) ? 0 : JNI_newObject(
s_Relation_class,
s_Relation_init,
Invocation_createLocalWrapper(td));
}
extern void Relation_initialize(void);
void Relation_initialize(void)
{
JNINativeMethod methods[] =
{
{
"_free",
"(J)V",
Java_org_postgresql_pljava_internal_Relation__1free
},
{
"_getName",
"(J)Ljava/lang/String;",
Java_org_postgresql_pljava_internal_Relation__1getName
},
{
"_getSchema",
"(J)Ljava/lang/String;",
Java_org_postgresql_pljava_internal_Relation__1getSchema
},
{
"_getTupleDesc",
"(J)Lorg/postgresql/pljava/internal/TupleDesc;",
Java_org_postgresql_pljava_internal_Relation__1getTupleDesc
},
{
"_modifyTuple",
"(JJ[I[Ljava/lang/Object;)Lorg/postgresql/pljava/internal/Tuple;",
Java_org_postgresql_pljava_internal_Relation__1modifyTuple
},
{ 0, 0, 0 }
};
s_Relation_class = JNI_newGlobalRef(PgObject_getJavaClass("org/postgresql/pljava/internal/Relation"));
PgObject_registerNatives2(s_Relation_class, methods);
s_Relation_init = PgObject_getJavaMethod(s_Relation_class, "<init>", "(J)V");
}
/****************************************
* JNI methods
****************************************/
/*
* Class: org_postgresql_pljava_internal_Relation
* Method: _free
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_postgresql_pljava_internal_Relation__1free(JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Invocation_freeLocalWrapper(pointer);
END_NATIVE
}
/*
* Class: org_postgresql_pljava_internal_Relation
* Method: _getName
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_postgresql_pljava_internal_Relation__1getName(JNIEnv* env, jclass clazz, jlong _this)
{
jstring result = 0;
Relation self = Invocation_getWrappedPointer(_this);
if(self != 0)
{
BEGIN_NATIVE
PG_TRY();
{
char* relName = SPI_getrelname(self);
result = String_createJavaStringFromNTS(relName);
pfree(relName);
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_getrelname");
}
PG_END_TRY();
END_NATIVE
}
return result;
}
/*
* Class: org_postgresql_pljava_internal_Relation
* Method: _getSchema
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_postgresql_pljava_internal_Relation__1getSchema(JNIEnv* env, jclass clazz, jlong _this)
{
jstring result = 0;
Relation self = Invocation_getWrappedPointer(_this);
if(self != 0)
{
BEGIN_NATIVE
PG_TRY();
{
char* schema = SPI_getnspname(self);
result = String_createJavaStringFromNTS(schema);
pfree(schema);
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_getnspname");
}
PG_END_TRY();
END_NATIVE
}
return result;
}
/*
* Class: org_postgresql_pljava_internal_Relation
* Method: _getTupleDesc
* Signature: (J)Lorg/postgresql/pljava/internal/TupleDesc;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_internal_Relation__1getTupleDesc(JNIEnv* env, jclass clazz, jlong _this)
{
jobject result = 0;
Relation self = Invocation_getWrappedPointer(_this);
if(self != 0)
{
BEGIN_NATIVE
result = TupleDesc_create(self->rd_att);
END_NATIVE
}
return result;
}
/*
* Class: org_postgresql_pljava_internal_Relation
* Method: _modifyTuple
* Signature: (JJ[I[Ljava/lang/Object;)Lorg/postgresql/internal/pljava/Tuple;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_internal_Relation__1modifyTuple(JNIEnv* env, jclass clazz, jlong _this, jlong _tuple, jintArray _indexes, jobjectArray _values)
{
Relation self = Invocation_getWrappedPointer(_this);
jobject result = 0;
if(self != 0 && _tuple != 0)
{
Ptr2Long p2l;
p2l.longVal = _tuple;
BEGIN_NATIVE
HeapTuple tuple = (HeapTuple)p2l.ptrVal;
PG_TRY();
{
jint idx;
TupleDesc tupleDesc = self->rd_att;
jobject typeMap = Invocation_getTypeMap();
jint count = JNI_getArrayLength(_indexes);
Datum* values = (Datum*)palloc(count * sizeof(Datum));
char* nulls = 0;
jint* javaIdxs = JNI_getIntArrayElements(_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;
Type type;
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 */
}
type = Type_fromOid(typeId, typeMap);
value = JNI_getObjectArrayElement(_values, idx);
if(value != 0)
values[idx] = Type_coerceObject(type, 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);
JNI_releaseIntArrayElements(_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;
Exception_throw_ERROR("SPI_gettypeid");
}
PG_END_TRY();
if(tuple != 0)
result = Tuple_create(tuple);
END_NATIVE
}
return result;
}