eerhardt commented on code in PR #697:
URL: https://github.com/apache/arrow-adbc/pull/697#discussion_r1205765476


##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);

Review Comment:
   ```suggestion
           private static NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
   ```
   
   why is `unsafe` needed here?



##########
csharp/Directory.Build.targets:
##########
@@ -0,0 +1,29 @@
+<!--
+  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.
+-->
+
+<Project>
+
+  <!-- The following works around 
https://github.com/dotnet/sourcelink/issues/572  -->

Review Comment:
   Let's leave this out until we need it. This issue should be fixed in recent 
versions of the SDK. Can you delete this file?



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop

Review Comment:
   Does this _need_ to be public?



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
+        private static NativeDelegate<DriverRelease> releaseDriver = new 
NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private static NativeDelegate<DatabaseFn> databaseInit = new 
NativeDelegate<DatabaseFn>(InitDatabase);
+        private static NativeDelegate<DatabaseFn> databaseRelease = new 
NativeDelegate<DatabaseFn>(ReleaseDatabase);
+        private static NativeDelegate<DatabaseSetOption> databaseSetOption = 
new NativeDelegate<DatabaseSetOption>(SetDatabaseOption);
+        private static NativeDelegate<ConnectionInit> connectionInit = new 
NativeDelegate<ConnectionInit>(InitConnection);
+        private static NativeDelegate<ConnectionFn> connectionRelease = new 
NativeDelegate<ConnectionFn>(ReleaseConnection);
+        private static NativeDelegate<ConnectionSetOption> connectionSetOption 
= new NativeDelegate<ConnectionSetOption>(SetConnectionOption);
+        private static unsafe NativeDelegate<StatementExecuteQuery> 
statementExecuteQuery = new 
NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);
+        private static NativeDelegate<StatementNew> statementNew = new 
NativeDelegate<StatementNew>(NewStatement);
+        private static NativeDelegate<StatementFn> statementRelease = new 
NativeDelegate<StatementFn>(ReleaseStatement);
+        private static NativeDelegate<StatementSetSqlQuery> 
statementSetSqlQuery = new 
NativeDelegate<StatementSetSqlQuery>(SetStatementSqlQuery);
+
+        public unsafe static AdbcStatusCode AdbcDriverInit(int version, 
NativeAdbcDriver* nativeDriver, NativeAdbcError* error, AdbcDriver driver)
+        {
+            DriverStub stub = new DriverStub(driver);
+            GCHandle handle = GCHandle.Alloc(stub);
+            (*nativeDriver).private_data = GCHandle.ToIntPtr(handle);
+            (*nativeDriver).release = releaseDriver;
+            (*nativeDriver).DatabaseNew = stub.newDatabase;
+            (*nativeDriver).DatabaseInit = databaseInit;
+            (*nativeDriver).DatabaseRelease = databaseRelease;
+            (*nativeDriver).DatabaseSetOption = databaseSetOption;
+            (*nativeDriver).ConnectionNew = stub.newConnection;
+            (*nativeDriver).ConnectionInit = connectionInit;
+            (*nativeDriver).ConnectionRelease = connectionRelease;
+            (*nativeDriver).ConnectionSetOption = connectionSetOption;
+            (*nativeDriver).StatementNew = statementNew;
+            (*nativeDriver).StatementSetSqlQuery = statementSetSqlQuery;
+            (*nativeDriver).StatementExecuteQuery = statementExecuteQuery;
+            (*nativeDriver).StatementRelease = statementRelease;
+            return 0;
+        }
+
+        private unsafe static void ReleaseError(NativeAdbcError* error)
+        {
+            if (error != null && (*error).message != IntPtr.Zero)
+            {
+                Marshal.FreeCoTaskMem((*error).message);
+            }
+        }
+
+        private unsafe static AdbcStatusCode SetError(NativeAdbcError* error, 
Exception exception)
+        {
+            ReleaseError(error);
+
+#if NETSTANDARD
+            error->message = 
MarshalExtensions.StringToCoTaskMemUTF8(exception.Message);
+#else
+            error->message = Marshal.StringToCoTaskMemUTF8(exception.Message);
+#endif
+
+            error->sqlstate0 = (char)0;
+            error->sqlstate1 = (char)0;
+            error->sqlstate2 = (char)0;
+            error->sqlstate3 = (char)0;
+            error->sqlstate4 = (char)0;
+            error->vendor_code = 0;
+            error->vendor_code = 0;
+            error->release = (delegate* unmanaged[Stdcall]<NativeAdbcError*, 
void>)(IntPtr)releaseError.Pointer;
+            
+            return AdbcStatusCode.UnknownError;
+        }
+
+        private sealed class PinnedArray : IDisposable
+        {
+            IArrowArray _array;
+            MemoryHandle[] pinnedHandles;
+
+            public PinnedArray(IArrowArray array)
+            {
+                _array = array;
+                pinnedHandles = new MemoryHandle[GetHandleCount(array.Data)];
+                int index = 0;
+                PinBuffers(array.Data, pinnedHandles, ref index);
+                Debug.Assert(index == pinnedHandles.Length);
+            }
+
+            public void Dispose()
+            {
+                if (_array != null)
+                {
+                    _array.Dispose();
+                    foreach (MemoryHandle handle in pinnedHandles)
+                    {
+                        handle.Dispose();
+                    }
+                    _array = null;
+                }
+            }
+
+            static int GetHandleCount(ArrayData data)
+            {
+                int handleCount = data.Buffers.Length;
+                foreach (ArrayData child in data.Children)
+                {
+                    handleCount += GetHandleCount(child);
+                }
+                if (data.Dictionary != null)
+                {
+                    handleCount += GetHandleCount(data.Dictionary);
+                }
+                return handleCount;
+            }
+
+            static void PinBuffers(ArrayData data, MemoryHandle[] handles, ref 
int index)
+            {
+                foreach (ArrowBuffer buffer in data.Buffers)
+                {
+                    handles[index++] = buffer.Memory.Pin();
+                }
+                foreach (ArrayData child in data.Children)
+                {
+                    PinBuffers(child, handles, ref index);
+                }
+                if (data.Dictionary != null)
+                {
+                    PinBuffers(data.Dictionary, handles, ref index);
+                }
+            }
+        }
+
+        private static IntPtr FromDisposable(IDisposable d)
+        {
+            GCHandle gch = GCHandle.Alloc(d);
+            return GCHandle.ToIntPtr(gch);
+        }
+
+        private static void Dispose(ref IntPtr p)
+        {
+            GCHandle gch = GCHandle.FromIntPtr(p);
+            ((IDisposable)gch.Target).Dispose();
+            gch.Free();
+            p = IntPtr.Zero;
+        }
+
+        private static AdbcStatusCode ReleaseDriver(ref NativeAdbcDriver 
nativeDriver, ref NativeAdbcError error)
+        {
+            GCHandle gch = GCHandle.FromIntPtr(nativeDriver.private_data);
+            DriverStub stub = (DriverStub)gch.Target;
+            stub.Dispose();
+            gch.Free();
+            nativeDriver.private_data = IntPtr.Zero;
+            return 0;
+        }
+
+        private unsafe static AdbcStatusCode InitDatabase(ref 
NativeAdbcDatabase nativeDatabase, ref NativeAdbcError error)
+        {
+            GCHandle gch = 
GCHandle.FromIntPtr((IntPtr)nativeDatabase.private_data);
+            DatabaseStub stub = (DatabaseStub)gch.Target;
+            return stub.Init(ref error);
+        }
+
+        private unsafe static AdbcStatusCode ReleaseDatabase(ref 
NativeAdbcDatabase nativeDatabase, ref NativeAdbcError error)
+        {
+            if ((IntPtr)nativeDatabase.private_data == IntPtr.Zero)

Review Comment:
   ```suggestion
               if (nativeDatabase.private_data == null)
   ```



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
+        private static NativeDelegate<DriverRelease> releaseDriver = new 
NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private static NativeDelegate<DatabaseFn> databaseInit = new 
NativeDelegate<DatabaseFn>(InitDatabase);
+        private static NativeDelegate<DatabaseFn> databaseRelease = new 
NativeDelegate<DatabaseFn>(ReleaseDatabase);
+        private static NativeDelegate<DatabaseSetOption> databaseSetOption = 
new NativeDelegate<DatabaseSetOption>(SetDatabaseOption);
+        private static NativeDelegate<ConnectionInit> connectionInit = new 
NativeDelegate<ConnectionInit>(InitConnection);
+        private static NativeDelegate<ConnectionFn> connectionRelease = new 
NativeDelegate<ConnectionFn>(ReleaseConnection);
+        private static NativeDelegate<ConnectionSetOption> connectionSetOption 
= new NativeDelegate<ConnectionSetOption>(SetConnectionOption);
+        private static unsafe NativeDelegate<StatementExecuteQuery> 
statementExecuteQuery = new 
NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);
+        private static NativeDelegate<StatementNew> statementNew = new 
NativeDelegate<StatementNew>(NewStatement);
+        private static NativeDelegate<StatementFn> statementRelease = new 
NativeDelegate<StatementFn>(ReleaseStatement);
+        private static NativeDelegate<StatementSetSqlQuery> 
statementSetSqlQuery = new 
NativeDelegate<StatementSetSqlQuery>(SetStatementSqlQuery);
+
+        public unsafe static AdbcStatusCode AdbcDriverInit(int version, 
NativeAdbcDriver* nativeDriver, NativeAdbcError* error, AdbcDriver driver)
+        {
+            DriverStub stub = new DriverStub(driver);
+            GCHandle handle = GCHandle.Alloc(stub);
+            (*nativeDriver).private_data = GCHandle.ToIntPtr(handle);
+            (*nativeDriver).release = releaseDriver;
+            (*nativeDriver).DatabaseNew = stub.newDatabase;
+            (*nativeDriver).DatabaseInit = databaseInit;
+            (*nativeDriver).DatabaseRelease = databaseRelease;
+            (*nativeDriver).DatabaseSetOption = databaseSetOption;
+            (*nativeDriver).ConnectionNew = stub.newConnection;
+            (*nativeDriver).ConnectionInit = connectionInit;
+            (*nativeDriver).ConnectionRelease = connectionRelease;
+            (*nativeDriver).ConnectionSetOption = connectionSetOption;
+            (*nativeDriver).StatementNew = statementNew;
+            (*nativeDriver).StatementSetSqlQuery = statementSetSqlQuery;
+            (*nativeDriver).StatementExecuteQuery = statementExecuteQuery;
+            (*nativeDriver).StatementRelease = statementRelease;
+            return 0;
+        }
+
+        private unsafe static void ReleaseError(NativeAdbcError* error)
+        {
+            if (error != null && (*error).message != IntPtr.Zero)
+            {
+                Marshal.FreeCoTaskMem((*error).message);
+            }
+        }
+
+        private unsafe static AdbcStatusCode SetError(NativeAdbcError* error, 
Exception exception)
+        {
+            ReleaseError(error);
+
+#if NETSTANDARD
+            error->message = 
MarshalExtensions.StringToCoTaskMemUTF8(exception.Message);
+#else
+            error->message = Marshal.StringToCoTaskMemUTF8(exception.Message);
+#endif
+
+            error->sqlstate0 = (char)0;
+            error->sqlstate1 = (char)0;
+            error->sqlstate2 = (char)0;
+            error->sqlstate3 = (char)0;
+            error->sqlstate4 = (char)0;
+            error->vendor_code = 0;
+            error->vendor_code = 0;
+            error->release = (delegate* unmanaged[Stdcall]<NativeAdbcError*, 
void>)(IntPtr)releaseError.Pointer;

Review Comment:
   ```suggestion
               error->release = (delegate* unmanaged[Stdcall]<NativeAdbcError*, 
void>)releaseError.Pointer;
   ```



##########
csharp/src/Apache.Arrow.Adbc/Apache.Arrow.Adbc.csproj:
##########
@@ -0,0 +1,18 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <Version>0.1.0</Version>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <Compile Remove="Extensions\MatchCollectionExtensions.netstandard.cs" />

Review Comment:
   Does this need a `Condition=TFM == netstandard` on it?



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
+        private static NativeDelegate<DriverRelease> releaseDriver = new 
NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private static NativeDelegate<DatabaseFn> databaseInit = new 
NativeDelegate<DatabaseFn>(InitDatabase);

Review Comment:
   ```suggestion
           private static readonly NativeDelegate<DatabaseFn> databaseInit = 
new NativeDelegate<DatabaseFn>(InitDatabase);
   ```
   
   These can all be `readonly`.



##########
csharp/src/Apache.Arrow.Adbc/Apache.Arrow.Adbc.csproj:
##########
@@ -0,0 +1,18 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <Version>0.1.0</Version>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <Compile Remove="Extensions\MatchCollectionExtensions.netstandard.cs" />
+    <Compile Remove="Interop\NativeMemoryManager.cs" />

Review Comment:
   ```suggestion
   
   ```
   
   This can be removed.



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
+        private static NativeDelegate<DriverRelease> releaseDriver = new 
NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private static NativeDelegate<DatabaseFn> databaseInit = new 
NativeDelegate<DatabaseFn>(InitDatabase);
+        private static NativeDelegate<DatabaseFn> databaseRelease = new 
NativeDelegate<DatabaseFn>(ReleaseDatabase);
+        private static NativeDelegate<DatabaseSetOption> databaseSetOption = 
new NativeDelegate<DatabaseSetOption>(SetDatabaseOption);
+        private static NativeDelegate<ConnectionInit> connectionInit = new 
NativeDelegate<ConnectionInit>(InitConnection);
+        private static NativeDelegate<ConnectionFn> connectionRelease = new 
NativeDelegate<ConnectionFn>(ReleaseConnection);
+        private static NativeDelegate<ConnectionSetOption> connectionSetOption 
= new NativeDelegate<ConnectionSetOption>(SetConnectionOption);
+        private static unsafe NativeDelegate<StatementExecuteQuery> 
statementExecuteQuery = new 
NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);

