Skip to content

Commit

Permalink
DuckDBResult class to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Sep 13, 2023
1 parent 462cbc4 commit 5a5e1e1
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 156 deletions.
34 changes: 2 additions & 32 deletions DuckDB.NET.Bindings/DuckDBNativeObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum DuckDBType
}

[StructLayout(LayoutKind.Sequential)]
public class DuckDBResult : IDisposable
public struct DuckDBResult : IDisposable
{
[Obsolete]
private long ColumnCount;
Expand All @@ -74,42 +74,12 @@ public class DuckDBResult : IDisposable

private IntPtr internal_data;

public static implicit operator DuckDBResultStruct(DuckDBResult d) => d.ToStruct();

private DuckDBResultStruct ToStruct()
{
return new DuckDBResultStruct()
{
internal_data = internal_data
};
}

public void Dispose()
{
NativeMethods.Query.DuckDBDestroyResult(this);
NativeMethods.Query.DuckDBDestroyResult(ref this);
}
}

public struct DuckDBResultStruct
{
[Obsolete]
internal long ColumnCount;

[Obsolete]
internal long RowCount;

[Obsolete]
internal long RowsChanged;

[Obsolete]
internal IntPtr columns;

[Obsolete]
internal IntPtr ErrorMessage;

internal IntPtr internal_data;
}

