Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix null reference exception in setter that sets DbConnection property of the TDengineCommand class. #90

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/Data/Client/TDengineCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using System.Collections.Generic;

using TDengine.Driver;

namespace TDengine.Data.Client
Expand Down Expand Up @@ -127,14 +127,7 @@ public override CommandType CommandType
protected override DbConnection DbConnection
{
get => _connection;
set
{
_connection = (TDengineConnection)value;
if (_stmt == null && _connection != null)
{
_stmt = _connection.client.StmtInit();
}
}
set => _connection = value as TDengineConnection ?? throw new ArgumentException($"The specified connection is not of type {nameof(TDengineConnection)}.");
}

protected override DbParameterCollection DbParameterCollection => _parameters.Value;
Expand All @@ -158,6 +151,11 @@ private IRows Query()

private IRows Statement()
{
if(_stmt == null && _connection != null)
{
_stmt = _connection.client.StmtInit();
}

if (!_isPrepared)
{
_isPrepared = true;
Expand Down
7 changes: 2 additions & 5 deletions src/Driver/Client/Native/NativeStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ public void SetTags(object[] tags)
}
}


private TAOS_MULTI_BIND[] GenerateBindList(object[] data, TaosFieldE[] fields, out IntPtr[] needFree,
bool isInsert)
private TAOS_MULTI_BIND[] GenerateBindList(object[] data, TaosFieldE[] fields, out IntPtr[] needFree, bool isInsert)
{
TAOS_MULTI_BIND[] binds = new TAOS_MULTI_BIND[data.Length];
var needFreePointer = new List<IntPtr>();
Expand All @@ -92,7 +90,7 @@ private TAOS_MULTI_BIND[] GenerateBindList(object[] data, TaosFieldE[] fields, o
{
num = 1
};
if (data[i] == null)
if (data[i] == null || Convert.IsDBNull(data[i]))
{
bind.buffer_type = (int)TDengineDataType.TSDB_DATA_TYPE_BOOL;
IntPtr p = Marshal.AllocHGlobal(TDengineConstant.ByteSize);
Expand Down Expand Up @@ -471,7 +469,6 @@ private TAOS_MULTI_BIND GenerateBindColumn(Array array, TaosFieldE field)
}
}


public void AddBatch()
{
var code = NativeMethods.StmtAddBatch(_stmt);
Expand Down
41 changes: 41 additions & 0 deletions test/Data.Tests/TDengineCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ public void Dispose()
}

[Fact]
public void SetConnection_DoesNotThrowException()
{
//CASE 1: Set an already open connection.
using(var command = new TDengineCommand())
{
command.CommandText = "SELECT * FROM t";
var ex = Record.Exception(() => command.Connection = _connection);
Assert.Null(ex);
}

//CASE 2: Set an connection that is not yet open.
using(var command = new TDengineCommand())
{
using(var connection = new TDengineConnection("username=root;password=taosdata"))
{
command.CommandText = "SELECT * FROM t";
var ex = Record.Exception(() => command.Connection = connection);
Assert.Null(ex);
}
}
}

[Fact]
public void ExecuteNonQuery_WithDBNull()
{
using(var command = new TDengineCommand(_connection))
{
command.CommandText = "create table if not exists t_dbnull (ts timestamp,v int,description nchar(100))";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO t_dbnull VALUES (?,?,?)";
command.Parameters.AddWithValue(DateTime.Now);
command.Parameters.AddWithValue(123);
command.Parameters.AddWithValue(DBNull.Value);

int affectedRows = command.ExecuteNonQuery();

Assert.Equal(1, affectedRows);
}
}

[Fact]
public void ExecuteNonQuery_WithValidCommand_ReturnsAffectedRows()
{
using (var command = new TDengineCommand(_connection))
Expand Down