This is an automated email from the ASF dual-hosted git repository.

curth pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new 4cffa7bc8 fix(csharp/src/Apache.Arrow.Adbc/C): imported errors don't 
return a native error or SQL state (#1815)
4cffa7bc8 is described below

commit 4cffa7bc8bc7acff43fb07533c548ce5ce953494
Author: Curt Hagenlocher <[email protected]>
AuthorDate: Sat May 4 05:39:37 2024 -0700

    fix(csharp/src/Apache.Arrow.Adbc/C): imported errors don't return a native 
error or SQL state (#1815)
    
    Creates subclass of AdbcException for imported drivers which reads the
    native error code and SQLState from the C error structure.
    
    Closes #1813
---
 csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs     |  2 --
 .../src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs | 31 +++++++++++++++++-----
 .../Client/DuckDbClientTests.cs                    |  2 +-
 .../Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs |  6 ++---
 .../test/Drivers/Interop/Snowflake/ValueTests.cs   |  6 ++---
 5 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs 
b/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
index 07cb586c8..1efe89e07 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
@@ -17,8 +17,6 @@
 
 using System;
 using System.Collections.Generic;
-using System.Linq;
-using Apache.Arrow.Adbc.C;
 using Apache.Arrow.Ipc;
 
 namespace Apache.Arrow.Adbc
diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs 
b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
index b7b755405..786c347de 100644
--- a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
+++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
@@ -20,6 +20,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Runtime.InteropServices;
+using System.Text;
 using System.Threading;
 using Apache.Arrow.Adbc.Extensions;
 using Apache.Arrow.C;
@@ -1099,6 +1100,28 @@ namespace Apache.Arrow.Adbc.C
             }
         }
 
+        internal class ImportedAdbcException : AdbcException
+        {
+            private readonly string? _sqlState;
+            private readonly int _nativeError;
+
+            public unsafe ImportedAdbcException(ref CAdbcError error, 
AdbcStatusCode statusCode)
+                : base(MarshalExtensions.PtrToStringUTF8(error.message) ?? 
"Undefined error", statusCode)
+            {
+                if (error.sqlstate0 != 0)
+                {
+                    fixed (CAdbcError* fixedError = &error)
+                    {
+                        _sqlState = 
Encoding.ASCII.GetString(&fixedError->sqlstate0, 5);
+                    }
+                    _nativeError = error.vendor_code;
+                }
+            }
+
+            public override string? SqlState => _sqlState;
+            public override int NativeError => _nativeError;
+        }
+
         /// <summary>
         /// Assists with UTF8/string marshalling
         /// </summary>
@@ -1424,15 +1447,11 @@ namespace Apache.Arrow.Adbc.C
             {
                 if (statusCode != AdbcStatusCode.Success)
                 {
-                    string message = "Undefined error";
-                    if (_error.message != null)
-                    {
-                        message = 
MarshalExtensions.PtrToStringUTF8(_error.message)!;
-                    }
+                    ImportedAdbcException exception = new 
ImportedAdbcException(ref _error, statusCode);
 
                     Dispose();
 
-                    throw new AdbcException(message);
+                    throw exception;
                 }
             }
         }
diff --git a/csharp/test/Apache.Arrow.Adbc.Tests/Client/DuckDbClientTests.cs 
b/csharp/test/Apache.Arrow.Adbc.Tests/Client/DuckDbClientTests.cs
index 0d276db07..31e25f662 100644
--- a/csharp/test/Apache.Arrow.Adbc.Tests/Client/DuckDbClientTests.cs
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/Client/DuckDbClientTests.cs
@@ -111,7 +111,7 @@ namespace Apache.Arrow.Adbc.Tests.Client
             using var connection = 
_duckDb.CreateConnection("clientisolation.db", null);
             connection.Open();
 
-            Assert.Throws<AdbcException>(() =>
+            Assert.ThrowsAny<AdbcException>(() =>
             {
                 
connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
             });
diff --git a/csharp/test/Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs 
b/csharp/test/Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs
index 0cd25a14c..8816b828f 100644
--- a/csharp/test/Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs
@@ -125,7 +125,7 @@ namespace Apache.Arrow.Adbc.Tests
 
             connection.ReadOnly = true;
 
-            AdbcException exception = Assert.Throws<AdbcException>(() =>
+            AdbcException exception = Assert.ThrowsAny<AdbcException>(() =>
             {
                 statement.SqlQuery = "INSERT INTO test VALUES (3), (5), (7);";
                 statement.ExecuteUpdate();
@@ -144,7 +144,7 @@ namespace Apache.Arrow.Adbc.Tests
             using var database = _duckDb.OpenDatabase("readonly.db");
             using var connection = database.Connect(null);
 
-            Assert.Throws<AdbcException>(() =>
+            Assert.ThrowsAny<AdbcException>(() =>
             {
                 connection.ReadOnly = true;
             });
@@ -156,7 +156,7 @@ namespace Apache.Arrow.Adbc.Tests
             using var database = _duckDb.OpenDatabase("isolation.db");
             using var connection = database.Connect(null);
 
-            Assert.Throws<AdbcException>(() =>
+            Assert.ThrowsAny<AdbcException>(() =>
             {
                 connection.IsolationLevel = IsolationLevel.Default;
             });
diff --git a/csharp/test/Drivers/Interop/Snowflake/ValueTests.cs 
b/csharp/test/Drivers/Interop/Snowflake/ValueTests.cs
index 3a48eb50b..131111270 100644
--- a/csharp/test/Drivers/Interop/Snowflake/ValueTests.cs
+++ b/csharp/test/Drivers/Interop/Snowflake/ValueTests.cs
@@ -107,7 +107,7 @@ namespace Apache.Arrow.Adbc.Tests.Drivers.Interop.Snowflake
         {
             string columnName = "SMALLNUMBER";
             string table = CreateTemporaryTable(string.Format("{0} 
NUMBER(2,0)", columnName));
-            Assert.Throws<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, value));
+            Assert.ThrowsAny<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, value));
         }
 
         /// <summary>
@@ -138,7 +138,7 @@ namespace Apache.Arrow.Adbc.Tests.Drivers.Interop.Snowflake
         {
             string columnName = "LARGESCALENUMBER";
             string table = CreateTemporaryTable(string.Format("{0} 
NUMBER(38,37)", columnName));
-            Assert.Throws<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, 
SqlDecimal.Parse(value)));
+            Assert.ThrowsAny<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, 
SqlDecimal.Parse(value)));
         }
 
         /// <summary>
@@ -166,7 +166,7 @@ namespace Apache.Arrow.Adbc.Tests.Drivers.Interop.Snowflake
         {
             string columnName = "SMALLSCALENUMBER";
             string table = CreateTemporaryTable(string.Format("{0} 
NUMBER(38,2)", columnName));
-            Assert.Throws<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, 
SqlDecimal.Parse(value)));
+            Assert.ThrowsAny<AdbcException>(() => 
ValidateInsertSelectDeleteSingleDecimalValue(table, columnName, 
SqlDecimal.Parse(value)));
         }
 
 

Reply via email to