diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcException.cs b/csharp/src/Apache.Arrow.Adbc/AdbcException.cs index 8c1e6ed673..288d9df855 100644 --- a/csharp/src/Apache.Arrow.Adbc/AdbcException.cs +++ b/csharp/src/Apache.Arrow.Adbc/AdbcException.cs @@ -16,73 +16,75 @@ */ using System; -using Apache.Arrow.Adbc; -/// -/// The root exception when working with Adbc drivers. -/// -public class AdbcException : Exception +namespace Apache.Arrow.Adbc { - private AdbcStatusCode _statusCode = AdbcStatusCode.UnknownError; - - public AdbcException() + /// + /// The root exception when working with Adbc drivers. + /// + 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) + { + } - /// - /// 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. - /// - public virtual string SqlState - { - get => null; - } + public static AdbcException NotImplemented(string message) + { + return new AdbcException(message, AdbcStatusCode.NotImplemented); + } - /// - /// Gets or sets the for the error. - /// - public AdbcStatusCode Status - { - get => _statusCode; - } + /// + /// 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. + /// + public virtual string SqlState + { + get => null; + } - /// - /// Gets a native error number. - /// - public virtual int NativeError - { - get => 0; + /// + /// Gets or sets the for the error. + /// + public AdbcStatusCode Status + { + get => _statusCode; + } + + /// + /// Gets a native error number. + /// + public virtual int NativeError + { + get => 0; + } } } diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs index eb0c5e2520..33d80f0f0d 100644 --- a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs +++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs @@ -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 s_connectionGetObjects = new NativeDelegate(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 s_connectionGetTableSchema = new NativeDelegate(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 s_connectionGetTableTypes = new NativeDelegate(GetConnectionTableTypes); private static IntPtr ConnectionGetTableTypesPtr => s_connectionGetTableTypes.Pointer; internal unsafe delegate AdbcStatusCode ConnectionInit(CAdbcConnection* connection, CAdbcDatabase* database, CAdbcError* error); diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs index 108087645b..13449a3ce9 100644 --- a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs +++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs @@ -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; + } } /// @@ -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 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(fn)(connection, bCatalog, bDb_schema, bTable_name, nativeSchema, e)); + } + } + } +#endif + +#if NET5_0_OR_GREATER + public unsafe void Call(delegate* unmanaged 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(fn)(connection, arrowStream, e)); + } + } +#endif + public unsafe void Dispose() { if (_error.release != default) @@ -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(); } @@ -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(fn)(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e)); + TranslateCode(Marshal.GetDelegateForFunctionPointer(fn)(cn, depth, bcatalog, bDb_schema, bTable_name, bTable_type, bColumn_Name, stream, e)); #endif + } } }