Skip to content

Commit

Permalink
Fixed null parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuVirtanen committed Feb 12, 2024
1 parent dd75081 commit 3bfc129
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.0.1] - 2024-02-12
### Fixed
- Fixed handling of null query parameters by changing the parameter value to DBNull.Value.

## [1.0.0] - 2024-02-07
### Changed
- Initial implementation
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,30 @@ public async Task ExecuteQueryToFile_WithBinaryDBTypeWithWhereClause()

Assert.AreEqual(BitConverter.ToString(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_destination), "Test_text.txt"))), output.TrimEnd(new char[] { '\r', '\n' }));
}

[Test]
public async Task ExecuteQueryToFile_WithNULLParameter()
{
var query = new Input
{
Query = $"SELECT * FROM {_tableName} WHERE TestNull = @param",
QueryParameters = new Definitions.SqlParameter[]
{
new Definitions.SqlParameter
{
Name = "@param",
Value = null,
SqlDataType = SqlDataTypes.VarChar,
},
},
ConnectionString = _connString,
OutputFilePath = _destination,
};

await MicrosoftSQL.ExecuteQueryToFile(query, _options, default);

var output = File.ReadAllText(_destination);

Assert.IsNotNull(output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static void CreateTestTable(string connString, string tableName)
using var connection = new SqlConnection(connString);
connection.Open();
var createTable = connection.CreateCommand();
createTable.CommandText = $@"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{tableName}') BEGIN CREATE TABLE {tableName} ( Id int, LastName varchar(255), FirstName varchar(255), Salary decimal(6,2), Image Image, TestText VarBinary(MAX)); END";
createTable.CommandText = $@"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{tableName}') BEGIN CREATE TABLE {tableName} ( Id int, LastName varchar(255), FirstName varchar(255), Salary decimal(6,2), Image Image, TestText VarBinary(MAX), TestNull Varchar(255)); END";
createTable.ExecuteNonQuery();
connection.Close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SqlParameter

/// <summary>
/// The value of the parameter.
/// If value is null, it is changed to DBNull in the Task.
/// </summary>
/// <example>FirstName</example>
public object Value { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public static async Task<Result> ExecuteQueryToFile([PropertyTab] Input input, [
}
else
{
if (parameter.Value is null)
parameter.Value = DBNull.Value;

var sqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), parameter.SqlDataType.ToString());
var commandParameter = command.Parameters.Add(parameter.Name, sqlDbType);
commandParameter.Value = parameter.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "Following Frends guidelines")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:Enumeration items should be documented", Justification = "Following Frends guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Enums")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:Using directives should be placed correctly", Justification = "Following Frends guidelines")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "Following Frends guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Definitions")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "Following Frends guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile")]

0 comments on commit 3bfc129

Please sign in to comment.