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 SqlDataReader.IsDBNull() for json #2892

Merged
merged 2 commits into from
Oct 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5820,6 +5820,10 @@ internal static object GetNullSqlValue(SqlBuffer nullVal, SqlMetaDataPriv md, Sq
}
break;

case SqlDbTypeExtensions.Json:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Json);
break;

default:
Debug.Fail("unknown null sqlType!" + md.type.ToString());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6667,6 +6667,10 @@ internal static object GetNullSqlValue(
}
break;

case SqlDbTypeExtensions.Json:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Json);
break;

default:
Debug.Fail("unknown null sqlType!" + md.type.ToString());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ private void ValidateSchema(SqlDataReader reader)
}
}

private void ValidateNullJson(SqlDataReader reader)
{
while (reader.Read())
{
bool IsNull = reader.IsDBNull(0);
_output.WriteLine(IsNull ? "null" : "not null");
Assert.True(IsNull);
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
public void TestJsonWrite()
{
Expand Down Expand Up @@ -295,5 +305,36 @@ public async Task TestJsonReadAsync()
}
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
public void TestNullJson()
{
string tableName = "jsonTest";

string tableInsert = "INSERT INTO " + tableName + " VALUES (@jsonData)";
string tableRead = "SELECT * FROM " + tableName;

using SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString);
connection.Open();
using SqlCommand command = connection.CreateCommand();

//Create Table
DataTestUtility.CreateTable(connection, tableName, "(Data json)");

//Insert Null value
command.CommandText = tableInsert;
var parameter = new SqlParameter("@jsonData", SqlDbTypeExtensions.Json);
parameter.Value = DBNull.Value;
command.Parameters.Add(parameter);
command.ExecuteNonQuery();

//Query the table
command.CommandText = tableRead;
var reader = command.ExecuteReader();
ValidateNullJson(reader);

reader.Close();
DataTestUtility.DropTable(connection, tableName);
}
}
}
Loading