Skip to content

Commit

Permalink
various
Browse files Browse the repository at this point in the history
  • Loading branch information
kenstott committed Nov 26, 2024
1 parent 0e62bd1 commit b8388ec
Show file tree
Hide file tree
Showing 19 changed files with 1,565 additions and 858 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public VectorSchemaRoot executeQuery(String query) throws Exception {
try {
ArrowResultSet resultSet = executeQueryBatched(query);
VectorSchemaRoot result = resultSet.nextBatch();
resultSet.close();
logger.info("Successfully executed query and got results");
logger.info("Successfully executed query and got results. Remember to close the vector root to release memory.");
return result;
} catch (Exception e) {
logger.severe("Error executing query: " + e.getMessage());
Expand Down
21 changes: 21 additions & 0 deletions calcite-rs-jni/odbc/.run/DDN-ODBC-Tester.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="DDN-ODBC-Tester" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/bin/ARM64/Debug/DDN-ODBC-Tester.exe" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/bin/ARM64/Debug" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/DDN-ODBC-Tester/DDN-ODBC-Tester.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="Unloaded" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" default="false" projectName="DDN-ODBC-Tester" projectPath="$PROJECT_DIR$/DDN-ODBC-Tester/DDN-ODBC-Tester.csproj" />
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_External Tools_Powershell" />
</method>
</configuration>
</component>
5 changes: 5 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC-Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ set(SOURCE_FILES
src/SQLGetStmtAttr.cpp
src/SQLSetDescField.cpp
src/SQLGetTypeInfo.cpp
src/SQLBindParameter.cpp
)

# Header files
Expand All @@ -110,6 +111,8 @@ set(HEADER_FILES
include/globals.hpp
include/logging.hpp
include/statement.hpp
include/JniParam.hpp
src/JniParam.cpp
)

# Function to filter files starting with `._`
Expand All @@ -125,6 +128,8 @@ endfunction()
filter_files(SOURCE_FILES)
filter_files(HEADER_FILES)

add_definitions(-D_SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING)

# Create the library
add_library(${PROJECT_NAME} SHARED
${SOURCE_FILES}
Expand Down
63 changes: 63 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC-Driver/include/JniParam.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Created by kennethstott on 11/26/2024.
//

#ifndef JNIPARAM_H
#define JNIPARAM_H

#endif //JNIPARAM_H

#pragma once

#include <jni.h>
#include <string>
#include <vector>

class JniParam {
public:
enum class Type {
String,
StringArray,
Integer,
Float,
Double,
Boolean
};

// Constructors for different types
explicit JniParam(const std::string& value);
explicit JniParam(const std::vector<std::string>& value);
explicit JniParam(int value);
explicit JniParam(float value);
explicit JniParam(double value);
explicit JniParam(bool value);

JniParam();

// Get the JNI signature for this type
std::string getSignature() const;

// Convert the parameter to JNI value
jvalue toJValue(JNIEnv* env) const;

// Clean up any JNI resources
void cleanup(JNIEnv* env, const jvalue& value) const;

// Get the parameter type
[[nodiscard]] Type getType() const { return type_; }
[[nodiscard]] std::string getString() const { return stringValue_; }
[[nodiscard]] std::vector<std::string> getStringArray() const { return stringArrayValue_; }
[[nodiscard]] int getInt() const { return intValue_; }
[[nodiscard]] float getFloat() const { return floatValue_; }
[[nodiscard]] double getDouble() const { return doubleValue_; }
[[nodiscard]] bool getBool() const { return boolValue_; }

private:
Type type_;
std::string stringValue_;
std::vector<std::string> stringArrayValue_;
int intValue_ = 0;
float floatValue_ = 0.0f;
double doubleValue_ = 0.0;
bool boolValue_ = false;
};
151 changes: 89 additions & 62 deletions calcite-rs-jni/odbc/DDN-ODBC-Driver/include/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,120 @@
#pragma once

// Windows includes must come first
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#define WIN32_LEAN_AND_MEAN

// SQL includes
// ODBC includes
#include <sql.h>

// JNI includes
#include <jni.h>

// Standard includes
#include <complex.h>
#include <string>
#include <vector>


// JNI includes
#include <jni.h>

#include "JniParam.hpp"
#include "statement.hpp"

// Forward declarations
class Environment;
class Statement;

struct ConnectionParams {
std::string server;
std::string port;
std::string database;
std::string role;
std::string auth;
std::string uid;
std::string pwd;
std::string encrypt = "no";
std::string timeout;

bool isValid() const {
return !server.empty() && !port.empty() && !database.empty();
}
};

// struct ColumnDesc {
// std::string name;
// SQLSMALLINT sqlType;
// SQLULEN columnSize;
// SQLSMALLINT decimalDigits;
// SQLSMALLINT nullable;
// };

class Connection {
private:
JavaVM* jvm{};
jobject wrapperInstance{};
jclass wrapperClass{};
std::string connectionString;
bool isConnected{};

struct ConnectionParams {
std::string server;
std::string port;
std::string database;
std::string role;
std::string auth;
std::string uid;
std::string pwd;
std::string encrypt;
std::string timeout;

[[nodiscard]] bool isValid() const {
return !server.empty() && !port.empty() && !database.empty();
}
};
public:
Connection() = default;
~Connection();

bool initJVM();
bool initWrapper(const ConnectionParams& params);
// Delete copy constructor and assignment operator
Connection(const Connection&) = delete;
Connection& operator=(const Connection&) = delete;

static bool parseConnectionString(const std::string& connStr, ConnectionParams& params);
// Connection string methods
void setConnectionString(const std::string& dsn, const std::string& uid, const std::string& authStr);
void setConnectionString(const std::string& dsn);

static std::string buildJdbcUrl(const ConnectionParams &params);
std::vector<Statement*> activeStmts;
// Connection management
SQLRETURN connect();
SQLRETURN disconnect();

public:
// Query methods
SQLRETURN Query(const std::string& query, Statement* stmt);
SQLRETURN GetTables(
const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
const std::string& tableType,
Statement* stmt) const;
SQLRETURN GetColumns(
const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
const std::string& columnName,
Statement* stmt) const;

// Statement management
bool hasActiveStmts() const;

void cleanupActiveStmts();

SQLRETURN GetTables(const std::string &catalogName, const std::string &schemaName, const std::string &tableName,
const std::string &tableType, Statement *stmt) const;

SQLRETURN GetColumns(const std::string &catalogName, const std::string &schemaName, const std::string &tableName,
const std::string &columnName, Statement *stmt) const;

Connection() = default;
~Connection();

SQLRETURN connect();
// Get connection state
bool isConnected() const { return connected; }
JNIEnv* env = nullptr;
[[nodiscard]] const std::string& getConnectionString() const { return connectionString; }

void setConnectionString(const std::string &dsn, const std::string &uid, const std::string &authStr);
void setConnectionString(const std::string &dsn);
private:
// Connection state
bool connected = false;
std::string connectionString;
std::vector<Statement*> activeStmts;

// JVM/JNI state
JavaVM* jvm = nullptr;
jclass wrapperClass = nullptr;
jobject wrapperInstance = nullptr;

// Initialization helpers
bool initJVM();
bool initWrapper(const ConnectionParams& params);
bool parseConnectionString(const std::string& connStr, ConnectionParams& params);
std::string buildJdbcUrl(const ConnectionParams& params);

SQLRETURN populateColumnDescriptors(jobject schemaRoot, Statement *stmt) const;

// Delete copy constructor and assignment operator
Connection(const Connection&) = delete;
Connection& operator=(const Connection&) = delete;

SQLRETURN disconnect();
JNIEnv* env{};
// Result set handling
SQLRETURN executeAndGetArrowResult(
const char *methodName,
const std::vector<JniParam> &params,
Statement *stmt) const;

// Getters
[[nodiscard]] JavaVM* getJVM() const { return jvm; }
[[nodiscard]] jobject getWrapperInstance() const { return wrapperInstance; }
[[nodiscard]] jclass getWrapperClass() const { return wrapperClass; }
[[nodiscard]] const std::string& getConnectionString() const { return connectionString; }
// Type mapping helpers
static SQLSMALLINT mapArrowTypeToSQL(JNIEnv* env, jobject arrowType);
static SQLULEN getSQLTypeSize(SQLSMALLINT sqlType);
};

// Helper functions declarations
std::string GetModuleDirectory(HMODULE hModule);
std::string GetModuleDirectory();
std::string WideStringToString(const std::wstring& wstr);
2 changes: 2 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC-Driver/include/environment.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <winsock2.h>
#include <windows.h>
#define WIN32_LEAN_AND_MEAN
#include <sql.h>
#include <sqlext.h>
#include <string>
Expand Down
41 changes: 36 additions & 5 deletions calcite-rs-jni/odbc/DDN-ODBC-Driver/include/statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string>
#include <jni.h> // Add this if you're using JNI types

#include "JniParam.hpp"

// Forward declaration of Connection class
class Connection;

Expand All @@ -17,6 +19,27 @@ struct ColumnDesc {
SQLULEN columnSize;
SQLSMALLINT decimalDigits;
SQLSMALLINT nullable;
const char* catalogName;
SQLSMALLINT catalogNameLength;
const char* schemaName;
SQLSMALLINT schemaNameLength;
const char* tableName;
SQLSMALLINT tableNameLength;
const char* baseColumnName;
SQLSMALLINT baseColumnNameLength;
const char* baseTableName;
SQLSMALLINT baseTableNameLength;
const char* literalPrefix;
SQLSMALLINT literalPrefixLength;
const char* literalSuffix;
SQLSMALLINT literalSuffixLength;
const char* localTypeName;
SQLSMALLINT localTypeNameLength;
SQLSMALLINT unnamed;
const char* label;
SQLSMALLINT labelLength;
SQLULEN displaySize;
SQLSMALLINT scale;
};

struct ColumnData {
Expand All @@ -25,9 +48,11 @@ struct ColumnData {
};

class Statement {
public:
std::vector<ColumnDesc> setupColumnResultColumns();
private:
std::vector<JniParam> boundParams;
std::string originalQuery;

public:
explicit Statement(Connection* connection);

// Delete copy constructor and assignment operator
Expand All @@ -36,11 +61,17 @@ class Statement {

void clearResults();

std::vector<ColumnDesc> setupTableResultColumns();
SQLRETURN bindParameter(SQLUSMALLINT parameterNumber, SQLSMALLINT inputOutputType, SQLSMALLINT valueType,
SQLSMALLINT parameterType, SQLULEN columnSize, SQLSMALLINT decimalDigits,
SQLPOINTER parameterValuePtr, SQLLEN bufferLength, SQLLEN *strLen_or_IndPtr);

std::string escapeString(const std::string &str) const;
std::string buildInterpolatedQuery() const;
SQLRETURN setArrowResult(jobject schemaRoot, const std::vector<ColumnDesc> &columnDescriptors);
SQLRETURN setOriginalQuery(const std::string &query) { originalQuery = query; return SQL_SUCCESS; }
SQLRETURN getData(SQLUSMALLINT colNum, SQLSMALLINT targetType,
SQLPOINTER targetValue, SQLLEN bufferLength,
SQLLEN* strLengthOrIndicator);
SQLPOINTER targetValue, SQLLEN bufferLength,
SQLLEN* strLengthOrIndicator);
SQLRETURN fetch();
[[nodiscard]] SQLRETURN getFetchStatus() const;
[[nodiscard]] bool hasData() const;
Expand Down
Loading

0 comments on commit b8388ec

Please sign in to comment.