Get some of the java code working

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@23323 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2006-08-24 16:53:15 +00:00
parent 94f9151f12
commit 563e93f83e
3 changed files with 122 additions and 64 deletions

176
JDBC.m
View file

@ -130,7 +130,8 @@ JNIEnv *SQLClientJNIEnv ()
libraryPath: (NSString *)libraryPath libraryPath: (NSString *)libraryPath
{ {
JavaVMInitArgs jvm_args; JavaVMInitArgs jvm_args;
JavaVMOption options[2]; JavaVMOption options[32];
int args = 0;
jint result; jint result;
JNIEnv *env; JNIEnv *env;
NSString *path; NSString *path;
@ -161,22 +162,27 @@ JNIEnv *SQLClientJNIEnv ()
} }
} }
JNI_GetDefaultJavaVMInitArgs(&jvm_args); path = [NSString stringWithFormat: @"-Djava.library.path=%@", libraryPath];
options[args].optionString = strdup([path UTF8String]);
jvm_args.nOptions = 2; options[args++].extraInfo = 0;
path = [NSString stringWithFormat: @"-Djava.class.path=%@", classPath]; path = [NSString stringWithFormat: @"-Djava.class.path=%@", classPath];
options[0].optionString = (char *)[path UTF8String]; options[args].optionString = strdup([path UTF8String]);
options[args++].extraInfo = 0;
path = [NSString stringWithFormat: @"-Djava.library.path=%@", libraryPath]; path = [NSString stringWithFormat: @"-Xbootclasspath/a:%@", classPath];
options[1].optionString = (char *)[path UTF8String]; options[args].optionString = strdup([path UTF8String]);
options[args++].extraInfo = 0;
options[args].optionString = "-verbose:class,jni";
options[args++].extraInfo = 0;
jvm_args.nOptions = args;
jvm_args.version = JNI_VERSION_1_2; jvm_args.version = JNI_VERSION_1_2;
jvm_args.options = options; jvm_args.options = options;
jvm_args.ignoreUnrecognized = JNI_FALSE; jvm_args.ignoreUnrecognized = JNI_FALSE;
result = JNI_CreateJavaVM (&SQLClientJavaVM, (void **)&env, &jvm_args); result = JNI_CreateJavaVM (&SQLClientJavaVM, (void **)&env, &jvm_args);
if (result < 0) if (result < 0)
{ {
[NSException raise: NSGenericException [NSException raise: NSGenericException
@ -595,11 +601,13 @@ static NSNull *null = nil;
{ {
JNIEnv *env = SQLClientJNIEnv(); JNIEnv *env = SQLClientJNIEnv();
JInfo *ji = (JInfo*)extra; JInfo *ji = (JInfo*)extra;
jclass jc;
jmethodID jm; jmethodID jm;
if (ji->statement != 0) if (ji->statement != 0)
{ {
jm = (*env)->GetMethodID (env, ji->statement, "close", "()V"); jc = (*env)->GetObjectClass(env, ji->statement);
jm = (*env)->GetMethodID (env, jc, "close", "()V");
if (jm == 0) JExceptionClear(env); if (jm == 0) JExceptionClear(env);
else (*env)->CallVoidMethod (env, ji->statement, jm); else (*env)->CallVoidMethod (env, ji->statement, jm);
if (jm == 0) JExceptionClear(env); if (jm == 0) JExceptionClear(env);
@ -608,7 +616,8 @@ static NSNull *null = nil;
} }
if (ji->connection != 0) if (ji->connection != 0)
{ {
jm = (*env)->GetMethodID (env, ji->connection, "close", "()V"); jc = (*env)->GetObjectClass(env, ji->connection);
jm = (*env)->GetMethodID (env, jc, "close", "()V");
if (jm == 0) JExceptionClear(env); if (jm == 0) JExceptionClear(env);
else (*env)->CallVoidMethod (env, ji->connection, jm); else (*env)->CallVoidMethod (env, ji->connection, jm);
if (jm == 0) JExceptionClear(env); if (jm == 0) JExceptionClear(env);
@ -653,20 +662,56 @@ static NSNull *null = nil;
return NO; return NO;
} }
/* Ensure the driver for the database is loaded. #if 0
*/ jc = (*env)->FindClass(env, "java/lang/ClassLoader");
jc = (*env)->FindClass(env, [cname UTF8String]); JException (env);
jm = (*env)->GetStaticMethodID(env, jc,
"getSystemClassLoader", "()Ljava/lang/ClassLoader;");
JException (env);
jo = (*env)->CallStaticObjectMethod(env, jc, jm);
JException (env);
jm = (*env)->GetMethodID(env, jc,
"findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
JException (env);
jc = (*env)->CallObjectMethod (env, jo, jm,
JStringFromNSString(env, cname));
JExceptionClear (env);
if (jc == 0) if (jc == 0)
{ {
jc = (*env)->CallObjectMethod (env, jo, jm,
JStringFromNSString(env, cname));
JException (env);
}
#endif
#if 1
/* Ensure the driver for the database is loaded.
*/
cname = [cname stringByReplacingString: @"." withString: @"/"];
if ((*env)->FindClass(env, [cname UTF8String]) == 0)
{
jclass retry;
JExceptionClear (env); JExceptionClear (env);
[self debug: @"Connect to '%@' failed to load driver '%@'", [self debug: @"Connect to '%@' failed to load driver '%@'",
[self name], cname]; [self name], cname];
retry = (*env)->FindClass(env, [cname UTF8String]);
if (retry != 0)
{
NSLog(@"WORKED ON RETRY");
}
else
{
return NO; return NO;
} }
}
#endif
/* Get the driver manager class. /* Get the driver manager class.
*/ */
jc = (*env)->FindClass(env, "java.sql.DriverManager"); jc = (*env)->FindClass(env, "java/sql/DriverManager");
if (jc == 0) if (jc == 0)
{ {
JExceptionClear (env); JExceptionClear (env);
@ -679,7 +724,7 @@ static NSNull *null = nil;
*/ */
jm = (*env)->GetStaticMethodID(env, jc, "getConnection", jm = (*env)->GetStaticMethodID(env, jc, "getConnection",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)" "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)"
"Ljava/sql/Connection"); "Ljava/sql/Connection;");
if (jm == 0) if (jm == 0)
{ {
JExceptionClear (env); JExceptionClear (env);
@ -690,7 +735,7 @@ static NSNull *null = nil;
/* Get the new connection object /* Get the new connection object
*/ */
jo = (void*)(*env)->CallStaticObjectMethod(env, jc, jm, jo = (*env)->CallStaticObjectMethod(env, jc, jm,
JStringFromNSString(env, url), JStringFromNSString(env, url),
JStringFromNSString(env, [self user]), JStringFromNSString(env, [self user]),
JStringFromNSString(env, [self password])); JStringFromNSString(env, [self password]));
@ -702,29 +747,9 @@ static NSNull *null = nil;
return NO; return NO;
} }
/* Get the method to set autocommit.
*/
jm = (*env)->GetMethodID(env, jo, "setAutoCommit", "(Z)V");
if (jm == 0)
{
JExceptionClear (env);
[self debug: @"Connect to '%@' failed to get commit method",
[self name]];
return NO;
}
/* Turn off autocommit
*/
(*env)->CallVoidMethod (env, jo, jm, 0);
if (JExceptionClear (env))
{
[self debug: @"Connect to '%@' failed to get change commit",
[self name]];
return NO;
}
/* Make a reference so it can't be garbage collected. /* Make a reference so it can't be garbage collected.
*/ */
jo = (void*)(*env)->NewGlobalRef(env, jo); jo = (*env)->NewGlobalRef(env, jo);
if (jo == 0) if (jo == 0)
{ {
JExceptionClear (env); JExceptionClear (env);
@ -743,37 +768,52 @@ static NSNull *null = nil;
NS_DURING NS_DURING
{ {
ji->connection = jo; ji->connection = jo;
jc = (*env)->GetObjectClass(env, ji->connection);
ji->commit = (*env)->GetMethodID (env, /* Get the method to set autocommit.
ji->connection, "commit", "()V"); */
jm = (*env)->GetMethodID(env, jc,
"setAutoCommit", "(Z)V");
JException (env);
/* Turn off autocommit
*/
(*env)->CallVoidMethod (env, ji->connection,
jm, JNI_FALSE);
JException (env);
ji->commit = (*env)->GetMethodID (env, jc,
"commit", "()V");
JException(env); JException(env);
ji->rollback = (*env)->GetMethodID (env, ji->rollback = (*env)->GetMethodID (env, jc,
ji->connection, "rollback", "()V"); "rollback", "()V");
JException(env); JException(env);
ji->prepare = (*env)->GetMethodID (env, ji->prepare = (*env)->GetMethodID (env, jc,
ji->connection, "prepareStatement", "prepareStatement",
"(Ljava/lang/String;)Ljava/sql/PreparedStatement"); "(Ljava/lang/String;)Ljava/sql/PreparedStatement;");
JException(env); JException(env);
jm = (*env)->GetMethodID (env, ji->connection, jm = (*env)->GetMethodID (env, jc,
"createStatement", "()Ljava/sql/Statement"); "createStatement",
"()Ljava/sql/Statement;");
JException(env); JException(env);
jo = (*env)->CallObjectMethod (env, ji->connection, jm); jo = (*env)->CallObjectMethod (env, ji->connection, jm);
JException(env); JException(env);
ji->statement = (*env)->NewGlobalRef(env, jo); ji->statement = (*env)->NewGlobalRef(env, jo);
JException(env); JException(env);
jc = (*env)->GetObjectClass(env, ji->statement);
ji->executeUpdate = (*env)->GetMethodID (env, ji->executeUpdate = (*env)->GetMethodID (env, jc,
ji->statement, "executeUpdate", "executeUpdate",
"(Ljava/lang/String;)"); "(Ljava/lang/String;)I");
JException(env); JException(env);
ji->executeQuery = (*env)->GetMethodID (env, ji->executeQuery = (*env)->GetMethodID (env, jc,
ji->statement, "executeQuery", "executeQuery",
"(Ljava/lang/String;)"); "(Ljava/lang/String;)Ljava/sql/ResultSet;");
JException(env); JException(env);
} }
NS_HANDLER NS_HANDLER
@ -842,6 +882,8 @@ static NSNull *null = nil;
{ {
CREATE_AUTORELEASE_POOL(arp); CREATE_AUTORELEASE_POOL(arp);
NSString *stmt = [info objectAtIndex: 0]; NSString *stmt = [info objectAtIndex: 0];
JNIEnv *env = SQLClientJNIEnv();
JInfo *ji = (JInfo*)extra;
if ([stmt length] == 0) if ([stmt length] == 0)
{ {
@ -852,8 +894,6 @@ static NSNull *null = nil;
NS_DURING NS_DURING
{ {
JNIEnv *env = SQLClientJNIEnv();
JInfo *ji = (JInfo*)extra;
jmethodID jm; jmethodID jm;
jobject js; jobject js;
@ -870,6 +910,7 @@ static NSNull *null = nil;
if ([info count] > 1) if ([info count] > 1)
{ {
unsigned i; unsigned i;
jclass jc;
stmt = [stmt stringByReplacingString: @"'?'''?'" withString: @"?"]; stmt = [stmt stringByReplacingString: @"'?'''?'" withString: @"?"];
@ -877,7 +918,9 @@ static NSNull *null = nil;
JStringFromNSString(env, stmt)); JStringFromNSString(env, stmt));
JException(env); JException(env);
jm = (*env)->GetMethodID (env, js, "setBytes", "(I[B)V"); jc = (*env)->GetObjectClass(env, js);
JException(env);
jm = (*env)->GetMethodID (env, jc, "setBytes", "(I[B)V");
JException(env); JException(env);
for (i = 1; i < [info count]; i++) for (i = 1; i < [info count]; i++)
@ -887,8 +930,7 @@ static NSNull *null = nil;
JException(env); JException(env);
} }
jm = (*env)->GetMethodID (env, js, jm = (*env)->GetMethodID (env, jc, "executeUpdate", "()I");
"executeUpdate", "()I");
JException(env); JException(env);
(*env)->CallIntMethod (env, js, jm); (*env)->CallIntMethod (env, js, jm);
} }
@ -898,9 +940,21 @@ static NSNull *null = nil;
ji->executeUpdate, JStringFromNSString(env, stmt)); ji->executeUpdate, JStringFromNSString(env, stmt));
} }
JException(env); JException(env);
if (_inTransaction == NO)
{
// Not in a transaction ... commit at once.
(*env)->CallVoidMethod (env, ji->connection, ji->commit);
JExceptionClear (env);
}
} }
NS_HANDLER NS_HANDLER
{ {
if (_inTransaction == NO)
{
// Not in a transaction ... rollback to clear error state
(*env)->CallVoidMethod (env, ji->connection, ji->rollback);
JExceptionClear (env);
}
if ([self debugging] > 0) if ([self debugging] > 0)
{ {
[self debug: @"Error executing statement:\n%@\n%@", [self debug: @"Error executing statement:\n%@\n%@",
@ -1125,9 +1179,9 @@ static unsigned int trim(char *str)
NS_DURING NS_DURING
{ {
JNIEnv *env = SQLClientJNIEnv(); JNIEnv *env = SQLClientJNIEnv();
JInfo *info = (JInfo*)extra; JInfo *ji = (JInfo*)extra;
(*env)->CallVoidMethod (env, info->connection, info->commit); (*env)->CallVoidMethod (env, ji->connection, ji->commit);
JException(env); JException(env);
_inTransaction = NO; _inTransaction = NO;
[lock unlock]; // Locked at start of -commit [lock unlock]; // Locked at start of -commit
@ -1152,9 +1206,9 @@ static unsigned int trim(char *str)
NS_DURING NS_DURING
{ {
JNIEnv *env = SQLClientJNIEnv(); JNIEnv *env = SQLClientJNIEnv();
JInfo *info = (JInfo*)extra; JInfo *ji = (JInfo*)extra;
(*env)->CallVoidMethod (env, info->connection, info->rollback); (*env)->CallVoidMethod (env, ji->connection, ji->rollback);
JException(env); JException(env);
[lock unlock]; // Locked at start of -rollback [lock unlock]; // Locked at start of -rollback
[lock unlock]; // Locked by -begin [lock unlock]; // Locked by -begin

2
configure vendored
View file

@ -1735,6 +1735,7 @@ if test "$postgres_topdir" != "no"; then
LIBD="$LIBD -L$postgres_topdir/lib" LIBD="$LIBD -L$postgres_topdir/lib"
fi fi
CPPFLAGS="$CPPFLAGS -I$JAVA_HOME/include"
ac_ext=c ac_ext=c
ac_cpp='$CPP $CPPFLAGS' ac_cpp='$CPP $CPPFLAGS'
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@ -3599,6 +3600,7 @@ fi
done done
if test "$ac_cv_header_jni_h" = "yes"; then if test "$ac_cv_header_jni_h" = "yes"; then
INCD="$INCD -I$JAVA_HOME/include"
JDBC=yes JDBC=yes
JDBC_VM_LIBS="-ljava -ljvm -lhpi" JDBC_VM_LIBS="-ljava -ljvm -lhpi"
jre_lib="$JAVA_HOME/jre/lib" jre_lib="$JAVA_HOME/jre/lib"

View file

@ -49,8 +49,10 @@ if test "$postgres_topdir" != "no"; then
LIBD="$LIBD -L$postgres_topdir/lib" LIBD="$LIBD -L$postgres_topdir/lib"
fi fi
CPPFLAGS="$CPPFLAGS -I$JAVA_HOME/include"
AC_CHECK_HEADERS(jni.h) AC_CHECK_HEADERS(jni.h)
if test "$ac_cv_header_jni_h" = "yes"; then if test "$ac_cv_header_jni_h" = "yes"; then
INCD="$INCD -I$JAVA_HOME/include"
JDBC=yes JDBC=yes
JDBC_VM_LIBS="-ljava -ljvm -lhpi" JDBC_VM_LIBS="-ljava -ljvm -lhpi"
jre_lib="$JAVA_HOME/jre/lib" jre_lib="$JAVA_HOME/jre/lib"