root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2Utils.java @ 24491
History | View | Annotate | Download (15 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc.h2; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.ResultSetMetaData; |
6 |
import java.sql.Statement; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Iterator; |
9 |
|
10 |
import org.gvsig.fmap.dal.exceptions.DataException; |
11 |
import org.gvsig.fmap.dal.exceptions.InitializeException; |
12 |
import org.gvsig.fmap.dal.exceptions.ReadException; |
13 |
import org.gvsig.fmap.dal.feature.AttributeDescriptor; |
14 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.exceptions.IsNotAttributeSettingException; |
16 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureType; |
17 |
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor; |
18 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
19 |
import org.gvsig.fmap.data.feature.db.jdbc.SQLException; |
20 |
import org.gvsig.fmap.geom.Geometry; |
21 |
|
22 |
|
23 |
public class H2Utils { |
24 |
|
25 |
|
26 |
|
27 |
static String getDefaultSchema(Connection conn, String catalog) throws InitializeException { |
28 |
String sql= "SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE IS_DEFAULT = TRUE"; |
29 |
if (catalog != null && catalog !=""){ |
30 |
sql= sql+ " AND CATALOG_NAME = '"+catalog+"'"; |
31 |
} |
32 |
|
33 |
String schema = null; |
34 |
Statement st = null; |
35 |
ResultSet rs= null; |
36 |
try{
|
37 |
// if (conn.isClosed()){
|
38 |
// return "";
|
39 |
// }
|
40 |
st = conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.CONCUR_READ_ONLY); |
41 |
rs = st.executeQuery(sql); |
42 |
if (!rs.next()){
|
43 |
throw new InitializeException("Can't find default schema.","getDefaulSchema"); |
44 |
} |
45 |
schema = rs.getString("SCHEMA_NAME");
|
46 |
if (rs.next()){
|
47 |
throw new InitializeException("Checks catalog parm.","getDefaulSchema"); |
48 |
} |
49 |
|
50 |
|
51 |
} catch (java.sql.SQLException e) {
|
52 |
// TODO Auto-generated catch block
|
53 |
throw new SQLException(sql,"getDefaultSchema",e); |
54 |
} finally{
|
55 |
try{
|
56 |
if (rs != null){ |
57 |
rs.close(); |
58 |
} else if (st != null){ |
59 |
st.close(); |
60 |
} |
61 |
} catch (java.sql.SQLException e1){
|
62 |
//Ignore ??
|
63 |
} |
64 |
|
65 |
} |
66 |
return schema;
|
67 |
} |
68 |
|
69 |
private static void loadFieldsToFeatureType(Connection conn,H2StoreParameters params,DBFeatureType featureType) throws ReadException{ |
70 |
String sql=""; |
71 |
String columns=params.getFieldsString();
|
72 |
|
73 |
if (params.getSqlSoure() != null && !params.getSqlSoure().equals("")){ |
74 |
sql = params.getSqlSoure(); |
75 |
} else {
|
76 |
sql = "Select "+columns+" from " + params.tableID() + " limit 1;"; |
77 |
} |
78 |
|
79 |
try {
|
80 |
|
81 |
Statement stAux = conn.createStatement();
|
82 |
Statement stAux1 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
83 |
ResultSet rs = stAux.executeQuery(sql);
|
84 |
ResultSetMetaData rsMetadata = rs.getMetaData();
|
85 |
String schemaFilter=""; |
86 |
if (params.getSchema() != null && params.getSchema() != ""){ |
87 |
schemaFilter = " TABLE_SCHEMA='" + params.getSchema() +"' AND "; |
88 |
} |
89 |
String sqlAllMeta = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where " |
90 |
+ schemaFilter |
91 |
+ "TABLE_NAME='"+ params.getTableName() +"'"; |
92 |
|
93 |
ResultSet rsAllMeta = stAux1.executeQuery(sqlAllMeta);
|
94 |
|
95 |
// ResultSet rsAllMeta = conn.getMetaData().getAttributes(null, null, tableName, "*");
|
96 |
|
97 |
String colName;
|
98 |
int i;
|
99 |
|
100 |
featureType.setTableID(params.tableID()); |
101 |
AttributeDescriptor attr; |
102 |
for (i=1;i<=rsMetadata.getColumnCount();i++){ |
103 |
colName= rsMetadata.getColumnName(i); |
104 |
rsAllMeta.first(); |
105 |
|
106 |
while (true){ |
107 |
if (rsAllMeta.isAfterLast()){
|
108 |
attr = getAttributeFromJDBC(featureType,conn,rsMetadata,i); |
109 |
featureType.add(attr); |
110 |
// attr.setOrdinal(i-1);
|
111 |
break;
|
112 |
} else if(rsAllMeta.getString("COLUMN_NAME").equals(colName)){ |
113 |
attr = getAttributeFromJDBC(featureType,conn,rsAllMeta); |
114 |
featureType.add(attr); |
115 |
// attr.setOrdinal(i-1);
|
116 |
break;
|
117 |
} |
118 |
rsAllMeta.next(); |
119 |
} |
120 |
if (attr.getName().equals(params.getDefaultGeometryField())){
|
121 |
if (attr.getDataType().equals(FeatureAttributeDescriptor.OBJECT)){
|
122 |
attr.loading(); |
123 |
attr.setType(FeatureAttributeDescriptor.GEOMETRY); |
124 |
attr.stopLoading(); |
125 |
}else{
|
126 |
throw new InitializeException( |
127 |
"H2Utils.getFeatureType",
|
128 |
new Exception("Geometry Field '" |
129 |
+ params.getDefaultGeometryField() |
130 |
+ "' is a "
|
131 |
+ attr.getDataType() |
132 |
+ " but sould be "
|
133 |
+ FeatureAttributeDescriptor.OBJECT)); |
134 |
} |
135 |
|
136 |
} |
137 |
} |
138 |
|
139 |
rs.close(); |
140 |
rsAllMeta.close(); |
141 |
} catch (java.sql.SQLException e) {
|
142 |
// TODO Auto-generated catch block
|
143 |
throw new SQLException(sql,"getFeatureType",e); |
144 |
} catch (IsNotAttributeSettingException e) {
|
145 |
e.printStackTrace(); |
146 |
} |
147 |
|
148 |
} |
149 |
|
150 |
static DBFeatureType getFeatureType(Connection conn,H2StoreParameters params) throws ReadException{ |
151 |
DBFeatureType featureType = new DBFeatureType();
|
152 |
|
153 |
|
154 |
loadFieldsToFeatureType(conn, params, featureType); |
155 |
|
156 |
try {
|
157 |
if (params.getFieldsId() == null || params.getFieldsId().length == 0){ |
158 |
String[] ids =loadFieldsId(conn,params); |
159 |
if (ids == null){ |
160 |
featureType.setReadOnly(true);
|
161 |
ids = new String[featureType.size()]; |
162 |
Iterator iter = featureType.iterator();
|
163 |
for (int i=0;i<featureType.size();i++){ |
164 |
ids[i]=((AttributeDescriptor)iter.next()).getName(); |
165 |
} |
166 |
} |
167 |
featureType.setFieldsId(ids); |
168 |
} else{
|
169 |
featureType.setFieldsId(params.getFieldsId()); |
170 |
} |
171 |
} catch (DataException e) {
|
172 |
throw new ReadException(H2Store.DATASTORE_NAME,e); |
173 |
} |
174 |
|
175 |
if (params.getDefaultGeometryField() != null && params.getDefaultGeometryField() != ""){ |
176 |
// if (featureType.getFieldIndex(params.getGeometryField())< 0){
|
177 |
// // FIXME: crear una nueva excepcion??
|
178 |
// throw new InitializeException(
|
179 |
// H2Store.DATASTORE_NAME,
|
180 |
// new Exception("Geometry Field '"+ params.getGeometryField() +"' not Found"));
|
181 |
//
|
182 |
// }
|
183 |
|
184 |
featureType.setDefaultGeometry(params.getDefaultGeometryField()); |
185 |
featureType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY}); |
186 |
} |
187 |
|
188 |
return featureType;
|
189 |
|
190 |
} |
191 |
|
192 |
private static String[] loadFieldsId(Connection conn, H2StoreParameters params) throws ReadException { |
193 |
Statement st;
|
194 |
StringBuffer sql = new StringBuffer(); |
195 |
ResultSet rs;
|
196 |
ArrayList list = new ArrayList(); |
197 |
/*SELECT column_name FROM INFORMATION_SCHEMA.INDEXES
|
198 |
* WHERE table_name='' AND
|
199 |
* table_schema='' AND
|
200 |
* table_catalog=''
|
201 |
* AND index_type_name='PRIMARY KEY'
|
202 |
*
|
203 |
*/
|
204 |
sql.append("SELECT column_name FROM INFORMATION_SCHEMA.INDEXES WHERE table_name like '");
|
205 |
sql.append(params.getTableName()); |
206 |
sql.append("' AND table_schema like '");
|
207 |
|
208 |
if (params.getSchema() == null || params.getSchema() == ""){ |
209 |
sql.append(getDefaultSchema(conn, params.getDb())); |
210 |
} else{
|
211 |
sql.append(params.getSchema()); |
212 |
} |
213 |
|
214 |
|
215 |
if (params.getCatalog() == null || params.getCatalog() == ""){ |
216 |
if (params.getDb() != null && params.getDb() != ""){ |
217 |
sql.append("' AND table_catalog like '");
|
218 |
sql.append(params.getDb()); |
219 |
} |
220 |
} else {
|
221 |
sql.append("' AND table_catalog like '");
|
222 |
sql.append(params.getCatalog()); |
223 |
} |
224 |
|
225 |
|
226 |
|
227 |
sql.append("' AND index_type_name='PRIMARY KEY'");
|
228 |
|
229 |
// System.out.println(sql.toString());
|
230 |
try {
|
231 |
st = conn.createStatement(); |
232 |
rs = st.executeQuery(sql.toString()); |
233 |
while (rs.next()){
|
234 |
list.add(rs.getString(1));
|
235 |
} |
236 |
rs.close(); |
237 |
st.close(); |
238 |
|
239 |
} catch (java.sql.SQLException e) {
|
240 |
throw new ReadException(params.getDataStoreName(),e); |
241 |
} |
242 |
if (list.size() == 0){ |
243 |
return null; |
244 |
} |
245 |
|
246 |
String[] x = new String[] {""}; |
247 |
return (String[])list.toArray(x); |
248 |
|
249 |
} |
250 |
|
251 |
private static AttributeDescriptor getAttributeFromJDBC(DefaultFeatureType fType,Connection conn,ResultSetMetaData rsMetadata,int colIndex) throws SQLException{ |
252 |
DBAttributeDescriptor column= (DBAttributeDescriptor) fType.createAttributeDescriptor(); |
253 |
try {
|
254 |
column.loading(); |
255 |
column.setName(rsMetadata.getColumnName(colIndex)); |
256 |
column.setCaseSensitive(rsMetadata.isCaseSensitive(colIndex)); |
257 |
column.setSqlType(rsMetadata.getColumnType(colIndex)); |
258 |
column.setAllowNull(rsMetadata.isNullable(colIndex) == ResultSetMetaData.columnNullable);
|
259 |
column.setAutoIncrement(rsMetadata.isAutoIncrement(colIndex)); |
260 |
column.setReadOnly(rsMetadata.isReadOnly(colIndex)); |
261 |
column.setWritable(rsMetadata.isWritable(colIndex)); |
262 |
column.setClassName(rsMetadata.getColumnClassName(colIndex)); |
263 |
column.setCatalogName(rsMetadata.getCatalogName(colIndex)); |
264 |
column.setDefinitelyWritable(rsMetadata.isDefinitelyWritable(colIndex)); |
265 |
column.setLabel(rsMetadata.getColumnLabel(colIndex)); |
266 |
column.setSchemaName(rsMetadata.getSchemaName(colIndex)); |
267 |
column.setTableName(rsMetadata.getTableName(colIndex)); |
268 |
column.setCatalogName(rsMetadata.getCatalogName(colIndex)); |
269 |
column.setSqlTypeName(rsMetadata.getColumnTypeName(colIndex)); |
270 |
column.setSearchable(rsMetadata.isSearchable(colIndex)); |
271 |
column.setSigned(rsMetadata.isSigned(colIndex)); |
272 |
column.setCurrency(rsMetadata.isCurrency(colIndex)); |
273 |
column.setPrecision(rsMetadata.getPrecision(colIndex)); |
274 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); switch (rsMetadata.getColumnType(colIndex)) {
|
275 |
case java.sql.Types.INTEGER:
|
276 |
column.setType(FeatureAttributeDescriptor.INT); |
277 |
break;
|
278 |
case java.sql.Types.BIGINT:
|
279 |
column.setType(FeatureAttributeDescriptor.LONG); |
280 |
break;
|
281 |
case java.sql.Types.REAL:
|
282 |
column.setType(FeatureAttributeDescriptor.DOUBLE); |
283 |
break;
|
284 |
case java.sql.Types.DOUBLE:
|
285 |
column.setType(FeatureAttributeDescriptor.DOUBLE); |
286 |
break;
|
287 |
case java.sql.Types.CHAR:
|
288 |
column.setType(FeatureAttributeDescriptor.STRING); |
289 |
break;
|
290 |
case java.sql.Types.VARCHAR:
|
291 |
column.setType(FeatureAttributeDescriptor.STRING); |
292 |
break;
|
293 |
case java.sql.Types.FLOAT:
|
294 |
column.setType(FeatureAttributeDescriptor.FLOAT); |
295 |
break;
|
296 |
case java.sql.Types.DECIMAL:
|
297 |
column.setType(FeatureAttributeDescriptor.FLOAT); |
298 |
break;
|
299 |
case java.sql.Types.DATE:
|
300 |
column.setType(FeatureAttributeDescriptor.DATE); |
301 |
break;
|
302 |
case java.sql.Types.TIME:
|
303 |
column.setType(FeatureAttributeDescriptor.TIME); |
304 |
break;
|
305 |
case java.sql.Types.TIMESTAMP:
|
306 |
column.setType(FeatureAttributeDescriptor.TIMESTAMP); |
307 |
break;
|
308 |
case java.sql.Types.BOOLEAN:
|
309 |
column.setType(FeatureAttributeDescriptor.BOOLEAN); |
310 |
break;
|
311 |
default:
|
312 |
column.setType(FeatureAttributeDescriptor.OBJECT); |
313 |
break;
|
314 |
} |
315 |
column.stopLoading(); |
316 |
} catch (java.sql.SQLException e){
|
317 |
throw new SQLException("","load attribute definition",e); |
318 |
} catch (IsNotAttributeSettingException e) {
|
319 |
e.printStackTrace(); |
320 |
} |
321 |
|
322 |
return column;
|
323 |
|
324 |
} |
325 |
|
326 |
private static AttributeDescriptor getAttributeFromJDBC(DefaultFeatureType fType, Connection conn, ResultSet rsMetadata) throws SQLException{ |
327 |
DBAttributeDescriptor column= (DBAttributeDescriptor) fType.createAttributeDescriptor(); |
328 |
try {
|
329 |
column.loading(); |
330 |
column.setName(rsMetadata.getString("COLUMN_NAME"));
|
331 |
column.setSqlType(rsMetadata.getInt("DATA_TYPE"));
|
332 |
switch (rsMetadata.getInt("DATA_TYPE")) { |
333 |
case java.sql.Types.INTEGER:
|
334 |
column.setType(FeatureAttributeDescriptor.INT); |
335 |
break;
|
336 |
case java.sql.Types.BIGINT:
|
337 |
column.setType(FeatureAttributeDescriptor.LONG); |
338 |
break;
|
339 |
case java.sql.Types.REAL:
|
340 |
column.setType(FeatureAttributeDescriptor.DOUBLE); |
341 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
342 |
column.setPrecision(rsMetadata.getInt("NUMERIC_PRECISION"));
|
343 |
break;
|
344 |
case java.sql.Types.DOUBLE:
|
345 |
column.setType(FeatureAttributeDescriptor.DOUBLE); |
346 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
347 |
column.setPrecision(rsMetadata.getInt("NUMERIC_PRECISION"));
|
348 |
break;
|
349 |
case java.sql.Types.CHAR:
|
350 |
column.setType(FeatureAttributeDescriptor.STRING); |
351 |
// column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
352 |
column.setSize(rsMetadata.getInt("CHARACTER_MAXIMUM_LENGTH"));
|
353 |
break;
|
354 |
case java.sql.Types.VARCHAR:
|
355 |
column.setType(FeatureAttributeDescriptor.STRING); |
356 |
// column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
357 |
column.setSize(rsMetadata.getInt("CHARACTER_MAXIMUM_LENGTH"));
|
358 |
|
359 |
break;
|
360 |
case java.sql.Types.FLOAT:
|
361 |
column.setType(FeatureAttributeDescriptor.FLOAT); |
362 |
// column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
363 |
column.setSize(rsMetadata.getInt("CHARACTER_MAXIMUM_LENGTH"));
|
364 |
break;
|
365 |
case java.sql.Types.DECIMAL:
|
366 |
column.setType(FeatureAttributeDescriptor.FLOAT); |
367 |
// column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
368 |
column.setSize(rsMetadata.getInt("CHARACTER_MAXIMUM_LENGTH"));
|
369 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
370 |
column.setPrecision(rsMetadata.getInt("NUMERIC_PRECISION"));
|
371 |
break;
|
372 |
case java.sql.Types.DATE:
|
373 |
column.setType(FeatureAttributeDescriptor.DATE); |
374 |
break;
|
375 |
case java.sql.Types.BOOLEAN:
|
376 |
column.setType(FeatureAttributeDescriptor.BOOLEAN); |
377 |
break;
|
378 |
default:
|
379 |
//FIXME: Falta comprobar si es geometrica!!!
|
380 |
column.setType(FeatureAttributeDescriptor.OBJECT); |
381 |
break;
|
382 |
} |
383 |
|
384 |
column.setDefaultValue(rsMetadata.getObject("COLUMN_DEFAULT"));
|
385 |
column.setAllowNull(rsMetadata.getBoolean("IS_NULLABLE"));
|
386 |
column.stopLoading(); |
387 |
} catch (java.sql.SQLException e){
|
388 |
throw new SQLException("","load attribute definition",e); |
389 |
} catch (IsNotAttributeSettingException e) {
|
390 |
e.printStackTrace(); |
391 |
} |
392 |
|
393 |
return column;
|
394 |
|
395 |
} |
396 |
|
397 |
|
398 |
|
399 |
// static void initializeTableEPSG_and_shapeType(Connection conn,String tableID, DBFeatureType dbld) throws ReadException {
|
400 |
// try {
|
401 |
// Statement stAux = conn.createStatement();
|
402 |
//
|
403 |
//// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '"
|
404 |
//// + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'";
|
405 |
// String sql= "SELECT SRID("+dbld.getDefaultGeometry()+"), GeometryType("+dbld.getDefaultGeometry()+") FROM "+tableID +" WHERE "+dbld.getDefaultGeometry()+" is not null LIMIT 1";
|
406 |
//
|
407 |
// ResultSet rs = stAux.executeQuery(sql);
|
408 |
// if(!rs.next()){
|
409 |
// dbld.setDefaultSRS("");
|
410 |
// dbld.setGeometryTypes(new int[]{FShape.MULTI});
|
411 |
// return;
|
412 |
// }
|
413 |
// dbld.setDefaultSRS("EPSG:"+rs.getInt(1));
|
414 |
//
|
415 |
// String geometryType = rs.getString(2);
|
416 |
// int shapeType = FShape.MULTI;
|
417 |
// if (geometryType.compareToIgnoreCase("Point") == 0)
|
418 |
// shapeType = FShape.POINT;
|
419 |
// else if (geometryType.compareToIgnoreCase("LineString") == 0)
|
420 |
// shapeType = FShape.LINE;
|
421 |
// else if (geometryType.compareToIgnoreCase("Polygon") == 0)
|
422 |
// shapeType = FShape.POLYGON;
|
423 |
// else if (geometryType.compareToIgnoreCase("MultiPoint") == 0)
|
424 |
// shapeType = FShape.POINT;
|
425 |
// else if (geometryType.compareToIgnoreCase("MultiLineString") == 0)
|
426 |
// shapeType = FShape.LINE;
|
427 |
// else if (geometryType.compareToIgnoreCase("MultiPolygon") == 0)
|
428 |
// shapeType = FShape.POLYGON;
|
429 |
//
|
430 |
// dbld.setGeometryTypes(new int[]{shapeType});
|
431 |
// rs.close();
|
432 |
//
|
433 |
// } catch (java.sql.SQLException e) {
|
434 |
// dbld.setDefaultSRS("");
|
435 |
// dbld.setGeometryTypes(new int[]{FShape.MULTI});
|
436 |
// throw new ReadException("H2Utils.getTableEPSG_and_shapeType",e);
|
437 |
// }
|
438 |
//
|
439 |
// }
|
440 |
|
441 |
static String getConnectionResourceID(String dbUrl,String dbUser){ |
442 |
return H2Store.CONNECTION_STRING+";"+dbUrl+";"+dbUser; |
443 |
|
444 |
} |
445 |
|
446 |
|
447 |
static String getJDBCUrl(String host,String db){ |
448 |
String url;
|
449 |
url = "jdbc:h2:tcp://"+host;
|
450 |
if (db == null || db == ""){ |
451 |
url=url+"/default";
|
452 |
}else {
|
453 |
url=url+"/"+db;
|
454 |
} |
455 |
|
456 |
return url;
|
457 |
} |
458 |
|
459 |
static String addLimitsToSQL(String aSql, int fetchSize, int page) { |
460 |
return aSql + " limit " + fetchSize + " offset " + (fetchSize * page); |
461 |
} |
462 |
|
463 |
} |