Skip to content

Commit 9c4f2d6

Browse files
authored
Merge pull request #63 from FrendsPlatform/issue-61
Microsoft.ExecuteQueryToFile - Added Microsoft.SqlServer.Types dependency
2 parents b765373 + 0cd2b37 commit 9c4f2d6

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [2.1.0] - 2024-12-16
4+
### Added
5+
- Added Microsoft.SqlServer.Types dependency so that SqlGeography and SqlGeometry typed objects can be handled.
6+
37
## [2.0.0] - 2024-08-05
48
### Changed
59
- [Breaking] The task now uses Microsoft.Data.SqlClient instead of System.Data.SqlClient.

Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
[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")]
77
[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")]
88
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "Following Frends documentation guidelines", Scope = "namespaceanddescendants", Target = "~N:Frends.MicrosoftSQL.ExecuteQueryToFile.Tests")]
9+
[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)")]

Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/UnitTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Frends.MicrosoftSQL.ExecuteQueryToFile.Tests;
77
using Frends.MicrosoftSQL.ExecuteQueryToFile.Definitions;
88
using Frends.MicrosoftSQL.ExecuteQueryToFile.Enums;
99
using Microsoft.Data.SqlClient;
10+
using Newtonsoft.Json.Linq;
1011
using NUnit.Framework;
1112

1213
/// <summary>
@@ -67,7 +68,7 @@ public void Init()
6768
},
6869
};
6970

70-
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);
71+
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);
7172
}
7273

7374
[TearDown]
@@ -267,4 +268,39 @@ public async Task ExecuteQueryToFile_WithNULLParameter()
267268

268269
Assert.IsNotNull(output);
269270
}
271+
272+
[Test]
273+
public async Task ExecuteQueryToFile_TestWithGeographyData()
274+
{
275+
var table = "geographytest";
276+
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";
277+
278+
Helper.CreateTestTable(_connString, table, query);
279+
280+
Helper.ExecuteNonQuery(_connString, $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));");
281+
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));");
282+
283+
var input = new Input
284+
{
285+
ConnectionString = _connString,
286+
Query = $"SELECT * From {table}",
287+
OutputFilePath = _destination,
288+
};
289+
290+
try
291+
{
292+
input.Query = $"SELECT * From {table}";
293+
294+
var select = await MicrosoftSQL.ExecuteQueryToFile(input, _options, default);
295+
296+
var output = File.ReadAllLines(_destination);
297+
Assert.AreEqual(2, output.Length);
298+
Assert.IsTrue(output[0].Split(";")[1].StartsWith("LINESTRING"));
299+
Assert.IsTrue(output[1].Split(";")[1].StartsWith("POLYGON"));
300+
}
301+
finally
302+
{
303+
Helper.ExecuteNonQuery(_connString, $"DROP TABLE {table}");
304+
}
305+
}
270306
}

Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.Tests/lib/Helper.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ internal static string GetConnectionString()
1212
return $"Server=127.0.0.1,1433;Database=Master;User Id={user};Password={pwd};TrustServerCertificate=True";
1313
}
1414

15-
internal static void CreateTestTable(string connString, string tableName)
15+
internal static void CreateTestTable(string connString, string tableName, string query = null)
1616
{
1717
using var connection = new SqlConnection(connString);
1818
connection.Open();
1919
var createTable = connection.CreateCommand();
20-
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";
20+
21+
if (query == null)
22+
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";
23+
else
24+
createTable.CommandText = query;
25+
2126
createTable.ExecuteNonQuery();
2227
connection.Close();
2328
}
2429

25-
internal static void InsertTestData(string connString, string commandText, Microsoft.Data.SqlClient.SqlParameter[] parameters = null)
30+
internal static void ExecuteNonQuery(string connString, string commandText, SqlParameter[] parameters = null)
2631
{
2732
using var sqlConnection = new SqlConnection(connString);
2833
sqlConnection.Open();

Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile/Frends.MicrosoftSQL.ExecuteQueryToFile.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<LangVersion>Latest</LangVersion>
6-
<Version>2.0.0</Version>
6+
<Version>2.1.0</Version>
77
<Authors>Frends</Authors>
88
<Copyright>Frends</Copyright>
99
<Company>Frends</Company>
@@ -35,6 +35,7 @@
3535
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
3636
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3737
<PackageReference Include="CsvHelper" Version="30.0.1" />
38+
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
3839
</ItemGroup>
3940

4041
<ItemGroup>

0 commit comments

Comments
 (0)