Skip to content

Commit

Permalink
fix(csharp): include GetTableTypes and GetTableSchema call for .NET 4…
Browse files Browse the repository at this point in the history
….7.2 (#950)

Continuation of #930

Resolve #947

---------

Co-authored-by: David Coe <[email protected]>
  • Loading branch information
davidhcoe and David Coe authored Jul 31, 2023
1 parent 0a5afa3 commit 2de52f3
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 83 deletions.
116 changes: 59 additions & 57 deletions csharp/src/Apache.Arrow.Adbc/AdbcException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,75 @@
*/

using System;
using Apache.Arrow.Adbc;

/// <summary>
/// The root exception when working with Adbc drivers.
/// </summary>
public class AdbcException : Exception
namespace Apache.Arrow.Adbc
{
private AdbcStatusCode _statusCode = AdbcStatusCode.UnknownError;

public AdbcException()
/// <summary>
/// The root exception when working with Adbc drivers.
/// </summary>
public class AdbcException : Exception
{
}
private AdbcStatusCode _statusCode = AdbcStatusCode.UnknownError;

public AdbcException(string message)
: base(message)
{
}
public AdbcException()
{
}

public AdbcException(string message, AdbcStatusCode statusCode)
: base(message)
{
_statusCode = statusCode;
}
public AdbcException(string message)
: base(message)
{
}

public AdbcException(string message, AdbcStatusCode statusCode, Exception innerException)
: base(message, innerException)
{
}
public AdbcException(string message, AdbcStatusCode statusCode)
: base(message)
{
_statusCode = statusCode;
}

public AdbcException(string message, Exception innerException)
: base(message, innerException)
{
}
public AdbcException(string message, AdbcStatusCode statusCode, Exception innerException)
: base(message, innerException)
{
}

public static AdbcException NotImplemented(string message)
{
return new AdbcException(message, AdbcStatusCode.NotImplemented);
}
public AdbcException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// For database providers which support it, contains a standard
/// SQL 5-character return code indicating the success or failure
/// of the database operation. The first 2 characters represent the
/// class of the return code (e.g. error, success), while the
/// last 3 characters represent the subclass, allowing detection
/// of error scenarios in a database-portable way.
/// For database providers which don't support it, or for
/// inapplicable error scenarios, contains null.
/// </summary>
public virtual string SqlState
{
get => null;
}
public static AdbcException NotImplemented(string message)
{
return new AdbcException(message, AdbcStatusCode.NotImplemented);
}

/// <summary>
/// Gets or sets the <see cref="AdbcStatusCode"/> for the error.
/// </summary>
public AdbcStatusCode Status
{
get => _statusCode;
}
/// <summary>
/// For database providers which support it, contains a standard
/// SQL 5-character return code indicating the success or failure
/// of the database operation. The first 2 characters represent the
/// class of the return code (e.g. error, success), while the
/// last 3 characters represent the subclass, allowing detection
/// of error scenarios in a database-portable way.
/// For database providers which don't support it, or for
/// inapplicable error scenarios, contains null.
/// </summary>
public virtual string SqlState
{
get => null;
}

/// <summary>
/// Gets a native error number.
/// </summary>
public virtual int NativeError
{
get => 0;
/// <summary>
/// Gets or sets the <see cref="AdbcStatusCode"/> for the error.
/// </summary>
public AdbcStatusCode Status
{
get => _statusCode;
}

/// <summary>
/// Gets a native error number.
/// </summary>
public virtual int NativeError
{
get => 0;
}
}
}
4 changes: 2 additions & 2 deletions csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public class CAdbcDriverExporter
internal unsafe delegate AdbcStatusCode ConnectionGetObjects(CAdbcConnection* connection, int depth, byte* catalog, byte* db_schema, byte* table_name, byte** table_type, byte* column_name, CArrowArrayStream* stream, CAdbcError* error);
private static unsafe readonly NativeDelegate<ConnectionGetObjects> s_connectionGetObjects = new NativeDelegate<ConnectionGetObjects>(GetConnectionObjects);
private static IntPtr ConnectionGetObjectsPtr => s_connectionGetObjects.Pointer;
private unsafe delegate AdbcStatusCode ConnectionGetTableSchema(CAdbcConnection* connection, byte* catalog, byte* db_schema, byte* table_name, CArrowSchema* schema, CAdbcError* error);
internal unsafe delegate AdbcStatusCode ConnectionGetTableSchema(CAdbcConnection* connection, byte* catalog, byte* db_schema, byte* table_name, CArrowSchema* schema, CAdbcError* error);
private static unsafe readonly NativeDelegate<ConnectionGetTableSchema> s_connectionGetTableSchema = new NativeDelegate<ConnectionGetTableSchema>(GetConnectionTableSchema);
private static IntPtr ConnectionGetTableSchemaPtr => s_connectionGetTableSchema.Pointer;
private unsafe delegate AdbcStatusCode ConnectionGetTableTypes(CAdbcConnection* connection, CArrowArrayStream* stream, CAdbcError* error);
internal unsafe delegate AdbcStatusCode ConnectionGetTableTypes(CAdbcConnection* connection, CArrowArrayStream* stream, CAdbcError* error);
private static unsafe readonly NativeDelegate<ConnectionGetTableTypes> s_connectionGetTableTypes = new NativeDelegate<ConnectionGetTableTypes>(GetConnectionTableTypes);
private static IntPtr ConnectionGetTableTypesPtr => s_connectionGetTableTypes.Pointer;
internal unsafe delegate AdbcStatusCode ConnectionInit(CAdbcConnection* connection, CAdbcDatabase* database, CAdbcError* error);
Expand Down
129 changes: 105 additions & 24 deletions csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,34 @@ public override unsafe IArrowArrayStream GetObjects(GetObjectsDepth depth, strin

return arrowArrayStream;
}

public override unsafe IArrowArrayStream GetTableTypes()
{
CArrowArrayStream* nativeArrayStream = CArrowArrayStream.Create();

using (CallHelper caller = new CallHelper())
{
caller.Call(_nativeDriver.ConnectionGetTableTypes, ref _nativeConnection, nativeArrayStream);
}

IArrowArrayStream arrowArrayStream = CArrowArrayStreamImporter.ImportArrayStream(nativeArrayStream);

return arrowArrayStream;
}

public override unsafe Schema GetTableSchema(string catalog, string db_schema, string table_name)
{
CArrowSchema* nativeSchema = CArrowSchema.Create();

using (CallHelper caller = new CallHelper())
{
caller.Call(_nativeDriver.ConnectionGetTableSchema, ref _nativeConnection, catalog, db_schema, table_name, nativeSchema);
}

Schema schema = CArrowSchemaImporter.ImportSchema(nativeSchema);

return schema;
}
}

