Skip to content

Commit

Permalink
Merge pull request #23 from FrendsPlatform/issue-21
Browse files Browse the repository at this point in the history
ExecuteProcedure - Changed ProcedureParameter value type
  • Loading branch information
Svenskapojkarna authored Jan 3, 2024
2 parents 093a177 + 73a20e1 commit 3bb76b0
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Frends.MicrosoftSQL.ExecuteProcedure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -84,6 +84,5 @@ public async Task TestExecuteProcedure_Invalid_Creds_ReturnErrorMessage()
Assert.IsNull(result.Data, $"ExecuteType: {executeType}, IsolationLevel: {isolation}");
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -255,6 +255,58 @@ public async Task TestExecuteProcedure_ProcedureParameter()
Assert.IsTrue(((IEnumerable<dynamic>)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()
{
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace Frends.MicrosoftSQL.ExecuteProcedure.Definitions;

Expand All @@ -14,6 +13,7 @@ public class Input
/// </summary>
/// <example>Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;</example>
[PasswordPropertyText]
[DisplayFormat(DataFormatString = "Text")]
[DefaultValue("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")]
public string ConnectionString { get; set; }

Expand Down Expand Up @@ -61,7 +61,7 @@ public class ProcedureParameter
/// The value of the parameter.
/// </summary>
/// <example>FirstName</example>
public string Value { get; set; }
public object Value { get; set; }

/// <summary>
/// SQL Server-specific data type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.0.1</Version>
<Version>1.2.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down

0 comments on commit 3bb76b0

Please sign in to comment.