Revision 213 trunk/org.gvsig.mssqlserver/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerSQLBuilder.java
MSSQLServerSQLBuilder.java | ||
---|---|---|
3 | 3 |
import java.text.MessageFormat; |
4 | 4 |
import java.util.ArrayList; |
5 | 5 |
import java.util.List; |
6 |
import org.apache.commons.lang3.StringUtils; |
|
6 | 7 |
import org.cresques.cts.IProjection; |
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
7 | 10 |
import org.gvsig.fmap.dal.DataTypes; |
8 | 11 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
9 | 12 |
import org.gvsig.fmap.geom.Geometry; |
10 | 13 |
import org.gvsig.fmap.geom.primitive.Envelope; |
14 |
import org.gvsig.mssqlserver.dal.expressionbuilderformatter.MSSQLServerFormatter; |
|
11 | 15 |
|
12 | 16 |
public class MSSQLServerSQLBuilder extends JDBCSQLBuilderBase { |
13 |
|
|
14 |
public interface MSSQLServerSQLConfig extends SQLConfig { |
|
15 |
public static final String ST_GeomFromTextEx = "ST_GeomFromTextEx"; |
|
16 |
public static final String ST_GeomFromWKBEx = "ST_GeomFromWKBEx"; |
|
17 |
public static final String ST_GeomFromEWKBEx = "ST_GeomFromEWKBEx"; |
|
18 |
public static final String ST_ExtentAggregateEx = "ST_ExtentAggregateEx"; |
|
19 |
public static final String ST_UnionAggregateEx = "ST_UnionAggregateEx"; |
|
20 |
} |
|
17 |
|
|
18 |
protected Formatter formatter = null; |
|
19 |
public static final String FORMAT_ST_GEOMFROMEWKB= "geometry::STGeomFromWKB({0}, {1})"; |
|
20 |
public static final String FORMAT_ST_GEOMFROMWKB= "geometry::STGeomFromWKB({0}, {1})"; |
|
21 |
public static final String FORMAT_ST_GEOMFROMTEXT= "geometry::STGeomFromText({0}, {1})"; |
|
22 |
|
|
23 |
// public interface MSSQLServerSQLConfig extends SQLConfig { |
|
24 |
// public static final String ST_GeomFromTextEx = "ST_GeomFromTextEx"; |
|
25 |
// public static final String ST_GeomFromWKBEx = "ST_GeomFromWKBEx"; |
|
26 |
// public static final String ST_GeomFromEWKBEx = "ST_GeomFromEWKBEx"; |
|
27 |
// public static final String ST_ExtentAggregateEx = "ST_ExtentAggregateEx"; |
|
28 |
// public static final String ST_UnionAggregateEx = "ST_UnionAggregateEx"; |
|
29 |
// } |
|
21 | 30 |
|
22 | 31 |
public MSSQLServerSQLBuilder(MSSQLServerHelper helper) { |
23 | 32 |
super(helper); |
24 | 33 |
|
25 |
config.set(SQLConfig.default_schema, "dbo");
|
|
26 |
config.set(SQLConfig.allowAutomaticValues, true);
|
|
27 |
config.set(SQLConfig.geometry_type_support, helper.getGeometrySupportType());
|
|
28 |
config.set(SQLConfig.has_spatial_functions, helper.hasSpatialFunctions());
|
|
34 |
this.defaultSchema = "dbo";
|
|
35 |
this.allowAutomaticValues = true;
|
|
36 |
this.geometrySupportType = this.helper.getGeometrySupportType();
|
|
37 |
this.hasSpatialFunctions = this.helper.hasSpatialFunctions();
|
|
29 | 38 |
|
30 |
config.remove_functionality(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table);
|
|
31 |
config.remove_functionality(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_table);
|
|
39 |
STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null;
|
|
40 |
STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null;
|
|
32 | 41 |
|
33 |
config.set(SQLConfig.UPDATE_TABLE_STATISTICS_table,"UPDATE STATISTICS {0}");
|
|
42 |
this.STMT_UPDATE_TABLE_STATISTICS_table ="UPDATE STATISTICS {0}";
|
|
34 | 43 |
|
35 |
config.set(SQLConfig.type_geometry, "GEOMETRY"); |
|
44 |
this.type_geometry = "GEOMETRY"; |
|
45 |
this.constant_true = "1=1"; // TODO ? |
|
46 |
this.constant_false = "1<>1"; // TODO ? |
|
47 |
|
|
48 |
this.type_boolean = "BIT"; |
|
49 |
this.type_double = "FLOAT"; //float con 53 bits de mantisa = float(54) |
|
50 |
this.type_numeric_p = "NUMERIC({0})"; |
|
51 |
this.type_numeric_ps = "NUMERIC({0},{1})"; |
|
52 |
this.type_bigdecimal = "NUMERIC({0},{1})"; |
|
53 |
this.type_float = "REAL"; //float con 24 bits de mantisa = float(24) |
|
54 |
this.type_int = "INT"; |
|
55 |
this.type_long = "BIGINT"; |
|
56 |
this.type_byte = "TINYINT"; |
|
36 | 57 |
|
37 |
config.set(SQLConfig.type_boolean, "BIT"); |
|
38 |
config.set(SQLConfig.type_double, "FLOAT"); //float con 53 bits de mantisa, float(54) |
|
39 |
config.set(SQLConfig.type_numeric_p, "NUMERIC({0})"); |
|
40 |
config.set(SQLConfig.type_numeric_ps, "NUMERIC({0},{1})"); |
|
41 |
config.set(SQLConfig.type_bigdecimal, "NUMERIC({0},{1})"); |
|
42 |
config.set(SQLConfig.type_float, "REAL"); //float con 24 bits de mantisa, float(24) |
|
43 |
config.set(SQLConfig.type_int, "INT"); |
|
44 |
config.set(SQLConfig.type_long, "BIGINT"); |
|
45 |
config.set(SQLConfig.type_byte, "TINYINT"); |
|
46 |
|
|
47 |
config.set(SQLConfig.type_date, "DATE"); |
|
48 |
config.set(SQLConfig.type_time, "TIME"); |
|
58 |
this.type_date = "DATE"; |
|
59 |
this.type_time = "TIME"; |
|
49 | 60 |
|
50 |
config.set(SQLConfig.type_char, "CHAR(1)");
|
|
51 |
config.set(SQLConfig.type_string, "TEXT");
|
|
52 |
config.set(SQLConfig.type_string_p, "VARCHAR({0})");
|
|
61 |
this.type_char = "CHAR(1)";
|
|
62 |
this.type_string = "TEXT";
|
|
63 |
this.type_string_p = "VARCHAR({0})";
|
|
53 | 64 |
|
54 |
config.set(SQLConfig.type_version, "VARCHAR(30)");
|
|
55 |
config.set(SQLConfig.type_URI, "TEXT");
|
|
56 |
config.set(SQLConfig.type_URL, "TEXT");
|
|
57 |
config.set(SQLConfig.type_FILE, "TEXT");
|
|
58 |
config.set(SQLConfig.type_FOLDER, "TEXT");
|
|
65 |
this.type_version = "VARCHAR(30)";
|
|
66 |
this.type_URI = "TEXT";
|
|
67 |
this.type_URL = "TEXT";
|
|
68 |
this.type_FILE = "TEXT";
|
|
69 |
this.type_FOLDER = "TEXT";
|
|
59 | 70 |
|
60 |
config.set(SQLConfig.type_bytearray, "VARBINARY");
|
|
71 |
this.type_bytearray = "VARBINARY";
|
|
61 | 72 |
|
62 |
config.set(SQLConfig.type_timestamp, "TIMESTAMP");
|
|
73 |
this.type_timestamp = "TIMESTAMP";
|
|
63 | 74 |
|
64 |
config.set(SQLConfig.ST_SRID, "(({0}).STSrid)"); |
|
65 |
config.set(SQLConfig.ST_AsText, "({0}).STAsText()"); |
|
66 |
config.set(SQLConfig.ST_AsBinary, "({0}).STAsBinary()"); |
|
67 |
config.set(SQLConfig.ST_AsEWKB, "({0}).STAsBinary()"); |
|
68 |
config.set(SQLConfig.ST_ExtentAggregate, "geometry::EnvelopeAggregate({0})"); |
|
69 |
config.set(SQLConfig.ST_UnionAggregate, "geometry::UnionAggregate({0})"); |
|
70 |
config.set(SQLConfig.ST_Contains, "(({0}).STContains({1})=1)"); |
|
71 |
config.set(SQLConfig.ST_Crosses, "(({0}).STCrosses({1})=1)"); |
|
72 |
config.set(SQLConfig.ST_Disjoint, "(({0}).STDisjoint({1})=1)"); |
|
73 |
config.set(SQLConfig.ST_Equals, "(({0}).STEquals({1})=1)"); |
|
74 |
config.set(SQLConfig.ST_IsClosed, "(({0}).STIsClosed()=1)"); |
|
75 |
config.set(SQLConfig.ST_Overlaps, "(({0}).STOverlaps({1})=1)"); |
|
76 |
config.set(SQLConfig.ST_Touches, "(({0}).STTouches({1})=1)"); |
|
77 |
config.set(SQLConfig.ST_Within, "(({0}).STWithin ({1})=1)"); |
|
78 |
config.set(SQLConfig.ST_Envelope, "({0}).STEnvelope()"); |
|
79 |
config.set(SQLConfig.ST_Intersects, "(({0}).STIntersects({1})=1)"); |
|
80 |
config.set(SQLConfig.ST_GeomFromText, "geometry::STGeomFromText({0}, {1})"); |
|
81 |
config.set(SQLConfig.ST_GeomFromWKB, "geometry::STGeomFromWKB({0}, {1})"); |
|
82 |
config.set(SQLConfig.ST_GeomFromEWKB, "geometry::STGeomFromWKB({0}, {1})"); |
|
83 |
config.set(SQLConfig.ST_Simplify, "({0}).Reduce({1})"); |
|
84 |
config.set(SQLConfig.lcase, "LOWER({0})"); |
|
85 |
config.set(SQLConfig.ucase, "UPPER({0})"); |
|
86 |
config.set(SQLConfig.operator_ILIKE, "LOWER({0}) LIKE LOWER({1})"); |
|
87 |
config.set(SQLConfig.notIsNull,"( ({0}) IS NOT NULL )" ); |
|
75 |
// DID this.ST_SRID, "(({0}).STSrid)"); |
|
76 |
// DID this.ST_AsText, "({0}).STAsText()"); |
|
77 |
// DID this.ST_AsBinary, "({0}).STAsBinary()"); |
|
78 |
// this.ST_AsEWKB, "({0}).STAsBinary()"); |
|
79 |
// DID this.ST_ExtentAggregate, "geometry::EnvelopeAggregate({0})"); |
|
80 |
// DID this.ST_UnionAggregate, "geometry::UnionAggregate({0})"); |
|
81 |
// DID this.ST_Contains, "(({0}).STContains({1})=1)"); |
|
82 |
// DID this.ST_Crosses, "(({0}).STCrosses({1})=1)"); |
|
83 |
// DID this.ST_Disjoint, "(({0}).STDisjoint({1})=1)"); |
|
84 |
// DID this.ST_Equals, "(({0}).STEquals({1})=1)"); |
|
85 |
// DID this.ST_IsClosed, "(({0}).STIsClosed()=1)"); |
|
86 |
// DID this.ST_Overlaps, "(({0}).STOverlaps({1})=1)"); |
|
87 |
// DID this.ST_Touches, "(({0}).STTouches({1})=1)"); |
|
88 |
// DID this.ST_Within, "(({0}).STWithin ({1})=1)"); |
|
89 |
// DID this.ST_Envelope, "({0}).STEnvelope()"); |
|
90 |
// DID this.ST_Intersects, "(({0}).STIntersects({1})=1)"); |
|
91 |
// DID this.ST_GeomFromText, "geometry::STGeomFromText({0}, {1})"); |
|
92 |
// DID this.ST_GeomFromWKB, "geometry::STGeomFromWKB({0}, {1})"); |
|
93 |
// DID this.ST_GeomFromEWKB, "geometry::STGeomFromWKB({0}, {1})"); |
|
94 |
// this.ST_Simplify, "({0}).Reduce({1})"); |
|
95 |
// this.lcase, "LOWER({0})"); |
|
96 |
// this.ucase, "UPPER({0})"); |
|
97 |
// this.operator_ILIKE, "LOWER({0}) LIKE LOWER({1})"); |
|
98 |
// this.notIsNull,"( ({0}) IS NOT NULL )" ); |
|
99 |
// |
|
100 |
// config.set(MSSQLServerSQLConfig.ST_ExtentAggregateEx, "{1}::EnvelopeAggregate({0})"); |
|
101 |
// config.set(MSSQLServerSQLConfig.ST_UnionAggregateEx, "{1}::UnionAggregate({0})"); |
|
102 |
// config.set(MSSQLServerSQLConfig.ST_GeomFromTextEx, "{2}::STGeomFromText({0}, {1})"); |
|
103 |
// config.set(MSSQLServerSQLConfig.ST_GeomFromWKBEx, "{2}::STGeomFromWKB({0}, {1})"); |
|
104 |
// config.set(MSSQLServerSQLConfig.ST_GeomFromEWKBEx, "{2}::STGeomFromWKB({0}, {1})"); |
|
88 | 105 |
|
89 |
config.set(MSSQLServerSQLConfig.ST_ExtentAggregateEx, "{1}::EnvelopeAggregate({0})"); |
|
90 |
config.set(MSSQLServerSQLConfig.ST_UnionAggregateEx, "{1}::UnionAggregate({0})"); |
|
91 |
config.set(MSSQLServerSQLConfig.ST_GeomFromTextEx, "{2}::STGeomFromText({0}, {1})"); |
|
92 |
config.set(MSSQLServerSQLConfig.ST_GeomFromWKBEx, "{2}::STGeomFromWKB({0}, {1})"); |
|
93 |
config.set(MSSQLServerSQLConfig.ST_GeomFromEWKBEx, "{2}::STGeomFromWKB({0}, {1})"); |
|
94 |
|
|
95 | 106 |
} |
96 | 107 |
|
97 | 108 |
@Override |
98 |
public MSSQLServerSQLConfig getConfig() { |
|
99 |
return (MSSQLServerSQLConfig) super.config; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 | 109 |
public MSSQLServerHelper getHelper() { |
104 | 110 |
return (MSSQLServerHelper) helper; |
105 | 111 |
} |
112 |
|
|
113 |
@Override |
|
114 |
protected Formatter formatter() { |
|
115 |
if( this.formatter==null ) { |
|
116 |
this.formatter = new MSSQLServerFormatter(this); |
|
117 |
} |
|
118 |
return this.formatter; |
|
119 |
} |
|
120 |
|
|
121 |
protected class MSSQLServerDropTableBuilder extends DropTableBuilderBase { |
|
106 | 122 |
|
123 |
@Override |
|
124 |
public List<String> toStrings(Formatter formatter) { |
|
125 |
List<String> sqls = new ArrayList<>(); |
|
126 |
|
|
127 |
sqls.add( |
|
128 |
MessageFormat.format( |
|
129 |
STMT_DROP_TABLE_table, |
|
130 |
this.table.toString(formatter) |
|
131 |
) |
|
132 |
); |
|
133 |
return sqls; |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
107 | 137 |
public class MSSQLServerCreateIndexBuilder extends CreateIndexBuilderBase { |
108 | 138 |
|
109 | 139 |
private Envelope boundingBox; |
... | ... | |
139 | 169 |
StringBuilder builder = new StringBuilder(); |
140 | 170 |
if( this.isSpatial ) { |
141 | 171 |
builder.append("CREATE SPATIAL INDEX "); |
142 |
builder.append(identifier(this.indexName)); |
|
172 |
builder.append(as_identifier(this.indexName));
|
|
143 | 173 |
builder.append(" ON "); |
144 | 174 |
builder.append(this.table.toString()); |
145 | 175 |
builder.append(" ( "); |
... | ... | |
177 | 207 |
// if( this.ifNotExist ) { |
178 | 208 |
// builder.append("IF NOT EXISTS "); |
179 | 209 |
// } |
180 |
builder.append(identifier(this.indexName)); |
|
210 |
builder.append(as_identifier(this.indexName));
|
|
181 | 211 |
builder.append(" ON "); |
182 | 212 |
builder.append(this.table.toString()); |
183 | 213 |
builder.append(" ( "); |
... | ... | |
199 | 229 |
|
200 | 230 |
} |
201 | 231 |
|
202 |
public class MSSQLServerParameter extends ParameterBase { |
|
232 |
// public class MSSQLServerParameter extends ParameterBase { |
|
233 |
// |
|
234 |
// public MSSQLServerParameter() { |
|
235 |
// super(); |
|
236 |
// } |
|
237 |
// |
|
238 |
// @Override |
|
239 |
// public String toString() { |
|
240 |
// if( this.type == ParameterType.Geometry ) { |
|
241 |
// String spatialType = getHelper().getSpatialType(this.getName()); |
|
242 |
// switch( config.getGeometryTypeSupport() ) { |
|
243 |
// case EWKB: |
|
244 |
// return MessageFormat.format( |
|
245 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
246 |
// "?", |
|
247 |
// String.valueOf(this.srs.toString()), |
|
248 |
// custom(spatialType) |
|
249 |
// ); |
|
250 |
// case NATIVE: |
|
251 |
// case WKB: |
|
252 |
// return MessageFormat.format( |
|
253 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
254 |
// "?", |
|
255 |
// String.valueOf(this.srs.toString()), |
|
256 |
// custom(spatialType) |
|
257 |
// ); |
|
258 |
// case WKT: |
|
259 |
// default: |
|
260 |
// return MessageFormat.format( |
|
261 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
262 |
// "?", |
|
263 |
// String.valueOf(this.srs.toString()), |
|
264 |
// custom(spatialType) |
|
265 |
// ); |
|
266 |
// } |
|
267 |
// } |
|
268 |
// return super.toString(); |
|
269 |
// } |
|
270 |
// } |
|
271 |
// |
|
272 |
// public class MSSQLServerGeometryValue extends GeometryValueBase { |
|
273 |
// |
|
274 |
// public MSSQLServerGeometryValue(Geometry geometry, IProjection projection) { |
|
275 |
// super(geometry, projection); |
|
276 |
// } |
|
277 |
// |
|
278 |
// @Override |
|
279 |
// public String toString() { |
|
280 |
// try { |
|
281 |
// String spatialType = getHelper().getSpatialType(); |
|
282 |
// switch( config.getGeometryTypeSupport() ) { |
|
283 |
// case EWKB: |
|
284 |
// return MessageFormat.format( |
|
285 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
286 |
// bytearray(this.geometry.convertToEWKB()), |
|
287 |
// String.valueOf(getSRSId(this.projection)), |
|
288 |
// custom(spatialType) |
|
289 |
// ); |
|
290 |
// case NATIVE: |
|
291 |
// case WKB: |
|
292 |
// return MessageFormat.format( |
|
293 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
294 |
// bytearray(this.geometry.convertToWKB()), |
|
295 |
// String.valueOf(getSRSId(this.projection)), |
|
296 |
// custom(spatialType) |
|
297 |
// ); |
|
298 |
// case WKT: |
|
299 |
// default: |
|
300 |
// return MessageFormat.format( |
|
301 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
302 |
// string(this.geometry.convertToWKT()), |
|
303 |
// String.valueOf(getSRSId(this.projection)), |
|
304 |
// custom(spatialType) |
|
305 |
// ); |
|
306 |
// } |
|
307 |
// } catch (Exception ex) { |
|
308 |
// throw new RuntimeException("Can't convert geometry to string.",ex); |
|
309 |
// } |
|
310 |
// } |
|
311 |
// } |
|
203 | 312 |
|
204 |
public MSSQLServerParameter() { |
|
205 |
super(); |
|
206 |
} |
|
207 |
|
|
313 |
protected class MSSQLServerSelectBuilder extends SelectBuilderBase { |
|
314 |
|
|
208 | 315 |
@Override |
209 |
public String toString() { |
|
210 |
if( this.type == ParameterType.Geometry ) { |
|
211 |
String spatialType = getHelper().getSpatialType(this.getName()); |
|
212 |
switch( config.getGeometryTypeSupport() ) { |
|
213 |
case EWKB: |
|
214 |
return MessageFormat.format( |
|
215 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
216 |
"?", |
|
217 |
String.valueOf(this.srs.toString()), |
|
218 |
custom(spatialType) |
|
219 |
); |
|
220 |
case NATIVE: |
|
221 |
case WKB: |
|
222 |
return MessageFormat.format( |
|
223 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
224 |
"?", |
|
225 |
String.valueOf(this.srs.toString()), |
|
226 |
custom(spatialType) |
|
227 |
); |
|
228 |
case WKT: |
|
229 |
default: |
|
230 |
return MessageFormat.format( |
|
231 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
232 |
"?", |
|
233 |
String.valueOf(this.srs.toString()), |
|
234 |
custom(spatialType) |
|
235 |
); |
|
236 |
} |
|
316 |
protected boolean isValid(StringBuilder message) { |
|
317 |
if( message == null ) { |
|
318 |
message = new StringBuilder(); |
|
237 | 319 |
} |
238 |
return super.toString(); |
|
320 |
if( this.has_offset() && !this.has_order_by() ) { |
|
321 |
// Algunos gestores de BBDD requieren que se especifique un |
|
322 |
// orden para poder usar OFFSET. Como eso parece buena idea para |
|
323 |
// asegurar que siempre tengamos los mismo resultados, lo exijimos |
|
324 |
// siempre. |
|
325 |
message.append("Can't use OFFSET without an ORDER BY."); |
|
326 |
return false; |
|
327 |
} |
|
328 |
return true; |
|
239 | 329 |
} |
240 |
} |
|
241 |
|
|
242 |
public class MSSQLServerGeometryValue extends GeometryValueBase { |
|
243 | 330 |
|
244 |
public MSSQLServerGeometryValue(Geometry geometry, IProjection projection) { |
|
245 |
super(geometry, projection); |
|
246 |
} |
|
247 |
|
|
248 | 331 |
@Override |
249 |
public String toString() { |
|
250 |
try { |
|
251 |
String spatialType = getHelper().getSpatialType(); |
|
252 |
switch( config.getGeometryTypeSupport() ) { |
|
253 |
case EWKB: |
|
254 |
return MessageFormat.format( |
|
255 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
256 |
bytearray(this.geometry.convertToEWKB()), |
|
257 |
String.valueOf(getSRSId(this.projection)), |
|
258 |
custom(spatialType) |
|
259 |
); |
|
260 |
case NATIVE: |
|
261 |
case WKB: |
|
262 |
return MessageFormat.format( |
|
263 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
264 |
bytearray(this.geometry.convertToWKB()), |
|
265 |
String.valueOf(getSRSId(this.projection)), |
|
266 |
custom(spatialType) |
|
267 |
); |
|
268 |
case WKT: |
|
269 |
default: |
|
270 |
return MessageFormat.format( |
|
271 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
272 |
string(this.geometry.convertToWKT()), |
|
273 |
String.valueOf(getSRSId(this.projection)), |
|
274 |
custom(spatialType) |
|
275 |
); |
|
276 |
} |
|
277 |
} catch (Exception ex) { |
|
278 |
throw new RuntimeException("Can't convert geometry to string.",ex); |
|
279 |
} |
|
280 |
} |
|
281 |
} |
|
282 |
|
|
283 |
protected class MSSQLServerSelectBuilder extends SelectBuilderBase { |
|
284 |
|
|
285 |
@Override |
|
286 |
public String toString() { |
|
332 |
public String toString(Formatter<ExpressionBuilder.Value> formatter) { |
|
287 | 333 |
// MSSQLServer usa TOP en lugar de LIMIT y la sintaxis para OFFSET |
288 | 334 |
// es ligeramente distinta de la que hay en SelectBuilderBase |
289 | 335 |
StringBuilder builder = new StringBuilder(); |
... | ... | |
309 | 355 |
|
310 | 356 |
if (this.has_from()) { |
311 | 357 |
builder.append(" FROM "); |
312 |
builder.append(this.from.toString()); |
|
358 |
builder.append(this.from.toString(formatter));
|
|
313 | 359 |
} |
314 | 360 |
if (this.has_where()) { |
315 | 361 |
builder.append(" WHERE "); |
316 |
builder.append(this.where.toString()); |
|
362 |
builder.append(this.where.toString(formatter));
|
|
317 | 363 |
} |
318 | 364 |
|
319 | 365 |
if( this.has_order_by() ) { |
... | ... | |
347 | 393 |
protected class MSSQLServerCreateTableBuilder extends CreateTableBuilderBase { |
348 | 394 |
|
349 | 395 |
@Override |
350 |
public List<String> toStrings() { |
|
396 |
// List<String> toStrings(Formatter formatter) |
|
397 |
public List<String> toStrings(Formatter formatter) { |
|
351 | 398 |
// |
352 | 399 |
// Respecto al base cambia la declaracion de campo automaticos |
353 | 400 |
// SQLServer usa IDENTITY en lugar de SERIAL. |
... | ... | |
365 | 412 |
} else { |
366 | 413 |
builder.append(", "); |
367 | 414 |
} |
368 |
builder.append(identifier(column.getName())); |
|
415 |
builder.append(as_identifier(column.getName()));
|
|
369 | 416 |
builder.append(" "); |
370 | 417 |
if( column.isAutomatic() && column.getType() == DataTypes.INT ) { |
371 | 418 |
builder.append("INT"); |
... | ... | |
374 | 421 |
builder.append("BIGINT"); |
375 | 422 |
builder.append(" IDENTITY(1,1)"); |
376 | 423 |
} else { |
377 |
builder.append(sqltype(column.getType(), column.getPrecision(), column.getSize())); |
|
424 |
builder.append( |
|
425 |
sqltype( |
|
426 |
column.getType(), |
|
427 |
column.getPrecision(), |
|
428 |
column.getSize(), |
|
429 |
column.getGeometryType(), |
|
430 |
column.getGeometrySubtype() |
|
431 |
) |
|
432 |
); |
|
378 | 433 |
} |
379 | 434 |
if (column.getDefaultValue() == null) { |
380 | 435 |
if (column.allowNulls()) { |
... | ... | |
410 | 465 |
protected CreateTableBuilder createCreateTableBuilder() { |
411 | 466 |
return new MSSQLServerCreateTableBuilder(); |
412 | 467 |
} |
413 |
|
|
468 |
|
|
469 |
protected DropTableBuilder createDropTableBuilder() { |
|
470 |
return new MSSQLServerDropTableBuilder(); |
|
471 |
} |
|
472 |
|
|
414 | 473 |
@Override |
415 | 474 |
protected CreateIndexBuilder createCreateIndexBuilder() { |
416 | 475 |
return new MSSQLServerCreateIndexBuilder(); |
417 | 476 |
} |
418 |
|
|
477 |
|
|
419 | 478 |
@Override |
420 |
public String identifier(String id) { |
|
479 |
public String as_identifier(String id) {
|
|
421 | 480 |
// En SQLServer se aceptan las comillas dobles pero se prefiere |
422 | 481 |
// corchetes [xx]. Asi que si hay comillas dobles las quitamos |
423 | 482 |
// y ponemos los corchetes. |
424 |
String quote = config.getString(Config.quote_for_identifiers);
|
|
483 |
String quote = this.quote_for_identifiers();
|
|
425 | 484 |
if (id.startsWith(quote)) { |
426 | 485 |
id = id.substring(1, id.length()-1); |
427 | 486 |
} else if( id.startsWith("[") ) { |
428 | 487 |
return id; |
429 | 488 |
} |
430 | 489 |
if( id.contains("(") ) { |
431 |
logger.warn("Suspicious use of 'identifier' in sql.");
|
|
490 |
LOGGER.warn("Suspicious use of 'identifier' in sql.");
|
|
432 | 491 |
} |
433 | 492 |
return "[" + id + "]"; |
434 | 493 |
} |
435 | 494 |
|
436 |
@Override |
|
437 |
public GeometryValue geometry(Geometry geom, IProjection projection) { |
|
438 |
return new MSSQLServerGeometryValue(geom, projection); |
|
439 |
} |
|
495 |
// @Override
|
|
496 |
// public GeometryValue geometry(Geometry geom, IProjection projection) {
|
|
497 |
// return new MSSQLServerGeometryValue(geom, projection);
|
|
498 |
// }
|
|
440 | 499 |
|
441 |
@Override |
|
442 |
public Parameter parameter() { |
|
443 |
return new MSSQLServerParameter(); |
|
444 |
} |
|
500 |
// @Override
|
|
501 |
// public Parameter parameter() {
|
|
502 |
// return new MSSQLServerParameter();
|
|
503 |
// }
|
|
445 | 504 |
|
446 |
@Override |
|
447 |
public Function ST_ExtentAggregate(Value geom) { |
|
448 |
String spatialType = getHelper().getSpatialType(); |
|
449 |
return builtin_function("ST_ExtentAggregate", |
|
450 |
config.getString(MSSQLServerSQLConfig.ST_ExtentAggregateEx), |
|
451 |
geom, |
|
452 |
custom(spatialType) |
|
453 |
); |
|
454 |
} |
|
505 |
// @Override |
|
506 |
// public Function ST_ExtentAggregate(Value geom) { |
|
507 |
// String spatialType = getHelper().getSpatialType(); |
|
508 |
// return builtin_function("ST_ExtentAggregate", |
|
509 |
// config.getString(MSSQLServerSQLConfig.ST_ExtentAggregateEx), |
|
510 |
// geom, |
|
511 |
// custom(spatialType) |
|
512 |
// ); |
|
513 |
// } |
|
514 |
// |
|
515 |
// @Override |
|
516 |
// public Function ST_UnionAggregate(Value geom) { |
|
517 |
// String spatialType = getHelper().getSpatialType(); |
|
518 |
// return builtin_function("ST_UnionAggregate", |
|
519 |
// config.getString(MSSQLServerSQLConfig.ST_UnionAggregateEx), |
|
520 |
// geom, |
|
521 |
// custom(spatialType) |
|
522 |
// ); |
|
523 |
// } |
|
524 |
// |
|
525 |
// @Override |
|
526 |
// public Function ST_GeomFromText(Value geom, Value crs) { |
|
527 |
// String spatialType = getHelper().getSpatialType(); |
|
528 |
// return builtin_function("ST_GeomFromText", |
|
529 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
530 |
// geom, |
|
531 |
// crs, |
|
532 |
// custom(spatialType) |
|
533 |
// ); |
|
534 |
// } |
|
535 |
// |
|
536 |
// @Override |
|
537 |
// public Function ST_GeomFromWKB(Value geom, Value crs) { |
|
538 |
// String spatialType = getHelper().getSpatialType(); |
|
539 |
// return builtin_function("ST_GeomFromWKB", |
|
540 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
541 |
// geom, |
|
542 |
// crs, |
|
543 |
// custom(spatialType) |
|
544 |
// ); |
|
545 |
// } |
|
546 |
// |
|
547 |
// @Override |
|
548 |
// public Function ST_GeomFromEWKB(Value geom, Value crs) { |
|
549 |
// String spatialType = getHelper().getSpatialType(); |
|
550 |
// return builtin_function("ST_GeomFromEWKB", |
|
551 |
// config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
552 |
// geom, |
|
553 |
// crs, |
|
554 |
// custom(spatialType) |
|
555 |
// ); |
|
556 |
// } |
|
455 | 557 |
|
456 |
@Override |
|
457 |
public Function ST_UnionAggregate(Value geom) { |
|
458 |
String spatialType = getHelper().getSpatialType(); |
|
459 |
return builtin_function("ST_UnionAggregate", |
|
460 |
config.getString(MSSQLServerSQLConfig.ST_UnionAggregateEx), |
|
461 |
geom, |
|
462 |
custom(spatialType) |
|
463 |
); |
|
464 |
} |
|
465 |
|
|
466 |
@Override |
|
467 |
public Function ST_GeomFromText(Value geom, Value crs) { |
|
468 |
String spatialType = getHelper().getSpatialType(); |
|
469 |
return builtin_function("ST_GeomFromText", |
|
470 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), |
|
471 |
geom, |
|
472 |
crs, |
|
473 |
custom(spatialType) |
|
474 |
); |
|
475 |
} |
|
476 |
|
|
477 |
@Override |
|
478 |
public Function ST_GeomFromWKB(Value geom, Value crs) { |
|
479 |
String spatialType = getHelper().getSpatialType(); |
|
480 |
return builtin_function("ST_GeomFromWKB", |
|
481 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), |
|
482 |
geom, |
|
483 |
crs, |
|
484 |
custom(spatialType) |
|
485 |
); |
|
486 |
} |
|
487 |
|
|
488 |
@Override |
|
489 |
public Function ST_GeomFromEWKB(Value geom, Value crs) { |
|
490 |
String spatialType = getHelper().getSpatialType(); |
|
491 |
return builtin_function("ST_GeomFromEWKB", |
|
492 |
config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), |
|
493 |
geom, |
|
494 |
crs, |
|
495 |
custom(spatialType) |
|
496 |
); |
|
497 |
} |
|
498 |
|
|
499 | 558 |
} |
Also available in: Unified diff