Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.ExecuteQueryToFile - Added Microsoft.SqlServer.Types dependency #63

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Frends.MicrosoftSQL.ExecuteQueryToFile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")]
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading