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