Revision 20058 trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/JDBCStore.java
JDBCStore.java | ||
---|---|---|
3 | 3 |
import java.sql.Connection; |
4 | 4 |
import java.sql.ResultSet; |
5 | 5 |
import java.sql.Statement; |
6 |
import java.util.ArrayList; |
|
6 | 7 |
import java.util.Iterator; |
8 |
import java.util.List; |
|
7 | 9 |
|
8 | 10 |
import org.gvsig.data.commands.implementation.AbstractAttributeCommand; |
9 | 11 |
import org.gvsig.data.commands.implementation.AbstractFeatureCommand; |
10 | 12 |
import org.gvsig.data.commands.implementation.UpdateAttributeCommand; |
11 | 13 |
import org.gvsig.data.commands.implementation.UpdateFeatureCommand; |
12 | 14 |
import org.gvsig.data.datastores.vectorial.ISelectiveWriter; |
15 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureID; |
|
16 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
|
13 | 17 |
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException; |
14 | 18 |
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2Utils; |
15 | 19 |
import org.gvsig.data.exception.ReadException; |
... | ... | |
18 | 22 |
import org.gvsig.data.vectorial.CreatedFeature; |
19 | 23 |
import org.gvsig.data.vectorial.IFeature; |
20 | 24 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
25 |
import org.gvsig.data.vectorial.IFeatureID; |
|
21 | 26 |
import org.gvsig.data.vectorial.IFeatureType; |
27 |
import org.gvsig.metadata.IMetadata; |
|
28 |
import org.gvsig.metadata.IMetadataManager; |
|
29 |
import org.gvsig.metadata.MetadataManager; |
|
22 | 30 |
|
23 | 31 |
public abstract class JDBCStore extends AbstractFeatureStore { |
24 | 32 |
|
... | ... | |
28 | 36 |
protected String baseOrder = null; |
29 | 37 |
protected String sqlSource = null; |
30 | 38 |
protected boolean useSqlSource = false; |
39 |
protected DBFeatureType featureType; |
|
40 |
protected IMetadata metadata; |
|
31 | 41 |
|
32 | 42 |
|
33 | 43 |
protected abstract Connection getConnection(); |
34 | 44 |
|
35 |
public abstract IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException;
|
|
45 |
protected abstract IFeature newFeatureInstance(DBFeatureType featureType,Object[] pk);
|
|
36 | 46 |
|
47 |
protected abstract void loadValueFromResulset(ResultSet rs,IFeature feature,IFeatureAttributeDescriptor attr) throws ReadException; |
|
48 |
|
|
37 | 49 |
public boolean isEditable() { |
38 | 50 |
return !this.useSqlSource; |
39 | 51 |
} |
... | ... | |
137 | 149 |
|
138 | 150 |
} |
139 | 151 |
|
152 |
public String getFilterForID(DBFeatureType fType, Object[] featureKey) { |
|
153 |
if (fType.getFieldsId().length != 1) |
|
154 |
throw new UnsupportedOperationException("ID fields > 1"); |
|
155 |
String id =fType.getFieldsId()[0]; |
|
156 |
return id + " = " + objectToSqlString(featureKey[0]); |
|
157 |
} |
|
158 |
|
|
159 |
protected String objectToSqlString(Object obj) { |
|
160 |
if (obj instanceof String){ |
|
161 |
return "'"+ scapeString((String)obj) +"'"; |
|
162 |
} else if (obj == null){ |
|
163 |
return "null"; |
|
164 |
}else{ |
|
165 |
// OJO con otros tipos!! |
|
166 |
return obj.toString(); |
|
167 |
} |
|
168 |
|
|
169 |
} |
|
170 |
|
|
171 |
protected String scapeString(String str) { |
|
172 |
return str.replace("'", "''"); |
|
173 |
} |
|
174 |
|
|
175 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException { |
|
176 |
return getFeatureByID((DBFeatureType)this.getDefaultFeatureType(),((DBFeatureID)id).getKey()); |
|
177 |
} |
|
178 |
|
|
179 |
public IFeatureType getDefaultFeatureType() { |
|
180 |
return this.featureType; |
|
181 |
} |
|
182 |
|
|
183 |
public IMetadata getMetadata() { |
|
184 |
if (metadata==null){ |
|
185 |
IMetadataManager manager=MetadataManager.getManager(); |
|
186 |
metadata=manager.create(this.getName()); |
|
187 |
//TODO: Apadir los meteadatos |
|
188 |
} |
|
189 |
return metadata; |
|
190 |
} |
|
191 |
|
|
192 |
public List getFeatureTypes() { |
|
193 |
ArrayList list = new ArrayList(); |
|
194 |
list.add(this.featureType); |
|
195 |
return list; |
|
196 |
} |
|
197 |
|
|
198 |
public boolean isWithDefaultLegend() { |
|
199 |
// TODO Auto-generated method stub |
|
200 |
return false; |
|
201 |
} |
|
202 |
|
|
203 |
public Object getDefaultLabelingStrategy() { |
|
204 |
// TODO Auto-generated method stub |
|
205 |
return null; |
|
206 |
} |
|
207 |
|
|
208 |
public Object getDefaultLegend() { |
|
209 |
// TODO Auto-generated method stub |
|
210 |
return null; |
|
211 |
} |
|
212 |
|
|
213 |
public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException { |
|
214 |
//TODO: Tener en cuenta el FeatureType por si es distinto |
|
215 |
if (useSqlSource){ |
|
216 |
throw new ReadException(this.getName(), |
|
217 |
new UnsupportedOperationException("Unsuported featureByID in sqlSource mode")); |
|
218 |
} |
|
219 |
ResultSet rs=null; |
|
220 |
try{ |
|
221 |
this.open(); |
|
222 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
223 |
String sql = this.getSqlSelectPart() + " WHERE "+ this.getFilterForID(this.featureType, featureKey); |
|
224 |
rs=st.executeQuery(sql); |
|
225 |
if(rs.next()){ |
|
226 |
return createFeature(rs, this.featureType); |
|
227 |
} |
|
228 |
|
|
229 |
} catch (java.sql.SQLException e) { |
|
230 |
e.printStackTrace(); |
|
231 |
throw new ReadException(this.getName(), e); |
|
232 |
} finally{ |
|
233 |
if (rs != null) |
|
234 |
try { |
|
235 |
rs.close(); |
|
236 |
} catch (java.sql.SQLException e) { |
|
237 |
// TODO ????? |
|
238 |
e.printStackTrace(); |
|
239 |
} |
|
240 |
} |
|
241 |
return null; |
|
242 |
} |
|
243 |
|
|
244 |
protected Object[] getPkFromResulset(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
|
245 |
String[] fieldsId = featureType.getFieldsId(); |
|
246 |
Object[] result = new Object[fieldsId.length]; |
|
247 |
for (int i=0;i<fieldsId.length;i++){ |
|
248 |
result[i] = rs.getObject(fieldsId[i]); |
|
249 |
} |
|
250 |
return result; |
|
251 |
|
|
252 |
} |
|
253 |
|
|
254 |
protected IFeature createFeature(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
|
255 |
JDBCFeature feature=null; |
|
256 |
|
|
257 |
if (featureType2 == null){ |
|
258 |
featureType2 = (DBFeatureType)this.getDefaultFeatureType(); |
|
259 |
} |
|
260 |
|
|
261 |
|
|
262 |
Object[] pk; |
|
263 |
try { |
|
264 |
pk = getPkFromResulset(rs, featureType2); |
|
265 |
|
|
266 |
feature=(JDBCFeature)this.newFeatureInstance(featureType2, pk); |
|
267 |
|
|
268 |
Iterator iter = featureType2.iterator(); |
|
269 |
while (iter.hasNext()) { |
|
270 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)iter.next(); |
|
271 |
this.loadValueFromResulset(rs, feature, fad); |
|
272 |
|
|
273 |
} |
|
274 |
return feature; |
|
275 |
} catch (java.sql.SQLException e) { |
|
276 |
throw new ReadException(this.getName(),e); |
|
277 |
} |
|
278 |
|
|
279 |
} |
|
140 | 280 |
} |
Also available in: Unified diff