/// <summary>
Expand Down Expand Up @@ -585,6 +613,68 @@ public unsafe void Call(IntPtr fn, ref CAdbcStatement nativeStatement, CArrowArr
}
#endif

#if NET5_0_OR_GREATER
public unsafe void Call(delegate* unmanaged<CAdbcConnection*, byte*, byte*, byte*, CArrowSchema*, CAdbcError*, AdbcStatusCode> fn, ref CAdbcConnection nativeconnection, string catalog, string dbSchema, string tableName, CArrowSchema* nativeSchema)
{
byte* bCatalog, bDb_schema, bTable_name;

using (Utf8Helper catalogHelper = new Utf8Helper(catalog))
using (Utf8Helper schemaHelper = new Utf8Helper(dbSchema))
using (Utf8Helper tableNameHelper = new Utf8Helper(tableName))
{
bCatalog = (byte*)(IntPtr)(catalogHelper);
bDb_schema = (byte*)(IntPtr)(schemaHelper);
bTable_name = (byte*)(IntPtr)(tableNameHelper);

fixed (CAdbcConnection* connection = &nativeconnection)
fixed (CAdbcError* e = &_error)
{
TranslateCode(fn(connection, bCatalog, bDb_schema, bTable_name, nativeSchema, e));
}
}
}
#else
public unsafe void Call(IntPtr fn, ref CAdbcConnection nativeconnection, string catalog, string dbSchema, string tableName, CArrowSchema* nativeSchema)
{
byte* bCatalog, bDb_schema, bTable_name;

using (Utf8Helper catalogHelper = new Utf8Helper(catalog))
using (Utf8Helper schemaHelper = new Utf8Helper(dbSchema))
using (Utf8Helper tableNameHelper = new Utf8Helper(tableName))
{
bCatalog = (byte*)(IntPtr)(catalogHelper);
bDb_schema = (byte*)(IntPtr)(schemaHelper);
bTable_name = (byte*)(IntPtr)(tableNameHelper);

fixed (CAdbcConnection* connection = &nativeconnection)
fixed (CAdbcError* e = &_error)
{
TranslateCode(Marshal.GetDelegateForFunctionPointer<CAdbcDriverExporter.ConnectionGetTableSchema>(fn)(connection, bCatalog, bDb_schema, bTable_name, nativeSchema, e));
}
}
}
#endif

