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

support geometry type #217

Merged
merged 2 commits into from
Oct 3, 2023
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
5 changes: 3 additions & 2 deletions Library/Library.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>10.0</LangVersion>
<TargetFrameworks>net5.0;net6.0;net48</TargetFrameworks>
Expand All @@ -22,7 +22,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.1" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions Library/Models/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private string ScriptBase() {
case "geography":
case "xml":
case "sysname":
case "geometry":
val.Append($" {IsNullableText}");
if (IncludeDefaultConstraint) val.Append(DefaultText);
if (Identity != null) val.Append(IdentityText);
Expand Down Expand Up @@ -161,6 +162,7 @@ internal static Type SqlTypeToNativeType(string sqlType) {
case "binary":
case "varbinary":
case "image":
case "geometry":
return typeof(byte[]);
default:
return typeof(string);
Expand Down
7 changes: 6 additions & 1 deletion Library/Models/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ public void ExportData(string conn, TextWriter data, string tableHint = null) {
using (var dr = cm.ExecuteReader()) {
while (dr.Read()) {
foreach (var c in cols) {
if (dr[c.Name] is DBNull)
var ordinal = dr.GetOrdinal(c.Name);
if (dr.IsDBNull(ordinal))
data.Write(_nullValue);
else if (dr.GetDataTypeName(ordinal).EndsWith(".sys.geometry"))
// https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ms143179(v=sql.110)?redirectedfrom=MSDN#sql-clr-data-types-geometry-geography-and-hierarchyid
data.Write(StringUtil.ToHexString(dr.GetSqlBytes(ordinal).Value));
else if (dr[c.Name] is byte[])
data.Write(StringUtil.ToHexString((byte[])dr[c.Name]));
else if (dr[c.Name] is DateTime)
Expand Down Expand Up @@ -311,6 +315,7 @@ public static object ConvertType(string sqlType, string val) {
case "binary":
case "varbinary":
case "image":
case "geometry":
return StringUtil.FromHexString(val);
default:
return val;
Expand Down
33 changes: 33 additions & 0 deletions Test/Integration/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,41 @@ public async Task TestScript() {
t.Columns.Add(new Column("bb", "varchar", -1, true, null));
t.Columns.Add(new Column("cc", "xml", true, null));
t.Columns.Add(new Column("dd", "hierarchyid", false, null));
t.Columns.Add(new Column("ee", "geometry", false, null));

await using var testDb = await _dbHelper.CreateTestDbAsync();
await testDb.ExecSqlAsync(t.ScriptCreate());
}

[Fact]
public async Task TestImportExportGeometryData() {
var t = new Table("dbo", "Shape");
t.Columns.Add(new Column("id", "int", false, null));
t.Columns.Add(new Column("shape", "geometry", false, null));
t.Columns.Find("id").Identity = new Identity(1, 1);
t.AddConstraint(new Constraint("PK_Shape", "PRIMARY KEY", "id"));

await using var testDb = await _dbHelper.CreateTestDbAsync();
await testDb.ExecSqlAsync(t.ScriptCreate());

var dataIn =
@"1 0000000001040300000000000000000059400000000000005940000000000000344000000000008066400000000000806640000000000080664001000000010000000001000000FFFFFFFF0000000002" + Environment.NewLine +
@"2 00000000010405000000000000000000000000000000000000000000000000C0624000000000000000000000000000C062400000000000C0624000000000000000000000000000C062400000000000000000000000000000000001000000020000000001000000FFFFFFFF0000000003" + Environment.NewLine;

var filename = Path.GetTempFileName();

var writer = File.AppendText(filename);
writer.Write(dataIn);
writer.Flush();
writer.Close();

try {
t.ImportData(testDb.GetConnString(), filename);
var sw = new StringWriter();
t.ExportData(testDb.GetConnString(), sw);
Assert.Equal(dataIn, sw.ToString());
} finally {
File.Delete(filename);
}
}
}