forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByte.c
More file actions
151 lines (133 loc) · 4.13 KB
/
Copy pathByte.c
File metadata and controls
151 lines (133 loc) · 4.13 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
/*
* 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 "pljava/type/Type_priv.h"
#include "pljava/type/Array.h"
#include "pljava/Invocation.h"
/* The byte maps to the postgres type "char", i.e. the
* 8 bit, one byte quantity. The java byte was chosen instead of
* char since a Java char is UTF-16 and "char" is not in any way
* subject to character set encodings.
*/
static TypeClass s_byteClass;
static jclass s_Byte_class;
static jmethodID s_Byte_init;
static jmethodID s_Byte_byteValue;
/*
* byte primitive type.
*/
static Datum _byte_invoke(Type self, jclass cls, jmethodID method, jvalue* args, PG_FUNCTION_ARGS)
{
jbyte v = JNI_callStaticByteMethodA(cls, method, args);
return CharGetDatum(v);
}
static jvalue _byte_coerceDatum(Type self, Datum arg)
{
jvalue result;
result.b = DatumGetChar(arg);
return result;
}
static jvalue _byteArray_coerceDatum(Type self, Datum arg)
{
jvalue result;
ArrayType* v = DatumGetArrayTypeP(arg);
jsize nElems = (jsize)ArrayGetNItems(ARR_NDIM(v), ARR_DIMS(v));
jbyteArray byteArray = JNI_newByteArray(nElems);
#if (PGSQL_MAJOR_VER == 8 && PGSQL_MINOR_VER < 2)
JNI_setByteArrayRegion(byteArray, 0, nElems, (jbyte*)ARR_DATA_PTR(v));
#else
if(ARR_HASNULL(v))
{
jsize idx;
jboolean isCopy = JNI_FALSE;
bits8* nullBitMap = ARR_NULLBITMAP(v);
jbyte* values = (jbyte*)ARR_DATA_PTR(v);
jbyte* elems = JNI_getByteArrayElements(byteArray, &isCopy);
for(idx = 0; idx < nElems; ++idx)
{
if(arrayIsNull(nullBitMap, idx))
elems[idx] = 0;
else
elems[idx] = *values++;
}
JNI_releaseByteArrayElements(byteArray, elems, JNI_COMMIT);
}
else
JNI_setByteArrayRegion(byteArray, 0, nElems, (jbyte*)ARR_DATA_PTR(v));
#endif
result.l = (jobject)byteArray;
return result;
}
static Datum _byteArray_coerceObject(Type self, jobject byteArray)
{
ArrayType* v;
jsize nElems;
if(byteArray == 0)
return 0;
nElems = JNI_getArrayLength((jarray)byteArray);
#if (PGSQL_MAJOR_VER == 8 && PGSQL_MINOR_VER < 2)
v = createArrayType(nElems, sizeof(jbyte), CHAROID);
#else
v = createArrayType(nElems, sizeof(jbyte), CHAROID, false);
#endif
JNI_getByteArrayRegion((jbyteArray)byteArray, 0, nElems, (jbyte*)ARR_DATA_PTR(v));
PG_RETURN_ARRAYTYPE_P(v);
}
/*
* java.lang.Byte type.
*/
static bool _Byte_canReplace(Type self, Type other)
{
TypeClass cls = Type_getClass(other);
return Type_getClass(self) == cls || cls == s_byteClass;
}
static jvalue _Byte_coerceDatum(Type self, Datum arg)
{
jvalue result;
result.l = JNI_newObject(s_Byte_class, s_Byte_init, DatumGetChar(arg));
return result;
}
static Datum _Byte_coerceObject(Type self, jobject byteObj)
{
return CharGetDatum(byteObj == 0 ? 0 : JNI_callByteMethod(byteObj, s_Byte_byteValue));
}
static Type _byte_createArrayType(Type self, Oid arrayTypeId)
{
return Array_fromOid2(arrayTypeId, self, _byteArray_coerceDatum, _byteArray_coerceObject);
}
/* Make this datatype available to the postgres system.
*/
extern void Byte_initialize(void);
void Byte_initialize(void)
{
Type t_byte;
Type t_Byte;
TypeClass cls;
s_Byte_class = JNI_newGlobalRef(PgObject_getJavaClass("java/lang/Byte"));
s_Byte_init = PgObject_getJavaMethod(s_Byte_class, "<init>", "(B)V");
s_Byte_byteValue = PgObject_getJavaMethod(s_Byte_class, "byteValue", "()B");
cls = TypeClass_alloc("type.Byte");
cls->canReplaceType = _Byte_canReplace;
cls->JNISignature = "Ljava/lang/Byte;";
cls->javaTypeName = "java.lang.Byte";
cls->coerceDatum = _Byte_coerceDatum;
cls->coerceObject = _Byte_coerceObject;
t_Byte = TypeClass_allocInstance(cls, CHAROID);
cls = TypeClass_alloc("type.byte");
cls->JNISignature = "B";
cls->javaTypeName = "byte";
cls->invoke = _byte_invoke;
cls->coerceDatum = _byte_coerceDatum;
cls->coerceObject = _Byte_coerceObject;
cls->createArrayType = _byte_createArrayType;
s_byteClass = cls;
t_byte = TypeClass_allocInstance(cls, CHAROID);
t_byte->objectType = t_Byte;
Type_registerType("byte", t_byte);
Type_registerType("java.lang.Byte", t_Byte);
}