From 0cd2b37bfe15f44773531ba053fa95e06a63680f Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Mon, 16 Dec 2024 11:12:33 +0200 Subject: [PATCH] Added Microsoft.SqlServer.Types dependency --- .../CHANGELOG.md | 4 ++ .../GlobalSuppressions.cs | 1 + .../UnitTests.cs | 38 ++++++++++++++++++- .../lib/Helper.cs | 11 ++++-- ...nds.MicrosoftSQL.ExecuteQueryToFile.csproj | 3 +- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md b/Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md index 7f7e754..528ac6f 100644 --- a/Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md +++ b/Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [2.1.0] - 2024-12-16 +### Added +- Added Microsoft.SqlServer.Types dependency so that SqlGeography and SqlGeometry typed objects can be handled. + ## [2.0.0] - 2024-08-05 ### Changed - [Breaking] The task now uses Microsoft.Data.SqlClient instead of System.Data.SqlClient. diff --git a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/GlobalSuppressions.cs b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/GlobalSuppressions.cs index d4a5ab6..82dc8de 100644 --- a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/GlobalSuppressions.cs +++ b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/GlobalSuppressions.cs @@ -6,3 +6,4 @@ [assembly: SuppressMessage("StyleCop.CSharp.NamingRules", "SA1309:Field names should not begin with underscore", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")] [assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1629:Documentation text should end with a period", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")] [assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")] +[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "Following latest .Net6 langVersion", Scope = "member", Target = "~M:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests.Helper.CreateTestTable(System.String,System.String,System.String)")] diff --git a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/UnitTests.cs b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/UnitTests.cs index 62d6f15..a1f5e4e 100644 --- a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/UnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/UnitTests.cs @@ -7,6 +7,7 @@ namespace Frends.MicrosoftSQL.ExecuteQueryToFile.Tests; using Frends.MicrosoftSQL.ExecuteQueryToFile.Definitions; using Frends.MicrosoftSQL.ExecuteQueryToFile.Enums; using Microsoft.Data.SqlClient; +using Newtonsoft.Json.Linq; using NUnit.Framework; /// @@ -67,7 +68,7 @@ public void Init() }, }; - Helper.InsertTestData(_connString, $"Insert into {_tableName} (Id, LastName, FirstName, Salary, Image, TestText) values (1,'Meikalainen','Matti',1523.25, {parameters[0].ParameterName}, {parameters[1].ParameterName});", parameters); + Helper.ExecuteNonQuery(_connString, $"Insert into {_tableName} (Id, LastName, FirstName, Salary, Image, TestText) values (1,'Meikalainen','Matti',1523.25, {parameters[0].ParameterName}, {parameters[1].ParameterName});", parameters); } [TearDown] @@ -267,4 +268,39 @@ public async Task ExecuteQueryToFile_WithNULLParameter() Assert.IsNotNull(output); } + + [Test] + public async Task ExecuteQueryToFile_TestWithGeographyData() + { + var table = "geographytest"; + var query = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{table}') BEGIN CREATE TABLE {table} ( Id int IDENTITY(1, 1), GeogCol1 geography, GeogCol2 AS GeogCol1.STAsText()); END"; + + Helper.CreateTestTable(_connString, table, query); + + Helper.ExecuteNonQuery(_connString, $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));"); + Helper.ExecuteNonQuery(_connString, $"INSERT INTO {table} (GeogCol1) VALUES(geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));"); + + var input = new Input + { + ConnectionString = _connString, + Query = $"SELECT * From {table}", + OutputFilePath = _destination, + }; + + try + { + input.Query = $"SELECT * From {table}"; + + var select = await MicrosoftSQL.ExecuteQueryToFile(input, _options, default); + + var output = File.ReadAllLines(_destination); + Assert.AreEqual(2, output.Length); + Assert.IsTrue(output[0].Split(";")[1].StartsWith("LINESTRING")); + Assert.IsTrue(output[1].Split(";")[1].StartsWith("POLYGON")); + } + finally + { + Helper.ExecuteNonQuery(_connString, $"DROP TABLE {table}"); + } + } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/lib/Helper.cs b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/lib/Helper.cs index 1d23a2e..f640a68 100644 --- a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/lib/Helper.cs +++ b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/lib/Helper.cs @@ -12,17 +12,22 @@ internal static string GetConnectionString() return $"Server=127.0.0.1,1433;Database=Master;User Id={user};Password={pwd};TrustServerCertificate=True"; } - internal static void CreateTestTable(string connString, string tableName) + internal static void CreateTestTable(string connString, string tableName, string query = null) { 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), TestNull Varchar(255)); END"; + + if (query == null) + 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"; + else + createTable.CommandText = query; + createTable.ExecuteNonQuery(); connection.Close(); } - internal static void InsertTestData(string connString, string commandText, Microsoft.Data.SqlClient.SqlParameter[] parameters = null) + internal static void ExecuteNonQuery(string connString, string commandText, SqlParameter[] parameters = null) { using var sqlConnection = new SqlConnection(connString); sqlConnection.Open(); diff --git a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.csproj b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.csproj index 7210800..ec6c0bf 100644 --- a/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.csproj +++ b/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.csproj @@ -3,7 +3,7 @@ net6.0 Latest - 2.0.0 + 2.1.0 Frends Frends Frends @@ -35,6 +35,7 @@ +