Review Comment:
   ```suggestion
           private static NativeDelegate<StatementExecuteQuery> 
statementExecuteQuery = new 
NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);
   ```



##########
csharp/src/Apache.Arrow.Adbc/Core/Interop.cs:
##########
@@ -0,0 +1,608 @@
+/*
+ * 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.Adbc.Interop;
+using Apache.Arrow.C;
+using static System.Net.Mime.MediaTypeNames;
+
+#if NETSTANDARD
+using Apache.Arrow.Adbc.Extensions;
+#endif
+
+namespace Apache.Arrow.Adbc.Core
+{
+    public static class AdbcInterop
+    {
+        private static unsafe NativeDelegate<ErrorRelease> releaseError = new 
NativeDelegate<ErrorRelease>(ReleaseError);
+        private static NativeDelegate<DriverRelease> releaseDriver = new 
NativeDelegate<DriverRelease>(ReleaseDriver);
+
+        private static NativeDelegate<DatabaseFn> databaseInit = new 
NativeDelegate<DatabaseFn>(InitDatabase);
+        private static NativeDelegate<DatabaseFn> databaseRelease = new 
NativeDelegate<DatabaseFn>(ReleaseDatabase);
+        private static NativeDelegate<DatabaseSetOption> databaseSetOption = 
new NativeDelegate<DatabaseSetOption>(SetDatabaseOption);
+        private static NativeDelegate<ConnectionInit> connectionInit = new 
NativeDelegate<ConnectionInit>(InitConnection);
+        private static NativeDelegate<ConnectionFn> connectionRelease = new 
NativeDelegate<ConnectionFn>(ReleaseConnection);
+        private static NativeDelegate<ConnectionSetOption> connectionSetOption 
= new NativeDelegate<ConnectionSetOption>(SetConnectionOption);
+        private static unsafe NativeDelegate<StatementExecuteQuery> 
statementExecuteQuery = new 
NativeDelegate<StatementExecuteQuery>(ExecuteStatementQuery);
+        private static NativeDelegate<StatementNew> statementNew = new 
NativeDelegate<StatementNew>(NewStatement);
+        private static NativeDelegate<StatementFn> statementRelease = new 
NativeDelegate<StatementFn>(ReleaseStatement);
+        private static NativeDelegate<StatementSetSqlQuery> 
statementSetSqlQuery = new 
NativeDelegate<StatementSetSqlQuery>(SetStatementSqlQuery);
+
+        public unsafe static AdbcStatusCode AdbcDriverInit(int version, 
NativeAdbcDriver* nativeDriver, NativeAdbcError* error, AdbcDriver driver)
+        {
+            DriverStub stub = new DriverStub(driver);
+            GCHandle handle = GCHandle.Alloc(stub);
+            (*nativeDriver).private_data = GCHandle.ToIntPtr(handle);
+            (*nativeDriver).release = releaseDriver;
+            (*nativeDriver).DatabaseNew = stub.newDatabase;
+            (*nativeDriver).DatabaseInit = databaseInit;
+            (*nativeDriver).DatabaseRelease = databaseRelease;
+            (*nativeDriver).DatabaseSetOption = databaseSetOption;
+            (*nativeDriver).ConnectionNew = stub.newConnection;
+            (*nativeDriver).ConnectionInit = connectionInit;
+            (*nativeDriver).ConnectionRelease = connectionRelease;
+            (*nativeDriver).ConnectionSetOption = connectionSetOption;
+            (*nativeDriver).StatementNew = statementNew;
+            (*nativeDriver).StatementSetSqlQuery = statementSetSqlQuery;
+            (*nativeDriver).StatementExecuteQuery = statementExecuteQuery;
+            (*nativeDriver).StatementRelease = statementRelease;
+            return 0;
+        }
+
+        private unsafe static void ReleaseError(NativeAdbcError* error)
+        {
+            if (error != null && (*error).message != IntPtr.Zero)
+            {
+                Marshal.FreeCoTaskMem((*error).message);
+            }
+        }
+
+        private unsafe static AdbcStatusCode SetError(NativeAdbcError* error, 
Exception exception)
+        {
+            ReleaseError(error);
+
+#if NETSTANDARD
+            error->message = 
MarshalExtensions.StringToCoTaskMemUTF8(exception.Message);
+#else
+            error->message = Marshal.StringToCoTaskMemUTF8(exception.Message);
+#endif
+
+            error->sqlstate0 = (char)0;
+            error->sqlstate1 = (char)0;
+            error->sqlstate2 = (char)0;
+            error->sqlstate3 = (char)0;
+            error->sqlstate4 = (char)0;
+            error->vendor_code = 0;
+            error->vendor_code = 0;
+            error->release = (delegate* unmanaged[Stdcall]<NativeAdbcError*, 
void>)(IntPtr)releaseError.Pointer;
+            
+            return AdbcStatusCode.UnknownError;
+        }
+
+        private sealed class PinnedArray : IDisposable
+        {
+            IArrowArray _array;
+            MemoryHandle[] pinnedHandles;
+
+            public PinnedArray(IArrowArray array)
+            {
+                _array = array;
+                pinnedHandles = new MemoryHandle[GetHandleCount(array.Data)];
+                int index = 0;
+                PinBuffers(array.Data, pinnedHandles, ref index);
+                Debug.Assert(index == pinnedHandles.Length);
+            }
+
+            public void Dispose()
+            {
+                if (_array != null)
+                {
+                    _array.Dispose();
+                    foreach (MemoryHandle handle in pinnedHandles)
+                    {
+                        handle.Dispose();
+                    }
+                    _array = null;
+                }
+            }
+
+            static int GetHandleCount(ArrayData data)
+            {
+                int handleCount = data.Buffers.Length;
+                foreach (ArrayData child in data.Children)
+                {
+                    handleCount += GetHandleCount(child);
+                }
+                if (data.Dictionary != null)
+                {
+                    handleCount += GetHandleCount(data.Dictionary);
+                }
+                return handleCount;
+            }
+
+            static void PinBuffers(ArrayData data, MemoryHandle[] handles, ref 
int index)
+            {
+                foreach (ArrowBuffer buffer in data.Buffers)
+                {
+                    handles[index++] = buffer.Memory.Pin();
+                }
+                foreach (ArrayData child in data.Children)
+                {
+                    PinBuffers(child, handles, ref index);
+                }
+                if (data.Dictionary != null)
+                {
+                    PinBuffers(data.Dictionary, handles, ref index);
+                }
+            }
+        }
+
+        private static IntPtr FromDisposable(IDisposable d)
+        {
+            GCHandle gch = GCHandle.Alloc(d);
+            return GCHandle.ToIntPtr(gch);
+        }
+
+        private static void Dispose(ref IntPtr p)
+        {
+            GCHandle gch = GCHandle.FromIntPtr(p);
+            ((IDisposable)gch.Target).Dispose();
+            gch.Free();
+            p = IntPtr.Zero;
+        }
+
+        private static AdbcStatusCode ReleaseDriver(ref NativeAdbcDriver 
nativeDriver, ref NativeAdbcError error)
+        {
+            GCHandle gch = GCHandle.FromIntPtr(nativeDriver.private_data);
+            DriverStub stub = (DriverStub)gch.Target;
+            stub.Dispose();
+            gch.Free();
+            nativeDriver.private_data = IntPtr.Zero;
+            return 0;
+        }
+
+        private unsafe static AdbcStatusCode InitDatabase(ref 
NativeAdbcDatabase nativeDatabase, ref NativeAdbcError error)
+        {
+            GCHandle gch = 
GCHandle.FromIntPtr((IntPtr)nativeDatabase.private_data);
+            DatabaseStub stub = (DatabaseStub)gch.Target;
+            return stub.Init(ref error);
+        }
+
+        private unsafe static AdbcStatusCode ReleaseDatabase(ref 
NativeAdbcDatabase nativeDatabase, ref NativeAdbcError error)
+        {
+            if ((IntPtr)nativeDatabase.private_data == IntPtr.Zero)
+            {
+                return AdbcStatusCode.UnknownError;
+            }
+
+            GCHandle gch = 
GCHandle.FromIntPtr((IntPtr)nativeDatabase.private_data);
+            DatabaseStub stub = (DatabaseStub)gch.Target;
+            stub.Dispose();
+            gch.Free();
+            nativeDatabase.private_data = null;//IntPtr.Zero;

Review Comment:
   ```suggestion
               nativeDatabase.private_data = null;
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to