Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Birschick bq/ad 638/refactor connection #48

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/odbc-test/src/java_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ struct JavaTestSuiteFixture : OdbcTestSuite {
SharedPointer< JniContext > _ctx;

JavaTestSuiteFixture& operator=(const JavaTestSuiteFixture& other) = delete;

JniErrorCode GetConnection(const std::string connectionString,
SharedPointer< GlobalJObject >& connection,
JniErrorInfo& errInfo) {
JniErrorCode success;
SharedPointer< GlobalJObject > connectionProperties;
success =
_ctx.Get()
->DocumentDbConnectionPropertiesGetPropertiesFromConnectionString(
connectionString, connectionProperties, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
return success;
}

success = _ctx.Get()->DocumentDbConnectionCtor(connectionProperties,
connection, errInfo);
return success;
}
};

struct AutoCloseConnection {
Expand Down Expand Up @@ -159,8 +177,9 @@ BOOST_AUTO_TEST_CASE(TestDriverManagerGetConnection) {

JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);

JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand All @@ -177,8 +196,8 @@ BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetSshTunnelPort) {
// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand Down Expand Up @@ -221,8 +240,8 @@ BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetSshTunnelPortSshTunnelNotActive,
// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand Down Expand Up @@ -260,8 +279,8 @@ BOOST_AUTO_TEST_CASE(TestDocumentDbConnectionGetDatabaseMetadata) {
// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand All @@ -285,8 +304,8 @@ BOOST_AUTO_TEST_CASE(TestDocumentDbDatabaseSchemaMetadataGetSchemaName) {
// get Driver manager connection
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand Down Expand Up @@ -320,8 +339,8 @@ BOOST_AUTO_TEST_CASE(TestConnectionGetMetaData) {

JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand All @@ -343,8 +362,8 @@ BOOST_AUTO_TEST_CASE(TestDatabaseMetaDataGetTables) {

JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand Down Expand Up @@ -490,8 +509,8 @@ BOOST_AUTO_TEST_CASE(TestDatabaseMetaDataGetColumns) {
JniErrorInfo errInfo;
SharedPointer< GlobalJObject > connection;
// get connection
JniErrorCode success = _ctx.Get()->DriverManagerGetConnection(
_jdbcConnectionString.c_str(), connection, errInfo);
JniErrorCode success =
GetConnection(_jdbcConnectionString, connection, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
BOOST_FAIL(errInfo.errMsg);
}
Expand Down
16 changes: 10 additions & 6 deletions src/odbc/include/ignite/odbc/jni/java.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,12 @@ struct JniMembers {
jmethodID m_DocumentDbConnectionIsSshTunnelActive;
jmethodID m_DocumentDbConnectionGetDatabaseMetadata;
jmethodID m_DocumentDbConnectionGetConnectionProperties;
jmethodID m_DocumentDbConnectionCtor;
jmethodID m_DocumentDbClose;

jclass c_DocumentDbDatabaseSchemaMetadata;
jmethodID m_DocumentDbDatabaseSchemaMetadataGetSchemaName;

jclass c_DriverManager;
jmethodID m_DriverManagerGetConnection;

jclass c_Connection;
jmethodID m_ConnectionClose;
jmethodID m_ConnectionGetMetaData;
Expand Down Expand Up @@ -490,9 +488,6 @@ class IGNITE_IMPORT_EXPORT JniContext {

std::string JavaStringToCppString(const SharedPointer< GlobalJObject >& jstring);

JniErrorCode DriverManagerGetConnection(
const char* connectionString, SharedPointer< GlobalJObject >& connection,
JniErrorInfo& errInfo);
JniErrorCode ConnectionClose(const SharedPointer< GlobalJObject >& connection,
JniErrorInfo& errInfo);
JniErrorCode ConnectionGetMetaData(
Expand All @@ -511,6 +506,15 @@ class IGNITE_IMPORT_EXPORT JniContext {
JniErrorCode DocumentDbConnectionGetConnectionProperties(
const SharedPointer< GlobalJObject >& connection,
SharedPointer< GlobalJObject >& connectionProperties, JniErrorInfo& errInfo);
JniErrorCode DocumentDbConnectionCtor(
const SharedPointer< GlobalJObject >& connectionProperties,
SharedPointer< GlobalJObject >& connection,
JniErrorInfo& errInfo);

JniErrorCode DocumentDbConnectionPropertiesGetPropertiesFromConnectionString(
const std::string& connectionString,
SharedPointer< GlobalJObject >& connectionProperties,
JniErrorInfo& errInfo);

JniErrorCode DocumentDbDatabaseSchemaMetadataGetSchemaName(
const SharedPointer< GlobalJObject >& databaseMetadata,
Expand Down
20 changes: 16 additions & 4 deletions src/odbc/src/jni/documentdb_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,28 @@ JniErrorCode DocumentDbConnection::Open(const Configuration& config,

std::string connectionString = config.ToJdbcConnectionString();

SharedPointer< GlobalJObject > result;
JniErrorCode success = _jniContext.Get()->DriverManagerGetConnection(
connectionString.c_str(), result, errInfo);
JniErrorCode success;
SharedPointer< GlobalJObject > connectionProperties;
success =
_jniContext.Get()
->DocumentDbConnectionPropertiesGetPropertiesFromConnectionString(
connectionString, connectionProperties, errInfo);
if (success != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
JniErrorInfo closeErrInfo;
Close(closeErrInfo);
return success;
}

SharedPointer< GlobalJObject > connection;
success = _jniContext.Get()->DocumentDbConnectionCtor(connectionProperties,
connection, errInfo);
connected = (success == JniErrorCode::IGNITE_JNI_ERR_SUCCESS);
if (!connected) {
JniErrorInfo closeErrInfo;
Close(closeErrInfo);
return success;
}
_connection = result;
_connection = connection;

return success;
}
Expand Down
90 changes: 63 additions & 27 deletions src/odbc/src/jni/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ const char* const C_STRING = "java/lang/String";

const char* const C_DOCUMENTDB_CONNECTION_PROPERTIES =
"software/amazon/documentdb/jdbc/DocumentDbConnectionProperties";
JniMethod const
M_DOCUMENTDB_CONNECTION_PROPERTIES_GET_PROPERTIES_FROM_CONNECTION_STRING =
JniMethod("getPropertiesFromConnectionString",
"(Ljava/lang/String;)Lsoftware/amazon/documentdb/jdbc/"
"DocumentDbConnectionProperties;",
true);
JniMethod const M_DOCUMENTDB_CONNECTION_PROPERTIES_GET_PROPERTIES_FROM_CONNECTION_STRING =
JniMethod(
"getPropertiesFromConnectionString",
"(Ljava/lang/String;)"
"Lsoftware/amazon/documentdb/jdbc/DocumentDbConnectionProperties;",
true);

const char* const C_RECORD_SET = "java/sql/ResultSet";
JniMethod const M_RECORD_SET_CLOSE = JniMethod("close", "()V", false);
Expand Down Expand Up @@ -262,6 +262,10 @@ JniMethod const M_DOCUMENTDB_CONNECTION_GET_CONNECTION_PROPERTIES =
"()Lsoftware/amazon/documentdb/jdbc/"
"DocumentDbConnectionProperties;",
false);
JniMethod const M_DOCUMENTDB_CONNECTION_INIT = JniMethod(
"<init>",
"(Lsoftware/amazon/documentdb/jdbc/DocumentDbConnectionProperties;)V",
false);

const char* const C_DOCUMENTDB_DATABASE_SCHEMA_METADATA =
"software/amazon/documentdb/jdbc/metadata/DocumentDbDatabaseSchemaMetadata";
Expand Down Expand Up @@ -573,17 +577,22 @@ void JniMembers::Initialize(JNIEnv* env) {
m_DocumentDbConnectionGetConnectionProperties =
FindMethod(env, c_DocumentDbConnection,
M_DOCUMENTDB_CONNECTION_GET_CONNECTION_PROPERTIES);
m_DocumentDbConnectionCtor =
FindMethod(env, c_DocumentDbConnection,
M_DOCUMENTDB_CONNECTION_INIT);

c_DocumentDbConnectionProperties =
FindClass(env, C_DOCUMENTDB_CONNECTION_PROPERTIES);
m_DocumentDbConnectionPropertiesGetPropertiesFromConnectionString =
FindMethod(env, c_DocumentDbConnectionProperties,
M_DOCUMENTDB_CONNECTION_PROPERTIES_GET_PROPERTIES_FROM_CONNECTION_STRING);

c_DocumentDbDatabaseSchemaMetadata =
FindClass(env, C_DOCUMENTDB_DATABASE_SCHEMA_METADATA);
m_DocumentDbDatabaseSchemaMetadataGetSchemaName =
FindMethod(env, c_DocumentDbDatabaseSchemaMetadata,
M_DOCUMENTDB_DATABASE_SCHEMA_METADATA_GET_SCHEMA_NAME);

c_DriverManager = FindClass(env, C_DRIVERMANAGER);
m_DriverManagerGetConnection =
FindMethod(env, c_DriverManager, M_DRIVERMANAGER_GET_CONNECTION);

c_ResultSet = FindClass(env, C_RECORD_SET);
m_ResultSetClose = FindMethod(env, c_ResultSet, M_RECORD_SET_CLOSE);
m_ResultSetNext = FindMethod(env, c_ResultSet, M_RECORD_SET_NEXT);
Expand Down Expand Up @@ -956,23 +965,6 @@ std::string JniContext::JavaStringToCppString(
len);
}

JniErrorCode JniContext::DriverManagerGetConnection(
const char* connectionString, SharedPointer< GlobalJObject >& connection,
JniErrorInfo& errInfo) {
JNIEnv* env = Attach();
jstring jConnectionString = env->NewStringUTF(connectionString);
jobject result = env->CallStaticObjectMethod(
jvm->GetMembers().c_DriverManager,
jvm->GetMembers().m_DriverManagerGetConnection, jConnectionString);
ExceptionCheck(env, &errInfo);
if (!result || errInfo.code != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
connection = nullptr;
} else {
connection = new GlobalJObject(env, env->NewGlobalRef(result));
}
return errInfo.code;
}

JniErrorCode JniContext::ConnectionClose(
const SharedPointer< GlobalJObject >& connection, JniErrorInfo& errInfo) {
if (connection.Get() == nullptr) {
Expand Down Expand Up @@ -1069,6 +1061,50 @@ JniErrorCode JniContext::DocumentDbConnectionGetConnectionProperties(
return errInfo.code;
}

JniErrorCode JniContext::DocumentDbConnectionCtor(
const SharedPointer< GlobalJObject >& connectionProperties,
SharedPointer< GlobalJObject >& connection, JniErrorInfo& errInfo) {
if (!connectionProperties.IsValid()) {
errInfo.code = JniErrorCode::IGNITE_JNI_ERR_GENERIC;
errInfo.errMsg =
"Connection Properties and Database Metadata objects must be set.";
return errInfo.code;
}

JNIEnv* env = Attach();
jobject result = env->NewObject(jvm->GetMembers().c_DocumentDbConnection,
jvm->GetMembers().m_DocumentDbConnectionCtor,
connectionProperties.Get()->GetRef());
ExceptionCheck(env, &errInfo);
if (!result || errInfo.code != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
connection = nullptr;
return errInfo.code;
}
connection = new GlobalJObject(env, env->NewGlobalRef(result));
return errInfo.code;
}

JniErrorCode
JniContext::DocumentDbConnectionPropertiesGetPropertiesFromConnectionString(
const std::string& connectionString,
SharedPointer< GlobalJObject >& connectionProperties,
JniErrorInfo& errInfo) {
JNIEnv* env = Attach();
jstring jConnectionString = env->NewStringUTF(connectionString.c_str());
jobject result = env->CallStaticObjectMethod(
jvm->GetMembers().c_DocumentDbConnectionProperties,
jvm->GetMembers()
.m_DocumentDbConnectionPropertiesGetPropertiesFromConnectionString,
jConnectionString);
ExceptionCheck(env, &errInfo);
if (!result || errInfo.code != JniErrorCode::IGNITE_JNI_ERR_SUCCESS) {
connectionProperties = nullptr;
return errInfo.code;
}
connectionProperties = new GlobalJObject(env, env->NewGlobalRef(result));
return errInfo.code;
}

JniErrorCode JniContext::DocumentDbDatabaseSchemaMetadataGetSchemaName(
const SharedPointer< GlobalJObject >& databaseMetadata, std::string& value,
bool& wasNull, JniErrorInfo& errInfo) {
Expand Down