diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/CHANGELOG.md b/Frends.MicrosoftSQL.ExecuteProcedure/CHANGELOG.md index 5605c6c..07d4d92 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/CHANGELOG.md +++ b/Frends.MicrosoftSQL.ExecuteProcedure/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.2.0] - 2024-01-03 +### Changed +- [Breaking] ProcedureParameter.Value type to object so that binary data can be used. + ## [1.0.1] - 2023-08-03 ### Changed - Documentation update to Input.Execute parameter. diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExceptionUnitTests.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExceptionUnitTests.cs index 575f504..100d607 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExceptionUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExceptionUnitTests.cs @@ -7,7 +7,7 @@ namespace Frends.MicrosoftSQL.ExecuteProcedure.Tests; public class ExceptionUnitTests { /* - docker-compose up + docker-compose up -d How to use via terminal: docker exec -it sql1 "bash" @@ -84,6 +84,5 @@ public async Task TestExecuteProcedure_Invalid_Creds_ReturnErrorMessage() Assert.IsNull(result.Data, $"ExecuteType: {executeType}, IsolationLevel: {isolation}"); } } - } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExecuteReaderUnitTests.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExecuteReaderUnitTests.cs index fccf1e4..f2ee00b 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExecuteReaderUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ExecuteReaderUnitTests.cs @@ -9,7 +9,7 @@ namespace Frends.MicrosoftSQL.ExecuteProcedure.Tests; public class ExecuteReaderUnitTests { /* - docker-compose up + docker-compose up -d How to use via terminal: docker exec -it sql1 "bash" @@ -255,6 +255,58 @@ public async Task TestExecuteProcedure_ProcedureParameter() Assert.IsTrue(((IEnumerable)query.Data).Any(x => x.Id == 1 && x.LastName == "Suku")); } + [TestMethod] + public async Task ExecuteQueryTestWithBinaryData() + { + var table = "binarytest"; + var command = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{table}') BEGIN CREATE TABLE {table} ( Id int, Data varbinary(MAX)); END"; + ExecuteNonQuery(command); + + var binary = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../TestData/", "Test_image.png")); + + //Insert + command = $"CREATE PROCEDURE InsertBinaryValues(@binary as varbinary(MAX)) AS INSERT INTO {table} (Id, Data) VALUES (1, @binary)"; + ExecuteNonQuery(command); + + var input = new Input + { + ConnectionString = _connString, + Execute = "InsertBinaryValues", + ExecuteType = ExecuteTypes.NonQuery, + Parameters = new ProcedureParameter[] { new ProcedureParameter { Name = "binary", Value = binary, SqlDataType = SqlDataTypes.VarBinary } } + }; + + var options = new Options + { + CommandTimeoutSeconds = 30, + SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted, + ThrowErrorOnFailure = true + }; + + var result = await MicrosoftSQL.ExecuteProcedure(input, options, default); + Assert.IsTrue(result.Success); + + //Select single + command = $"CREATE PROCEDURE SelectSingleBinary AS select * from {table} where Id = 1"; + ExecuteNonQuery(command); + + input = new Input + { + ConnectionString = _connString, + Execute = "SelectSingleBinary", + ExecuteType = ExecuteTypes.ExecuteReader, + Parameters = null + }; + + result = await MicrosoftSQL.ExecuteProcedure(input, options, default); + + command = $"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{table}') BEGIN DROP TABLE IF EXISTS {table}; END"; + ExecuteNonQuery(command); + + Assert.IsTrue(result.Success); + Assert.AreEqual(Convert.ToBase64String(binary), Convert.ToBase64String((byte[])result.Data[0]["Data"])); + } + // Simple select query private static int GetRowCount() { @@ -266,5 +318,16 @@ private static int GetRowCount() connection.Close(); connection.Dispose(); return count; + } + + private static void ExecuteNonQuery(string command) + { + using var connection = new SqlConnection(_connString); + connection.Open(); + var createTable = connection.CreateCommand(); + createTable.CommandText = command; + createTable.ExecuteNonQuery(); + connection.Close(); + connection.Dispose(); } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ManualTesting.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ManualTesting.cs index a62dbe8..0878994 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ManualTesting.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ManualTesting.cs @@ -10,7 +10,7 @@ public class ManualTesting /* These tests requires code editing so they must be skipped in workflow. - docker-compose up + docker-compose up -d How to use via terminal: docker exec -it sql1 "bash" diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/NonQueryUnitTests.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/NonQueryUnitTests.cs index 63af83a..8c78c00 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/NonQueryUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/NonQueryUnitTests.cs @@ -8,7 +8,7 @@ namespace Frends.MicrosoftSQL.ExecuteProcedure.Tests; public class NonQueryUnitTests { /* - docker-compose up + docker-compose up -d How to use via terminal: docker exec -it sql1 "bash" diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ScalarUnitTests.cs index e7447f6..58d9460 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/ScalarUnitTests.cs @@ -8,7 +8,7 @@ namespace Frends.MicrosoftSQL.ExecuteProcedure.Tests; public class ScalarUnitTests { /* - docker-compose up + docker-compose up -d How to use via terminal: docker exec -it sql1 "bash" diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/TestData/Test_image.png b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/TestData/Test_image.png new file mode 100644 index 0000000..7055e41 Binary files /dev/null and b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.Tests/TestData/Test_image.png differ diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Definitions/Input.cs b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Definitions/Input.cs index 8646665..c12412a 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Definitions/Input.cs +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Definitions/Input.cs @@ -1,6 +1,5 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; namespace Frends.MicrosoftSQL.ExecuteProcedure.Definitions; @@ -14,6 +13,7 @@ public class Input /// /// Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword; [PasswordPropertyText] + [DisplayFormat(DataFormatString = "Text")] [DefaultValue("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")] public string ConnectionString { get; set; } @@ -61,7 +61,7 @@ public class ProcedureParameter /// The value of the parameter. /// /// FirstName - public string Value { get; set; } + public object Value { get; set; } /// /// SQL Server-specific data type. diff --git a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.csproj b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.csproj index 74d2e13..a159c29 100644 --- a/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.csproj +++ b/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure/Frends.MicrosoftSQL.ExecuteProcedure.csproj @@ -2,7 +2,7 @@ net6.0 - 1.0.1 + 1.2.0 Frends Frends Frends