#if NET5_0_OR_GREATER
public unsafe void Call(delegate* unmanaged<CAdbcConnection*, CArrowArrayStream*, CAdbcError*, AdbcStatusCode> fn, ref CAdbcConnection nativeconnection, CArrowArrayStream* arrowStream)
{
fixed (CAdbcConnection* connection = &nativeconnection)
fixed (CAdbcError* e = &_error)
{
TranslateCode(fn(connection, arrowStream, e));
}
}
#else
public unsafe void Call(IntPtr fn, ref CAdbcConnection nativeconnection, CArrowArrayStream* arrowStream)
{
fixed (CAdbcConnection* connection = &nativeconnection)
fixed (CAdbcError* e = &_error)
{
TranslateCode(Marshal.GetDelegateForFunctionPointer<CAdbcDriverExporter.ConnectionGetTableTypes>(fn)(connection, arrowStream, e));
}
}
#endif

public unsafe void Dispose()
{
if (_error.release != default)
Expand Down Expand Up @@ -636,7 +726,7 @@ public unsafe void Call(IntPtr fn, ref CAdbcConnection connection, int depth, st
{
byte* bcatalog, bDb_schema, bTable_name, bColumn_Name;

if(table_types == null)
if (table_types == null)
{
table_types = new List<string>();
}
Expand All @@ -656,34 +746,25 @@ public unsafe void Call(IntPtr fn, ref CAdbcConnection connection, int depth, st
#endif
}

using (Utf8Helper helper = new Utf8Helper(catalog))
{
bcatalog = (byte*)(IntPtr)(helper);
}

using (Utf8Helper helper = new Utf8Helper(db_schema))
using (Utf8Helper catalogHelper = new Utf8Helper(catalog))
using (Utf8Helper schemaHelper = new Utf8Helper(db_schema))
using (Utf8Helper tableNameHelper = new Utf8Helper(table_name))
using (Utf8Helper columnNameHelper = new Utf8Helper(column_name))
{
bDb_schema = (byte*)(IntPtr)(helper);
}

using (Utf8Helper helper = new Utf8Helper(table_name))
{
bTable_name = (byte*)(IntPtr)(helper);
}
bcatalog = (byte*)(IntPtr)(catalogHelper);
bDb_schema = (byte*)(IntPtr)(schemaHelper);
bTable_name = (byte*)(IntPtr)(tableNameHelper);
bColumn_Name = (byte*)(IntPtr)(columnNameHelper);

using (Utf8Helper helper = new Utf8Helper(column_name))
{
bColumn_Name = (byte*)(IntPtr)(helper);
}

fixed (CAdbcConnection* cn = &connection)
fixed (CAdbcError* e = &_error)
{
fixed (CAdbcConnection* cn = &connection)
fixed (CAdbcError* e = &_error)
{
#if NET5_0_OR_GREATER
TranslateCode(fn(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e));
TranslateCode(fn(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e));
#else
TranslateCode(Marshal.GetDelegateForFunctionPointer<CAdbcDriverExporter.ConnectionGetObjects>(fn)(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e));
TranslateCode(Marshal.GetDelegateForFunctionPointer<CAdbcDriverExporter.ConnectionGetObjects>(fn)(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e));
#endif
}
}
}

Expand Down

0 comments on commit 2de52f3

Please sign in to comment.