Revision 19610
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/ConnectionFactory.java | ||
---|---|---|
73 | 73 |
String user, String _pw) { |
74 | 74 |
IConnection connection = null; |
75 | 75 |
|
76 |
if (connectionStr.startsWith("jdbc")) { |
|
77 |
connection = new ConnectionJDBC(); |
|
78 |
connection.setDataConnection(connectionStr, user, _pw); |
|
79 |
return connection; |
|
80 |
} else { |
|
81 |
// if (lastConnections.containsKey(connectionStr)) |
|
82 |
// return (IConnection)lastConnections.get(connectionStr); |
|
76 |
// if (connectionStr.startsWith("jdbc")) { |
|
77 |
// connection = new ConnectionJDBC(); |
|
78 |
// connection.setDataConnection(connectionStr, user, _pw); |
|
79 |
// return connection; |
|
80 |
// } else { |
|
81 |
//// if (lastConnections.containsKey(connectionStr)) |
|
82 |
//// return (IConnection)lastConnections.get(connectionStr); |
|
83 |
// |
|
84 |
// refreshExtensionPointsConnections(); |
|
85 |
// |
|
86 |
// IConnection[] conns = (IConnection[]) connections.toArray(new IConnection[0]); |
|
87 |
// String type = connectionStr.substring(0,connectionStr.indexOf(":")); |
|
88 |
// |
|
89 |
// for (int i = 0; i < conns.length; i++) { |
|
90 |
// if (conns[i].getTypeConnection().equals(type)) { |
|
91 |
// connection=conns[i]; |
|
92 |
// connection.setDataConnection(connectionStr, user, _pw); |
|
93 |
//// lastConnections.put(connectionStr,connection); |
|
94 |
// return connection; |
|
95 |
// } |
|
96 |
// } |
|
97 |
// } |
|
83 | 98 |
|
84 |
refreshExtensionPointsConnections(); |
|
85 |
|
|
86 |
IConnection[] conns = (IConnection[]) connections.toArray(new IConnection[0]); |
|
87 |
String type = connectionStr.substring(0,connectionStr.indexOf(":")); |
|
88 |
|
|
89 |
for (int i = 0; i < conns.length; i++) { |
|
90 |
if (conns[i].getTypeConnection().equals(type)) { |
|
91 |
connection=conns[i]; |
|
92 |
connection.setDataConnection(connectionStr, user, _pw); |
|
93 |
// lastConnections.put(connectionStr,connection); |
|
94 |
return connection; |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 | 99 |
return connection; |
100 | 100 |
} |
101 | 101 |
|
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/DBDriverJDBCParameters.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc; |
|
2 |
|
|
3 |
public abstract class DBDriverJDBCParameters extends |
|
4 |
DataStoreDBParameters { |
|
5 |
|
|
6 |
public abstract String getUrl(); |
|
7 |
|
|
8 |
|
|
9 |
} |
|
0 | 10 |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2DataSourceParameters.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
|
2 |
|
|
3 |
import java.sql.Connection; |
|
4 |
|
|
5 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.ConnectionJDBC; |
|
6 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DataSourceDBDriverJDBCParameters; |
|
7 |
|
|
8 |
public class H2DataSourceParameters extends DataSourceDBDriverJDBCParameters { |
|
9 |
|
|
10 |
public String getDataSourceName() { |
|
11 |
return H2DataSource.DATASOURCE_NAME; |
|
12 |
} |
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
} |
|
0 | 17 |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2DriverParameters.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
|
2 |
|
|
3 |
import java.sql.Connection; |
|
4 |
|
|
5 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
|
6 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType; |
|
7 |
|
|
8 |
public class H2DriverParameters implements IDriverParameters { |
|
9 |
private Connection connection; |
|
10 |
private DBFeatureType featureType; |
|
11 |
public Connection getConnection() { |
|
12 |
return connection; |
|
13 |
} |
|
14 |
public void setConnection(Connection conn){ |
|
15 |
this.connection = conn; |
|
16 |
} |
|
17 |
public DBFeatureType getFeatureType() { |
|
18 |
return featureType; |
|
19 |
} |
|
20 |
public void setFeatureType(DBFeatureType dbFeatureType) { |
|
21 |
this.featureType=dbFeatureType; |
|
22 |
|
|
23 |
} |
|
24 |
|
|
25 |
} |
|
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
|
2 |
|
|
3 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBDriverJDBCParameters; |
|
4 |
import org.gvsig.data.spatialprovisional.IExtent; |
|
5 |
|
|
6 |
public class H2DriverParameters extends DBDriverJDBCParameters { |
|
7 |
|
|
8 |
private IExtent workingArea=null; |
|
9 |
private String SRISD = null; |
|
10 |
private String geometryField=null; |
|
11 |
|
|
12 |
|
|
13 |
public String getGeometryField() { |
|
14 |
return geometryField; |
|
15 |
} |
|
16 |
|
|
17 |
public void setGeometryField(String geometryColumn) { |
|
18 |
this.geometryField = geometryColumn; |
|
19 |
} |
|
20 |
|
|
21 |
public H2DriverParameters(){ |
|
22 |
super(); |
|
23 |
} |
|
24 |
|
|
25 |
public String getUrl() { |
|
26 |
return H2DriverUtils.getJDBCUrl(this.getHost(), this.getDb()); |
|
27 |
} |
|
28 |
|
|
29 |
public String getDataStoreName() { |
|
30 |
return H2Driver.DRIVER_NAME; |
|
31 |
} |
|
32 |
|
|
33 |
public String tableID(){ |
|
34 |
return this.getTableName(); |
|
35 |
} |
|
36 |
|
|
37 |
public String getSRISD() { |
|
38 |
return SRISD; |
|
39 |
} |
|
40 |
|
|
41 |
public void setSRISD(String srisd) { |
|
42 |
SRISD = srisd; |
|
43 |
} |
|
44 |
|
|
45 |
public IExtent getWorkingArea() { |
|
46 |
return workingArea; |
|
47 |
} |
|
48 |
|
|
49 |
public void setWorkingArea(IExtent workingArea) { |
|
50 |
this.workingArea = workingArea; |
|
51 |
} |
|
52 |
} |
|
26 | 53 |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2FeaturesReader.java | ||
---|---|---|
14 | 14 |
|
15 | 15 |
public IFeatureCollection getFeatures(FeatureManager featureManager, IFeatureType type, String filter, String order) { |
16 | 16 |
IFeatureCollection coll; |
17 |
if (order == null){
|
|
18 |
coll=new H2FeatureCollection(featureManager,driver,type,filter);
|
|
17 |
if (featureManager == null){
|
|
18 |
coll=new H2FeatureCollection(driver,type,filter,order);
|
|
19 | 19 |
}else{ |
20 | 20 |
coll=new H2FeatureCollectionWithFeatureID(featureManager,driver,type,filter,order); |
21 | 21 |
} |
22 | 22 |
|
23 | 23 |
return coll; |
24 | 24 |
|
25 |
// //TODO Filtrar y ordenar, quedar? tambi?n pendiente el IFeatureType que se pasa como |
|
26 |
// //par?metro ya que aqu? lo ?nico que introducimos al DataCollection son IFeatureID, |
|
27 |
// //todav?a no creamos IFeatures |
|
28 |
// for (int i=0;i<driver.getFeatureCount();i++){ |
|
29 |
// DBFFeatureID id=new DBFFeatureID(driver,i); |
|
30 |
// collection.add(id); |
|
31 |
// } |
|
32 |
// return collection; |
|
33 | 25 |
|
34 | 26 |
} |
35 | 27 |
|
28 |
|
|
36 | 29 |
} |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2FeatureCollection.java | ||
---|---|---|
1 | 1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
2 | 2 |
|
3 |
import java.sql.Connection; |
|
3 | 4 |
import java.sql.ResultSet; |
4 |
import java.sql.SQLException; |
|
5 | 5 |
import java.sql.Statement; |
6 | 6 |
import java.util.ArrayList; |
7 | 7 |
import java.util.Collection; |
... | ... | |
10 | 10 |
import java.util.NoSuchElementException; |
11 | 11 |
|
12 | 12 |
import org.gvsig.data.ComplexObservable; |
13 |
import org.gvsig.data.datastores.vectorial.driver.AbstractDriver; |
|
14 | 13 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType; |
14 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.SQLException; |
|
15 |
import org.gvsig.data.exception.ReadException; |
|
15 | 16 |
import org.gvsig.data.vectorial.AbstractFeatureCollection; |
16 | 17 |
import org.gvsig.data.vectorial.IFeature; |
17 | 18 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
18 | 19 |
import org.gvsig.data.vectorial.IFeatureType; |
19 |
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
|
|
20 |
import org.gvsig.exceptions.BaseException;
|
|
20 | 21 |
|
21 | 22 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
22 | 23 |
|
23 | 24 |
public class H2FeatureCollection extends AbstractFeatureCollection { |
24 | 25 |
protected ComplexObservable observable = new ComplexObservable(); |
25 |
protected ArrayList featureIDs=new ArrayList();//<IFeatureID> |
|
26 | 26 |
protected DBFeatureType featureType; |
27 | 27 |
protected String filter; |
28 |
protected AbstractDriver driver;
|
|
29 |
protected FeatureManager featureManager;
|
|
28 |
protected String totalFilter;
|
|
29 |
protected H2Driver driver;
|
|
30 | 30 |
protected ResultSet rs; |
31 | 31 |
private WKBParser2 wkbParser = new WKBParser2(); |
32 |
private String order; |
|
33 |
private int numReg=-1; |
|
34 |
private String sql; |
|
35 |
private String sqlCount; |
|
36 |
private String totalOrder; |
|
37 |
private Connection connection; |
|
38 |
private Statement statement; |
|
32 | 39 |
|
33 |
public H2FeatureCollection(FeatureManager fm,H2Driver driver,IFeatureType type, String filter) { |
|
34 |
this.featureManager=fm; |
|
40 |
public H2FeatureCollection(H2Driver driver,IFeatureType type, String filter, String order) { |
|
35 | 41 |
this.driver=driver; |
36 |
this.rs=driver.getResultSet(); |
|
37 | 42 |
this.featureType=(DBFeatureType)type; |
38 | 43 |
this.filter=filter; |
39 |
// if (this.filter!=null) |
|
40 |
// parser = new FeatureFilterParser(filter,this.featureType); |
|
44 |
this.order=order; |
|
45 |
this.calculateWhere(); |
|
46 |
this.calculateOrder(); |
|
41 | 47 |
|
48 |
this.sql = this.driver.getSqlSelectPart(); |
|
49 |
this.sqlCount = "Select count(" + type.getFieldId() +") "; |
|
50 |
if (!isStringEmpty(this.totalFilter)){ |
|
51 |
this.sql= this.sql + " Where " + this.totalFilter; |
|
52 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
|
53 |
} |
|
54 |
if (!isStringEmpty(this.totalOrder)){ |
|
55 |
this.sql= this.sql + " Order by " + this.totalOrder; |
|
56 |
} |
|
42 | 57 |
} |
43 | 58 |
|
59 |
public H2FeatureCollection(H2Driver driver,IFeatureType type, String sql) { |
|
60 |
this.driver=driver; |
|
61 |
this.featureType=(DBFeatureType)type; |
|
62 |
this.filter=null; |
|
63 |
this.order=null; |
|
64 |
|
|
65 |
this.sql = sql; |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
|
|
70 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
|
71 |
this.driver.open(); |
|
72 |
|
|
73 |
this.connection = this.driver.getConnection(); |
|
74 |
|
|
75 |
try { |
|
76 |
|
|
77 |
if (this.statement == null){ |
|
78 |
this.statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
|
79 |
} |
|
80 |
|
|
81 |
return rs = this.statement.executeQuery(aSql); |
|
82 |
} catch (java.sql.SQLException e) { |
|
83 |
throw new SQLException(aSql,"H2FeatureCollection",e); |
|
84 |
} |
|
85 |
|
|
86 |
} |
|
87 |
|
|
88 |
|
|
89 |
private boolean isStringEmpty(String s){ |
|
90 |
return s == null || s == ""; |
|
91 |
} |
|
92 |
|
|
93 |
private void calculateWhere(){ |
|
94 |
if (isStringEmpty(this.driver.getBaseWhereClause())){ |
|
95 |
this.totalFilter = this.filter; |
|
96 |
} else { |
|
97 |
this.totalFilter = "(" + this.driver.getBaseWhereClause() + ") and " +this.filter; |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
private void calculateOrder(){ |
|
102 |
if (isStringEmpty(this.driver.getBaseOrder())){ |
|
103 |
this.totalOrder = this.order; |
|
104 |
} else { |
|
105 |
this.totalOrder = this.driver.getBaseOrder() + ", " +this.order; |
|
106 |
} |
|
107 |
|
|
108 |
} |
|
109 |
|
|
44 | 110 |
protected void checkModified(){ |
45 | 111 |
if (modified) |
46 | 112 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
47 | 113 |
} |
48 | 114 |
|
49 | 115 |
public int size() { |
116 |
try { |
|
117 |
if (this.numReg < 0){ |
|
118 |
ResultSet r=null; |
|
119 |
r = this.getNewResulset(this.sqlCount); |
|
120 |
try { |
|
121 |
r.next(); |
|
122 |
numReg = r.getInt(1); |
|
123 |
} catch (java.sql.SQLException e) { |
|
124 |
throw new ReadException("H2FeatureCollection.size",e); |
|
125 |
} finally{ |
|
126 |
try { |
|
127 |
r.close(); |
|
128 |
} catch (java.sql.SQLException e) { |
|
129 |
throw new ReadException("H2FeatureCollection.size",e); |
|
130 |
} |
|
50 | 131 |
|
51 |
int numReg =0; |
|
52 |
try { |
|
53 |
Statement s = featureType.getConnection().createStatement(); |
|
54 |
String stringFilter=""; |
|
55 |
if (filter!=null){ |
|
56 |
stringFilter=" AND "+ filter; |
|
57 |
} |
|
58 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + featureType.getTableName() + " " + featureType.getWhereClause()+ stringFilter); |
|
59 |
r.next(); |
|
60 |
numReg = r.getInt(1); |
|
61 |
System.err.println("numReg = " + numReg); |
|
62 |
} catch (SQLException e){ |
|
63 |
e.printStackTrace(); |
|
64 |
// throw new ReadDriverException(getName(),e); |
|
65 |
} |
|
66 |
return numReg+featureManager.getNum(); |
|
132 |
} |
|
133 |
|
|
134 |
System.err.println("numReg = " + numReg); |
|
135 |
} |
|
136 |
return numReg; |
|
137 |
} catch (BaseException e){ |
|
138 |
throw new RuntimeException(e); |
|
139 |
} |
|
140 |
|
|
67 | 141 |
} |
68 | 142 |
|
69 | 143 |
public boolean isEmpty() { |
... | ... | |
82 | 156 |
} |
83 | 157 |
|
84 | 158 |
public Iterator iterator() { |
85 |
H2Iterator dbfIter=new H2Iterator(); |
|
159 |
ResultSet r; |
|
160 |
try { |
|
161 |
r = this.getNewResulset(this.sqlCount); |
|
162 |
} catch (ReadException e) { |
|
163 |
throw new RuntimeException(e); |
|
164 |
} |
|
165 |
H2Iterator dbfIter=new H2Iterator(this.driver,this.featureType,r); |
|
86 | 166 |
return dbfIter; |
87 | 167 |
} |
88 | 168 |
|
... | ... | |
143 | 223 |
|
144 | 224 |
|
145 | 225 |
protected class H2Iterator implements Iterator{ |
146 |
protected long position=0;
|
|
147 |
private boolean nextChecked=false;
|
|
148 |
private IFeature feature;
|
|
226 |
private ResultSet rs;
|
|
227 |
private H2Driver driver;
|
|
228 |
private DBFeatureType featureType;
|
|
149 | 229 |
|
150 |
public H2Iterator(){ |
|
151 |
position=0; |
|
230 |
public H2Iterator(H2Driver driver,DBFeatureType featureType ,ResultSet rs){ |
|
231 |
this.rs = rs; |
|
232 |
this.driver = driver; |
|
233 |
this.featureType = featureType; |
|
152 | 234 |
} |
153 | 235 |
|
154 |
protected void checkModified(){ |
|
155 |
if (modified) |
|
156 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
|
157 |
} |
|
158 |
|
|
159 | 236 |
public boolean hasNext(){ |
160 |
checkModified(); |
|
161 |
|
|
162 |
IFeature feature=null; |
|
163 |
if (nextChecked){ |
|
164 |
return feature != null; |
|
165 |
} |
|
166 |
nextChecked=true; |
|
167 |
while (true){ |
|
168 |
try { |
|
169 |
if (!rs.isLast()){ |
|
170 |
// if (position<driverFeatureCount){ |
|
171 |
// IFeatureID featureID = this.createCurrectDriverFeatureID(position); |
|
172 |
feature=nextFeature(); |
|
173 |
}else if (position<featureManager.getNum()){ |
|
174 |
feature=featureManager.getFeature((int)position); |
|
175 |
position++; |
|
176 |
}else{ |
|
177 |
this.feature = null; |
|
178 |
return false; |
|
179 |
} |
|
180 |
} catch (SQLException e) { |
|
181 |
// TODO Auto-generated catch block |
|
182 |
e.printStackTrace(); |
|
237 |
try { |
|
238 |
if (rs.isLast()){ |
|
239 |
return false; |
|
240 |
} else { |
|
241 |
return true; |
|
183 | 242 |
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
if(featureManager.isDeleted(feature)) |
|
188 |
continue; |
|
189 |
this.feature=feature; |
|
190 |
return true; |
|
191 |
|
|
192 |
// if (filter == null) { |
|
193 |
// this.feature=feature; |
|
194 |
// return true; |
|
195 |
// |
|
196 |
// } else { |
|
197 |
// try { |
|
198 |
// if (parser.match(feature)){ |
|
199 |
// this.feature=feature; |
|
200 |
// return true; |
|
201 |
// }else{ |
|
202 |
// continue; |
|
203 |
// } |
|
204 |
// } catch (Exception e) { |
|
205 |
// // TODO Auto-generated catch block |
|
206 |
// e.printStackTrace(); |
|
207 |
// } |
|
208 |
// } |
|
243 |
} catch (java.sql.SQLException e) { |
|
244 |
throw new RuntimeException( |
|
245 |
new ReadException("H2FeatureCollection",e) |
|
246 |
); |
|
209 | 247 |
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
// try { |
|
221 |
// if (rs.isLast()) { |
|
222 |
// rs.close(); |
|
223 |
// return false; |
|
224 |
// } else |
|
225 |
// return true; |
|
226 |
// } catch (SQLException e) { |
|
227 |
// SqlDriveExceptionType type = new SqlDriveExceptionType(); |
|
228 |
// type.setDriverName("H2 Spatial Driver"); |
|
229 |
// try { |
|
230 |
// type.setSql(rs.getStatement().toString()); |
|
231 |
// } catch (SQLException e1) { |
|
232 |
// e1.printStackTrace(); |
|
233 |
// } |
|
234 |
// return false; |
|
235 |
//// throw new ReadDriverException("H2Spatial Driver", e); |
|
236 |
// } |
|
237 | 248 |
} |
238 | 249 |
|
239 | 250 |
public Object next() { |
240 |
// checkModified(); |
|
241 |
if (!nextChecked){ |
|
242 |
hasNext(); |
|
243 |
} |
|
244 |
if (feature == null) |
|
251 |
if (!hasNext()) |
|
245 | 252 |
throw new NoSuchElementException(); |
246 |
nextChecked=false; |
|
247 |
return feature; |
|
253 |
return nextFeature(); |
|
248 | 254 |
} |
249 | 255 |
|
250 | 256 |
private IFeature nextFeature() { |
251 |
IFeature feature=null; |
|
252 | 257 |
try { |
253 |
if(rs.next()){ |
|
254 |
String pk=rs.getString(featureType.getFieldId()); |
|
255 |
feature=new H2Feature(featureType,(H2Driver)driver,pk); |
|
256 |
|
|
257 |
for (int fieldId = 1; fieldId <= featureType.size(); fieldId++) { |
|
258 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get(fieldId-1); |
|
259 |
if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_STRING)) { |
|
260 |
String strAux = rs.getString(fieldId+1); |
|
261 |
if (strAux == null) |
|
262 |
strAux = ""; |
|
263 |
feature.set(fieldId-1,strAux); |
|
264 |
// regAtt[fieldId - 2] = ValueFactory.createValue(strAux); |
|
265 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_INT)) { |
|
266 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
267 |
// .getInt(fieldId)); |
|
268 |
feature.set(fieldId-1,rs.getInt(fieldId+1)); |
|
269 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)) { |
|
270 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
271 |
// .getDouble(fieldId)); |
|
272 |
feature.set(fieldId-1,rs.getDouble(fieldId+1)); |
|
273 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_FLOAT)) { |
|
274 |
feature.set(fieldId-1,rs.getFloat(fieldId+1)); |
|
275 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
276 |
// .getFloat(fieldId)); |
|
277 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_LONG)) { |
|
278 |
feature.set(fieldId-1,rs.getLong(fieldId+1)); |
|
279 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
280 |
// .getLong(fieldId)); |
|
281 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)) { |
|
282 |
feature.set(fieldId-1,rs.getBoolean(fieldId+1)); |
|
283 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
284 |
// .getBoolean(fieldId)); |
|
285 |
// else if(metaData.getColumnType(fieldId) == Types.BOOLEAN) |
|
286 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
287 |
// .getBoolean(fieldId)); |
|
288 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_DATE)) { |
|
289 |
feature.set(fieldId-1,rs.getDate(fieldId+1)); |
|
290 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
291 |
// .getDate(fieldId)); |
|
292 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) { |
|
293 |
byte[] data = rs.getBytes(featureType.getDefaultGeometry()); |
|
294 |
// IGeometry geom = wkbParser.parse(data); |
|
295 |
feature.setDefaultGeometry(null); |
|
296 |
} |
|
258 |
IFeature feature=null; |
|
259 |
try { |
|
260 |
if(rs.next()){ |
|
261 |
feature = H2DriverUtils.createFeature(this.driver, this.rs, this.featureType); |
|
262 |
} else { |
|
263 |
throw new NoSuchElementException(); |
|
297 | 264 |
} |
298 |
return feature; |
|
265 |
if (rs.isAfterLast()){ |
|
266 |
rs.close(); |
|
267 |
} |
|
268 |
|
|
269 |
|
|
270 |
} catch (java.sql.SQLException e) { |
|
271 |
throw new RuntimeException( |
|
272 |
new ReadException("H2FeatureCollection",e) |
|
273 |
); |
|
299 | 274 |
} |
300 |
} catch (SQLException e) { |
|
301 |
e.printStackTrace(); |
|
302 |
// throw new ReadDriverException("H2Spatial Driver", e); |
|
275 |
return feature; |
|
276 |
} catch (BaseException e){ |
|
277 |
throw new RuntimeException( |
|
278 |
new ReadException("H2FeatureCollection",e) |
|
279 |
); |
|
280 |
|
|
303 | 281 |
} |
304 |
return feature; |
|
305 | 282 |
} |
306 | 283 |
|
307 | 284 |
public void remove() { |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2FeatureID.java | ||
---|---|---|
1 | 1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
2 | 2 |
|
3 |
import org.gvsig.data.exception.ReadException; |
|
3 | 4 |
import org.gvsig.data.vectorial.IFeature; |
4 | 5 |
import org.gvsig.data.vectorial.IFeatureID; |
5 | 6 |
import org.gvsig.data.vectorial.IFeatureType; |
6 | 7 |
|
7 | 8 |
public class H2FeatureID implements IFeatureID{ |
8 |
private String featureIndex;
|
|
9 |
private Object featureKey;
|
|
9 | 10 |
private H2Driver driver; |
10 | 11 |
|
11 | 12 |
public H2FeatureID(H2Driver driver,String featureIndex) { |
12 |
this.featureIndex=featureIndex;
|
|
13 |
this.featureKey=featureIndex;
|
|
13 | 14 |
this.driver=driver; |
14 | 15 |
} |
15 |
public String getIndex(){
|
|
16 |
return featureIndex;
|
|
16 |
public Object getKey(){
|
|
17 |
return featureKey;
|
|
17 | 18 |
} |
18 |
public IFeature getFeature(IFeatureType featureType) { |
|
19 |
return driver.getFeatureByID(featureType, featureIndex);
|
|
19 |
public IFeature getFeature(IFeatureType featureType) throws ReadException {
|
|
20 |
return driver.getFeatureByID(featureType, featureKey);
|
|
20 | 21 |
} |
21 | 22 |
public boolean equals(Object obj) { |
22 | 23 |
if (obj instanceof H2FeatureID) |
23 |
return ((H2FeatureID)obj).featureIndex.equals(featureIndex);
|
|
24 |
return ((H2FeatureID)obj).featureKey.equals(featureKey);
|
|
24 | 25 |
return false; |
25 | 26 |
} |
26 | 27 |
|
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2DataSource.java | ||
---|---|---|
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.SQLException; |
|
6 |
|
|
7 |
import org.gvsig.data.IDataSourceParameters; |
|
8 |
import org.gvsig.data.IDataStoreParameters; |
|
9 |
import org.gvsig.data.datastores.vectorial.driver.dbf.DBFDataSourceParameters; |
|
10 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.AbstractDataSourceDBDriverJDBC; |
|
11 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.JDBCDriverNotFoundException; |
|
12 |
import org.gvsig.data.exception.InitializeException; |
|
13 |
import org.gvsig.data.exception.ReadException; |
|
14 |
|
|
15 |
public class H2DataSource extends AbstractDataSourceDBDriverJDBC { |
|
16 |
public static String DATASOURCE_NAME="H2DataSource"; |
|
17 |
|
|
18 |
private Connection connection; |
|
19 |
|
|
20 |
public String getName() { |
|
21 |
// TODO Auto-generated method stub |
|
22 |
return null; |
|
23 |
} |
|
24 |
|
|
25 |
public IDataStoreParameters[] getSources() throws ReadException { |
|
26 |
// TODO Auto-generated method stub |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
public void init(IDataSourceParameters parameters) { |
|
31 |
// TODO Auto-generated method stub |
|
32 |
|
|
33 |
} |
|
34 |
|
|
35 |
Connection getConnection(){ |
|
36 |
// FIXME: OJO REsource manager |
|
37 |
return this.connection; |
|
38 |
|
|
39 |
} |
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
private void initConnection() throws ReadException{ |
|
44 |
// String dburl = this.getParameters().getUrl(); |
|
45 |
// String dbuser = this.getParameters().getUser(); |
|
46 |
// String dbpass = this.getParameters().getPassw(); |
|
47 |
|
|
48 |
|
|
49 |
//TODO: Aqu? habria que implementar la llamada |
|
50 |
// al Resource Manager para comprobar si ya hay |
|
51 |
// una connexion a la BD |
|
52 |
|
|
53 |
Connection conn = null; |
|
54 |
|
|
55 |
// conn = H2DriverUtils.getConnection(dburl, dbuser, dbpass); |
|
56 |
|
|
57 |
|
|
58 |
this.connection = conn; |
|
59 |
|
|
60 |
// conn.getMetaData(). |
|
61 |
} |
|
62 |
|
|
63 |
|
|
64 |
} |
|
0 | 65 |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2Driver.java | ||
---|---|---|
1 | 1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2; |
2 | 2 |
|
3 |
import java.awt.geom.Rectangle2D; |
|
4 |
import java.io.File; |
|
5 |
import java.io.IOException; |
|
6 |
import java.math.BigDecimal; |
|
7 |
import java.nio.ByteBuffer; |
|
8 | 3 |
import java.sql.Connection; |
9 | 4 |
import java.sql.ResultSet; |
10 | 5 |
import java.sql.ResultSetMetaData; |
11 |
import java.sql.SQLException; |
|
12 | 6 |
import java.sql.Statement; |
13 |
import java.sql.Timestamp; |
|
14 |
import java.sql.Types; |
|
15 |
import java.text.DateFormat; |
|
16 |
import java.text.ParseException; |
|
17 |
import java.util.Date; |
|
18 | 7 |
import java.util.Iterator; |
19 |
import java.util.List; |
|
20 | 8 |
import java.util.Locale; |
21 | 9 |
|
22 | 10 |
import org.gvsig.data.datastores.vectorial.driver.AbstractDriver; |
23 | 11 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
24 | 12 |
import org.gvsig.data.datastores.vectorial.driver.IFeaturesReader; |
25 | 13 |
import org.gvsig.data.datastores.vectorial.driver.IFeaturesWriter; |
26 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.ConnectionJDBC; |
|
27 | 14 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType; |
15 |
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.SQLException; |
|
28 | 16 |
import org.gvsig.data.exception.CloseException; |
29 | 17 |
import org.gvsig.data.exception.InitializeException; |
18 |
import org.gvsig.data.exception.OpenException; |
|
30 | 19 |
import org.gvsig.data.exception.ReadException; |
31 |
import org.gvsig.data.exception.UnsupportedVersionException; |
|
32 | 20 |
import org.gvsig.data.spatialprovisional.IExtent; |
33 |
import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
|
34 |
import org.gvsig.data.vectorial.DefaultFeatureType; |
|
35 | 21 |
import org.gvsig.data.vectorial.IFeature; |
36 | 22 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
37 | 23 |
import org.gvsig.data.vectorial.IFeatureCollection; |
... | ... | |
40 | 26 |
import org.gvsig.data.vectorial.expansionadapter.FeatureManager; |
41 | 27 |
import org.gvsig.metadata.IMetadata; |
42 | 28 |
import org.gvsig.metadata.IMetadataManager; |
43 |
import org.gvsig.metadata.Metadata; |
|
44 | 29 |
import org.gvsig.metadata.MetadataManager; |
45 | 30 |
|
46 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
47 | 31 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
48 | 32 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
49 | 33 |
|
... | ... | |
53 | 37 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
54 | 38 |
private DBFeatureType featureType; |
55 | 39 |
protected IMetadata metadata; |
56 |
private Connection connection; |
|
57 |
private String originalEPSG; |
|
58 |
private String sqlOrig; |
|
59 |
protected String whereClause; |
|
60 |
protected String strSRID; |
|
61 |
protected IExtent workingArea; |
|
62 |
private String strEPSG = null; |
|
63 |
private String completeWhere; |
|
64 |
protected Statement st; |
|
65 |
private String sqlTotal; |
|
66 |
private ResultSet rs; |
|
67 |
private ResultSetMetaData metaData; |
|
68 |
private int numReg=-1; |
|
40 |
private Connection connection = null; |
|
41 |
private String sqlSelectPart; //sqlOrig |
|
42 |
private String baseWhereClause = null; |
|
43 |
private String tableId; |
|
44 |
private String baseOrder = null; |
|
69 | 45 |
|
70 |
public H2Driver() {
|
|
46 |
private String sql;
|
|
71 | 47 |
|
48 |
private ResultSetMetaData rsMetaData; |
|
49 |
private int numReg=-1; |
|
50 |
|
|
51 |
private H2DriverParameters getParametersH2(){ |
|
52 |
return (H2DriverParameters)this.parameters; |
|
72 | 53 |
} |
73 | 54 |
|
55 |
Connection getConnection(){ |
|
56 |
// FIXME: OJO REsource manager |
|
57 |
return this.connection; |
|
58 |
|
|
59 |
} |
|
60 |
|
|
74 | 61 |
public void init(IDriverParameters parameters) throws InitializeException { |
75 | 62 |
super.init(parameters); |
76 |
H2DriverParameters h2Parameters=(H2DriverParameters)parameters; |
|
77 |
connection=h2Parameters.getConnection(); |
|
78 |
featureType=h2Parameters.getFeatureType(); |
|
79 |
featureType.setConnection(connection); |
|
80 | 63 |
|
81 |
// getTableEPSG_and_shapeType(connection, featureType); |
|
82 | 64 |
|
83 |
featureType.setDefaultSRS(originalEPSG); |
|
84 | 65 |
|
85 |
try { |
|
86 |
connection.setAutoCommit(false); |
|
87 |
//sqlOrig = "SELECT " + getTotalFields() +", SRID("+ lyrDef.getFieldGeometry()+") AS "+SRID_FIELD_NAME+", GeometryType("+lyrDef.getFieldGeometry()+") AS "+GEOMETRY_TYPE_FIELD_NAME+" FROM "+ getLyrDef().getComposedTableName() + " "; |
|
88 |
sqlOrig = "SELECT " + getTotalFields() +" FROM "+ featureType.getComposedTableName() + " "; |
|
66 |
this.initConnection(); |
|
67 |
this.initFeatureType(); |
|
68 |
this.initSqlProperties(); |
|
89 | 69 |
|
90 |
if (canReproject(strEPSG)) { |
|
91 |
completeWhere = getCompoundWhere(sqlOrig, workingArea, strEPSG); |
|
92 |
} else { |
|
93 |
completeWhere = getCompoundWhere(sqlOrig, workingArea, |
|
94 |
originalEPSG); |
|
95 |
} |
|
96 |
// completeWhere = getLyrDef().getWhereClause() + completeWhere; |
|
97 | 70 |
|
98 |
String sqlAux = sqlOrig + completeWhere + " ORDER BY " |
|
99 |
+ featureType.getFieldId(); |
|
100 | 71 |
|
101 |
sqlTotal = sqlAux; |
|
102 |
// logger.info("Cadena SQL:" + sqlAux); |
|
103 |
st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
|
104 |
ResultSet.CONCUR_READ_ONLY); |
|
105 |
// Statement stAux = connection.createStatement(); |
|
106 |
// st.setFetchSize(FETCH_SIZE); |
|
107 | 72 |
|
108 |
rs = st.executeQuery(sqlAux); |
|
109 | 73 |
|
110 |
metaData = rs.getMetaData(); |
|
111 |
|
|
112 |
// doRelateID_FID(rs); |
|
113 |
|
|
114 | 74 |
//writer.setCreateTable(false); |
115 | 75 |
//writer.setWriteAll(false); |
116 | 76 |
//writer.initialize(lyrDef); |
117 | 77 |
|
78 |
} |
|
79 |
private void initConnection() throws InitializeException{ |
|
80 |
H2DriverParameters dParams = this.getParametersH2(); |
|
118 | 81 |
|
119 |
} catch (SQLException e) { |
|
120 |
e.printStackTrace(); |
|
121 |
// throw new DBException(e); |
|
122 |
} |
|
82 |
String dburl = dParams.getUrl(); |
|
83 |
String dbuser = dParams.getUser(); |
|
84 |
String dbpass = dParams.getPassw(); |
|
123 | 85 |
|
86 |
this.connection = H2DriverUtils.getConnection(dburl, dbuser, dbpass); |
|
124 | 87 |
|
88 |
} |
|
125 | 89 |
|
90 |
private void initSqlProperties() throws InitializeException{ |
|
91 |
H2DriverParameters dParams = this.getParametersH2(); |
|
92 |
sqlSelectPart = "SELECT " + dParams.getFields() +" FROM "+ dParams.tableID(); |
|
126 | 93 |
|
94 |
baseWhereClause = dParams.getBaseFilter(); |
|
127 | 95 |
|
96 |
if (dParams.getWorkingArea() != null){ |
|
97 |
String waWhere = getWorkingAreaWhere(dParams.getWorkingArea(), dParams.getSRISD()); |
|
98 |
if (waWhere != null){ |
|
99 |
baseWhereClause = "(("+baseWhereClause+") and "+waWhere +")"; |
|
100 |
} |
|
128 | 101 |
|
102 |
} |
|
129 | 103 |
|
104 |
this.baseOrder = dParams.getBaseOrder(); |
|
130 | 105 |
|
106 |
} |
|
131 | 107 |
|
108 |
private void initFeatureType() throws InitializeException{ |
|
109 |
H2DriverParameters dParams = this.getParametersH2(); |
|
110 |
this.tableId = dParams.tableID(); |
|
111 |
try { |
|
112 |
this.featureType = H2DriverUtils.getFeatureType(this.connection, dParams.getTableName(), dParams.getFields()); |
|
113 |
} catch (ReadException e) { |
|
114 |
throw new InitializeException("H2Driver",e); |
|
115 |
} |
|
116 |
if (dParams.getFieldId() == null || dParams.getFieldId() == ""){ |
|
117 |
throw new InitializeException( |
|
118 |
"H2Driver", |
|
119 |
new Exception("Field Id not set")); |
|
132 | 120 |
|
121 |
} else if (this.featureType.getFieldIndex(dParams.getFieldId()) < 0) { |
|
122 |
throw new InitializeException( |
|
123 |
"H2Driver", |
|
124 |
new Exception("Field id '"+ dParams.getFieldId() +"' not Found")); |
|
133 | 125 |
|
134 | 126 |
|
127 |
} |
|
128 |
this.featureType.setFieldId(dParams.getFieldId()); |
|
135 | 129 |
|
130 |
if (dParams.getGeometryField() != null && dParams.getGeometryField() != ""){ |
|
131 |
if (this.featureType.getFieldIndex(dParams.getGeometryField())< 0){ |
|
132 |
// FIXME: crear una nueva excepcion?? |
|
133 |
throw new InitializeException( |
|
134 |
"H2Driver", |
|
135 |
new Exception("Geometry Field '"+ dParams.getGeometryField() +"' not Found")); |
|
136 | 136 |
|
137 |
} |
|
137 | 138 |
|
139 |
this.featureType.setDefaultGeometry(dParams.getGeometryField()); |
|
140 |
} |
|
138 | 141 |
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
// featureType=new DefaultFeatureType(); |
|
147 |
// for (int i = 0; i < dbf.getFieldCount(); i++) { |
|
148 |
// featureType.add(createFeatureAttribute(i)); |
|
149 |
// } |
|
150 |
|
|
151 |
|
|
152 |
//Para el writer |
|
153 |
// int aux = (int)(Math.random() * 1000); |
|
154 |
// fTemp = new File(tempDirectoryPath + "/tmpDbf" + aux + ".dbf"); |
|
155 |
// dbfWriter.setFile(fTemp); |
|
156 |
|
|
157 |
|
|
158 | 142 |
} |
159 |
// private IFeatureAttributeDescriptor createFeatureAttribute(int i) { |
|
160 |
// char fieldType = dbf.getFieldType(i); |
|
161 |
// DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor(); |
|
162 |
// dad.setOrdinal(i); |
|
163 |
// dad.setName(dbf.getFieldName(i)); |
|
164 |
// dad.setSize(dbf.getFieldLength(i)); |
|
165 |
// if (fieldType == 'L') { |
|
166 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_BOOLEAN); |
|
167 |
// |
|
168 |
// } else if ((fieldType == 'F') || (fieldType == 'N')) { |
|
169 |
// int precision = dbf.getFieldDecimalLength(i); |
|
170 |
// if (precision > 0){ |
|
171 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
|
172 |
// dad.setPrecision(precision); |
|
173 |
// } else{ |
|
174 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_INT); |
|
175 |
// } |
|
176 |
// } else if (fieldType == 'C') { |
|
177 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
|
178 |
// } else if (fieldType == 'D') { |
|
179 |
// dad.setType(IFeatureAttributeDescriptor.TYPE_DATE); |
|
180 |
// } else { |
|
181 |
//// throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType)); |
|
182 |
// } |
|
183 |
// return dad; |
|
184 |
// |
|
185 |
// } |
|
186 | 143 |
|
187 |
public void open() { |
|
188 |
|
|
144 |
public void open() throws OpenException {
|
|
145 |
// FIXME: Resource Manager |
|
189 | 146 |
} |
147 |
|
|
190 | 148 |
public void close() throws CloseException { |
149 |
// FIXME: Resource Manager |
|
191 | 150 |
try { |
192 | 151 |
connection.close(); |
193 |
} catch (SQLException e) { |
|
152 |
} catch (java.sql.SQLException e) {
|
|
194 | 153 |
throw new CloseException("H2",e); |
195 | 154 |
} |
196 | 155 |
} |
... | ... | |
213 | 172 |
return reader; |
214 | 173 |
} |
215 | 174 |
|
216 |
public IFeature getFeatureByID(IFeatureID id) { |
|
217 |
return getFeatureByID(featureType,((H2FeatureID)id).getIndex());
|
|
175 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException {
|
|
176 |
return getFeatureByID(featureType,((H2FeatureID)id).getKey());
|
|
218 | 177 |
} |
219 | 178 |
|
220 |
// public IFeature getFeatureByPosition(IFeatureType ft,long position) { |
|
221 |
// IFeature feature=new H2Feature(ft,this,position); |
|
222 |
// try { |
|
223 |
// setAbsolutePosition((int)position); |
|
224 |
// |
|
225 |
// |
|
226 |
// ResultSetMetaData metaData = rs.getMetaData(); |
|
227 |
// for (int i=0;i<ft.size();i++) { |
|
228 |
// IFeatureAttributeDescriptor descriptor= (IFeatureAttributeDescriptor)ft.get(i); |
|
229 |
// try{ |
|
230 |
// Object o=rs.getObject(i); |
|
231 |
// if (o == null) |
|
232 |
// feature.set(i,null); |
|
233 |
// else { |
|
234 |
// int columnType=metaData.getColumnType(i); |
|
235 |
// if (columnType == Types.VARCHAR) |
|
236 |
// feature.set(i,rs.getString(i)); |
|
237 |
// if (columnType == Types.FLOAT) |
|
238 |
// feature.set(i,rs.getFloat(i)); |
|
239 |
// if (columnType == Types.DOUBLE) |
|
240 |
// feature.set(i,rs.getDouble(i)); |
|
241 |
// if (columnType == Types.REAL) |
|
242 |
// feature.set(i,rs.getFloat(i)); |
|
243 |
// if (columnType == Types.INTEGER) |
|
244 |
// feature.set(i,rs.getInt(i)); |
|
245 |
// if (columnType == Types.BIGINT) |
|
246 |
// feature.set(i,rs.getLong(i)); |
|
247 |
// if (columnType == Types.BIT) |
|
248 |
// feature.set(i,(rs.getBytes(i)[0] == 1)); |
|
249 |
// if (columnType == Types.BOOLEAN) |
|
250 |
// feature.set(i,rs.getBoolean(i)); |
|
251 |
// if (columnType == Types.DATE) |
|
252 |
// { |
|
253 |
// ByteBuffer buf = ByteBuffer.wrap(rs.getBytes(i)); |
|
254 |
// long daysAfter2000 = buf.getInt() + 1; |
|
255 |
// long msecs = daysAfter2000*24*60*60*1000; |
|
256 |
// long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs); |
|
257 |
// Date realDate1 = new Date(real_msecs_date1); |
|
258 |
// feature.set(i,realDate1); |
|
259 |
// } |
|
260 |
// if (columnType == Types.TIME) |
|
261 |
// { |
|
262 |
// // TODO: |
|
263 |
// // throw new RuntimeException("TIME type not implemented yet"); |
|
264 |
//// return ValueFactory.createValue("NOT IMPLEMENTED YET"); |
|
265 |
// } |
|
266 |
// if (columnType == Types.TIMESTAMP) |
|
267 |
// { |
|
268 |
// ByteBuffer buf = ByteBuffer.wrap(rs.getBytes(i)); |
|
269 |
// double segsReferredTo2000 = buf.getDouble(); |
|
270 |
// long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000); |
|
271 |
// Timestamp valTimeStamp = new Timestamp(real_msecs); |
|
272 |
// feature.set(i,valTimeStamp); |
|
273 |
// } |
|
274 |
// |
|
275 |
// if (columnType == Types.NUMERIC) { |
|
276 |
// // System.out.println(metaData.getColumnName(fieldId) + " " |
|
277 |
// // + metaData.getColumnClassName(fieldId)); |
|
278 |
// ByteBuffer buf = ByteBuffer.wrap(rs.getBytes(i)); |
|
279 |
// short ndigits = buf.getShort(); |
|
280 |
// short weight = buf.getShort(); |
|
281 |
// short sign = buf.getShort(); |
|
282 |
// |
|
283 |
// String strAux; |
|
284 |
// if (sign == 0) |
|
285 |
// strAux = "+"; |
|
286 |
// else |
|
287 |
// strAux = "-"; |
|
288 |
// |
|
289 |
// for (int iDigit = 0; iDigit < ndigits; iDigit++) { |
|
290 |
// short digit = buf.getShort(); |
|
291 |
// strAux = strAux + digit; |
|
292 |
// if (iDigit == weight) |
|
293 |
// strAux = strAux + "."; |
|
294 |
// |
|
295 |
// } |
|
296 |
// strAux = strAux + "0"; |
|
297 |
// BigDecimal dec; |
|
298 |
// dec = new BigDecimal(strAux); |
|
299 |
// // System.out.println(ndigits + "_" + weight + "_" + dscale |
|
300 |
// // + "_" + strAux); |
|
301 |
// // System.out.println(strAux + " Big= " + dec); |
|
302 |
// feature.set(i,dec.doubleValue()); |
|
303 |
// } |
|
304 |
// |
|
305 |
// //return ValueFactory.createValue(byteBuf); |
|
306 |
// } |
|
307 |
// } |
|
308 |
// catch(SQLException except){ |
|
309 |
//// feature.set(i,null); |
|
310 |
// } |
|
311 |
// } |
|
312 |
// } catch (SQLException e) { |
|
313 |
// // TODO Auto-generated catch block |
|
314 |
// e.printStackTrace(); |
|
315 |
// } |
|
316 |
// return feature; |
|
317 |
// |
|
318 |
// |
|
319 |
// |
|
320 |
// |
|
321 |
// |
|
322 |
// |
|
323 |
// |
|
324 |
// |
|
325 |
// |
|
326 |
// |
|
327 |
// |
|
328 |
// |
|
329 |
// |
|
330 |
// if (dbf.getFieldCount()<=i) |
|
331 |
// return feature; |
|
332 |
// String value=dbf.getStringFieldValue((int) position, i); |
|
333 |
// value=value.trim(); |
|
334 |
// String fieldType=descriptor.getDataType(); |
|
335 |
// if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_STRING)){ |
|
336 |
// feature.set(i,value); |
|
337 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){ |
|
338 |
// try{ |
|
339 |
// Double.parseDouble(value); |
|
340 |
// }catch (NumberFormatException e) { |
|
341 |
// value="0"; |
|
342 |
// } |
|
343 |
// feature.set(i,Double.parseDouble(value)); |
|
344 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_INT)){ |
|
345 |
// feature.set(i,Integer.parseInt(value)); |
|
346 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){ |
|
347 |
// feature.set(i,Float.parseFloat(value)); |
|
348 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_LONG)){ |
|
349 |
// feature.set(i,Long.parseLong(value)); |
|
350 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){ |
|
351 |
// feature.set(i,Boolean.parseBoolean(value)); |
|
352 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){ |
|
353 |
// feature.set(i,Byte.parseByte(value)); |
|
354 |
// }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DATE)){ |
|
355 |
// String year = value.substring(0, 4); |
|
356 |
// String month = value.substring(4, 6); |
|
357 |
// String day = value.substring(6, 8); |
|
358 |
// DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ukLocale); |
|
359 |
// /* Calendar c = Calendar.getInstance(); |
|
360 |
// c.clear(); |
|
361 |
// c.set(Integer.parseInt(year), Integer.parseInt(month), |
|
362 |
// Integer.parseInt(day)); |
|
363 |
// c.set(Calendar.MILLISECOND, 0); */ |
|
364 |
// String strAux = month + "/" + day + "/" + year; |
|
365 |
// Date dat=null; |
|
366 |
// try { |
|
367 |
// dat = df.parse(strAux); |
|
368 |
// } catch (ParseException e) { |
|
369 |
//// throw new ReadDriverException(getName(),e); |
|
370 |
// } |
|
371 |
// feature.set(i,dat); |
|
372 |
// }else{ |
|
373 |
// feature.set(i,null); |
|
374 |
// } |
|
375 |
// } |
|
376 |
// return feature; |
|
377 |
// } |
|
378 |
|
|
379 | 179 |
public IFeatureType getFeatureType() { |
380 | 180 |
return featureType; |
381 | 181 |
} |
... | ... | |
398 | 198 |
public boolean canAlterFeatureType() { |
399 | 199 |
return true; |
400 | 200 |
} |
401 |
public long getFeatureCount(){ |
|
201 |
public long getFeatureCount() throws ReadException{
|
|
402 | 202 |
if (numReg == -1) |
403 | 203 |
{ |
204 |
String sql = ""; |
|
404 | 205 |
try |
405 | 206 |
{ |
406 |
Statement s = connection.createStatement(); |
|
407 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + featureType.getTableName() + " " + featureType.getWhereClause()); |
|
207 |
Statement s = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
|
208 |
sql = "SELECT COUNT("+ |
|
209 |
this.featureType.getFieldId() + |
|
210 |
") AS NUMREG FROM " + |
|
211 |
this.tableId + " " + this.getBaseWhereClause(); |
|
212 |
ResultSet r = s.executeQuery(sql); |
|
408 | 213 |
r.next(); |
409 | 214 |
numReg = r.getInt(1); |
410 | 215 |
System.err.println("numReg = " + numReg); |
411 | 216 |
} |
412 |
catch (SQLException e) |
|
217 |
catch (java.sql.SQLException e)
|
|
413 | 218 |
{ |
414 |
// throw new ReadException(getName(),e);
|
|
219 |
throw new SQLException(sql,"getFeatureCount",e);
|
|
415 | 220 |
} |
416 | 221 |
} |
417 | 222 |
|
... | ... | |
422 | 227 |
if (metadata==null){ |
423 | 228 |
IMetadataManager manager=MetadataManager.getManager(); |
424 | 229 |
metadata=manager.create(DRIVER_NAME); |
230 |
//TODO: Apadir los meteadatos |
|
425 | 231 |
} |
426 | 232 |
return metadata; |
427 | 233 |
} |
428 |
private void getTableEPSG_and_shapeType(Connection conn, DBFeatureType dbld) { |
|
429 |
try { |
|
430 |
Statement stAux = conn.createStatement(); |
|
431 |
|
|
432 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '" |
|
433 |
// + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'"; |
|
434 |
String sql= "SELECT SRID("+dbld.getDefaultGeometry()+"), GeometryType("+dbld.getDefaultGeometry()+") FROM "+dbld.getComposedTableName() +" WHERE "+dbld.getDefaultGeometry()+" is not null LIMIT 1"; |
|
435 |
|
|
436 |
ResultSet rs = stAux.executeQuery(sql); |
|
437 |
if(!rs.next()){ |
|
438 |
originalEPSG="-1"; |
|
439 |
dbld.setGeometryTypes(new int[]{FShape.MULTI}); |
|
440 |
return; |
|
441 |
} |
|
442 |
originalEPSG = "" + rs.getInt(1); |
|
443 |
String geometryType = rs.getString(2); |
|
444 |
int shapeType = FShape.MULTI; |
|
445 |
if (geometryType.compareToIgnoreCase("Point") == 0) |
|
446 |
shapeType = FShape.POINT; |
|
447 |
else if (geometryType.compareToIgnoreCase("LineString") == 0) |
|
448 |
shapeType = FShape.LINE; |
|
449 |
else if (geometryType.compareToIgnoreCase("Polygon") == 0) |
|
450 |
shapeType = FShape.POLYGON; |
|
451 |
else if (geometryType.compareToIgnoreCase("MultiPoint") == 0) |
|
452 |
shapeType = FShape.POINT; |
|
453 |
else if (geometryType.compareToIgnoreCase("MultiLineString") == 0) |
|
454 |
shapeType = FShape.LINE; |
|
455 |
else if (geometryType.compareToIgnoreCase("MultiPolygon") == 0) |
|
456 |
shapeType = FShape.POLYGON; |
|
457 |
|
|
458 |
dbld.setGeometryTypes(new int[]{shapeType}); |
|
459 |
rs.close(); |
|
460 |
|
|
461 |
} catch (SQLException e) { |
|
462 |
// TODO Auto-generated catch block |
|
463 |
originalEPSG = "-1"; |
|
464 |
// logger.error(e); |
|
465 |
e.printStackTrace(); |
|
234 |
private String getWorkingAreaWhere(IExtent r, String strEPSG) { |
|
235 |
if (r==null){ |
|
236 |
return null; |
|
466 | 237 |
} |
467 |
|
|
238 |
return null; |
|
468 | 239 |
} |
469 |
public String getTotalFields() |
|
470 |
{ |
|
471 |
String strAux = getFeatureType().getDefaultGeometry(); |
|
472 |
String[] fieldNames = getFields(); |
|
473 |
for (int i=0; i< fieldNames.length; i++) |
|
474 |
{ |
|
475 |
strAux = strAux + ", " + fieldNames[i]; |
|
476 |
} |
|
477 |
return strAux; |
|
478 |
} |
|
479 |
public String[] getFields() { |
|
480 |
String[] names=new String[featureType.size()]; |
|
481 |
for (int i = 0; i < names.length; i++) { |
|
482 |
names[i]=((IFeatureAttributeDescriptor)featureType.get(i)).getName(); |
|
483 |
} |
|
484 | 240 |
|
485 |
return names;
|
|
241 |
private String getWorkingAreaWhereOld(IExtent r, String strEPSG) {
|
|
486 | 242 |
|
487 |
} |
|
488 |
public boolean canReproject(String toEPSGdestinyProjection) { |
|
489 |
// TODO POR AHORA, REPROYECTA SIEMPRE gvSIG. |
|
490 |
return false; |
|
491 |
} |
|
492 |
private String getCompoundWhere(String sql, IExtent r, String strEPSG) { |
|
493 |
if (r == null) |
|
494 |
return getWhereClause(); |
|
495 |
|
|
496 | 243 |
// double xMin = r.getMinX(); |
497 | 244 |
// double yMin = r.getMinY(); |
498 | 245 |
// double xMax = r.getMaxX(); |
... | ... | |
515 | 262 |
//String wktBox="Intersects(envelope("+this.geometryField+"), "+this.geometryField+")"; |
516 | 263 |
|
517 | 264 |
String sqlAux=""; |
518 |
|
|
265 |
/* |
|
519 | 266 |
if(getWhereClause().equals("")){ |
520 | 267 |
sqlAux="WHERE "+wktBox; |
521 | 268 |
} |
... | ... | |
525 | 272 |
else{ |
526 | 273 |
sqlAux="WHERE "+getWhereClause()+" AND "+wktBox; |
527 | 274 |
} |
528 |
|
|
275 |
*/ |
|
529 | 276 |
return sqlAux; |
530 |
} |
|
531 |
public String getWhereClause(){ |
|
532 |
if (featureType.getWhereClause()==null) |
|
533 |
return ""; |
|
534 |
return featureType.getWhereClause().toUpperCase(); |
|
535 |
} |
|
536 |
private void setAbsolutePosition(int index) throws SQLException { |
|
537 | 277 |
|
538 |
//OJO: SE SUMA 1 AL INDICE PROPORCIONADO PORQUE |
|
539 |
//PARA EL RESULTSET LA PRIMERA FILA ES LA 1 Y PARA |
|
540 |
//EL DRIVER DE GVSIG LA PRIMERA FILA ES LA 0 |
|
541 |
|
|
542 |
if(!this.rs.absolute(index+1)){ |
|
543 |
throw new SQLException("The index is out of bounds"); |
|
544 |
} |
|
545 | 278 |
} |
546 | 279 |
|
547 |
public ResultSet getResultSet() { |
|
548 |
return rs; |
|
549 |
} |
|
550 | 280 |
|
551 |
public IFeature getFeatureByID(IFeatureType featureType2, String featureIndex) { |
|
281 |
public IFeature getFeatureByID(IFeatureType featureType2, Object featureKey) throws ReadException{ |
|
282 |
ResultSet rs=null; |
|
552 | 283 |
try{ |
553 |
Statement st=((DBFeatureType)featureType).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
554 |
String columns=""; |
|
555 |
for (Iterator iter = featureType2.iterator(); iter.hasNext();) { |
|
556 |
IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iter.next(); |
|
557 |
columns+=fad.getName()+ ", "; |
|
558 |
} |
|
559 |
columns=columns.substring(0,columns.length()-2); |
|
560 |
String sql = "SELECT "+ columns +" FROM " |
|
561 |
+ ((DBFeatureType)featureType).getComposedTableName() + " WHERE "+ ((DBFeatureType)featureType).getFieldId()+" = "+ "'"+ featureIndex +"'"; |
|
562 |
ResultSet rs=st.executeQuery(sql); |
|
563 |
if (rs.isLast()) { |
|
564 |
rs.close(); |
|
565 |
return null; |
|
566 |
}else{ |
|
567 |
WKBParser2 wkbParser = new WKBParser2(); |
|
284 |
this.open(); |
|
285 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
286 |
String columns=""; |
|
287 |
for (Iterator iter = featureType2.iterator(); iter.hasNext();) { |
|
288 |
IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iter.next(); |
|
289 |
columns+=fad.getName()+ ", "; |
|
290 |
} |
|
291 |
columns=columns.substring(0,columns.length()-2); |
|
292 |
String sql = this.getSqlSelectPart() + " WHERE "+ this.featureType.getFieldId() +" = "+ H2DriverUtils.objectToSqlString(featureKey); |
|
293 |
rs=st.executeQuery(sql); |
|
294 |
if (rs.isLast()) { |
|
568 | 295 |
|
569 |
|
|
570 |
|
|
571 |
|
|
572 |
IFeature feature=null; |
|
573 |
|
|
574 |
if(rs.next()){ |
|
575 |
String pk=rs.getString(featureType.getFieldId()); |
|
576 |
feature=new H2Feature(featureType,(H2Driver)this,pk); |
|
577 |
|
|
578 |
for (int fieldId = 2; fieldId <= featureType.size(); fieldId++) { |
|
579 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.get(fieldId); |
|
580 |
if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_STRING)) { |
|
581 |
String strAux = rs.getString(fieldId); |
|
582 |
if (strAux == null) |
|
583 |
strAux = ""; |
|
584 |
feature.set(fieldId-2,strAux); |
|
585 |
// regAtt[fieldId - 2] = ValueFactory.createValue(strAux); |
|
586 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_INT)) { |
|
587 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
588 |
// .getInt(fieldId)); |
|
589 |
feature.set(fieldId-2,rs.getInt(fieldId)); |
|
590 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)) { |
|
591 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
592 |
// .getDouble(fieldId)); |
|
593 |
feature.set(fieldId-2,rs.getDouble(fieldId)); |
|
594 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_FLOAT)) { |
|
595 |
feature.set(fieldId-2,rs.getFloat(fieldId)); |
|
596 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
597 |
// .getFloat(fieldId)); |
|
598 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_LONG)) { |
|
599 |
feature.set(fieldId-2,rs.getLong(fieldId)); |
|
600 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
601 |
// .getLong(fieldId)); |
|
602 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)) { |
|
603 |
feature.set(fieldId-2,rs.getBoolean(fieldId)); |
|
604 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
605 |
// .getBoolean(fieldId)); |
|
606 |
// else if(metaData.getColumnType(fieldId) == Types.BOOLEAN) |
|
607 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
608 |
// .getBoolean(fieldId)); |
|
609 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_DATE)) { |
|
610 |
feature.set(fieldId-2,rs.getDate(fieldId)); |
|
611 |
// regAtt[fieldId - 2] = ValueFactory.createValue(rs |
|
612 |
// .getDate(fieldId)); |
|
613 |
}else if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) { |
|
614 |
byte[] data = rs.getBytes(featureType.getDefaultGeometry()); |
|
615 |
IGeometry geom = wkbParser.parse(data); |
|
616 |
feature.setDefaultGeometry(geom); |
|
617 |
feature.set(fieldId-2,geom); |
|
618 |
} |
|
619 |
|
|
296 |
return null; |
|
297 |
}else{ |
|
298 |
if(rs.next()){ |
|
299 |
return H2DriverUtils.createFeature(this, rs, this.featureType); |
|
620 | 300 |
} |
621 | 301 |
|
622 |
|
|
623 |
return feature; |
|
624 | 302 |
} |
625 | 303 |
|
304 |
} catch (java.sql.SQLException e) { |
|
305 |
e.printStackTrace(); |
|
306 |
throw new ReadException("H2Spatial Driver", e); |
|
307 |
} finally{ |
|
308 |
if (rs != null) |
|
309 |
try { |
|
310 |
rs.close(); |
|
311 |
} catch (java.sql.SQLException e) { |
|
312 |
// TODO ????? |
|
313 |
e.printStackTrace(); |
|
314 |
} |
|
626 | 315 |
} |
316 |
return null; |
|
317 |
} |
|
627 | 318 |
|
628 |
} catch (SQLException e) { |
|
629 |
e.printStackTrace(); |
|
630 |
// throw new ReadException("H2Spatial Driver", e); |
|
319 |
public String getBaseWhereClause() { |
|
320 |
return baseWhereClause; |
|
631 | 321 |
} |
632 |
return null; |
|
322 |
|
|
323 |
public String getSql() { |
|
324 |
return sql; |
|
633 | 325 |
} |
634 | 326 |
|
327 |
public String getSqlSelectPart() { |
|
328 |
return sqlSelectPart; |
|
329 |
} |
|
330 |
|
|
331 |
public String getBaseOrder() { |
|
332 |
// TODO Auto-generated method stub |
|
333 |
return this.baseOrder; |
|
334 |
} |
|
335 |
|
|
336 |
|
|
635 | 337 |
} |
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2Feature.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
public class H2Feature extends Feature{ |
11 | 11 |
|
12 |
/** |
|
13 |
* |
|
14 |
*/ |
|
15 |
private static final long serialVersionUID = -5836612523458472800L; |
|
12 | 16 |
private H2Driver driver; |
13 | 17 |
private String featureIndex; |
14 | 18 |
|
trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2FeaturesWriter.java | ||
---|---|---|
8 | 8 |
import java.io.UnsupportedEncodingException; |
9 | 9 |
import java.nio.channels.WritableByteChannel; |
10 | 10 |
import java.sql.Connection; |
11 |
import java.sql.DatabaseMetaData; |
|
11 | 12 |
import java.sql.ResultSet; |
12 | 13 |
import java.sql.SQLException; |
13 | 14 |
import java.sql.Statement; |
... | ... | |
37 | 38 |
private Statement st; |
38 | 39 |
private boolean bCreateTable=false; |
39 | 40 |
private String toEncode; |
41 |
private H2Driver driver; |
|
42 |
private H2DriverParameters parameters; |
|
40 | 43 |
|
44 |
|
|
41 | 45 |
public void init(IDriver driver) { |
42 |
H2DriverParameters parameters=(H2DriverParameters)driver.getParameters(); |
|
43 |
this.featureType = parameters.getFeatureType(); |
|
44 |
conex = featureType.getConnection(); |
|
46 |
this.driver = (H2Driver)driver; |
|
47 |
this.parameters=(H2DriverParameters)driver.getParameters(); |
|
48 |
|
|
49 |
this.featureType = (DBFeatureType)this.driver.getFeatureType(); |
|
50 |
conex = this.driver.getConnection(); |
|
45 | 51 |
try { |
Also available in: Unified diff