forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgObject.c
More file actions
112 lines (99 loc) · 3.21 KB
/
Copy pathPgObject.c
File metadata and controls
112 lines (99 loc) · 3.21 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
/*
* This file contains software that has been made available under
* The Mozilla Public License 1.1. Use and distribution hereof are
* subject to the restrictions set forth therein.
*
* Copyright (c) 2003 TADA AB - Taby Sweden
* All Rights Reserved
*/
#include <postgres.h>
#include <executor/spi.h>
#include "pljava/PgObject_priv.h"
void PgObject_free(PgObject object)
{
Finalizer finalizer = object->m_class->finalize;
if(finalizer != 0)
finalizer(object);
pfree(object);
}
PgObject PgObjectClass_allocInstance(PgObjectClass clazz, MemoryContext ctx)
{
Size sz = clazz->instanceSize;
PgObject infant = (PgObject)MemoryContextAlloc(ctx, sz);
memset(infant, 0, sz);
infant->m_class = clazz;
return infant;
}
void PgObjectClass_init(PgObjectClass clazz, const char* name, Size instanceSize, Finalizer finalizer)
{
clazz->name = name;
clazz->instanceSize = instanceSize;
clazz->finalize = finalizer;
}
PgObjectClass PgObjectClass_create(const char* name, Size instanceSize, Finalizer finalizer)
{
PgObjectClass self = (PgObjectClass)MemoryContextAlloc(TopMemoryContext, sizeof(struct PgObjectClass_));
memset(self, 0, sizeof(struct PgObjectClass_));
PgObjectClass_init(self, name, instanceSize, finalizer);
return self;
}
void _PgObject_pureVirtualCalled(PgObject object)
{
ereport(ERROR, (errmsg("Pure virtual method called")));
}
void PgObject_throwMemberError(const char* memberName, const char* signature, bool isMethod, bool isStatic)
{
ereport(ERROR, (
errmsg("Unable to find%s %s %s with signature %s",
(isStatic ? " static" : ""),
(isMethod ? "method" : "field"),
memberName,
signature)));
}
jclass PgObject_getJavaClass(JNIEnv* env, const char* className)
{
jclass cls = (*env)->FindClass(env, className);
if(cls == 0)
{
ereport(ERROR, (
errmsg("Unable to load class %s using CLASSPATH '%s'",
className,
getenv("CLASSPATH"))));
}
return cls;
}
jmethodID PgObject_getJavaMethod(JNIEnv* env, jclass cls, const char* methodName, const char* signature)
{
jmethodID m = (*env)->GetMethodID(env, cls, methodName, signature);
if(m == 0)
PgObject_throwMemberError(methodName, signature, true, false);
return m;
}
jmethodID PgObject_getStaticJavaMethod(JNIEnv* env, jclass cls, const char* methodName, const char* signature)
{
jmethodID m = (*env)->GetStaticMethodID(env, cls, methodName, signature);
if(m == 0)
PgObject_throwMemberError(methodName, signature, true, true);
return m;
}
jfieldID PgObject_getJavaField(JNIEnv* env, jclass cls, const char* fieldName, const char* signature)
{
jfieldID m = (*env)->GetFieldID(env, cls, fieldName, signature);
if(m == 0)
PgObject_throwMemberError(fieldName, signature, false, false);
return m;
}
jfieldID PgObject_getStaticJavaField(JNIEnv* env, jclass cls, const char* fieldName, const char* signature)
{
jfieldID m = (*env)->GetStaticFieldID(env, cls, fieldName, signature);
if(m == 0)
PgObject_throwMemberError(fieldName, signature, false, true);
return m;
}
HeapTuple PgObject_getValidTuple(int cacheId, Oid tupleId, const char* tupleType)
{
HeapTuple tuple = SearchSysCache(cacheId, ObjectIdGetDatum(tupleId), 0, 0, 0);
if(!HeapTupleIsValid(tuple))
ereport(ERROR, (errmsg("cache lookup failed for %s %u", tupleType, tupleId)));
return tuple;
}