From ebf104a8eb6b53df1aa2c0bfb3434603acfe0f18 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Thu, 5 Oct 2023 08:36:59 +0300 Subject: [PATCH] support import/export of geography data --- Library/Models/Column.cs | 1 + Library/Models/Table.cs | 4 +++- Test/Integration/TableTest.cs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Library/Models/Column.cs b/Library/Models/Column.cs index 5a5e1f5..e4c2e40 100644 --- a/Library/Models/Column.cs +++ b/Library/Models/Column.cs @@ -163,6 +163,7 @@ internal static Type SqlTypeToNativeType(string sqlType) { case "varbinary": case "image": case "geometry": + case "geography": return typeof(byte[]); default: return typeof(string); diff --git a/Library/Models/Table.cs b/Library/Models/Table.cs index 51c3d79..9c288d3 100644 --- a/Library/Models/Table.cs +++ b/Library/Models/Table.cs @@ -165,7 +165,8 @@ public void ExportData(string conn, TextWriter data, string tableHint = null) { var ordinal = dr.GetOrdinal(c.Name); if (dr.IsDBNull(ordinal)) data.Write(_nullValue); - else if (dr.GetDataTypeName(ordinal).EndsWith(".sys.geometry")) + else if (dr.GetDataTypeName(ordinal).EndsWith(".sys.geometry") + || dr.GetDataTypeName(ordinal).EndsWith(".sys.geography")) // 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[]) @@ -316,6 +317,7 @@ public static object ConvertType(string sqlType, string val) { case "varbinary": case "image": case "geometry": + case "geography": return StringUtil.FromHexString(val); default: return val; diff --git a/Test/Integration/TableTest.cs b/Test/Integration/TableTest.cs index f65dc67..f18c50e 100644 --- a/Test/Integration/TableTest.cs +++ b/Test/Integration/TableTest.cs @@ -247,6 +247,7 @@ public async Task TestScript() { 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)); + t.Columns.Add(new Column("ff", "geography", false, null)); await using var testDb = await _dbHelper.CreateTestDbAsync(); await testDb.ExecSqlAsync(t.ScriptCreate()); @@ -283,4 +284,35 @@ public async Task TestImportExportGeometryData() { File.Delete(filename); } } + + [Fact] + public async Task TestImportExportGeographyData() { + var t = new Table("dbo", "PointsOfInterest"); + t.Columns.Add(new Column("id", "int", false, null)); + t.Columns.Add(new Column("location", "geography", false, null)); + t.Columns.Find("id").Identity = new Identity(1, 1); + t.AddConstraint(new Constraint("PK_PointsOfInterest", "PRIMARY KEY", "id")); + + await using var testDb = await _dbHelper.CreateTestDbAsync(); + await testDb.ExecSqlAsync(t.ScriptCreate()); + + var dataIn = + @"1 E610000001148716D9CEF7D34740D7A3703D0A975EC08716D9CEF7D34740CBA145B6F3955EC0" + 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); + } + } }