static Datum _doubleArray_coerceObject(Type self, jobject doubleArray) { ArrayType* v; jsize nElems; if(doubleArray == 0) return 0; nElems = JNI_getArrayLength((jarray)doubleArray); #if (PGSQL_MAJOR_VER == 8 && PGSQL_MINOR_VER < 2) v = createArrayType(nElems, sizeof(jdouble), FLOAT8OID); #else v = createArrayType(nElems, sizeof(jdouble), FLOAT8OID, false); #endif if(!JNI_isInstanceOf( doubleArray, s_DoubleArray_class)) JNI_getDoubleArrayRegion((jdoubleArray)doubleArray, 0, nElems, (jdouble*)ARR_DATA_PTR(v)); else { int idx = 0; jdouble *array = (jdouble*)ARR_DATA_PTR(v); for(idx = 0; idx < nElems; ++idx) { array[idx] = JNI_callDoubleMethod(JNI_getObjectArrayElement(doubleArray, idx), s_Double_doubleValue); } } PG_RETURN_ARRAYTYPE_P(v); }
/* * 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; }
static Datum _Array_coerceObject(Type self, jobject objArray) { ArrayType* v; jsize idx; int lowerBound = 1; Type elemType = Type_getElementType(self); int nElems = (int)JNI_getArrayLength((jarray)objArray); Datum* values = (Datum*)palloc(nElems * sizeof(Datum) + nElems * sizeof(bool)); bool* nulls = (bool*)(values + nElems); for(idx = 0; idx < nElems; ++idx) { jobject obj = JNI_getObjectArrayElement(objArray, idx); if(obj == 0) { nulls[idx] = true; values[idx] = 0; } else { nulls[idx] = false; values[idx] = Type_coerceObject(elemType, obj); JNI_deleteLocalRef(obj); } } v = construct_md_array( values, nulls, 1, &nElems, &lowerBound, Type_getOid(elemType), Type_getLength(elemType), Type_isByValue(elemType), Type_getAlign(elemType)); pfree(values); PG_RETURN_ARRAYTYPE_P(v); }