forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathException.c
More file actions
137 lines (108 loc) · 3.58 KB
/
Copy pathException.c
File metadata and controls
137 lines (108 loc) · 3.58 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
/*
* 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/Exception.h"
#include "pljava/type/String.h"
static jclass s_Class_class;
static jmethodID s_Class_getName;
static jclass s_Throwable_class;
static jmethodID s_Throwable_getMessage;
static jclass s_SQLException_class;
static jmethodID s_SQLException_init;
static jmethodID s_SQLException_getSQLState;
void Exception_checkException(JNIEnv* env)
{
jthrowable exh = (*env)->ExceptionOccurred(env);
if(exh == 0)
/*
* No exception has been thrown.
*/
return;
(*env)->ExceptionClear(env);
int sqlState = ERRCODE_INTERNAL_ERROR;
StringInfoData buf;
initStringInfo(&buf);
jclass exhClass = (*env)->GetObjectClass(env, exh);
jstring jtmp = (jstring)(*env)->CallObjectMethod(env, exhClass, s_Class_getName);
String_appendJavaString(env, &buf, jtmp);
(*env)->DeleteLocalRef(env, exhClass);
(*env)->DeleteLocalRef(env, jtmp);
jtmp = (jstring)(*env)->CallObjectMethod(env, exh, s_Throwable_getMessage);
if(jtmp != 0)
{
appendStringInfoString(&buf, ": ");
String_appendJavaString(env, &buf, jtmp);
(*env)->DeleteLocalRef(env, jtmp);
}
if((*env)->IsInstanceOf(env, exh, s_SQLException_class))
{
jtmp = (*env)->CallObjectMethod(env, exh, s_SQLException_getSQLState);
if(jtmp != 0)
{
char* s = String_createNTS(env, jtmp);
(*env)->DeleteLocalRef(env, jtmp);
if(strlen(s) >= 5)
sqlState = MAKE_SQLSTATE(s[0], s[1], s[2], s[3], s[4]);
pfree(s);
}
}
/* There's no return from this call.
*/
ereport(ERROR, (errcode(sqlState), errmsg(buf.data)));
}
void Exception_throw(JNIEnv* env, int errCode, const char* errMessage, ...)
{
va_list args;
va_start(args, errMessage);
char buf[1024];
vsnprintf(buf, sizeof(buf), errMessage, args);
jstring message = String_createJavaStringFromNTS(env, buf);
/* unpack MAKE_SQLSTATE code
*/
int idx;
for (idx = 0; idx < 5; ++idx)
{
buf[idx] = PGUNSIXBIT(errCode);
errCode >>= 6;
}
buf[idx] = 0;
jstring sqlState = String_createJavaStringFromNTS(env, buf);
jobject ex = (*env)->NewObject(
env, s_SQLException_class, s_SQLException_init,
message, sqlState);
(*env)->DeleteLocalRef(env, message);
(*env)->DeleteLocalRef(env, sqlState);
(*env)->Throw(env, ex);
}
void Exception_throwSPI(JNIEnv* env, const char* function)
{
Exception_throw(env, ERRCODE_INTERNAL_ERROR,
"SPI function SPI_%s failed with error code %d", function, SPI_result);
}
PG_FUNCTION_INFO_V1(Exception_initialize);
Datum Exception_initialize(PG_FUNCTION_ARGS)
{
JNIEnv* env = (JNIEnv*)PG_GETARG_POINTER(0);
s_Class_class = (jclass)(*env)->NewGlobalRef(
env, PgObject_getJavaClass(env, "java/lang/Class"));
s_Throwable_class = (jclass)(*env)->NewGlobalRef(
env, PgObject_getJavaClass(env, "java/lang/Throwable"));
s_SQLException_class = (jclass)(*env)->NewGlobalRef(
env, PgObject_getJavaClass(env, "java/sql/SQLException"));
s_Class_getName = PgObject_getJavaMethod(env, s_Class_class,
"getName", "()Ljava/lang/String;");
s_SQLException_init = PgObject_getJavaMethod(env, s_SQLException_class,
"<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
s_Throwable_getMessage = PgObject_getJavaMethod(env, s_Throwable_class,
"getMessage", "()Ljava/lang/String;");
s_SQLException_getSQLState = PgObject_getJavaMethod(env, s_SQLException_class,
"getSQLState", "()Ljava/lang/String;");
PG_RETURN_VOID();
}