forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.c
More file actions
232 lines (206 loc) · 6.45 KB
/
Copy pathString.c
File metadata and controls
232 lines (206 loc) · 6.45 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
/*
* 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/String_priv.h"
#include "pljava/HashMap.h"
static TypeClass s_StringClass;
jclass s_String_class;
jclass s_Object_class;
static jmethodID s_Object_toString;
/*
* Default type. Uses Posgres String conversion routines.
*/
static bool _String_canReplaceType(Type self, Type type)
{
/* All known postgres types can perform String coercsions.
*/
return true;
}
jvalue _String_coerceDatum(Type self, Datum arg)
{
jvalue result;
char* tmp = DatumGetCString(FunctionCall3(
&((String)self)->textOutput,
arg,
ObjectIdGetDatum(((String)self)->elementType),
Int32GetDatum(-1)));
result.l = String_createJavaStringFromNTS(tmp);
pfree(tmp);
return result;
}
Datum _String_coerceObject(Type self, jobject jstr)
{
char* tmp;
Datum ret;
if(jstr == 0)
return 0;
jstr = JNI_callObjectMethod(jstr, s_Object_toString);
if(JNI_exceptionCheck())
return 0;
tmp = String_createNTS(jstr);
JNI_deleteLocalRef(jstr);
ret = FunctionCall3(
&((String)self)->textInput,
CStringGetDatum(tmp),
ObjectIdGetDatum(((String)self)->elementType),
Int32GetDatum(-1));
pfree(tmp);
return ret;
}
static String String_create(TypeClass cls, Oid typeId)
{
HeapTuple typeTup = PgObject_getValidTuple(TYPEOID, typeId, "type");
Form_pg_type pgType = (Form_pg_type)GETSTRUCT(typeTup);
String self = (String)TypeClass_allocInstance(cls, typeId);
MemoryContext ctx = GetMemoryChunkContext(self);
fmgr_info_cxt(pgType->typoutput, &self->textOutput, ctx);
fmgr_info_cxt(pgType->typinput, &self->textInput, ctx);
self->elementType = pgType->typelem;
ReleaseSysCache(typeTup);
return self;
}
Type String_obtain(Oid typeId)
{
return (Type)StringClass_obtain(s_StringClass, typeId);
}
String StringClass_obtain(TypeClass self, Oid typeId)
{
return String_create(self, typeId);
}
jstring String_createJavaString(text* t)
{
jstring result = 0;
if(t != 0)
{
char* utf8;
char* src = VARDATA(t);
int srcLen = VARSIZE(t) - VARHDRSZ;
if(srcLen == 0)
return 0;
/* Would be nice if a direct conversion to UTF16 was provided.
*/
utf8 = (char*)pg_do_encoding_conversion((unsigned char*)src, srcLen, GetDatabaseEncoding(), PG_UTF8);
result = JNI_newStringUTF(utf8);
/* pg_do_encoding_conversion will return the source argument
* when no conversion is required. We don't want to accidentally
* free that pointer.
*/
if(utf8 != src)
pfree(utf8);
}
return result;
}
jstring String_createJavaStringFromNTS(const char* cp)
{
jstring result = 0;
if(cp != 0)
{
/* Would be nice if a direct conversion to UTF16 was provided.
*/
char* utf8 = (char*)pg_do_encoding_conversion((unsigned char*)cp, strlen(cp), GetDatabaseEncoding(), PG_UTF8);
result = JNI_newStringUTF(utf8);
/* pg_do_encoding_conversion will return the source argument
* when no conversion is required. We don't want to accidentally
* free that pointer.
*/
if(utf8 != cp)
pfree(utf8);
}
return result;
}
text* String_createText(jstring javaString)
{
text* result = 0;
if(javaString != 0)
{
/* Would be nice if a direct conversion from UTF16 was provided.
*/
char* utf8 = (char*)JNI_getStringUTFChars(javaString, 0);
char* denc = (char*)pg_do_encoding_conversion(
(unsigned char*)utf8, strlen(utf8), PG_UTF8, GetDatabaseEncoding());
int dencLen = strlen(denc);
int varSize = dencLen + VARHDRSZ;
/* Allocate and initialize the text structure.
*/
result = (text*)palloc(varSize);
#if (PGSQL_MAJOR_VER == 8 && PGSQL_MINOR_VER < 3)
VARATT_SIZEP(result) = varSize; /* Total size of structure, not just data */
#else
SET_VARSIZE(result, varSize); /* Total size of structure, not just data */
#endif
memcpy(VARDATA(result), denc, dencLen);
/* pg_do_encoding_conversion will return the source argument
* when no conversion is required. We don't want to accidentally
* free that pointer.
*/
if(denc != utf8)
pfree(denc);
JNI_releaseStringUTFChars(javaString, utf8);
}
return result;
}
char* String_createNTS(jstring javaString)
{
char* result = 0;
if(javaString != 0)
{
/* Would be nice if a direct conversion from UTF16 was provided.
*/
char* utf8 = (char*)JNI_getStringUTFChars(javaString, 0);
result = (char*)pg_do_encoding_conversion(
(unsigned char*)utf8, strlen(utf8), PG_UTF8, GetDatabaseEncoding());
/* pg_do_encoding_conversion will return the source argument
* when no conversion is required. We always want a copy here.
*/
if(result == utf8)
result = pstrdup(result);
JNI_releaseStringUTFChars(javaString, utf8);
}
return result;
}
void String_appendJavaString(StringInfoData* buf, jstring javaString)
{
if(javaString != 0)
{
/* Would be nice if a direct conversion from UTF16 was provided.
*/
char* utf8 = (char*)JNI_getStringUTFChars(javaString, 0);
char* dbEnc = (char*)pg_do_encoding_conversion(
(unsigned char*)utf8, strlen(utf8), PG_UTF8, GetDatabaseEncoding());
appendStringInfoString(buf, dbEnc);
/* pg_do_encoding_conversion will return the source argument
* when no conversion is required. We don't want to accidentally
* free that pointer.
*/
if(dbEnc != utf8)
pfree(dbEnc);
JNI_releaseStringUTFChars(javaString, utf8);
}
}
extern void String_initialize(void);
void String_initialize(void)
{
s_Object_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/Object"));
s_Object_toString = PgObject_getJavaMethod(s_Object_class, "toString", "()Ljava/lang/String;");
s_String_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/String"));
s_StringClass = TypeClass_alloc2("type.String", sizeof(struct TypeClass_), sizeof(struct String_));
s_StringClass->JNISignature = "Ljava/lang/String;";
s_StringClass->javaTypeName = "java.lang.String";
s_StringClass->canReplaceType = _String_canReplaceType;
s_StringClass->coerceDatum = _String_coerceDatum;
s_StringClass->coerceObject = _String_coerceObject;
/*
* Registering known types will increase the performance
* a bit. The "default" is used when all else fails.
*/
Type_registerType2(TEXTOID, 0, String_obtain);
Type_registerType2(CSTRINGOID, 0, String_obtain);
Type_registerType2(BPCHAROID, 0, String_obtain);
Type_registerType2(NAMEOID, 0, String_obtain);
Type_registerType2(VARCHAROID, "java.lang.String", String_obtain);
}