eerhardt commented on code in PR #697: URL: https://github.com/apache/arrow-adbc/pull/697#discussion_r1221968792
########## csharp/src/Apache.Arrow.Adbc/Interop/LoadDriver.cs: ########## @@ -0,0 +1,492 @@ +/* + * 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); Review Comment: Are we ignoring the `error` here? And above in the MacInterop. ########## csharp/src/Apache.Arrow.Adbc/Interop/LoadDriver.cs: ########## @@ -0,0 +1,492 @@ +/* + * 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] == '/') Review Comment: What's the plan for Linux? Might be good to have a follow-up issue for supporting Linux. ########## csharp/src/Apache.Arrow.Adbc/Interop.cs: ########## @@ -0,0 +1,1203 @@ +/* + * 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 Apache.Arrow.Ipc; + +#if NETSTANDARD +using Apache.Arrow.Adbc.Extensions; +#endif + +namespace Apache.Arrow.Adbc +{ + internal static class AdbcInterop + { + 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, NativeAdbcDriver* nativeDriver, NativeAdbcError* 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]<NativeAdbcDriver*, NativeAdbcError*, AdbcStatusCode>)releaseDriver.Pointer; + + nativeDriver->DatabaseInit = (delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode>)databaseInit.Pointer; + nativeDriver->DatabaseNew = (delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode>)stub.newDatabase.Pointer; + nativeDriver->DatabaseSetOption = (delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, byte*, byte*, NativeAdbcError*, AdbcStatusCode>)databaseSetOption.Pointer; + nativeDriver->DatabaseRelease = (delegate* unmanaged[Stdcall]<NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode>)databaseRelease.Pointer; + + nativeDriver->ConnectionCommit = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcError*, AdbcStatusCode>)connectionRelease.Pointer; + nativeDriver->ConnectionGetInfo = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, byte*, int, CArrowArrayStream*, NativeAdbcError*, AdbcStatusCode>)connectionGetInfo.Pointer; + nativeDriver->ConnectionGetObjects = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, int, byte*, byte*, byte*, byte**, byte*, CArrowArrayStream*, NativeAdbcError*, AdbcStatusCode>)connectionGetObjects.Pointer; + nativeDriver->ConnectionGetTableSchema = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, byte*, byte*, byte*, CArrowSchema*, NativeAdbcError*, AdbcStatusCode>)connectionGetTableSchema.Pointer; + nativeDriver->ConnectionGetTableTypes = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, CArrowArrayStream*, NativeAdbcError*, AdbcStatusCode>)connectionGetTableTypes.Pointer; + nativeDriver->ConnectionInit = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcDatabase*, NativeAdbcError*, AdbcStatusCode>)connectionInit.Pointer; + nativeDriver->ConnectionNew = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcError*, AdbcStatusCode>)stub.newConnection.Pointer; + nativeDriver->ConnectionSetOption = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, byte*, byte*, NativeAdbcError*, AdbcStatusCode>)connectionSetOption.Pointer; + nativeDriver->ConnectionReadPartition = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, byte*, int, CArrowArrayStream*, NativeAdbcError*, AdbcStatusCode>)connectionReadPartition.Pointer; + nativeDriver->ConnectionRelease = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcError*, AdbcStatusCode>)connectionRelease.Pointer; + nativeDriver->ConnectionRollback = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcError*, AdbcStatusCode>)connectionRelease.Pointer; + + nativeDriver->StatementBind = (delegate* unmanaged[Stdcall]<NativeAdbcStatement*, CArrowArray*, CArrowSchema*, NativeAdbcError*, AdbcStatusCode>)statementBind.Pointer; + nativeDriver->StatementNew = (delegate* unmanaged[Stdcall]<NativeAdbcConnection*, NativeAdbcStatement*, NativeAdbcError*, AdbcStatusCode>)statementNew.Pointer; + nativeDriver->StatementSetSqlQuery = (delegate* unmanaged[Stdcall]<NativeAdbcStatement*, byte*, NativeAdbcError*, AdbcStatusCode>)statementSetSqlQuery.Pointer; + nativeDriver->StatementExecuteQuery = (delegate* unmanaged[Stdcall]<NativeAdbcStatement*, CArrowArrayStream*, long*, NativeAdbcError*, AdbcStatusCode>)statementExecuteQuery.Pointer; + nativeDriver->StatementPrepare = (delegate* unmanaged[Stdcall]<NativeAdbcStatement*, NativeAdbcError*, AdbcStatusCode>)statementRelease.Pointer; + nativeDriver->StatementRelease = (delegate* unmanaged[Stdcall]<NativeAdbcStatement*, NativeAdbcError*, AdbcStatusCode>)statementRelease.Pointer; + + return 0; + } + + private unsafe static void ReleaseError(NativeAdbcError* error) + { + if (error != null && ((IntPtr)error->message) != IntPtr.Zero) + { + Marshal.FreeHGlobal((IntPtr)error->message); + } + } + + private unsafe static AdbcStatusCode SetError(NativeAdbcError* 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; + error->release = (delegate* unmanaged[Stdcall]<NativeAdbcError*, void>)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)]; Review Comment: Would it make sense to make this a `List<MemoryHandle>` instead, so we don't have to count all the handles first, and then pin them? ########## dev/release/rat_exclude_files.txt: ########## @@ -30,3 +30,8 @@ c/vendor/sqlite3/sqlite3.c c/vendor/sqlite3/sqlite3.h *.Rproj *.Rd +csharp/Apache.Arrow.Adbc.sln +csharp/src/Apache.Arrow.Adbc.FlightSql/Apache.Arrow.Adbc.FlightSql.csproj +csharp/src/Apache.Arrow.Adbc/Apache.Arrow.Adbc.csproj +csharp/test/Apache.Arrow.Adbc.FlightSql.Tests/Apache.Arrow.Adbc.FlightSql.Tests.csproj +csharp/test/Apache.Arrow.Adbc.Tests/Apache.Arrow.Adbc.Tests.csproj Review Comment: I didn't know these rat_exclude_files support wildcards. Can we just have: ```suggestion *.sln *.csproj ``` -- 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]
