Revision 41536
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/Appender.java | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
package org.gvsig.fmap.dal.store.jdbc; |
|
4 |
|
|
5 |
import java.sql.Connection; |
|
6 |
import java.sql.PreparedStatement; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.List; |
|
10 |
import org.gvsig.fmap.dal.exception.DataException; |
|
11 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
12 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
13 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
14 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
|
15 |
import org.slf4j.Logger; |
|
16 |
import org.slf4j.LoggerFactory; |
|
17 |
|
|
18 |
public interface Appender { |
|
19 |
|
|
20 |
public void begin() throws DataException; |
|
21 |
|
|
22 |
public void end() throws DataException; |
|
23 |
|
|
24 |
public void abort() throws DataException ; |
|
25 |
|
|
26 |
public void append(FeatureProvider feature) throws DataException ; |
|
27 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCResource.java | ||
---|---|---|
234 | 234 |
this.dataSource = dataSource; |
235 | 235 |
} |
236 | 236 |
|
237 |
|
|
237 |
public void beginUse() { |
|
238 |
this.executeBegins(); |
|
239 |
} |
|
240 |
|
|
241 |
public void endUse() { |
|
242 |
this.executeEnds(); |
|
243 |
} |
|
238 | 244 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCAppender.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.store.jdbc; |
|
2 |
|
|
3 |
import java.sql.Connection; |
|
4 |
import java.sql.PreparedStatement; |
|
5 |
import java.sql.SQLException; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
import java.util.logging.Level; |
|
9 |
import org.gvsig.fmap.dal.exception.DataException; |
|
10 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
11 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
|
12 |
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException; |
|
13 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
14 |
import org.gvsig.fmap.dal.feature.exception.StoreCancelEditingException; |
|
15 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
16 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
|
17 |
import org.slf4j.Logger; |
|
18 |
import org.slf4j.LoggerFactory; |
|
19 |
|
|
20 |
public class JDBCAppender implements Appender { |
|
21 |
|
|
22 |
final static private Logger logger = LoggerFactory |
|
23 |
.getLogger(JDBCAppender.class); |
|
24 |
|
|
25 |
public String sql; |
|
26 |
public List<FeatureAttributeDescriptor> attributes; |
|
27 |
public Connection connection = null; |
|
28 |
protected JDBCStoreProviderWriter provider; |
|
29 |
|
|
30 |
public JDBCAppender(JDBCStoreProviderWriter provider) { |
|
31 |
this.provider = provider; |
|
32 |
} |
|
33 |
|
|
34 |
public void begin() throws DataException { |
|
35 |
if (this.connection != null) { |
|
36 |
throw new AlreadyEditingException(this.getSourceId()); |
|
37 |
} |
|
38 |
try { |
|
39 |
StringBuilder sqlb = new StringBuilder(); |
|
40 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<FeatureAttributeDescriptor>(); |
|
41 |
|
|
42 |
prepareSQLAndAttributeListForInsert(sqlb, attrs); |
|
43 |
|
|
44 |
sql = sqlb.toString(); |
|
45 |
attributes = attrs; |
|
46 |
this.connection = this.provider.getHelper().getConnection(); |
|
47 |
this.provider.getHelper().getResource().beginUse(); |
|
48 |
this.connection.setAutoCommit(false); |
|
49 |
} catch (SQLException ex) { |
|
50 |
try { |
|
51 |
this.connection = null; |
|
52 |
this.provider.getHelper().getResource().endUse(); |
|
53 |
} catch(Exception ex1) { |
|
54 |
// Ignore errors |
|
55 |
} |
|
56 |
throw new PerformEditingException(this.getSourceId(), ex); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
public void end() throws DataException { |
|
61 |
if (this.connection == null) { |
|
62 |
throw new NeedEditingModeException(this.getSourceId()); |
|
63 |
} |
|
64 |
sql = null; |
|
65 |
attributes = null; |
|
66 |
resetCount(); |
|
67 |
try { |
|
68 |
this.connection.commit(); |
|
69 |
} catch (SQLException ex) { |
|
70 |
throw new PerformEditingException(this.getSourceId().toString(), ex); |
|
71 |
} finally { |
|
72 |
this.connection = null; |
|
73 |
this.provider.getHelper().getResource().endUse(); |
|
74 |
} |
|
75 |
|
|
76 |
} |
|
77 |
|
|
78 |
public void abort() throws DataException { |
|
79 |
if (this.connection == null) { |
|
80 |
throw new StoreCancelEditingException(null, this.getSourceId()); |
|
81 |
} |
|
82 |
sql = null; |
|
83 |
attributes = null; |
|
84 |
resetCount(); |
|
85 |
try { |
|
86 |
this.connection.rollback(); |
|
87 |
} catch (SQLException ex) { |
|
88 |
throw new PerformEditingException(this.getSourceId(), ex); |
|
89 |
} finally { |
|
90 |
this.connection = null; |
|
91 |
this.provider.getHelper().getResource().endUse(); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
public void append(FeatureProvider feature) throws DataException { |
|
96 |
if (this.connection == null) { |
|
97 |
throw new NeedEditingModeException(this.getSourceId()); |
|
98 |
} |
|
99 |
|
|
100 |
PreparedStatement st = null; |
|
101 |
try { |
|
102 |
st = connection.prepareStatement(sql); |
|
103 |
perfomInsert(connection, st, sql, feature, attributes); |
|
104 |
resetCount(); |
|
105 |
|
|
106 |
} catch (SQLException e) { |
|
107 |
throw new JDBCPreparingSQLException(sql, e); |
|
108 |
} catch (Exception e) { |
|
109 |
throw new PerformEditingException(this.getSourceId(), e); |
|
110 |
} finally { |
|
111 |
try { |
|
112 |
st.close(); |
|
113 |
} catch (Exception ex) { |
|
114 |
// Do nothing |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
protected void prepareSQLAndAttributeListForInsert(StringBuilder sqlb, |
|
120 |
List<FeatureAttributeDescriptor> attributes) throws DataException { |
|
121 |
this.provider.prepareSQLAndAttributeListForInsert(sqlb, attributes); |
|
122 |
} |
|
123 |
|
|
124 |
private void perfomInsert(Connection conn, PreparedStatement insertSt, |
|
125 |
String sql, FeatureProvider feature, List<FeatureAttributeDescriptor> attributes) |
|
126 |
throws DataException { |
|
127 |
this.provider.perfomInsert(conn, insertSt, sql, feature, attributes); |
|
128 |
} |
|
129 |
|
|
130 |
private void resetCount() { |
|
131 |
this.provider.resetCount(); |
|
132 |
} |
|
133 |
|
|
134 |
private String getSourceId() { |
|
135 |
return this.provider.getSourceId().toString(); |
|
136 |
} |
|
137 |
|
|
138 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCStoreProviderWriter.java | ||
---|---|---|
60 | 60 |
final static private Logger logger = LoggerFactory |
61 | 61 |
.getLogger(JDBCStoreProviderWriter.class); |
62 | 62 |
|
63 |
protected Appender appender; |
|
63 | 64 |
|
64 |
protected String appendModeSql; |
|
65 |
protected List<FeatureAttributeDescriptor> appendModeAttributes; |
|
66 |
|
|
67 |
|
|
68 | 65 |
public JDBCStoreProviderWriter(JDBCStoreParameters params, |
69 | 66 |
DataStoreProviderServices storeServices) |
70 | 67 |
throws InitializeException { |
71 | 68 |
super(params, storeServices); |
69 |
this.appender = this.createAppender(); |
|
72 | 70 |
} |
73 | 71 |
|
74 | 72 |
protected JDBCStoreProviderWriter(JDBCStoreParameters params, |
75 | 73 |
DataStoreProviderServices storeServices, DynObject metadata) |
76 | 74 |
throws InitializeException { |
77 | 75 |
super(params, storeServices, metadata); |
76 |
this.appender = this.createAppender(); |
|
78 | 77 |
} |
79 | 78 |
|
79 |
protected Appender createAppender() { |
|
80 |
return new JDBCAppender(this); |
|
81 |
} |
|
80 | 82 |
|
83 |
public boolean supportsAppendMode() { |
|
84 |
return true; |
|
85 |
} |
|
81 | 86 |
|
87 |
public void endAppend() throws DataException { |
|
88 |
this.appender.end(); |
|
89 |
} |
|
90 |
|
|
91 |
public void beginAppend() throws DataException { |
|
92 |
this.appender.begin(); |
|
93 |
} |
|
94 |
|
|
95 |
public void append(final FeatureProvider featureProvider) throws DataException { |
|
96 |
this.appender.append(featureProvider); |
|
97 |
} |
|
98 |
|
|
82 | 99 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
83 | 100 |
FeatureAttributeDescriptor attrOfList, |
84 | 101 |
FeatureAttributeDescriptor attr, List<Object> values) throws DataException { |
... | ... | |
244 | 261 |
|
245 | 262 |
} |
246 | 263 |
|
247 |
public boolean supportsAppendMode() { |
|
248 |
return true; |
|
249 |
} |
|
250 |
|
|
251 |
public void endAppend() throws DataException { |
|
252 |
appendModeSql = null; |
|
253 |
appendModeAttributes = null; |
|
254 |
} |
|
255 |
|
|
256 | 264 |
protected List<String> getSqlStatementAlterField( |
257 | 265 |
FeatureAttributeDescriptor attrOrg, |
258 | 266 |
FeatureAttributeDescriptor attrTrg, List<String> additionalStatement) |
... | ... | |
411 | 419 |
} |
412 | 420 |
|
413 | 421 |
|
414 |
private void perfomInsert(Connection conn, PreparedStatement insertSt,
|
|
422 |
public void perfomInsert(Connection conn, PreparedStatement insertSt,
|
|
415 | 423 |
String sql, FeatureProvider feature, List<FeatureAttributeDescriptor> attributes) |
416 | 424 |
throws DataException { |
417 | 425 |
|
... | ... | |
440 | 448 |
} |
441 | 449 |
} |
442 | 450 |
|
443 |
public void append(final FeatureProvider featureProvider) throws DataException { |
|
444 |
TransactionalAction action = new TransactionalAction() { |
|
445 |
public Object action(Connection conn) throws DataException { |
|
446 | 451 |
|
447 |
PreparedStatement st; |
|
448 |
try { |
|
449 |
st = conn.prepareStatement(appendModeSql); |
|
450 |
} catch (SQLException e) { |
|
451 |
throw new JDBCPreparingSQLException(appendModeSql, e); |
|
452 |
} |
|
453 |
try { |
|
454 |
perfomInsert(conn, st, appendModeSql, featureProvider, |
|
455 |
appendModeAttributes); |
|
456 |
} finally { |
|
457 |
try { |
|
458 |
st.close(); |
|
459 |
} catch (SQLException e) { |
|
460 |
} |
|
461 |
; |
|
462 |
} |
|
463 |
return null; |
|
464 |
} |
|
465 |
|
|
466 |
public boolean continueTransactionAllowed() { |
|
467 |
return false; |
|
468 |
} |
|
469 |
}; |
|
470 |
try { |
|
471 |
this.helper.doConnectionAction(action); |
|
472 |
|
|
473 |
resetCount(); |
|
474 |
|
|
475 |
} catch (Exception e) { |
|
476 |
throw new PerformEditingException(this.getSourceId().toString(), e); |
|
477 |
} |
|
478 |
} |
|
479 |
|
|
480 | 452 |
protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr, |
481 | 453 |
List<String> values) { |
482 | 454 |
values.add(helper.escapeFieldName(attr.getName()) + " = ?"); |
... | ... | |
614 | 586 |
} |
615 | 587 |
|
616 | 588 |
|
617 |
public void beginAppend() throws DataException { |
|
618 |
StringBuilder sqlb = new StringBuilder(); |
|
619 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<FeatureAttributeDescriptor>(); |
|
620 | 589 |
|
621 |
prepareSQLAndAttributeListForInsert(sqlb, attrs); |
|
622 |
|
|
623 |
appendModeSql = sqlb.toString(); |
|
624 |
appendModeAttributes = attrs; |
|
625 |
} |
|
626 |
|
|
627 |
|
|
628 | 590 |
protected TransactionalAction getPerformChangesAction( |
629 | 591 |
final Iterator<FeatureReferenceProviderServices> deleteds, |
630 | 592 |
final Iterator<FeatureProvider> inserteds, |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.spi/src/main/java/org/gvsig/fmap/dal/feature/spi/AbstractFeatureStoreProvider.java | ||
---|---|---|
442 | 442 |
throw new UnsupportedOperationException(); |
443 | 443 |
} |
444 | 444 |
|
445 |
public void abortAppend() throws DataException { |
|
446 |
// FIXME exception |
|
447 |
throw new UnsupportedOperationException(); |
|
448 |
} |
|
449 |
|
|
445 | 450 |
/** |
446 | 451 |
* unsupported by default, override this otherwise |
447 | 452 |
* |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.spi/src/main/java/org/gvsig/fmap/dal/feature/spi/FeatureStoreProvider.java | ||
---|---|---|
223 | 223 |
public void endAppend() throws DataException; |
224 | 224 |
|
225 | 225 |
public void append(FeatureProvider featureProvider) throws DataException; |
226 |
|
|
227 |
public void abortAppend() throws DataException; |
|
226 | 228 |
|
227 | 229 |
/** |
228 | 230 |
* Return if the provider knows the real envelope of a layer. If not, |
Also available in: Unified diff