Skip to content

Commit

Permalink
Merge pull request #90 from PopeyeZhong/3.0
Browse files Browse the repository at this point in the history
fix: Fix null reference exception in setter that sets DbConnection property of the TDengineCommand class.
  • Loading branch information
huskar-t authored Nov 27, 2024
2 parents 76c4e41 + e5aa70a commit 0447b8d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
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

0 comments on commit 0447b8d

Please sign in to comment.