From 0b6b90195b351786deb7e2939135b85a6e22adde Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Tue, 26 Nov 2024 11:36:29 +0200 Subject: [PATCH 1/4] MicrosoftSQL.ExecuteQuery - Added handling of SqlGeoraphy objects --- Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md | 6 ++ .../AutoUnitTests.cs | 68 +++++++++++++++++++ .../Lib/ExecuteQueryTestBase.cs | 2 +- .../ScalarUnitTests.cs | 46 +++++++++++++ .../ExecuteQuery.cs | 41 +++++++++-- .../Frends.MicrosoftSQL.ExecuteQuery.csproj | 3 +- 6 files changed, 160 insertions(+), 6 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md index 9aefddd..8d2b589 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md +++ b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [2.2.0] - 2024-11-26 +### Added +- Added method to form JToken from the SqlDataReader so that SqlGeography typed objects can be handled. +- Fixed how Scalar handles the data so that SqlGeography typed objects can be handled. +- Added Microsoft.SqlServer.Types version 160.1000.6 as dependency. + ## [2.1.0] - 2024-09-10 ### Fixed - Fixed how null values are handled by setting them as DBNull.Value. diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs index ce62158..ea0c63a 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs @@ -132,4 +132,72 @@ public async Task TestExecuteQuery_Auto() CleanUp(); } } + + [TestMethod] + public async Task TestWithGeographyData() + { + var table = "geographytest"; + + var options = new Options() + { + SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.None, + CommandTimeoutSeconds = 2, + ThrowErrorOnFailure = true + }; + + var createInput = new Input + { + ConnectionString = _connString, + 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", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var create = await MicrosoftSQL.ExecuteQuery(createInput, options, default); + Assert.IsTrue(create.Success, "Create table"); + + var insert1Input = new Input + { + ConnectionString = _connString, + Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var insert1 = await MicrosoftSQL.ExecuteQuery(insert1Input, options, default); + Assert.IsTrue(insert1.Success, "First insert"); + + var insert2Input = new Input + { + ConnectionString = _connString, + Query = $"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));", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var insert2 = await MicrosoftSQL.ExecuteQuery(insert2Input, options, default); + Assert.IsTrue(insert2.Success, "Second insert"); + + var selectInput = new Input + { + ConnectionString = _connString, + Query = $"SELECT * From {table}", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var select = await MicrosoftSQL.ExecuteQuery(selectInput, options, default); + Assert.IsTrue(select.Success, "Select"); + + var dropInput = new Input + { + ConnectionString = _connString, + Query = $"DROP TABLE {table}", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var drop = await MicrosoftSQL.ExecuteQuery(dropInput, options, default); + Assert.IsTrue(drop.Success, "Drop"); + } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs index d11b8bc..bf83618 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/Lib/ExecuteQueryTestBase.cs @@ -14,7 +14,7 @@ public void Init() 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) ); 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)); END"; createTable.ExecuteNonQuery(); connection.Close(); connection.Dispose(); diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs index 20f3be4..5d16e9b 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs @@ -130,4 +130,50 @@ public async Task TestExecuteQuery_Scalar() CleanUp(); } } + + [TestMethod] + public async Task TestWithGeographyData_Scalar() + { + var table = "geographytest"; + + var options = new Options() + { + SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.None, + CommandTimeoutSeconds = 2, + ThrowErrorOnFailure = true + }; + + var input = new Input + { + ConnectionString = _connString, + 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", + ExecuteType = ExecuteTypes.Auto, + Parameters = null + }; + + var create = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(create.Success, "Create table"); + + input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));"; + + var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert1.Success, "First insert"); + + input.Query = $"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 insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert2.Success, "Second insert"); + + input.Query = $"SELECT GeogCol1 From {table}"; + input.ExecuteType = ExecuteTypes.Scalar; + + var select = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(select.Success, "Select"); + + input.Query = $"DROP TABLE {table}"; + input.ExecuteType = ExecuteTypes.Auto; + + var drop = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(drop.Success, "Drop"); + } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index 50a9e13..aa467e6 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using IsolationLevel = System.Data.IsolationLevel; using Microsoft.Data.SqlClient; +using Microsoft.SqlServer.Types; namespace Frends.MicrosoftSQL.ExecuteQuery; @@ -102,8 +103,7 @@ private static async Task ExecuteHandler(Input input, Options options, S if (input.Query.ToLower().StartsWith("select")) { dataReader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); - table.Load(dataReader); - result = new Result(true, dataReader.RecordsAffected, null, JToken.FromObject(table)); + result = new Result(true, dataReader.RecordsAffected, null, await LoadData(dataReader, cancellationToken)); await dataReader.CloseAsync(); break; } @@ -116,12 +116,16 @@ private static async Task ExecuteHandler(Input input, Options options, S break; case ExecuteTypes.Scalar: dataObject = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); + + // JToken.FromObject() method can't handle SqlGeography typed objects so we convert it into string. + if (dataObject != null && dataObject.GetType() == typeof(SqlGeography)) + dataObject = dataObject.ToString(); + result = new Result(true, 1, null, JToken.FromObject(new { Value = dataObject })); break; case ExecuteTypes.ExecuteReader: dataReader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); - table.Load(dataReader); - result = new Result(true, dataReader.RecordsAffected, null, JToken.FromObject(table)); + result = new Result(true, dataReader.RecordsAffected, null, await LoadData(dataReader, cancellationToken)); await dataReader.CloseAsync(); break; default: @@ -167,6 +171,35 @@ private static async Task ExecuteHandler(Input input, Options options, S } } + private static async Task LoadData(SqlDataReader reader, CancellationToken cancellationToken) + { + var table = new JArray(); + while (reader.HasRows) + { + while (await reader.ReadAsync(cancellationToken)) + { + var row = new JObject(); + for (var i = 0; i < reader.FieldCount; i++) + { + dynamic value; + if (reader.GetValue(i).GetType() == typeof(DBNull)) + value = null; + else if (reader.GetValue(i).GetType() == typeof(SqlGeography)) + value = reader.GetValue(i).ToString(); + else + value = reader.GetValue(i); + + row.Add(new JProperty(reader.GetName(i), value)); + } + + table.Add(row); + } + await reader.NextResultAsync(cancellationToken).ConfigureAwait(false); + } + + return table; + } + private static IsolationLevel GetIsolationLevel(Options options) { return options.SqlTransactionIsolationLevel switch diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj index 49b7dd9..38389f9 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.csproj @@ -2,7 +2,7 @@ net6.0 - 2.1.0 + 2.2.0 Frends Frends Frends @@ -24,5 +24,6 @@ + \ No newline at end of file From 5fd4b4428b81a335403f96f0441e758c2a8d5c0d Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Tue, 26 Nov 2024 11:53:35 +0200 Subject: [PATCH 2/4] Implemented CodeRabbit requested changes --- .../AutoUnitTests.cs | 67 ++++++++----------- .../ScalarUnitTests.cs | 43 +++++++----- .../ExecuteQuery.cs | 11 +-- 3 files changed, 61 insertions(+), 60 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs index ea0c63a..4f91213 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/AutoUnitTests.cs @@ -145,7 +145,7 @@ public async Task TestWithGeographyData() ThrowErrorOnFailure = true }; - var createInput = new Input + var input = new Input { ConnectionString = _connString, 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", @@ -153,51 +153,42 @@ public async Task TestWithGeographyData() Parameters = null }; - var create = await MicrosoftSQL.ExecuteQuery(createInput, options, default); - Assert.IsTrue(create.Success, "Create table"); - - var insert1Input = new Input + try { - ConnectionString = _connString, - Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));", - ExecuteType = ExecuteTypes.Auto, - Parameters = null - }; + var create = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(create.Success, "Create table"); - var insert1 = await MicrosoftSQL.ExecuteQuery(insert1Input, options, default); - Assert.IsTrue(insert1.Success, "First insert"); + input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));"; - var insert2Input = new Input - { - ConnectionString = _connString, - Query = $"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));", - ExecuteType = ExecuteTypes.Auto, - Parameters = null - }; + var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert1.Success, "First insert"); - var insert2 = await MicrosoftSQL.ExecuteQuery(insert2Input, options, default); - Assert.IsTrue(insert2.Success, "Second insert"); + input.Query = $"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 selectInput = new Input - { - ConnectionString = _connString, - Query = $"SELECT * From {table}", - ExecuteType = ExecuteTypes.Auto, - Parameters = null - }; + var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert2.Success, "Second insert"); + + input.Query = $"SELECT * From {table}"; + + var select = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(select.Success, "Select"); + Assert.AreEqual(typeof(JArray), select.Data.GetType()); + Assert.AreEqual(2, select.Data.Count); - var select = await MicrosoftSQL.ExecuteQuery(selectInput, options, default); - Assert.IsTrue(select.Success, "Select"); + // Verify first row (LINESTRING) + Assert.IsNotNull(select.Data[0]["GeogCol1"]); + Assert.IsTrue(select.Data[0]["GeogCol2"].ToString().StartsWith("LINESTRING")); - var dropInput = new Input + // Verify second row (POLYGON) + Assert.IsNotNull(select.Data[1]["GeogCol1"]); + Assert.IsTrue(select.Data[1]["GeogCol2"].ToString().StartsWith("POLYGON")); + } + finally { - ConnectionString = _connString, - Query = $"DROP TABLE {table}", - ExecuteType = ExecuteTypes.Auto, - Parameters = null - }; + input.Query = $"DROP TABLE {table}"; - var drop = await MicrosoftSQL.ExecuteQuery(dropInput, options, default); - Assert.IsTrue(drop.Success, "Drop"); + var drop = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(drop.Success, "Drop"); + } } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs index 5d16e9b..aa57444 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery.Tests/ScalarUnitTests.cs @@ -1,6 +1,7 @@ using Frends.MicrosoftSQL.ExecuteQuery.Definitions; using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json.Linq; namespace Frends.MicrosoftSQL.ExecuteQuery.Tests; @@ -151,29 +152,37 @@ public async Task TestWithGeographyData_Scalar() Parameters = null }; - var create = await MicrosoftSQL.ExecuteQuery(input, options, default); - Assert.IsTrue(create.Success, "Create table"); - - input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));"; + try + { + var create = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(create.Success, "Create table"); - var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default); - Assert.IsTrue(insert1.Success, "First insert"); + input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));"; - input.Query = $"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 insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert1.Success, "First insert"); - var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default); - Assert.IsTrue(insert2.Success, "Second insert"); + input.Query = $"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));"; - input.Query = $"SELECT GeogCol1 From {table}"; - input.ExecuteType = ExecuteTypes.Scalar; + var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(insert2.Success, "Second insert"); - var select = await MicrosoftSQL.ExecuteQuery(input, options, default); - Assert.IsTrue(select.Success, "Select"); + input.Query = $"SELECT GeogCol1 From {table}"; + input.ExecuteType = ExecuteTypes.Scalar; - input.Query = $"DROP TABLE {table}"; - input.ExecuteType = ExecuteTypes.Auto; + var select = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(select.Success, "Select"); + Assert.IsNotNull(select.Data["Value"], "Selected data should not be null"); + Assert.IsInstanceOfType(select.Data["Value"], typeof(JValue), "Geography data should be converted to string"); + Assert.IsTrue(((string)select.Data["Value"]).StartsWith("LINESTRING"), "First row should be a LINESTRING"); + } + finally + { + input.Query = $"DROP TABLE {table}"; + input.ExecuteType = ExecuteTypes.Auto; - var drop = await MicrosoftSQL.ExecuteQuery(input, options, default); - Assert.IsTrue(drop.Success, "Drop"); + var drop = await MicrosoftSQL.ExecuteQuery(input, options, default); + Assert.IsTrue(drop.Success, "Drop"); + } } } \ No newline at end of file diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index aa467e6..a05bb84 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -181,13 +181,14 @@ private static async Task LoadData(SqlDataReader reader, CancellationTok var row = new JObject(); for (var i = 0; i < reader.FieldCount; i++) { - dynamic value; - if (reader.GetValue(i).GetType() == typeof(DBNull)) + object fieldValue = reader.GetValue(i); + object value; + if (fieldValue == DBNull.Value) value = null; - else if (reader.GetValue(i).GetType() == typeof(SqlGeography)) - value = reader.GetValue(i).ToString(); + else if (fieldValue is SqlGeography geography) + value = geography.ToString(); else - value = reader.GetValue(i); + value = fieldValue; row.Add(new JProperty(reader.GetName(i), value)); } From 56be8baa998825441af16f05b2c66e5bba04a8b8 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Tue, 26 Nov 2024 13:06:23 +0200 Subject: [PATCH 3/4] Added handling of SqlGeometry typed objects --- Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md | 4 ++-- .../Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md index 8d2b589..609650c 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md +++ b/Frends.MicrosoftSQL.ExecuteQuery/CHANGELOG.md @@ -2,8 +2,8 @@ ## [2.2.0] - 2024-11-26 ### Added -- Added method to form JToken from the SqlDataReader so that SqlGeography typed objects can be handled. -- Fixed how Scalar handles the data so that SqlGeography typed objects can be handled. +- Added method to form JToken from the SqlDataReader so that SqlGeography and SqlGeometry typed objects can be handled. +- Fixed how Scalar handles the data so that SqlGeography and SqlGeometry typed objects can be handled. - Added Microsoft.SqlServer.Types version 160.1000.6 as dependency. ## [2.1.0] - 2024-09-10 diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index a05bb84..c1196b8 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -118,7 +118,7 @@ private static async Task ExecuteHandler(Input input, Options options, S dataObject = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); // JToken.FromObject() method can't handle SqlGeography typed objects so we convert it into string. - if (dataObject != null && dataObject.GetType() == typeof(SqlGeography)) + if (dataObject != null && (dataObject.GetType() == typeof(SqlGeography) || dataObject.GetType() == typeof(SqlGeometry))) dataObject = dataObject.ToString(); result = new Result(true, 1, null, JToken.FromObject(new { Value = dataObject })); @@ -187,6 +187,8 @@ private static async Task LoadData(SqlDataReader reader, CancellationTok value = null; else if (fieldValue is SqlGeography geography) value = geography.ToString(); + else if (fieldValue is SqlGeometry geometry) + value = geometry.ToString(); else value = fieldValue; From 46d95d9b6252ca94de639d7b408df13b30c3c0d5 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Tue, 26 Nov 2024 13:51:06 +0200 Subject: [PATCH 4/4] Minor changes --- .../Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs index c1196b8..b3dc7be 100644 --- a/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs +++ b/Frends.MicrosoftSQL.ExecuteQuery/Frends.MicrosoftSQL.ExecuteQuery/ExecuteQuery.cs @@ -93,7 +93,6 @@ private static async Task ExecuteHandler(Input input, Options options, S Result result; object dataObject; SqlDataReader dataReader = null; - using var table = new DataTable(); try { @@ -169,6 +168,11 @@ private static async Task ExecuteHandler(Input input, Options options, S return new Result(false, 0, $"ExecuteHandler exception: (If required) transaction rollback completed without exception. {ex}.", null); } } + finally + { + if (dataReader != null && !dataReader.IsClosed) + await dataReader.CloseAsync(); + } } private static async Task LoadData(SqlDataReader reader, CancellationToken cancellationToken)