Skip to content

Commit

Permalink
Added ODBC driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenstott committed Nov 13, 2024
1 parent 062336a commit bf69e44
Show file tree
Hide file tree
Showing 21 changed files with 2,814 additions and 11 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ target/
adapters/file/data/files/2022CbpOceanEconomyTable__2022CbpIaOceanEconomy.json
adapters/file/data/files/2022CbpOceanEconomyTable__Notes.json
/grafana/tempo-data/

# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
*.sln.ide/

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
19 changes: 19 additions & 0 deletions calcite-rs-jni/odbc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
*.sln.ide/

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
12 changes: 12 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC-Tester/DDN-ODBC-Tester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>DDN_ODBC_Tester</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DDN-ODBC\DDN-ODBC.csproj" />
</ItemGroup>
</Project>
215 changes: 215 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC-Tester/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
using System;
using System.Data;
using DDN_ODBC;

namespace DDN_ODBC_Tester
{
class OdbcMetadataExample
{
static void Main()
{
string connectionString = "jdbc:graphql:http://localhost:3280/graphql";

try
{
using (DDN_ODBC.DDN_ODBC connection = new DDN_ODBC.DDN_ODBC(connectionString))
{
connection.Open();
Console.WriteLine("Connected successfully.\n");

// Get Tables
Console.WriteLine("=== Tables ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SQLTables";

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}

// Get Tables Parameterized
Console.WriteLine("=== Tables ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "{ CALL SQLTables(?, ?, ?, ?) }";

// Adding parameters (catalog, schema, table, table type)
// Create parameters
var param1 = new DDNDataParameter("@catalog", DbType.String) { Value = DBNull.Value };
var param2 = new DDNDataParameter("@schema", DbType.String) { Value = DBNull.Value };
var param3 = new DDNDataParameter("@table", DbType.String) { Value = DBNull.Value };
var param4 = new DDNDataParameter("@tableType", DbType.String) { Value = "TABLE" };

// Add parameters
command.Parameters.Add(param1);
command.Parameters.Add(param2);
command.Parameters.Add(param3);
command.Parameters.Add(param4);

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}

// Get Columns
Console.WriteLine("\n=== Columns for YourTable ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SQLColumns";

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}

// Get Columns
Console.WriteLine("\n=== Columns for YourTable ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "{ CALL SQLColumns(?, ?, ?, ?) }";

// Adding parameters (catalog, schema, table, table type)
// Create parameters
var param1 = new DDNDataParameter("@catalog", DbType.String) { Value = DBNull.Value };
var param2 = new DDNDataParameter("@schema", DbType.String) { Value = DBNull.Value };
var param3 = new DDNDataParameter("@table", DbType.String) { Value = DBNull.Value };
var param4 = new DDNDataParameter("@column", DbType.String) { Value = "fi%" };

// Add parameters
command.Parameters.Add(param1);
command.Parameters.Add(param2);
command.Parameters.Add(param3);
command.Parameters.Add(param4);

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}

// Get Columns
Console.WriteLine("\n=== Plain old SQL for Customer Table ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM Customer";

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}

// Get Primary Keys
Console.WriteLine("\n=== Primary Keys ===");
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SQLPrimaryKeys";

using (IDataReader reader = command.ExecuteReader())
{
// Print column names
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader.GetName(i)}\t");
}
Console.WriteLine();

// Print data
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write($"{reader[i]}\t");
}
Console.WriteLine();
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"\nError: {ex.Message}");
}
}
}
}
31 changes: 31 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDN-ODBC-Tester", "DDN-ODBC-Tester\DDN-ODBC-Tester.csproj", "{BD626E9E-0270-49A1-8634-82513B607B30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDN-ODBC", "DDN-ODBC\DDN-ODBC.csproj", "{F0F0EDBF-19F1-4B16-B1CE-45F86526ABAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BD626E9E-0270-49A1-8634-82513B607B30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD626E9E-0270-49A1-8634-82513B607B30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD626E9E-0270-49A1-8634-82513B607B30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD626E9E-0270-49A1-8634-82513B607B30}.Release|Any CPU.Build.0 = Release|Any CPU
{F0F0EDBF-19F1-4B16-B1CE-45F86526ABAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0F0EDBF-19F1-4B16-B1CE-45F86526ABAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0F0EDBF-19F1-4B16-B1CE-45F86526ABAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0F0EDBF-19F1-4B16-B1CE-45F86526ABAD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C87A2184-733D-4127-86EE-CDF175E7BDE5}
EndGlobalSection
EndGlobal
74 changes: 74 additions & 0 deletions calcite-rs-jni/odbc/DDN-ODBC/CommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Text.RegularExpressions;

namespace DDN_ODBC;

public class CommandParser
{
private static readonly Regex CallCommandRegex = new(@"{\s*CALL\s+(?<command>\w+)\s*\((?<params>[^)]*)\)\s*}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex SimpleCommandRegex = new(@"^(?<command>\w+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ParameterRegex = new(@"\?", RegexOptions.Compiled);

private readonly HashSet<string> _validCommands;

public CommandParser(IEnumerable<string> validCommands)
{
_validCommands = new HashSet<string>(validCommands, StringComparer.OrdinalIgnoreCase);
}

public CommandParser()
{
var validCommands = new List<string>
{
"SQLTables",
"SQLColumns",
"SQLProcedures",
"SQLProcedureColumns",
"SQLPrimaryKeys",
"SQLForeignKeys",
"SQLStatistics",
"SQLSpecialColumns",
"SQLGetTypeInfo",
"SQLTablePrivileges",
"SQLColumnPrivileges",
"SQLGetFunctions"
};
_validCommands = new HashSet<string>(validCommands, StringComparer.OrdinalIgnoreCase);
}

public bool TryParseCommand(string commandText, out string command, out int parameterCount)
{
command = null;
parameterCount = 0;

// Try to match the CALL command format
var match = CallCommandRegex.Match(commandText);
if (match.Success)
{
command = match.Groups["command"].Value;
if (!_validCommands.Contains(command))
{
return false;
}

string paramsGroup = match.Groups["params"].Value;
parameterCount = 0;
if (!string.IsNullOrEmpty(paramsGroup))
{
parameterCount = ParameterRegex.Matches(paramsGroup).Count;
}

return true;
}

// Try to match the simple command format
match = SimpleCommandRegex.Match(commandText);
if (match.Success)
{
command = match.Groups["command"].Value;
return _validCommands.Contains(command);
}

// No match
return false;
}
}
Loading

0 comments on commit bf69e44

Please sign in to comment.