You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "lidavidm (via GitHub)" <gi...@apache.org> on 2023/06/13 12:07:56 UTC

[GitHub] [arrow-adbc] lidavidm commented on a diff in pull request #749: refactor(csharp): Cleanup C API

lidavidm commented on code in PR #749:
URL: https://github.com/apache/arrow-adbc/pull/749#discussion_r1228021142


##########
csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs:
##########
@@ -1,492 +1,485 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-using System;
-using System.Buffers;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-using System.Threading;
-using System.Threading.Tasks;
-using System.Xml.Linq;
-using Apache.Arrow.Adbc;
-using Apache.Arrow.C;
-using Apache.Arrow.Ipc;
-using Apache.Arrow.Types;
-using Microsoft.Win32.SafeHandles;
-
-#if NETSTANDARD
-using Apache.Arrow.Adbc.Extensions;
-#endif
-
-namespace Apache.Arrow.Adbc.Interop
-{
-    public delegate byte AdbcDriverInit(int version, ref NativeAdbcDriver driver, ref NativeAdbcError error);
-
-    /// <summary>
-    /// Class for working with loading drivers from files
-    /// </summary>
-    public static class LoadDriver
-    {
-        private const string driverInit = "AdbcDriverInit";
-
-        class NativeDriver
-        {
-            public SafeHandle driverHandle;
-            public NativeAdbcDriver driver;
-        }
-
-        /// <summary>
-        /// Class used for Mac interoperability
-        /// </summary>
-        static class MacInterop
-        {
-            const string libdl = "libdl.dylib";
-
-            [DllImport(libdl)]
-            static extern SafeLibraryHandle dlopen(string fileName, int flags);
-
-            [DllImport(libdl)]
-            static extern IntPtr dlsym(SafeHandle libraryHandle, string symbol);
-
-            [DllImport(libdl)]
-            static extern int dlclose(IntPtr handle);
-
-            sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
-            {
-                SafeLibraryHandle() : base(true) { }
-
-                protected override bool ReleaseHandle()
-                {
-                    return dlclose(handle) == 0;
-                }
-            }
-
-            public static NativeDriver GetDriver(string file)
-            {
-                SafeHandle library = dlopen(file, 2); // TODO: find a symbol for 2
-                IntPtr symbol = dlsym(library, "AdbcDriverInit");
-                AdbcDriverInit init = Marshal.GetDelegateForFunctionPointer<AdbcDriverInit>(symbol);
-                NativeAdbcDriver driver = new NativeAdbcDriver();
-                NativeAdbcError error = new NativeAdbcError();
-                byte result = init(1000000, ref driver, ref error);
-                return new NativeDriver { driverHandle = library, driver = driver };
-            }
-        }
-
-        /// <summary>
-        /// Class used for Windows interoperability
-        /// </summary>
-        static class WindowsInterop
-        {
-            const string kernel32 = "kernel32.dll";
-
-            [DllImport(kernel32)]
-            [return: MarshalAs(UnmanagedType.Bool)]
-            static extern bool FreeLibrary(IntPtr libraryHandle);
-
-            [DllImport(kernel32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
-            static extern IntPtr GetProcAddress(SafeHandle libraryHandle, string functionName);
-
-            [DllImport(kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
-            static extern SafeLibraryHandle LoadLibraryEx(string fileName, IntPtr hFile, uint flags);
-
-            sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
-            {
-                SafeLibraryHandle() : base(true) { }
-
-                protected override bool ReleaseHandle()
-                {
-                    return FreeLibrary(handle);
-                }
-            }
-
-            public static NativeDriver GetDriver(string file)
-            {
-                SafeHandle library = LoadLibraryEx(file, IntPtr.Zero, 0x1100);
-                IntPtr symbol = GetProcAddress(library, "AdbcDriverInit");
-                AdbcDriverInit init = Marshal.GetDelegateForFunctionPointer<AdbcDriverInit>(symbol);
-                NativeAdbcDriver driver = new NativeAdbcDriver();
-                NativeAdbcError error = new NativeAdbcError();
-                byte result = init(1000000, ref driver, ref error);
-                return new NativeDriver { /* driverHandle = library, */ driver = driver };
-            }
-        }
-
-        /// <summary>
-        /// Loads an <see cref="AdbcDriver"/> from the file system.
-        /// </summary>
-        /// <param name="file">
-        /// The path to the file.
-        /// </param>
-        public static AdbcDriver Load(string file)
-        {
-            if (file[0] == '/')
-            {
-                return new AdbcDriverNative(MacInterop.GetDriver(file).driver);
-            }
-            else
-            {
-                return new AdbcDriverNative(WindowsInterop.GetDriver(file).driver);
-            }
-        }
-
-        /// <summary>
-        /// Native implementation of <see cref="AdbcDriver"/>
-        /// </summary>
-        sealed class AdbcDriverNative : AdbcDriver
-        {
-            private NativeAdbcDriver _nativeDriver;
-
-            public AdbcDriverNative(NativeAdbcDriver nativeDriver)
-            {
-                _nativeDriver = nativeDriver;
-            }
-
-            /// <summary>
-            /// Opens a database
-            /// </summary>
-            /// <param name="parameters">
-            /// Parameters to use when calling DatabaseNew.
-            /// </param>
-            public unsafe override AdbcDatabase Open(IReadOnlyDictionary<string, string> parameters)
-            {
-                NativeAdbcDatabase nativeDatabase = new NativeAdbcDatabase();
-
-                using (CallHelper caller = new CallHelper())
-                {
-                    caller.Call(_nativeDriver.DatabaseNew, ref nativeDatabase);
-
-                    if (parameters != null)
-                    {
-                        foreach (KeyValuePair<string, string> pair in parameters)
-                        {
-                            caller.Call(_nativeDriver.DatabaseSetOption, ref nativeDatabase, pair.Key, pair.Value);
-                        }
-                    }
-
-                    caller.Call(_nativeDriver.DatabaseInit, ref nativeDatabase);
-                }
-
-                return new AdbcDatabaseNative(_nativeDriver, nativeDatabase);
-            }
-
-            public unsafe override void Dispose()
-            {
-                if (_nativeDriver.release != null)
-                {
-                    using (CallHelper caller = new CallHelper())
-                    {
-                        try
-                        {
-                            caller.Call(_nativeDriver.release, ref _nativeDriver);
-                        }
-                        finally
-                        {
-                            _nativeDriver.release = null;
-                        }
-                    }
-                    base.Dispose();
-                }
-            }
-        }
-
-        /// <summary>
-        /// A native implementation of <see cref="AdbcDatabase"/>
-        /// </summary>
-        internal sealed class AdbcDatabaseNative : AdbcDatabase
-        {
-            private NativeAdbcDriver _nativeDriver;
-            private NativeAdbcDatabase _nativeDatabase;
-
-            public AdbcDatabaseNative(NativeAdbcDriver nativeDriver, NativeAdbcDatabase nativeDatabase)
-            {
-                _nativeDriver = nativeDriver;
-                _nativeDatabase = nativeDatabase;
-            }
-
-            public unsafe override AdbcConnection Connect(IReadOnlyDictionary<string, string> options)
-            {
-                NativeAdbcConnection nativeConnection = new NativeAdbcConnection();
-
-                using (CallHelper caller = new CallHelper())
-                {
-                    if (options != null)
-                    {
-                        foreach (KeyValuePair<string, string> pair in options)
-                        {
-                            caller.Call(_nativeDriver.ConnectionSetOption, ref nativeConnection, pair.Key, pair.Value);
-                        }
-                    }
-
-                    caller.Call(_nativeDriver.ConnectionInit, ref nativeConnection, ref _nativeDatabase);
-                }
-
-                return new AdbcConnectionNative(_nativeDriver, nativeConnection);
-            }
-
-            public override void Dispose()
-            {
-                base.Dispose();
-            }
-        }
-
-        /// <summary>
-        /// A native implementation of <see cref="AdbcConnection"/>
-        /// </summary>
-        internal sealed class AdbcConnectionNative : AdbcConnection
-        {
-            private NativeAdbcDriver _nativeDriver;
-            private NativeAdbcConnection _nativeConnection;
-
-            public AdbcConnectionNative(NativeAdbcDriver nativeDriver, NativeAdbcConnection nativeConnection)
-            {
-                _nativeDriver = nativeDriver;
-                _nativeConnection = nativeConnection;
-            }
-
-            public unsafe override AdbcStatement CreateStatement()
-            {
-                NativeAdbcStatement nativeStatement = new NativeAdbcStatement();
-
-                using (CallHelper caller = new CallHelper())
-                {
-                    caller.Call(_nativeDriver.StatementNew, ref _nativeConnection, ref nativeStatement);
-                }
-
-                return new AdbcStatementNative(_nativeDriver, nativeStatement);
-            }
-
-        }
-
-        /// <summary>
-        /// A native implementation of <see cref="AdbcStatement"/>
-        /// </summary>
-        sealed class AdbcStatementNative : AdbcStatement
-        {
-            private NativeAdbcDriver _nativeDriver;
-            private NativeAdbcStatement _nativeStatement;
-
-            public AdbcStatementNative(NativeAdbcDriver nativeDriver, NativeAdbcStatement nativeStatement)
-            {
-                _nativeDriver = nativeDriver;
-                _nativeStatement = nativeStatement;
-            }
-
-            public unsafe override QueryResult ExecuteQuery()
-            {
-                CArrowArrayStream* nativeArrayStream = CArrowArrayStream.Create();
-
-                using (CallHelper caller = new CallHelper())
-                {
-                    caller.Call(_nativeDriver.StatementSetSqlQuery, ref _nativeStatement, SqlQuery);
-
-                    long rows = 0;
-
-                    caller.Call(_nativeDriver.StatementExecuteQuery, ref _nativeStatement, nativeArrayStream, ref rows);
-
-                    return new QueryResult(rows, CArrowArrayStreamImporter.ImportArrayStream(nativeArrayStream));
-                }
-            }
-
-            public override unsafe UpdateResult ExecuteUpdate()
-            {
-                throw AdbcException.NotImplemented("Driver does not support ExecuteUpdate");
-            }
-
-            public override object GetValue(IArrowArray arrowArray, Field field, int index)
-            {
-                throw new NotImplementedException();
-            }
-        }
-
-        /// <summary>
-        /// Assists with UTF8/string marshalling
-        /// </summary>
-        struct Utf8Helper : IDisposable
-        {
-            private IntPtr _s;
-
-            public Utf8Helper(string s)
-            {
-#if NETSTANDARD
-                    _s = MarshalExtensions.StringToCoTaskMemUTF8(s);
-#else
-                _s = Marshal.StringToCoTaskMemUTF8(s);
-#endif
-            }
-
-            public static implicit operator IntPtr(Utf8Helper s) { return s._s; }
-            public void Dispose() { Marshal.FreeCoTaskMem(_s); }
-        }
-
-        /// <summary>
-        /// Assists with delegate calls and handling error codes
-        /// </summary>
-        struct CallHelper : IDisposable
-        {
-            private NativeAdbcError _error;
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcDriver*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcDriver nativeDriver)
-            {
-                fixed (NativeAdbcDriver* driver = &nativeDriver)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(driver, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcDatabase nativeDatabase)
-            {
-                fixed (NativeAdbcDatabase* db = &nativeDatabase)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(db, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, byte*, byte*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcDatabase nativeDatabase, string key, string value)
-            {
-                fixed (NativeAdbcDatabase* db = &nativeDatabase)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    using (Utf8Helper utf8Key = new Utf8Helper(key))
-                    using (Utf8Helper utf8Value = new Utf8Helper(value))
-                    {
-                        unsafe
-                        {
-                            IntPtr keyPtr = utf8Key;
-                            IntPtr valuePtr = utf8Value;
-
-                            TranslateCode(fn(db, (byte*)keyPtr, (byte*)valuePtr, e));
-                        }
-                    }
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcConnection nativeConnection)
-            {
-                fixed (NativeAdbcConnection* cn = &nativeConnection)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(cn, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcConnection*, byte*, byte*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcConnection nativeConnection, string key, string value)
-            {
-                fixed (NativeAdbcConnection* cn = &nativeConnection)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    using (Utf8Helper utf8Key = new Utf8Helper(key))
-                    using (Utf8Helper utf8Value = new Utf8Helper(value))
-                    {
-                        unsafe
-                        {
-                            IntPtr keyPtr = utf8Key;
-                            IntPtr valuePtr = utf8Value;
-
-                            TranslateCode(fn(cn, (byte*)keyPtr, (byte*)valuePtr, e));
-                        }
-                    }
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcConnection nativeConnection, ref NativeAdbcDatabase database)
-            {
-                fixed (NativeAdbcConnection* cn = &nativeConnection)
-                fixed (NativeAdbcDatabase* db = &database)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(cn, db, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcStatement*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcConnection nativeConnection, ref NativeAdbcStatement nativeStatement)
-            {
-                fixed (NativeAdbcConnection* cn = &nativeConnection)
-                fixed (NativeAdbcStatement* stmt = &nativeStatement)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(cn, stmt, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcStatement*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcStatement nativeStatement)
-            {
-                fixed (NativeAdbcStatement* stmt = &nativeStatement)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(stmt, e));
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcStatement*, byte*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcStatement nativeStatement, string sqlQuery)
-            {
-                fixed (NativeAdbcStatement* stmt = &nativeStatement)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    using (Utf8Helper query = new Utf8Helper(sqlQuery))
-                    {
-                        IntPtr bQuery = (IntPtr)(query);
-
-                        TranslateCode(fn(stmt, (byte*)bQuery, e));
-                    }
-                }
-            }
-
-            public unsafe void Call(delegate* unmanaged[Stdcall]<NativeAdbcStatement*, CArrowArrayStream*, long*, NativeAdbcError*, AdbcStatusCode> fn, ref NativeAdbcStatement nativeStatement, CArrowArrayStream* arrowStream, ref long nRows)
-            {
-                fixed (NativeAdbcStatement* stmt = &nativeStatement)
-                fixed (long* rows = &nRows)
-                fixed (NativeAdbcError* e = &_error)
-                {
-                    TranslateCode(fn(stmt, arrowStream, rows, e));
-                }
-            }
-
-            public unsafe void Dispose()
-            {
-                if (_error.release != null)
-                {
-                    fixed (NativeAdbcError* err = &_error)
-                    {
-                        _error.release(err);
-                        _error.release = null;
-                    }
-                }
-            }
-
-            internal unsafe void TranslateCode(AdbcStatusCode statusCode)
-            {
-                if (statusCode != AdbcStatusCode.Success)
-                {
-                    string message = "Undefined error";
-                    if ((IntPtr)_error.message != IntPtr.Zero)
-                    {
-#if NETSTANDARD
-                            message = MarshalExtensions.PtrToStringUTF8((IntPtr)_error.message);
-#else
-                        message = Marshal.PtrToStringUTF8((IntPtr)_error.message);
-#endif
-                    }
-                    Dispose();
-                    throw new AdbcException(message);
-                }
-            }
-        }
-    }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using Apache.Arrow.C;
+using Microsoft.Win32.SafeHandles;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.C
+{
+    internal delegate byte AdbcDriverInit(int version, ref CAdbcDriver driver, ref CAdbcError error);
+
+    /// <summary>
+    /// Class for working with loading drivers from files
+    /// </summary>
+    public static class CAdbcDriverImporter
+    {
+        private const string driverInit = "AdbcDriverInit";
+
+        class NativeDriver
+        {
+            public SafeHandle driverHandle;
+            public CAdbcDriver driver;
+        }
+
+        /// <summary>
+        /// Class used for Mac interoperability
+        /// </summary>
+        static class MacInterop
+        {
+            const string libdl = "libdl.dylib";
+
+            [DllImport(libdl)]
+            static extern SafeLibraryHandle dlopen(string fileName, int flags);
+
+            [DllImport(libdl)]
+            static extern IntPtr dlsym(SafeHandle libraryHandle, string symbol);
+
+            [DllImport(libdl)]
+            static extern int dlclose(IntPtr handle);
+
+            sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
+            {
+                SafeLibraryHandle() : base(true) { }
+
+                protected override bool ReleaseHandle()
+                {
+                    return dlclose(handle) == 0;
+                }
+            }
+
+            public static NativeDriver GetDriver(string file)
+            {
+                SafeHandle library = dlopen(file, 2); // TODO: find a symbol for 2
+                IntPtr symbol = dlsym(library, "AdbcDriverInit");
+                AdbcDriverInit init = Marshal.GetDelegateForFunctionPointer<AdbcDriverInit>(symbol);
+                CAdbcDriver driver = new CAdbcDriver();
+                CAdbcError error = new CAdbcError();
+                byte result = init(1000000, ref driver, ref error);

Review Comment:
   It might help to just declare the 2 and the 1_000_000 as constants here?



##########
csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs:
##########
@@ -0,0 +1,682 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Buffers;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using Apache.Arrow.C;
+using Apache.Arrow.Ipc;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.C
+{
+    public class CAdbcDriverExporter
+    {
+        private unsafe static readonly NativeDelegate<ErrorRelease> releaseError = new NativeDelegate<ErrorRelease>(ReleaseError);
+        private unsafe static readonly NativeDelegate<DriverRelease> releaseDriver = new NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private unsafe static readonly NativeDelegate<DatabaseFn> databaseInit = new NativeDelegate<DatabaseFn>(InitDatabase);
+        private unsafe static readonly NativeDelegate<DatabaseFn> databaseRelease = new NativeDelegate<DatabaseFn>(ReleaseDatabase);
+        private unsafe static readonly NativeDelegate<DatabaseSetOption> databaseSetOption = new NativeDelegate<DatabaseSetOption>(SetDatabaseOption);
+
+        private unsafe static readonly NativeDelegate<ConnectionGetObjects> connectionGetObjects = new NativeDelegate<ConnectionGetObjects>(GetConnectionObjects);
+        private unsafe static readonly NativeDelegate<ConnectionGetTableSchema> connectionGetTableSchema = new NativeDelegate<ConnectionGetTableSchema>(GetConnectionTableSchema);
+        private unsafe static readonly NativeDelegate<ConnectionGetTableTypes> connectionGetTableTypes = new NativeDelegate<ConnectionGetTableTypes>(GetConnectionTableTypes);
+        private unsafe static readonly NativeDelegate<ConnectionInit> connectionInit = new NativeDelegate<ConnectionInit>(InitConnection);
+        private unsafe static readonly NativeDelegate<ConnectionFn> connectionRelease = new NativeDelegate<ConnectionFn>(ReleaseConnection);
+        private unsafe static readonly NativeDelegate<ConnectionGetInfo> connectionGetInfo = new NativeDelegate<ConnectionGetInfo>(GetConnectionInfo);
+        private unsafe static readonly NativeDelegate<ConnectionReadPartition> connectionReadPartition = new NativeDelegate<ConnectionReadPartition>(ReadConnectionPartition);
+        private unsafe static readonly NativeDelegate<ConnectionSetOption> connectionSetOption = new NativeDelegate<ConnectionSetOption>(SetConnectionOption);
+
+        private unsafe static readonly NativeDelegate<StatementBind> statementBind = new NativeDelegate<StatementBind>(BindStatement);
+        private unsafe static readonly NativeDelegate<StatementExecuteQuery> statementExecuteQuery = new NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);
+        private unsafe static readonly NativeDelegate<StatementNew> statementNew = new NativeDelegate<StatementNew>(NewStatement);
+        private unsafe static readonly NativeDelegate<StatementFn> statementRelease = new NativeDelegate<StatementFn>(ReleaseStatement);
+        private unsafe static readonly NativeDelegate<StatementSetSqlQuery> statementSetSqlQuery = new NativeDelegate<StatementSetSqlQuery>(SetStatementSqlQuery);
+
+        public unsafe static AdbcStatusCode AdbcDriverInit(int version, CAdbcDriver* nativeDriver, CAdbcError* error, AdbcDriver driver)
+        {
+            DriverStub stub = new DriverStub(driver);
+            GCHandle handle = GCHandle.Alloc(stub);
+            nativeDriver->private_data = (void*)GCHandle.ToIntPtr(handle);
+            nativeDriver->release = (delegate* unmanaged[Stdcall]<CAdbcDriver*, CAdbcError*, AdbcStatusCode>)releaseDriver.Pointer;
+
+            nativeDriver->DatabaseInit = (delegate* unmanaged[Stdcall]<CAdbcDatabase*, CAdbcError*, AdbcStatusCode>)databaseInit.Pointer;
+            nativeDriver->DatabaseNew = (delegate* unmanaged[Stdcall]<CAdbcDatabase*, CAdbcError*, AdbcStatusCode>)stub.newDatabase.Pointer;
+            nativeDriver->DatabaseSetOption = (delegate* unmanaged[Stdcall]<CAdbcDatabase*, byte*, byte*, CAdbcError*, AdbcStatusCode>)databaseSetOption.Pointer;
+            nativeDriver->DatabaseRelease = (delegate* unmanaged[Stdcall]<CAdbcDatabase*, CAdbcError*, AdbcStatusCode>)databaseRelease.Pointer;
+
+            nativeDriver->ConnectionCommit = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcError*, AdbcStatusCode>)connectionRelease.Pointer;
+            nativeDriver->ConnectionGetInfo = (delegate* unmanaged[Stdcall]<CAdbcConnection*, byte*, int, CArrowArrayStream*, CAdbcError*, AdbcStatusCode>)connectionGetInfo.Pointer;
+            nativeDriver->ConnectionGetObjects = (delegate* unmanaged[Stdcall]<CAdbcConnection*, int, byte*, byte*, byte*, byte**, byte*, CArrowArrayStream*, CAdbcError*, AdbcStatusCode>)connectionGetObjects.Pointer;
+            nativeDriver->ConnectionGetTableSchema = (delegate* unmanaged[Stdcall]<CAdbcConnection*, byte*, byte*, byte*, CArrowSchema*, CAdbcError*, AdbcStatusCode>)connectionGetTableSchema.Pointer;
+            nativeDriver->ConnectionGetTableTypes = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CArrowArrayStream*, CAdbcError*, AdbcStatusCode>)connectionGetTableTypes.Pointer;
+            nativeDriver->ConnectionInit = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcDatabase*, CAdbcError*, AdbcStatusCode>)connectionInit.Pointer;
+            nativeDriver->ConnectionNew = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcError*, AdbcStatusCode>)stub.newConnection.Pointer;
+            nativeDriver->ConnectionSetOption = (delegate* unmanaged[Stdcall]<CAdbcConnection*, byte*, byte*, CAdbcError*, AdbcStatusCode>)connectionSetOption.Pointer;
+            nativeDriver->ConnectionReadPartition = (delegate* unmanaged[Stdcall]<CAdbcConnection*, byte*, int, CArrowArrayStream*, CAdbcError*, AdbcStatusCode>)connectionReadPartition.Pointer;
+            nativeDriver->ConnectionRelease = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcError*, AdbcStatusCode>)connectionRelease.Pointer;
+            nativeDriver->ConnectionRollback = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcError*, AdbcStatusCode>)connectionRelease.Pointer;
+
+            nativeDriver->StatementBind = (delegate* unmanaged[Stdcall]<CAdbcStatement*, CArrowArray*, CArrowSchema*, CAdbcError*, AdbcStatusCode>)statementBind.Pointer;
+            nativeDriver->StatementNew = (delegate* unmanaged[Stdcall]<CAdbcConnection*, CAdbcStatement*, CAdbcError*, AdbcStatusCode>)statementNew.Pointer;
+            nativeDriver->StatementSetSqlQuery = (delegate* unmanaged[Stdcall]<CAdbcStatement*, byte*, CAdbcError*, AdbcStatusCode>)statementSetSqlQuery.Pointer;
+            nativeDriver->StatementExecuteQuery = (delegate* unmanaged[Stdcall]<CAdbcStatement*, CArrowArrayStream*, long*, CAdbcError*, AdbcStatusCode>)statementExecuteQuery.Pointer;
+            nativeDriver->StatementPrepare = (delegate* unmanaged[Stdcall]<CAdbcStatement*, CAdbcError*, AdbcStatusCode>)statementRelease.Pointer;
+            nativeDriver->StatementRelease = (delegate* unmanaged[Stdcall]<CAdbcStatement*, CAdbcError*, AdbcStatusCode>)statementRelease.Pointer;
+
+            return 0;
+        }
+
+        private unsafe static void ReleaseError(CAdbcError* error)
+        {
+            if (error != null && ((IntPtr)error->message) != IntPtr.Zero)
+            {
+                Marshal.FreeHGlobal((IntPtr)error->message);
+            }
+        }
+
+        private unsafe static AdbcStatusCode SetError(CAdbcError* error, Exception exception)
+        {
+            ReleaseError(error);
+
+#if NETSTANDARD
+                error->message = (byte*)MarshalExtensions.StringToCoTaskMemUTF8(exception.Message);
+#else
+            error->message = (byte*)Marshal.StringToCoTaskMemUTF8(exception.Message);
+#endif
+
+            error->sqlstate0 = (byte)0;
+            error->sqlstate1 = (byte)0;
+            error->sqlstate2 = (byte)0;
+            error->sqlstate3 = (byte)0;
+            error->sqlstate4 = (byte)0;
+            error->vendor_code = 0;
+            error->vendor_code = 0;

Review Comment:
   nit: copy-paste oops?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org