root / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / driver / jdbc / h2 / H2DriverUtils.java @ 19610
History | View | Annotate | Download (10.2 KB)
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.DriverManager; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.ResultSetMetaData; |
7 |
import java.sql.Statement; |
8 |
|
9 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType; |
10 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.JDBCDriverNotFoundException; |
11 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.SQLException; |
12 |
import org.gvsig.data.exception.InitializeException; |
13 |
import org.gvsig.data.exception.ReadException; |
14 |
import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
15 |
import org.gvsig.data.vectorial.IFeature; |
16 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
17 |
|
18 |
import com.iver.cit.gvsig.fmap.core.FShape; |
19 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
20 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
21 |
|
22 |
public class H2DriverUtils { |
23 |
|
24 |
static DBFeatureType getFeatureType(Connection conn,String tableName,String columns) throws ReadException{ |
25 |
String sql=""; |
26 |
|
27 |
if (columns == null || columns == ""){ |
28 |
columns = "*";
|
29 |
} |
30 |
|
31 |
|
32 |
try {
|
33 |
sql = "Select "+columns+" from " + tableName + " limit 1;"; |
34 |
Statement stAux = conn.createStatement();
|
35 |
ResultSet rs = stAux.executeQuery(sql);
|
36 |
rs.next(); |
37 |
ResultSetMetaData rsMetadata = rs.getMetaData();
|
38 |
ResultSet rsAllMeta = conn.getMetaData().getAttributes(null, null, tableName, "*"); |
39 |
|
40 |
String colName;
|
41 |
int i;
|
42 |
DBFeatureType featureType = new DBFeatureType();
|
43 |
DefaultAttributeDescriptor attr; |
44 |
for (i=1;i<=rsMetadata.getColumnCount();i++){ |
45 |
colName= rsMetadata.getColumnName(i); |
46 |
rsAllMeta.first(); |
47 |
while (true){ |
48 |
if (rsAllMeta.isAfterLast()){
|
49 |
attr = getAttributeFromJDBC(conn,rsMetadata,i); |
50 |
featureType.add(attr); |
51 |
break;
|
52 |
} else if(rsAllMeta.getString("COLUMN_NAME").equals(colName)){ |
53 |
attr = getAttributeFromJDBC(conn,rsAllMeta); |
54 |
featureType.add(attr); |
55 |
break;
|
56 |
} |
57 |
rsAllMeta.next(); |
58 |
} |
59 |
} |
60 |
|
61 |
rs.close(); |
62 |
rsAllMeta.close(); |
63 |
|
64 |
return featureType;
|
65 |
} catch (java.sql.SQLException e) {
|
66 |
// TODO Auto-generated catch block
|
67 |
throw new SQLException(sql,"getFeatureType",e); |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
private static DefaultAttributeDescriptor getAttributeFromJDBC(Connection conn,ResultSetMetaData rsMetadata,int colIndex) throws SQLException{ |
73 |
DefaultAttributeDescriptor column= new DefaultAttributeDescriptor();
|
74 |
try {
|
75 |
column.setName(rsMetadata.getColumnName(colIndex)); |
76 |
/*
|
77 |
TYPE_BYTE = "BYTE";
|
78 |
TYPE_BOOLEAN = "BOOLEAN";
|
79 |
TYPE_INT = "INTEGER";
|
80 |
TYPE_LONG = "LONG";
|
81 |
TYPE_FLOAT = "FLOAT";
|
82 |
TYPE_DOUBLE = "DOUBLE";
|
83 |
TYPE_STRING = "STRING";
|
84 |
TYPE_DATE = "DATE";
|
85 |
TYPE_GEOMETRY = "GEOMETRY";
|
86 |
TYPE_OBJECT = "OBJECT";
|
87 |
TYPE_FEATURE = "FEATURE";
|
88 |
*/
|
89 |
switch (rsMetadata.getColumnType(colIndex)) {
|
90 |
case java.sql.Types.INTEGER:
|
91 |
column.setType(IFeatureAttributeDescriptor.TYPE_INT); |
92 |
case java.sql.Types.BIGINT:
|
93 |
column.setType(IFeatureAttributeDescriptor.TYPE_LONG); |
94 |
case java.sql.Types.REAL:
|
95 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
96 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
97 |
case java.sql.Types.DOUBLE:
|
98 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
99 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
100 |
case java.sql.Types.CHAR:
|
101 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
102 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
103 |
case java.sql.Types.VARCHAR:
|
104 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
105 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
106 |
case java.sql.Types.FLOAT:
|
107 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
108 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
109 |
case java.sql.Types.DECIMAL:
|
110 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
111 |
column.setSize(rsMetadata.getColumnDisplaySize(colIndex)); |
112 |
// column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
113 |
case java.sql.Types.DATE:
|
114 |
column.setType(IFeatureAttributeDescriptor.TYPE_DATE); |
115 |
default:
|
116 |
column.setType(IFeatureAttributeDescriptor.TYPE_OBJECT); |
117 |
} |
118 |
} catch (java.sql.SQLException e){
|
119 |
throw new SQLException("","load attribute definition",e); |
120 |
} |
121 |
|
122 |
return column;
|
123 |
|
124 |
} |
125 |
|
126 |
private static DefaultAttributeDescriptor getAttributeFromJDBC(Connection conn, ResultSet rsMetadata) throws SQLException{ |
127 |
DefaultAttributeDescriptor column= new DefaultAttributeDescriptor();
|
128 |
try {
|
129 |
column.setName(rsMetadata.getString("COLUMN_NAME"));
|
130 |
/*
|
131 |
TYPE_BYTE = "BYTE";
|
132 |
TYPE_BOOLEAN = "BOOLEAN";
|
133 |
TYPE_INT = "INTEGER";
|
134 |
TYPE_LONG = "LONG";
|
135 |
TYPE_FLOAT = "FLOAT";
|
136 |
TYPE_DOUBLE = "DOUBLE";
|
137 |
TYPE_STRING = "STRING";
|
138 |
TYPE_DATE = "DATE";
|
139 |
TYPE_GEOMETRY = "GEOMETRY";
|
140 |
TYPE_OBJECT = "OBJECT";
|
141 |
TYPE_FEATURE = "FEATURE";
|
142 |
*/
|
143 |
switch (rsMetadata.getInt("DATA_TYPE")) { |
144 |
case java.sql.Types.INTEGER:
|
145 |
column.setType(IFeatureAttributeDescriptor.TYPE_INT); |
146 |
case java.sql.Types.BIGINT:
|
147 |
column.setType(IFeatureAttributeDescriptor.TYPE_LONG); |
148 |
case java.sql.Types.REAL:
|
149 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
150 |
column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
151 |
case java.sql.Types.DOUBLE:
|
152 |
column.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
153 |
column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
154 |
case java.sql.Types.CHAR:
|
155 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
156 |
column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
157 |
case java.sql.Types.VARCHAR:
|
158 |
column.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
159 |
column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
160 |
case java.sql.Types.FLOAT:
|
161 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
162 |
column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
163 |
case java.sql.Types.DECIMAL:
|
164 |
column.setType(IFeatureAttributeDescriptor.TYPE_FLOAT); |
165 |
column.setSize(rsMetadata.getInt("COLUMN_SIZE"));
|
166 |
column.setPrecision(rsMetadata.getInt("DECIMAL_DIGITS"));
|
167 |
case java.sql.Types.DATE:
|
168 |
column.setType(IFeatureAttributeDescriptor.TYPE_DATE); |
169 |
default:
|
170 |
//FIXME: Falta comprobar si es geometrica!!!
|
171 |
column.setType(IFeatureAttributeDescriptor.TYPE_OBJECT); |
172 |
|
173 |
} |
174 |
} catch (java.sql.SQLException e){
|
175 |
throw new SQLException("","load attribute definition",e); |
176 |
} |
177 |
|
178 |
return column;
|
179 |
|
180 |
} |
181 |
|
182 |
|
183 |
|
184 |
static void initializeTableEPSG_and_shapeType(Connection conn,String tableID, DBFeatureType dbld) throws ReadException { |
185 |
try {
|
186 |
Statement stAux = conn.createStatement();
|
187 |
|
188 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '"
|
189 |
// + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'";
|
190 |
String sql= "SELECT SRID("+dbld.getDefaultGeometry()+"), GeometryType("+dbld.getDefaultGeometry()+") FROM "+tableID +" WHERE "+dbld.getDefaultGeometry()+" is not null LIMIT 1"; |
191 |
|
192 |
ResultSet rs = stAux.executeQuery(sql);
|
193 |
if(!rs.next()){
|
194 |
dbld.setDefaultSRS("");
|
195 |
dbld.setGeometryTypes(new int[]{FShape.MULTI}); |
196 |
return;
|
197 |
} |
198 |
dbld.setDefaultSRS("EPSG:"+rs.getInt(1)); |
199 |
|
200 |
String geometryType = rs.getString(2); |
201 |
int shapeType = FShape.MULTI;
|
202 |
if (geometryType.compareToIgnoreCase("Point") == 0) |
203 |
shapeType = FShape.POINT; |
204 |
else if (geometryType.compareToIgnoreCase("LineString") == 0) |
205 |
shapeType = FShape.LINE; |
206 |
else if (geometryType.compareToIgnoreCase("Polygon") == 0) |
207 |
shapeType = FShape.POLYGON; |
208 |
else if (geometryType.compareToIgnoreCase("MultiPoint") == 0) |
209 |
shapeType = FShape.POINT; |
210 |
else if (geometryType.compareToIgnoreCase("MultiLineString") == 0) |
211 |
shapeType = FShape.LINE; |
212 |
else if (geometryType.compareToIgnoreCase("MultiPolygon") == 0) |
213 |
shapeType = FShape.POLYGON; |
214 |
|
215 |
dbld.setGeometryTypes(new int[]{shapeType}); |
216 |
rs.close(); |
217 |
|
218 |
} catch (java.sql.SQLException e) {
|
219 |
dbld.setDefaultSRS("");
|
220 |
dbld.setGeometryTypes(new int[]{FShape.MULTI}); |
221 |
throw new ReadException("H2DriverUtils.getTableEPSG_and_shapeType",e); |
222 |
} |
223 |
|
224 |
} |
225 |
|
226 |
static String getConnectionResourceID(String dbUrl,String dbUser){ |
227 |
return H2Driver.DRIVER_NAME+";"+dbUrl+";"+dbUser; |
228 |
|
229 |
} |
230 |
|
231 |
|
232 |
static String getJDBCUrl(String host,String db){ |
233 |
String url;
|
234 |
url = "jdbc:h2:tcp://"+host;
|
235 |
if (db == null || db == ""){ |
236 |
url=url+"/default";
|
237 |
}else {
|
238 |
url=url+"/"+db;
|
239 |
} |
240 |
|
241 |
return url;
|
242 |
} |
243 |
|
244 |
static Connection getConnection(String dbUrl,String dbUser, String dbPass) throws InitializeException{ |
245 |
//TODO: Aqu? habria que implementar la llamada
|
246 |
// al Resource Manager para comprobar si ya hay
|
247 |
// una connexion a la BD
|
248 |
String connID = getConnectionResourceID(dbUrl, dbUser);
|
249 |
|
250 |
Connection conn = null; |
251 |
// IResource res = ResourceManager.getResourceManager().getResource(connID);
|
252 |
|
253 |
|
254 |
|
255 |
try {
|
256 |
Class.forName("org.h2.Driver"); |
257 |
} catch (ClassNotFoundException e) { |
258 |
throw new JDBCDriverNotFoundException("org.h2.Driver",e); |
259 |
} |
260 |
try {
|
261 |
conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
|
262 |
conn.setAutoCommit(false);
|
263 |
|
264 |
} catch (java.sql.SQLException e1) {
|
265 |
throw new InitializeException("H2Driver",e1); |
266 |
} |
267 |
//TODO: Registrar en el Resource manager
|
268 |
// ResourceManager.getResourceManager().addResource(res);
|
269 |
|
270 |
return conn;
|
271 |
} |
272 |
|
273 |
static IFeature createFeature(H2Driver driver,ResultSet rs,DBFeatureType featureType) throws ReadException{ |
274 |
WKBParser2 wkbParser = new WKBParser2();
|
275 |
|
276 |
H2Feature feature=null;
|
277 |
|
278 |
|
279 |
String pk;
|
280 |
try {
|
281 |
pk = rs.getString(featureType.getFieldId()); |
282 |
|
283 |
feature=new H2Feature(featureType,driver,pk);
|
284 |
|
285 |
for (int fieldId = 2; fieldId <= featureType.size(); fieldId++) { |
286 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get(fieldId); |
287 |
if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
|
288 |
byte[] data = rs.getBytes(featureType.getDefaultGeometry()); |
289 |
IGeometry geom = wkbParser.parse(data); |
290 |
feature.setDefaultGeometry(geom); |
291 |
feature.set(fieldId-2,geom);
|
292 |
} else {
|
293 |
feature.set(fieldId, rs.getObject(fieldId)); |
294 |
} |
295 |
|
296 |
} |
297 |
return feature;
|
298 |
} catch (java.sql.SQLException e) {
|
299 |
throw new ReadException("CreateFeature",e); |
300 |
} |
301 |
} |
302 |
|
303 |
static String objectToSqlString(Object obj){ |
304 |
if (obj instanceof String){ |
305 |
return "'"+ scapeString((String)obj) +"'"; |
306 |
} else if (obj == null){ |
307 |
return "null"; |
308 |
}else{
|
309 |
// OJO con otros tipos!!
|
310 |
return obj.toString();
|
311 |
} |
312 |
|
313 |
} |
314 |
|
315 |
static String scapeString(String str){ |
316 |
return str.replace("'", "''"); |
317 |
} |
318 |
} |