[StructLayout(LayoutKind.Sequential)]
public struct DuckDBDate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ public static class PreparedStatements
public static extern DuckDBState DuckDBBindTimestamp(DuckDBPreparedStatement preparedStatement, long index, DuckDBTimestampStruct val);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_execute_prepared")]
public static extern DuckDBState DuckDBExecutePrepared(DuckDBPreparedStatement preparedStatement, [In, Out] DuckDBResult result);
public static extern DuckDBState DuckDBExecutePrepared(DuckDBPreparedStatement preparedStatement, out DuckDBResult result);
}
}
24 changes: 12 additions & 12 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ public partial class NativeMethods
public static class Query
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_query")]
public static extern DuckDBState DuckDBQuery(DuckDBNativeConnection connection, string query, [In, Out] DuckDBResult result);
public static extern DuckDBState DuckDBQuery(DuckDBNativeConnection connection, string query, out DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_query")]
public static extern DuckDBState DuckDBQuery(DuckDBNativeConnection connection, SafeUnmanagedMemoryHandle query, [In, Out] DuckDBResult result);
public static extern DuckDBState DuckDBQuery(DuckDBNativeConnection connection, SafeUnmanagedMemoryHandle query, out DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_result")]
public static extern void DuckDBDestroyResult([In, Out] DuckDBResult result);
public static extern void DuckDBDestroyResult([In, Out] ref DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_column_name")]
public static extern IntPtr DuckDBColumnName([In, Out] DuckDBResult result, long col);
public static extern IntPtr DuckDBColumnName([In, Out] ref DuckDBResult result, long col);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_column_type")]
public static extern DuckDBType DuckDBColumnType([In, Out] DuckDBResult result, long col);
public static extern DuckDBType DuckDBColumnType([In, Out] ref DuckDBResult result, long col);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_column_logical_type")]
public static extern DuckDBLogicalType DuckDBColumnLogicalType([In, Out] DuckDBResult result, long col);
public static extern DuckDBLogicalType DuckDBColumnLogicalType([In, Out] ref DuckDBResult result, long col);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_column_count")]
public static extern long DuckDBColumnCount([In, Out] DuckDBResult result);
public static extern long DuckDBColumnCount([In, Out] ref DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_row_count")]
public static extern long DuckDBRowCount([In, Out] DuckDBResult result);
public static extern long DuckDBRowCount([In, Out] ref DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_rows_changed")]
public static extern long DuckDBRowsChanged([In, Out] DuckDBResult result);
public static extern long DuckDBRowsChanged([In, Out] ref DuckDBResult result);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_column_data")]
public static extern IntPtr DuckDBColumnData([In, Out] DuckDBResult result, long col);
public static extern IntPtr DuckDBColumnData([In, Out] ref DuckDBResult result, long col);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_nullmask_data")]
public static extern IntPtr DuckDBNullmaskData([In, Out] DuckDBResult result, long col);
public static extern IntPtr DuckDBNullmaskData([In, Out] ref DuckDBResult result, long col);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_result_error")]
public static extern IntPtr DuckDBResultError([In, Out] DuckDBResult result);
public static extern IntPtr DuckDBResultError([In, Out] ref DuckDBResult result);
}
}
40 changes: 20 additions & 20 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,63 @@ public partial class NativeMethods
public static class Types
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_boolean")]
public static extern bool DuckDBValueBoolean([In, Out] DuckDBResult result, long col, long row);
public static extern bool DuckDBValueBoolean([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_int8")]
public static extern sbyte DuckDBValueInt8([In, Out] DuckDBResult result, long col, long row);
public static extern sbyte DuckDBValueInt8([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_int16")]
public static extern short DuckDBValueInt16([In, Out] DuckDBResult result, long col, long row);
public static extern short DuckDBValueInt16([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_int32")]
public static extern int DuckDBValueInt32([In, Out] DuckDBResult result, long col, long row);
public static extern int DuckDBValueInt32([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_int64")]
public static extern long DuckDBValueInt64([In, Out] DuckDBResult result, long col, long row);
public static extern long DuckDBValueInt64([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_decimal")]
public static extern DuckDBDecimal DuckDBValueDecimal([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBDecimal DuckDBValueDecimal([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_uint8")]
public static extern byte DuckDBValueUInt8([In, Out] DuckDBResult result, long col, long row);
public static extern byte DuckDBValueUInt8([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_uint16")]
public static extern ushort DuckDBValueUInt16([In, Out] DuckDBResult result, long col, long row);
public static extern ushort DuckDBValueUInt16([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_uint32")]
public static extern uint DuckDBValueUInt32([In, Out] DuckDBResult result, long col, long row);
public static extern uint DuckDBValueUInt32([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_uint64")]
public static extern ulong DuckDBValueUInt64([In, Out] DuckDBResult result, long col, long row);
public static extern ulong DuckDBValueUInt64([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_float")]
public static extern float DuckDBValueFloat([In, Out] DuckDBResult result, long col, long row);
public static extern float DuckDBValueFloat([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_double")]
public static extern double DuckDBValueDouble([In, Out] DuckDBResult result, long col, long row);
public static extern double DuckDBValueDouble([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_interval")]
public static extern DuckDBInterval DuckDBValueInterval([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBInterval DuckDBValueInterval([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_varchar")]
public static extern IntPtr DuckDBValueVarchar([In, Out] DuckDBResult result, long col, long row);
public static extern IntPtr DuckDBValueVarchar([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_blob")]
public static extern DuckDBBlob DuckDBValueBlob([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBBlob DuckDBValueBlob([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_date")]
public static extern DuckDBDate DuckDBValueDate([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBDate DuckDBValueDate([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_time")]
public static extern DuckDBTime DuckDBValueTime([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBTime DuckDBValueTime([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_value_timestamp")]
public static extern DuckDBTimestampStruct DuckDBValueTimestamp([In, Out] DuckDBResult result, long col, long row);
public static extern DuckDBTimestampStruct DuckDBValueTimestamp([In, Out] ref DuckDBResult result, long col, long row);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_result_get_chunk")]
public static extern DuckDBDataChunk DuckDBResultGetChunk([In, Out] DuckDBResultStruct result, long chunkIndex);
public static extern DuckDBDataChunk DuckDBResultGetChunk([In, Out] DuckDBResult result, long chunkIndex);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_result_chunk_count")]
public static extern long DuckDBResultChunkCount([In, Out] DuckDBResultStruct result);
public static extern long DuckDBResultChunkCount([In, Out] DuckDBResult result);
}
}
5 changes: 3 additions & 2 deletions DuckDB.NET.Data/DuckDBCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public override int ExecuteNonQuery()

var count = 0;

foreach (var result in results)
for (var index = 0; index < results.Count; index++)
{
count += (int)NativeMethods.Query.DuckDBRowsChanged(result);
var result = results[index];
count += (int)NativeMethods.Query.DuckDBRowsChanged(ref result);

result.Dispose();
}
Expand Down
18 changes: 9 additions & 9 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private void InitReaderData()
{
currentRow = -1;

rowCount = NativeMethods.Query.DuckDBRowCount(currentResult);
fieldCount = (int)NativeMethods.Query.DuckDBColumnCount(currentResult);
rowCount = NativeMethods.Query.DuckDBRowCount(ref currentResult);
fieldCount = (int)NativeMethods.Query.DuckDBColumnCount(ref currentResult);
chunkCount = NativeMethods.Types.DuckDBResultChunkCount(currentResult);

currentChunkIndex = 0;
Expand Down Expand Up @@ -110,7 +110,7 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b

public override string GetDataTypeName(int ordinal)
{
return NativeMethods.Query.DuckDBColumnType(currentResult, ordinal).ToString();
return NativeMethods.Query.DuckDBColumnType(ref currentResult, ordinal).ToString();
}

public override DateTime GetDateTime(int ordinal)
Expand All @@ -137,7 +137,7 @@ private DuckDBTimeOnly GetTimeOnly(int ordinal)

public override decimal GetDecimal(int ordinal)
{
using (var logicalType = NativeMethods.Query.DuckDBColumnLogicalType(currentResult, ordinal))
using (var logicalType = NativeMethods.Query.DuckDBColumnLogicalType(ref currentResult, ordinal))
{
var scale = NativeMethods.LogicalType.DuckDBDecimalScale(logicalType);
var internalType = NativeMethods.LogicalType.DuckDBDecimalInternalType(logicalType);
Expand Down Expand Up @@ -180,7 +180,7 @@ public override double GetDouble(int ordinal)

public override Type GetFieldType(int ordinal)
{
return NativeMethods.Query.DuckDBColumnType(currentResult, ordinal) switch
return NativeMethods.Query.DuckDBColumnType(ref currentResult, ordinal) switch
{
DuckDBType.DuckdbTypeInvalid => throw new DuckDBException("Invalid type"),
DuckDBType.DuckdbTypeBoolean => typeof(bool),
Expand Down Expand Up @@ -263,15 +263,15 @@ private BigInteger GetBigInteger(int ordinal)

public override string GetName(int ordinal)
{
return NativeMethods.Query.DuckDBColumnName(currentResult, ordinal).ToManagedString(false);
return NativeMethods.Query.DuckDBColumnName(ref currentResult, ordinal).ToManagedString(false);
}

public override int GetOrdinal(string name)
{
var columnCount = NativeMethods.Query.DuckDBColumnCount(currentResult);
var columnCount = NativeMethods.Query.DuckDBColumnCount(ref currentResult);
for (var i = 0; i < columnCount; i++)
{
var columnName = NativeMethods.Query.DuckDBColumnName(currentResult, i).ToManagedString(false);
var columnName = NativeMethods.Query.DuckDBColumnName(ref currentResult, i).ToManagedString(false);
if (name == columnName)
{
return i;
Expand Down Expand Up @@ -305,7 +305,7 @@ public override object GetValue(int ordinal)
return DBNull.Value;
}

return NativeMethods.Query.DuckDBColumnType(currentResult, ordinal) switch
return NativeMethods.Query.DuckDBColumnType(ref currentResult, ordinal) switch
{
DuckDBType.DuckdbTypeInvalid => throw new DuckDBException("Invalid type"),
DuckDBType.DuckdbTypeBoolean => GetBoolean(ordinal),
Expand Down
5 changes: 2 additions & 3 deletions DuckDB.NET.Data/Internal/PreparedStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ void CleanUp()

public DuckDBResult Execute(DuckDBParameterCollection parameterCollection)
{
var queryResult = new DuckDBResult();
BindParameters(statement, parameterCollection);

var status = NativeMethods.PreparedStatements.DuckDBExecutePrepared(statement, queryResult);
var status = NativeMethods.PreparedStatements.DuckDBExecutePrepared(statement, out var queryResult);
if (!status.IsSuccess())
{
var errorMessage = NativeMethods.Query.DuckDBResultError(queryResult).ToManagedString(false);
var errorMessage = NativeMethods.Query.DuckDBResultError(ref queryResult).ToManagedString(false);
queryResult.Dispose();
throw new DuckDBException(string.IsNullOrEmpty(errorMessage) ? "DuckDBQuery failed" : errorMessage, status);
}
Expand Down
Loading

0 comments on commit 5a5e1e1

Please sign in to comment.