forked from nhibernate/NHibernate.Spatial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMySQL80SpatialDialect.cs
148 lines (133 loc) · 5.63 KB
/
MySQL80SpatialDialect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using NHibernate.Spatial.Metadata;
using NHibernate.SqlCommand;
using System;
using System.Data;
using System.Text;
namespace NHibernate.Spatial.Dialect
{
public class MySQL80SpatialDialect : MySQL57SpatialDialect
{
public MySQL80SpatialDialect()
{
// See: https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Dialect/MySQL8Dialect.cs#L7C48-L7C73
RegisterColumnType(DbType.Boolean, "BOOLEAN");
}
/// <summary>
/// Gets the spatial transform string.
/// </summary>
/// <param name="geometry">The geometry.</param>
/// <param name="srid">The srid.</param>
/// <returns></returns>
public override SqlString GetSpatialTransformString(object geometry, int srid)
{
return new SqlStringBuilder()
.Add(DialectPrefix)
.Add("Transform(")
.AddObject(geometry)
.Add(",")
.Add(srid.ToString())
.Add(")")
.ToSqlString();
}
/// <summary>
/// Gets the spatial validation string.
/// </summary>
/// <param name="geometry">The geometry.</param>
/// <param name="validation">The validation.</param>
/// <param name="criterion">if set to <c>true</c> [criterion].</param>
/// <returns></returns>
public override SqlString GetSpatialValidationString(object geometry, SpatialValidation validation, bool criterion)
{
return new SqlStringBuilder()
.Add(DialectPrefix)
.Add(validation.ToString())
.Add("(")
.AddObject(geometry)
.Add(")")
.ToSqlString();
}
/// <summary>
/// Gets the spatial aggregate string.
/// </summary>
/// <param name="geometry">The geometry.</param>
/// <param name="aggregate">The aggregate.</param>
/// <returns></returns>
public override SqlString GetSpatialAggregateString(object geometry, SpatialAggregate aggregate)
{
switch (aggregate)
{
case SpatialAggregate.Collect:
return new SqlStringBuilder()
.Add(DialectPrefix)
.Add(aggregate.ToString())
.Add("(")
.AddObject(geometry)
.Add(")")
.ToSqlString();
case SpatialAggregate.ConvexHull:
case SpatialAggregate.Envelope:
// MySQL only directly supports the ST_Collect spatial aggregate function, therefore
// we mimic these spatial agg functions by grouping the geometries from each row into
// a geometry collection and then performing the function on the geometry collection
// See: https://forums.mysql.com/read.php?23,249284,249284#msg-249284
var collectAggregate = GetSpatialAggregateString(geometry, SpatialAggregate.Collect);
return new SqlStringBuilder()
.Add(DialectPrefix)
.Add(aggregate.ToString())
.Add("(")
.Add(collectAggregate)
.Add(")")
.ToSqlString();
case SpatialAggregate.Intersection:
case SpatialAggregate.Union:
throw new NotSupportedException($"MySQL does not support {aggregate} spatial aggregate function");
default:
throw new ArgumentException("Invalid spatial aggregate argument");
}
}
/// <summary>
/// Gets the spatial create string.
/// </summary>
/// <param name="schema">The schema.</param>
/// <param name="table">The table.</param>
/// <param name="column">The column.</param>
/// <param name="srid">The srid.</param>
/// <param name="subtype">The subtype.</param>
/// <param name="dimension">The dimension.</param>
/// <param name="isNullable">Whether or not the column is nullable</param>
/// <returns></returns>
public override string GetSpatialCreateString(string schema, string table, string column, int srid, string subtype, int dimension, bool isNullable)
{
var builder = new StringBuilder();
string quotedSchema = QuoteSchema(schema);
string quoteForTableName = QuoteForTableName(table);
string quoteForColumnName = QuoteForColumnName(column);
builder.AppendFormat("ALTER TABLE {0}{1} DROP COLUMN {2}"
, quotedSchema
, quoteForTableName
, quoteForColumnName
);
builder.Append(MultipleQueriesSeparator);
builder.AppendFormat("ALTER TABLE {0}{1} ADD {2} {3} {4} SRID {5}"
, quotedSchema
, quoteForTableName
, quoteForColumnName
, subtype
, isNullable ? "NULL" : "NOT NULL"
, srid
);
builder.Append(MultipleQueriesSeparator);
return builder.ToString();
}
/// <summary>
/// Gets a value indicating whether it supports spatial metadata.
/// </summary>
/// <value>
/// <c>true</c> if it supports spatial metadata; otherwise, <c>false</c>.
/// </value>
public override bool SupportsSpatialMetadata(MetadataClass metadataClass)
{
return true;
}
}
}