/
GeoDBDialect.java
367 lines (337 loc) · 11.8 KB
/
GeoDBDialect.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for spatial (geographic) data.
*
* Copyright © 2007-2012 Geovise BVBA, Geodan IT b.v.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.hibernate.spatial.dialect.h2geodb;
import org.hibernate.HibernateException;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.spatial.GeometrySqlTypeDescriptor;
import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* Extends the H2Dialect by also including information on spatial functions.
*
* @author Jan Boonen, Geodan IT b.v.
*/
public class GeoDBDialect extends H2Dialect implements SpatialDialect {
/*
Contents of GeoDB's spatial registration script (geodb.sql):
CREATE ALIAS AddGeometryColumn for "geodb.GeoDB.AddGeometryColumn"
CREATE ALIAS CreateSpatialIndex for "geodb.GeoDB.CreateSpatialIndex"
CREATE ALIAS DropGeometryColumn for "geodb.GeoDB.DropGeometryColumn"
CREATE ALIAS DropGeometryColumns for "geodb.GeoDB.DropGeometryColumns"
CREATE ALIAS DropSpatialIndex for "geodb.GeoDB.DropSpatialIndex"
CREATE ALIAS EnvelopeAsText for "geodb.GeoDB.EnvelopeAsText"
CREATE ALIAS GeometryType for "geodb.GeoDB.GeometryType"
CREATE ALIAS ST_Area FOR "geodb.GeoDB.ST_Area"
CREATE ALIAS ST_AsEWKB FOR "geodb.GeoDB.ST_AsEWKB"
CREATE ALIAS ST_AsEWKT FOR "geodb.GeoDB.ST_AsEWKT"
CREATE ALIAS ST_AsHexEWKB FOR "geodb.GeoDB.ST_AsHexEWKB"
CREATE ALIAS ST_AsText FOR "geodb.GeoDB.ST_AsText"
CREATE ALIAS ST_BBOX FOR "geodb.GeoDB.ST_BBox"
CREATE ALIAS ST_Buffer FOR "geodb.GeoDB.ST_Buffer"
CREATE ALIAS ST_Centroid FOR "geodb.GeoDB.ST_Centroid"
CREATE ALIAS ST_Crosses FOR "geodb.GeoDB.ST_Crosses"
CREATE ALIAS ST_Contains FOR "geodb.GeoDB.ST_Contains"
CREATE ALIAS ST_DWithin FOR "geodb.GeoDB.ST_DWithin"
CREATE ALIAS ST_Disjoint FOR "geodb.GeoDB.ST_Disjoint"
CREATE ALIAS ST_Distance FOR "geodb.GeoDB.ST_Distance"
CREATE ALIAS ST_Envelope FOR "geodb.GeoDB.ST_Envelope"
CREATE ALIAS ST_Equals FOR "geodb.GeoDB.ST_Equals"
CREATE ALIAS ST_GeoHash FOR "geodb.GeoDB.ST_GeoHash"
CREATE ALIAS ST_GeomFromEWKB FOR "geodb.GeoDB.ST_GeomFromEWKB"
CREATE ALIAS ST_GeomFromEWKT FOR "geodb.GeoDB.ST_GeomFromEWKT"
CREATE ALIAS ST_GeomFromText FOR "geodb.GeoDB.ST_GeomFromText"
CREATE ALIAS ST_GeomFromWKB FOR "geodb.GeoDB.ST_GeomFromWKB"
CREATE ALIAS ST_Intersects FOR "geodb.GeoDB.ST_Intersects"
CREATE ALIAS ST_IsEmpty FOR "geodb.GeoDB.ST_IsEmpty"
CREATE ALIAS ST_IsSimple FOR "geodb.GeoDB.ST_IsSimple"
CREATE ALIAS ST_IsValid FOR "geodb.GeoDB.ST_IsValid"
CREATE ALIAS ST_MakePoint FOR "geodb.GeoDB.ST_MakePoint"
CREATE ALIAS ST_MakeBox2D FOR "geodb.GeoDB.ST_MakeBox2D"
CREATE ALIAS ST_Overlaps FOR "geodb.GeoDB.ST_Overlaps"
CREATE ALIAS ST_SRID FOR "geodb.GeoDB.ST_SRID"
CREATE ALIAS ST_SetSRID FOR "geodb.GeoDB.ST_SetSRID"
CREATE ALIAS ST_Simplify FOR "geodb.GeoDB.ST_Simplify"
CREATE ALIAS ST_Touches FOR "geodb.GeoDB.ST_Touches"
CREATE ALIAS ST_Within FOR "geodb.GeoDB.ST_Within"
CREATE ALIAS Version FOR "geodb.GeoDB.Version"
*/
/**
* Constructor. Registers OGC simple feature functions (see
* http://portal.opengeospatial.org/files/?artifact_id=829 for details).
* <p/>
* Note for the registerfunction method: it registers non-standard database
* functions: first argument is the internal (OGC standard) function name,
* second the name as it occurs in the spatial dialect
*/
public GeoDBDialect() {
super();
// Register Geometry column type
registerColumnType(
GeoDBGeometryTypeDescriptor.INSTANCE.getSqlType(),
GeoDBGeometryTypeDescriptor.INSTANCE.getTypeName()
);
// Register functions that operate on spatial types
// NOT YET AVAILABLE IN GEODB
// registerFunction("dimension", new StandardSQLFunction("dimension",
// Hibernate.INTEGER));
registerFunction(
"geometrytype", new StandardSQLFunction(
"GeometryType", StandardBasicTypes.STRING
)
);
registerFunction(
"srid", new StandardSQLFunction(
"ST_SRID",
StandardBasicTypes.INTEGER
)
);
registerFunction(
"envelope", new StandardSQLFunction(
"ST_Envelope"
)
);
registerFunction(
"astext", new StandardSQLFunction(
"ST_AsText",
StandardBasicTypes.STRING
)
);
registerFunction(
"asbinary", new StandardSQLFunction(
"ST_AsEWKB",
StandardBasicTypes.BINARY
)
);
registerFunction(
"isempty", new StandardSQLFunction(
"ST_IsEmpty",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"issimple", new StandardSQLFunction(
"ST_IsSimple",
StandardBasicTypes.BOOLEAN
)
);
// NOT YET AVAILABLE IN GEODB
// registerFunction("boundary", new StandardSQLFunction("boundary",
// new CustomType(GeoDBGeometryUserType.class, null)));
// Register functions for spatial relation constructs
registerFunction(
"overlaps", new StandardSQLFunction(
"ST_Overlaps",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"intersects", new StandardSQLFunction(
"ST_Intersects",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"equals", new StandardSQLFunction(
"ST_Equals",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"contains", new StandardSQLFunction(
"ST_Contains",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"crosses", new StandardSQLFunction(
"ST_Crosses",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"disjoint", new StandardSQLFunction(
"ST_Disjoint",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"touches", new StandardSQLFunction(
"ST_Touches",
StandardBasicTypes.BOOLEAN
)
);
registerFunction(
"within", new StandardSQLFunction(
"ST_Within",
StandardBasicTypes.BOOLEAN
)
);
// NOT YET AVAILABLE IN GEODB
// registerFunction("relate", new StandardSQLFunction("relate",
// Hibernate.BOOLEAN));
// register the spatial analysis functions
registerFunction(
"distance", new StandardSQLFunction(
"ST_Distance",
StandardBasicTypes.DOUBLE
)
);
registerFunction(
"buffer", new StandardSQLFunction(
"ST_Buffer"
)
);
// NOT YET AVAILABLE IN GEODB
// registerFunction("convexhull", new StandardSQLFunction("convexhull",
// new CustomType(GeoDBGeometryUserType.class, null)));
// registerFunction("difference", new StandardSQLFunction("difference",
// new CustomType(GeoDBGeometryUserType.class, null)));
// registerFunction("intersection", new StandardSQLFunction(
// "intersection", new CustomType(GeoDBGeometryUserType.class, null)));
// registerFunction("symdifference",
// new StandardSQLFunction("symdifference", new CustomType(
// GeoDBGeometryUserType.class, null)));
// registerFunction("geomunion", new StandardSQLFunction("geomunion",
// new CustomType(GeoDBGeometryUserType.class, null)));
//register Spatial Aggregate funciton
// NOT YET AVAILABLE IN GEODB
// registerFunction("extent", new StandardSQLFunction("extent",
// new CustomType(GeoDBGeometryUserType.class, null)));
registerFunction(
"dwithin", new StandardSQLFunction(
"ST_DWithin",
StandardBasicTypes.BOOLEAN
)
);
}
//TODO the getTypeName() override is necessary in the absence of HHH-6074
/**
* Get the name of the database type associated with the given
* {@link java.sql.Types} typecode with the given storage specification
* parameters. In the case of typecode == 3000, it returns this dialect's spatial type which is
* <code>GEOMETRY</code>.
*
* @param code The {@link java.sql.Types} typecode
* @param length The datatype length
* @param precision The datatype precision
* @param scale The datatype scale
*
* @return
*
* @throws org.hibernate.HibernateException
*/
@Override
public String getTypeName(int code, long length, int precision, int scale) throws HibernateException {
if ( code == 3000 ) {
return GeoDBGeometryTypeDescriptor.INSTANCE.getTypeName();
}
return super.getTypeName( code, length, precision, scale );
}
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
if ( sqlTypeDescriptor instanceof GeometrySqlTypeDescriptor ) {
return GeoDBGeometryTypeDescriptor.INSTANCE;
}
return super.remapSqlTypeDescriptor( sqlTypeDescriptor );
}
public String getSpatialAggregateSQL(String columnName, int aggregation) {
switch ( aggregation ) {
// NOT YET AVAILABLE IN GEODB
// case SpatialAggregate.EXTENT:
// StringBuilder stbuf = new StringBuilder();
// stbuf.append("extent(").append(columnName).append(")");
// return stbuf.toString();
default:
throw new IllegalArgumentException(
"Aggregations of type "
+ aggregation + " are not supported by this dialect"
);
}
}
public String getDWithinSQL(String columnName) {
return "ST_DWithin(" + columnName + ",?,?)";
}
public String getHavingSridSQL(String columnName) {
return "( ST_srid(" + columnName + ") = ?)";
}
public String getIsEmptySQL(String columnName, boolean isEmpty) {
String emptyExpr = " ST_IsEmpty(" + columnName + ") ";
return isEmpty ? emptyExpr : "( NOT " + emptyExpr + ")";
}
/* (non-Javadoc)
* @see org.hibernatespatial.SpatialDialect#getSpatialFilterExpression(java.lang.String)
*/
public String getSpatialFilterExpression(String columnName) {
return "(" + columnName + " && ? ) ";
}
/* (non-Javadoc)
* @see org.hibernatespatial.SpatialDialect#getSpatialRelateSQL(java.lang.String, int, boolean)
*/
public String getSpatialRelateSQL(String columnName, int spatialRelation) {
switch ( spatialRelation ) {
case SpatialRelation.WITHIN:
return " ST_Within(" + columnName + ", ?)";
case SpatialRelation.CONTAINS:
return " ST_Contains(" + columnName + ", ?)";
case SpatialRelation.CROSSES:
return " ST_Crosses(" + columnName + ", ?)";
case SpatialRelation.OVERLAPS:
return " ST_Overlaps(" + columnName + ", ?)";
case SpatialRelation.DISJOINT:
return " ST_Disjoint(" + columnName + ", ?)";
case SpatialRelation.INTERSECTS:
return " ST_Intersects(" + columnName + ", ?)";
case SpatialRelation.TOUCHES:
return " ST_Touches(" + columnName + ", ?)";
case SpatialRelation.EQUALS:
return " ST_Equals(" + columnName + ", ?)";
default:
throw new IllegalArgumentException(
"Spatial relation is not known by this dialect"
);
}
}
/* (non-Javadoc)
* @see org.hibernatespatial.SpatialDialect#getDbGeometryTypeName()
*/
public String getDbGeometryTypeName() {
return "GEOM";
}
/* (non-Javadoc)
* @see org.hibernatespatial.SpatialDialect#isTwoPhaseFiltering()
*/
public boolean isTwoPhaseFiltering() {
return false;
}
public boolean supportsFiltering() {
return false;
}
public boolean supports(SpatialFunction function) {
if ( function == SpatialFunction.difference ) {
return false;
}
return ( getFunctions().get( function.toString() ) != null );
}
}