forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTupleDesc.c
More file actions
300 lines (276 loc) · 7.02 KB
/
Copy pathTupleDesc.c
File metadata and controls
300 lines (276 loc) · 7.02 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
/*
* 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 <funcapi.h>
#include "org_postgresql_pljava_internal_TupleDesc.h"
#include "pljava/Backend.h"
#include "pljava/Exception.h"
#include "pljava/Invocation.h"
#include "pljava/type/Type_priv.h"
#include "pljava/type/String.h"
#include "pljava/type/Tuple.h"
#include "pljava/type/TupleDesc.h"
#include "pljava/type/Oid.h"
static jclass s_TupleDesc_class;
static jmethodID s_TupleDesc_init;
/*
* org.postgresql.pljava.TupleDesc type.
*/
jobject TupleDesc_create(TupleDesc td)
{
jobject jtd = 0;
if(td != 0)
{
MemoryContext curr = MemoryContextSwitchTo(JavaMemoryContext);
jtd = TupleDesc_internalCreate(td);
MemoryContextSwitchTo(curr);
}
return jtd;
}
jobject TupleDesc_internalCreate(TupleDesc td)
{
jobject jtd;
Ptr2Long tdH;
td = CreateTupleDescCopyConstr(td);
tdH.longVal = 0L; /* ensure that the rest is zeroed out */
tdH.ptrVal = td;
jtd = JNI_newObject(s_TupleDesc_class, s_TupleDesc_init, tdH.longVal, (jint)td->natts);
return jtd;
}
Type TupleDesc_getColumnType(TupleDesc tupleDesc, int index)
{
Type type;
Oid typeId = SPI_gettypeid(tupleDesc, index);
if(!OidIsValid(typeId))
{
Exception_throw(ERRCODE_INVALID_DESCRIPTOR_INDEX,
"Invalid attribute index \"%d\"", (int)index);
type = 0;
}
else
type = Type_objectTypeFromOid(typeId, Invocation_getTypeMap());
return type;
}
static jvalue _TupleDesc_coerceDatum(Type self, Datum arg)
{
jvalue result;
result.l = TupleDesc_create((TupleDesc)DatumGetPointer(arg));
return result;
}
/* Make this datatype available to the postgres system.
*/
extern void TupleDesc_initialize(void);
void TupleDesc_initialize(void)
{
TypeClass cls;
JNINativeMethod methods[] = {
{
"_getColumnName",
"(JI)Ljava/lang/String;",
Java_org_postgresql_pljava_internal_TupleDesc__1getColumnName
},
{
"_getColumnIndex",
"(JLjava/lang/String;)I",
Java_org_postgresql_pljava_internal_TupleDesc__1getColumnIndex
},
{
"_formTuple",
"(J[Ljava/lang/Object;)Lorg/postgresql/pljava/internal/Tuple;",
Java_org_postgresql_pljava_internal_TupleDesc__1formTuple
},
{
"_getOid",
"(JI)Lorg/postgresql/pljava/internal/Oid;",
Java_org_postgresql_pljava_internal_TupleDesc__1getOid
},
{
"_free",
"(J)V",
Java_org_postgresql_pljava_internal_TupleDesc__1free
},
{ 0, 0, 0 }};
s_TupleDesc_class = JNI_newGlobalRef(PgObject_getJavaClass("org/postgresql/pljava/internal/TupleDesc"));
PgObject_registerNatives2(s_TupleDesc_class, methods);
s_TupleDesc_init = PgObject_getJavaMethod(s_TupleDesc_class, "<init>", "(JI)V");
cls = JavaWrapperClass_alloc("type.TupleDesc");
cls->JNISignature = "Lorg/postgresql/pljava/internal/TupleDesc;";
cls->javaTypeName = "org.postgresql.pljava.internal.TupleDesc";
cls->coerceDatum = _TupleDesc_coerceDatum;
Type_registerType("org.postgresql.pljava.internal.TupleDesc", TypeClass_allocInstance(cls, InvalidOid));
}
/****************************************
* JNI methods
****************************************/
/*
* Class: org_postgresql_pljava_internal_TupleDesc
* Method: _getColumnName
* Signature: (JI)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_postgresql_pljava_internal_TupleDesc__1getColumnName(JNIEnv* env, jclass cls, jlong _this, jint index)
{
jstring result = 0;
BEGIN_NATIVE
PG_TRY();
{
char* name;
Ptr2Long p2l;
p2l.longVal = _this;
name = SPI_fname((TupleDesc)p2l.ptrVal, (int)index);
if(name == 0)
{
Exception_throw(ERRCODE_INVALID_DESCRIPTOR_INDEX,
"Invalid attribute index \"%d\"", (int)index);
}
else
{
result = String_createJavaStringFromNTS(name);
pfree(name);
}
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_fname");
}
PG_END_TRY();
END_NATIVE
return result;
}
/*
* Class: org_postgresql_pljava_internal_TupleDesc
* Method: _getColumnIndex
* Signature: (JLjava/lang/String;)I;
*/
JNIEXPORT jint JNICALL
Java_org_postgresql_pljava_internal_TupleDesc__1getColumnIndex(JNIEnv* env, jclass cls, jlong _this, jstring colName)
{
jint result = 0;
BEGIN_NATIVE
char* name = String_createNTS(colName);
if(name != 0)
{
Ptr2Long p2l;
p2l.longVal = _this;
PG_TRY();
{
result = SPI_fnumber((TupleDesc)p2l.ptrVal, name);
if(result == SPI_ERROR_NOATTRIBUTE)
{
Exception_throw(ERRCODE_UNDEFINED_COLUMN,
"Tuple has no attribute \"%s\"", name);
}
pfree(name);
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_fnumber");
}
PG_END_TRY();
}
END_NATIVE
return result;
}
/*
* Class: org_postgresql_pljava_internal_TupleDesc
* Method: _formTuple
* Signature: (J[Ljava/lang/Object;)Lorg/postgresql/pljava/internal/Tuple;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_internal_TupleDesc__1formTuple(JNIEnv* env, jclass cls, jlong _this, jobjectArray jvalues)
{
jobject result = 0;
BEGIN_NATIVE
Ptr2Long p2l;
p2l.longVal = _this;
PG_TRY();
{
jint idx;
HeapTuple tuple;
MemoryContext curr;
TupleDesc self = (TupleDesc)p2l.ptrVal;
int count = self->natts;
Datum* values = (Datum*)palloc(count * sizeof(Datum));
char* nulls = palloc(count);
jobject typeMap = Invocation_getTypeMap();
memset(values, 0, count * sizeof(Datum));
memset(nulls, 'n', count); /* all values null initially */
for(idx = 0; idx < count; ++idx)
{
jobject value = JNI_getObjectArrayElement(jvalues, idx);
if(value != 0)
{
Type type = Type_fromOid(SPI_gettypeid(self, idx + 1), typeMap);
values[idx] = Type_coerceObject(type, value);
nulls[idx] = ' ';
}
}
curr = MemoryContextSwitchTo(JavaMemoryContext);
tuple = heap_formtuple(self, values, nulls);
result = Tuple_internalCreate(tuple, false);
MemoryContextSwitchTo(curr);
pfree(values);
pfree(nulls);
}
PG_CATCH();
{
Exception_throw_ERROR("heap_formtuple");
}
PG_END_TRY();
END_NATIVE
return result;
}
/*
* Class: org_postgresql_pljava_internal_TupleDesc
* Method: _free
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_postgresql_pljava_internal_TupleDesc__1free(JNIEnv* env, jobject _this, jlong pointer)
{
BEGIN_NATIVE_NO_ERRCHECK
Ptr2Long p2l;
p2l.longVal = pointer;
FreeTupleDesc((TupleDesc)p2l.ptrVal);
END_NATIVE
}
/*
* Class: org_postgresql_pljava_internal_TupleDesc
* Method: _getOid
* Signature: (JI)Lorg/postgresql/pljava/internal/Oid;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_internal_TupleDesc__1getOid(JNIEnv* env, jclass cls, jlong _this, jint index)
{
jobject result = 0;
BEGIN_NATIVE
Ptr2Long p2l;
p2l.longVal = _this;
PG_TRY();
{
Oid typeId = SPI_gettypeid((TupleDesc)p2l.ptrVal, (int)index);
if(!OidIsValid(typeId))
{
Exception_throw(ERRCODE_INVALID_DESCRIPTOR_INDEX,
"Invalid attribute index \"%d\"", (int)index);
}
else
{
result = Oid_create(typeId);
}
}
PG_CATCH();
{
Exception_throw_ERROR("SPI_gettypeid");
}
PG_END_TRY();
END_NATIVE
return result;
}