root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2Explorer.java @ 24491
History | View | Annotate | Download (11.5 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc.h2; |
---|---|
2 |
|
3 |
import java.security.KeyException; |
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
|
11 |
import org.gvsig.fmap.dal.DataExplorerParameters; |
12 |
import org.gvsig.fmap.dal.DataStoreParameters; |
13 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
14 |
import org.gvsig.fmap.dal.exceptions.CloseException; |
15 |
import org.gvsig.fmap.dal.exceptions.DataException; |
16 |
import org.gvsig.fmap.dal.exceptions.InitializeException; |
17 |
import org.gvsig.fmap.dal.exceptions.ReadException; |
18 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
19 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
21 |
import org.gvsig.fmap.dal.feature.exceptions.InitializeWriterException; |
22 |
import org.gvsig.fmap.dal.resource.ResourceManager; |
23 |
import org.gvsig.fmap.dal.resource.impl.DefaultResourceManager; |
24 |
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor; |
25 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
26 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCExplorer; |
27 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCExplorerParameter; |
28 |
|
29 |
public class H2Explorer extends JDBCExplorer { |
30 |
public static String DATAEXPLORER_NAME = "H2Explorer"; |
31 |
private String defaultSchema; |
32 |
|
33 |
private void appendFieldToCreteSQL(DBAttributeDescriptor attr,StringBuffer sql) throws InitializeException{ |
34 |
/**
|
35 |
* {name dataType
|
36 |
[{AS computedColumnExpression | DEFAULT expression}]
|
37 |
[[NOT] NULL]
|
38 |
[{AUTO_INCREMENT | IDENTITY}[(startInt [, incrementInt])]]
|
39 |
[SELECTIVITY selectivity]
|
40 |
[PRIMARY KEY [HASH] | UNIQUE]
|
41 |
| constraint}
|
42 |
*/
|
43 |
|
44 |
//name
|
45 |
sql.append(attr.getName()); |
46 |
sql.append(" ");
|
47 |
|
48 |
//dataType
|
49 |
String type =attr.getDataType();
|
50 |
if (type.equals(FeatureAttributeDescriptor.STRING)){
|
51 |
if (attr.getSize() < 1){ |
52 |
sql.append("VARCHAR");
|
53 |
} else {
|
54 |
sql.append("VARCHAR("+attr.getSize()+")"); |
55 |
} |
56 |
} else if (type.equals(FeatureAttributeDescriptor.BOOLEAN)){ |
57 |
sql.append("BOOL");
|
58 |
} else if (type.equals(FeatureAttributeDescriptor.BYTE)){ |
59 |
sql.append("TINYINT");
|
60 |
} else if (type.equals(FeatureAttributeDescriptor.DATE)){ |
61 |
sql.append("DATE");
|
62 |
} else if (type.equals(FeatureAttributeDescriptor.TIMESTAMP)){ |
63 |
sql.append("TIMESTAMP");
|
64 |
} else if (type.equals(FeatureAttributeDescriptor.TIME)){ |
65 |
sql.append("TIME");
|
66 |
} else if (type.equals(FeatureAttributeDescriptor.DOUBLE)){ |
67 |
sql.append("DOUBLE");
|
68 |
if (attr.getPrecision() > 0){ |
69 |
sql.append(" "+ attr.getPrecision());
|
70 |
} |
71 |
} else if (type.equals(FeatureAttributeDescriptor.FLOAT)){ |
72 |
sql.append("REAL");
|
73 |
} else if (type.equals(FeatureAttributeDescriptor.GEOMETRY)){ |
74 |
sql.append("OTHER");
|
75 |
} else if (type.equals(FeatureAttributeDescriptor.INT)){ |
76 |
if (attr.isAutoIncrement()){
|
77 |
sql.append("IDENTITY");
|
78 |
}else{
|
79 |
sql.append("INT");
|
80 |
} |
81 |
} else if (type.equals(FeatureAttributeDescriptor.LONG)){ |
82 |
if (attr.isAutoIncrement()){
|
83 |
sql.append("IDENTITY");
|
84 |
}else{
|
85 |
sql.append("BIGINT");
|
86 |
} |
87 |
} else {
|
88 |
throw new InitializeException(this.getName(),new Exception("Unsuported type "+type)); |
89 |
} |
90 |
sql.append(" ");
|
91 |
|
92 |
// //DefeaultValue
|
93 |
// if (attr.getDefaultValue() != null || (attr.getDefaultValue() == null && attr.isAllowNull() )){
|
94 |
// sql.append("DEFAULT ? ");
|
95 |
// sqlParams.add(attr.getDefaultValue());
|
96 |
//
|
97 |
// }
|
98 |
|
99 |
//Null
|
100 |
if (attr.isAllowNull()){
|
101 |
sql.append("NOT NULL ");
|
102 |
} else {
|
103 |
sql.append("NULL ");
|
104 |
} |
105 |
|
106 |
//Primery key
|
107 |
if (attr.isPrimaryKey()){
|
108 |
sql.append("PRIMARY KEY ");
|
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* @deprecated
|
115 |
*
|
116 |
* @param attr
|
117 |
* @param sql
|
118 |
* @param sqlParams
|
119 |
* @throws InitializeException
|
120 |
*/
|
121 |
private void appendFieldToCreteSQL(DBAttributeDescriptor attr,StringBuffer sql,List sqlParams) throws InitializeException{ |
122 |
/**
|
123 |
* {name dataType
|
124 |
[{AS computedColumnExpression | DEFAULT expression}]
|
125 |
[[NOT] NULL]
|
126 |
[{AUTO_INCREMENT | IDENTITY}[(startInt [, incrementInt])]]
|
127 |
[SELECTIVITY selectivity]
|
128 |
[PRIMARY KEY [HASH] | UNIQUE]
|
129 |
| constraint}
|
130 |
*/
|
131 |
|
132 |
//name
|
133 |
sql.append(attr.getName()); |
134 |
sql.append(" ");
|
135 |
|
136 |
//dataType
|
137 |
String type =attr.getDataType();
|
138 |
if (type.equals(FeatureAttributeDescriptor.STRING)){
|
139 |
if (attr.getSize() < 1){ |
140 |
sql.append("VARCHAR");
|
141 |
} else {
|
142 |
sql.append("VARCHAR("+attr.getSize()+")"); |
143 |
} |
144 |
} else if (type.equals(FeatureAttributeDescriptor.BOOLEAN)){ |
145 |
sql.append("BOOL");
|
146 |
} else if (type.equals(FeatureAttributeDescriptor.BYTE)){ |
147 |
sql.append("TINYINT");
|
148 |
} else if (type.equals(FeatureAttributeDescriptor.DATE)){ |
149 |
sql.append("DATE");
|
150 |
} else if (type.equals(FeatureAttributeDescriptor.TIMESTAMP)){ |
151 |
sql.append("TIMESTAMP");
|
152 |
} else if (type.equals(FeatureAttributeDescriptor.TIME)){ |
153 |
sql.append("TIME");
|
154 |
} else if (type.equals(FeatureAttributeDescriptor.DOUBLE)){ |
155 |
sql.append("DOUBLE");
|
156 |
if (attr.getPrecision() > 0){ |
157 |
sql.append(" "+ attr.getPrecision());
|
158 |
} |
159 |
} else if (type.equals(FeatureAttributeDescriptor.FLOAT)){ |
160 |
sql.append("REAL");
|
161 |
} else if (type.equals(FeatureAttributeDescriptor.GEOMETRY)){ |
162 |
sql.append("OTHER");
|
163 |
} else if (type.equals(FeatureAttributeDescriptor.INT)){ |
164 |
if (attr.isAutoIncrement()){
|
165 |
sql.append("IDENTITY");
|
166 |
}else{
|
167 |
sql.append("INT");
|
168 |
} |
169 |
} else if (type.equals(FeatureAttributeDescriptor.LONG)){ |
170 |
if (attr.isAutoIncrement()){
|
171 |
sql.append("IDENTITY");
|
172 |
}else{
|
173 |
sql.append("BIGINT");
|
174 |
} |
175 |
} else {
|
176 |
throw new InitializeException(this.getName(),new Exception("Unsuported type "+type)); |
177 |
} |
178 |
sql.append(" ");
|
179 |
|
180 |
//DefeaultValue
|
181 |
if (attr.getDefaultValue() != null || (attr.getDefaultValue() == null && attr.isAllowNull() )){ |
182 |
sql.append("DEFAULT ? ");
|
183 |
sqlParams.add(attr.getDefaultValue()); |
184 |
} |
185 |
|
186 |
//Null
|
187 |
if (attr.isAllowNull()){
|
188 |
sql.append("NOT NULL ");
|
189 |
} |
190 |
|
191 |
//Primery key
|
192 |
if (attr.isPrimaryKey()){
|
193 |
sql.append("PRIMARY KEY ");
|
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
|
199 |
public DataStoreParameters add(NewFeatureStoreParameters ndsp)
|
200 |
throws InitializeException, InitializeWriterException {
|
201 |
Connection conn;
|
202 |
try {
|
203 |
conn = this.getConnection();
|
204 |
} catch (ReadException e1) {
|
205 |
throw new InitializeException(this.getName(),e1); |
206 |
} |
207 |
|
208 |
try {
|
209 |
conn.setAutoCommit(true);
|
210 |
} catch (SQLException e) { |
211 |
throw new InitializeException(this.getName(),e); |
212 |
} |
213 |
DataStoreParameters params = this.add(ndsp,conn);
|
214 |
|
215 |
return params;
|
216 |
} |
217 |
|
218 |
|
219 |
protected DataStoreParameters add(NewFeatureStoreParameters ndsp,Connection conn) |
220 |
throws InitializeException, InitializeWriterException {
|
221 |
|
222 |
|
223 |
/**
|
224 |
CREATE [CACHED | MEMORY | TEMP | [GLOBAL | LOCAL] TEMPORARY]
|
225 |
TABLE [IF NOT EXISTS] name
|
226 |
{ ( {name dataType
|
227 |
[{AS computedColumnExpression | DEFAULT expression}]
|
228 |
[[NOT] NULL]
|
229 |
[{AUTO_INCREMENT | IDENTITY}[(startInt [, incrementInt])]]
|
230 |
[SELECTIVITY selectivity]
|
231 |
[PRIMARY KEY [HASH] | UNIQUE]
|
232 |
| constraint} [,...] ) [ AS select ] } | { AS select }
|
233 |
*/
|
234 |
|
235 |
// PreparedStatement st=null;
|
236 |
Statement st=null; |
237 |
StringBuffer sql = new StringBuffer(); |
238 |
ArrayList sqlParamas = new ArrayList(); |
239 |
|
240 |
if (!ndsp.isValid()){
|
241 |
//TODO Exception
|
242 |
throw new InitializeException(this.getName(),new Exception("Parameters not valid")); |
243 |
} |
244 |
H2StoreParameters h2Param = (H2StoreParameters)ndsp.getDataStoreParameters(); |
245 |
DBFeatureType fType = (DBFeatureType)ndsp.getFeatureType(); |
246 |
|
247 |
sql.append("Create table "+ h2Param.tableID()+"("); |
248 |
DBAttributeDescriptor attr; |
249 |
int i;
|
250 |
FeatureAttributeDescriptor[] fads=(FeatureAttributeDescriptor[])fType.toArray(new FeatureAttributeDescriptor[0]); |
251 |
for (int j = 0; j < fads.length-1; j++) { |
252 |
|
253 |
// appendFieldToCreteSQL(attr, sql,sqlParamas);
|
254 |
appendFieldToCreteSQL((DBAttributeDescriptor)fads[j], sql); |
255 |
sql.append(",");
|
256 |
} |
257 |
attr = (DBAttributeDescriptor)fads[fads.length-1];
|
258 |
appendFieldToCreteSQL(attr, sql); |
259 |
sql.append(")");
|
260 |
|
261 |
try{
|
262 |
// st = conn.prepareStatement(sql.toString());
|
263 |
// for (i=0;i<sqlParamas.size();i++){
|
264 |
// st.setObject(i+1, sqlParamas.get(i));
|
265 |
// }
|
266 |
st = conn.createStatement(); |
267 |
st.execute(sql.toString()); |
268 |
} catch (SQLException e) { |
269 |
throw new InitializeException(this.getName(),e); |
270 |
} finally{
|
271 |
if (st != null){ |
272 |
try {
|
273 |
st.close(); |
274 |
} catch (SQLException e) { |
275 |
// TODO ???
|
276 |
e.printStackTrace(); |
277 |
} |
278 |
} |
279 |
} |
280 |
return h2Param;
|
281 |
} |
282 |
|
283 |
public NewDataStoreParameters createNewDataStoreParameter()
|
284 |
throws InitializeException {
|
285 |
return new H2NewStoreParameter(this.parameters.newStoreParameters()); |
286 |
} |
287 |
|
288 |
public void remove(DataStoreParameters dsp) throws ReadException { |
289 |
Connection conn = this.getConnection(); |
290 |
|
291 |
try {
|
292 |
conn.setAutoCommit(true);
|
293 |
} catch (SQLException e) { |
294 |
throw new InitializeException(this.getName(),e); |
295 |
} |
296 |
this.remove(dsp, conn);
|
297 |
} |
298 |
|
299 |
protected void remove(DataStoreParameters dsp,Connection conn) throws ReadException { |
300 |
Statement st=null; |
301 |
try{
|
302 |
st = conn.createStatement(); |
303 |
st.execute("Drop table "+ ((H2StoreParameters)dsp).tableID());
|
304 |
} catch (SQLException e) { |
305 |
throw new ReadException(this.getName(),e); |
306 |
} finally{
|
307 |
if (st != null){ |
308 |
try {
|
309 |
st.close(); |
310 |
} catch (SQLException e) { |
311 |
// TODO ???
|
312 |
e.printStackTrace(); |
313 |
} |
314 |
} |
315 |
} |
316 |
} |
317 |
|
318 |
public boolean canCreate() { |
319 |
return true; |
320 |
} |
321 |
|
322 |
public String getName() { |
323 |
return DATAEXPLORER_NAME;
|
324 |
} |
325 |
|
326 |
public void init(DataExplorerParameters parameters) throws InitializeException { |
327 |
|
328 |
H2Resource tmpResource = new H2Resource((H2ExplorerParameters)parameters);
|
329 |
H2Resource theResource; |
330 |
DefaultResourceManager resMan = DefaultResourceManager.getResourceManager(); |
331 |
|
332 |
try {
|
333 |
theResource = (H2Resource) resMan.addResource(tmpResource, this);
|
334 |
} catch (DataException e1) {
|
335 |
throw new InitializeException(this.getName(),e1); |
336 |
} |
337 |
|
338 |
super.init(parameters,theResource);
|
339 |
try {
|
340 |
((H2Resource)this.resource).close();
|
341 |
this.defaultSchema = H2Utils.getDefaultSchema(this.getConnection(), |
342 |
((JDBCExplorerParameter) this.parameters).getCatalog());
|
343 |
} catch (ReadException e) {
|
344 |
throw new InitializeException(this.getName(),e); |
345 |
} |
346 |
} |
347 |
|
348 |
|
349 |
public DataStoreParameters[] list() throws ReadException { |
350 |
return this.list(((H2ExplorerParameters)this.parameters).isShowInformationDBTables()); |
351 |
} |
352 |
|
353 |
public DataStoreParameters[] list(boolean showInformationDBTables) throws ReadException { |
354 |
Connection conn = this.getConnection(); |
355 |
String sql = "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"; |
356 |
if (!showInformationDBTables){
|
357 |
sql = sql + " WHERE TABLE_SCHEMA <> 'INFORMATION_SCHEMA'";
|
358 |
} |
359 |
ArrayList paramList = new ArrayList(); |
360 |
H2StoreParameters h2param = null;
|
361 |
|
362 |
try{
|
363 |
Statement st = conn.createStatement();
|
364 |
ResultSet rs = st.executeQuery(sql);
|
365 |
while (rs.next()){
|
366 |
h2param = (H2StoreParameters) this.parameters
|
367 |
.newStoreParameters(); |
368 |
h2param.setCatalog(rs.getString(1));
|
369 |
h2param.setSchema(rs.getString(2));
|
370 |
h2param.setTableName(rs.getString(3));
|
371 |
h2param.setFields(new String[] {"*"}); |
372 |
paramList.add(h2param); |
373 |
|
374 |
} |
375 |
|
376 |
|
377 |
} catch (SQLException e) { |
378 |
throw new ReadException(this.getName(),e); |
379 |
} |
380 |
|
381 |
return (H2StoreParameters[])paramList.toArray(new H2StoreParameters[0]); |
382 |
|
383 |
} |
384 |
|
385 |
public FeatureType[] getFeatureTypes(DataStoreParameters dsp) throws ReadException { |
386 |
return new FeatureType[] {H2Utils.getFeatureType(this.getConnection(), (H2StoreParameters)dsp)}; |
387 |
} |
388 |
|
389 |
public String getDefaultSchema(){ |
390 |
return this.defaultSchema; |
391 |
} |
392 |
|
393 |
/* (non-Javadoc)
|
394 |
* @see org.gvsig.fmap.dal.DataExplorer#dispose()
|
395 |
*/
|
396 |
public void dispose() throws DataException { |
397 |
ResourceManager resMan = DefaultResourceManager.getResourceManager(); |
398 |
|
399 |
try {
|
400 |
resMan.remove(this.resource, this); |
401 |
} catch (DataException e1) {
|
402 |
throw new CloseException(this.getName(),e1); |
403 |
} catch (KeyException e) { |
404 |
throw new CloseException(this.getName(),e); |
405 |
} |
406 |
|
407 |
} |
408 |
|
409 |
} |