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

Conversation

PopeyeZhong
Copy link
Contributor

var command = new TDengineCommand()
{
CommandText = "...",
CommandType = CommandType.Text,
};

var connection = new TDengineConnection("...");
command.DbConnection = connection;

上述代码片段中最后一行代码,因为 TDengineConnection 尚未打开,所以其内部的 client 变量为空,故而导致 TDegnineCommand.DbConnection 属性设置器中的代码引发了空引用异常。

现将 TDengineCommand 类中的 DbConnection 属性的设置器中的 _connection.client.StmtInit() 调用移动到 Statement() 方法中,将 StmtInit() 方法后置到执行方法也更为合理。

var command = new TDengineCommand()
{
	CommandText = "...",
	CommandType = CommandType.Text,
};

var connection = new TDengineConnection("...");
command.DbConnection = connection;

如上代码片段中最后一行代码,因为 TDengineConnection 尚未打开,所以其内部的 client 变量为空,故而导致 TDegnineCommand.DbConnection 属性设置器中的代码引发了空引用异常。

现将 TDengineCommand 类中的 DbConnection 属性的设置器中的 _connection.client.StmtInit() 调用移动到  Statement() 方法中,将 StmtInit() 方法后置到执行方法也更为合理。
添加对 DbParameter.Value 为 DBNull.Value 的支持。
@PopeyeZhong PopeyeZhong marked this pull request as draft November 6, 2024 01:27
@PopeyeZhong PopeyeZhong marked this pull request as ready for review November 6, 2024 01:28
@PopeyeZhong
Copy link
Contributor Author

PopeyeZhong commented Nov 11, 2024

Can someone please help me with the code pull request approvals?
请问,有人帮我处理下代码拉取请求(PullRequest)的评审与批准么?

@huskar-t huskar-t self-requested a review November 26, 2024 07:41
@huskar-t
Copy link
Collaborator

@PopeyeZhong Can you add some unit tests to cover it?

@huskar-t
Copy link
Collaborator

@PopeyeZhong Please rebase the 3.0 branch.

@PopeyeZhong
Copy link
Contributor Author

@PopeyeZhong Can you add some unit tests to cover it?

Okay, I've added two unit test cases for TDengineCommand, please check them:

  1. SetConnection_DoesNotThrowException
  2. ExecuteNonQuery_WithDBNull

@PopeyeZhong
Copy link
Contributor Author

@PopeyeZhong Please rebase the 3.0 branch.

YES, I just checked and it's based on the 3.0 branch.

Comment on lines 68 to 106
public void SetConnection_DoesNotThrowException()
{
using(var command = new TDengineCommand())
{
command.CommandText = "SELECT * FROM t";
var ex = Record.Exception(() => command.Connection = _connection);
Assert.Null(ex);
}

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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的哈~

Copy link
Collaborator

@huskar-t huskar-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format the test code

@PopeyeZhong
Copy link
Contributor Author

format the test code

Done! please check it.

@huskar-t huskar-t merged commit 0447b8d into taosdata:3.0 Nov 27, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants