Skip to content

Commit

Permalink
support import/export of geography data
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-hall committed Oct 5, 2023
1 parent 5904887 commit ebf104a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions Library/Models/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion Library/Models/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions Test/Integration/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit ebf104a

Please sign in to comment.