svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / operations / SQLitePerfomChangesOperation.java @ 47606
History | View | Annotate | Download (5.69 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.sqlite.dal.operations; |
7 |
|
8 |
import java.sql.Blob; |
9 |
import java.sql.PreparedStatement; |
10 |
import java.sql.ResultSet; |
11 |
import java.sql.SQLException; |
12 |
import java.sql.Statement; |
13 |
import java.util.Iterator; |
14 |
import java.util.List; |
15 |
import java.util.Map; |
16 |
import org.apache.commons.collections.CollectionUtils; |
17 |
import org.apache.commons.lang3.StringUtils; |
18 |
import org.gvsig.fmap.dal.DataTypes; |
19 |
import static org.gvsig.fmap.dal.SQLBuilder.AlterTableBuilder.ALTER_COLUMN_CREATE_INDEX; |
20 |
import org.gvsig.fmap.dal.exception.DataException; |
21 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
22 |
import org.gvsig.fmap.dal.feature.FeatureType; |
23 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
24 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
25 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
26 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
27 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
28 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
29 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
30 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
31 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation; |
32 |
import org.gvsig.fmap.geom.Geometry; |
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
34 |
import org.gvsig.sqlite.dal.geopackage.GeopackageGeometryParser; |
35 |
import org.gvsig.sqlite.dal.geopackage.GeopackageUtils; |
36 |
import org.gvsig.tools.util.Bitmask; |
37 |
import org.sqlite.SQLiteConnection; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author fdiaz
|
42 |
*/
|
43 |
public class SQLitePerfomChangesOperation extends PerformChangesOperation { |
44 |
|
45 |
public SQLitePerfomChangesOperation(JDBCHelper helper) {
|
46 |
super(helper);
|
47 |
} |
48 |
public SQLitePerfomChangesOperation(JDBCHelper helper, OperationsFactory.TableReference table, FeatureType featureType, Iterator<FeatureReferenceProviderServices> deleteds, Iterator<FeatureProvider> inserteds, Iterator<FeatureProvider> updateds, Iterator<FeatureType.FeatureTypeChanged> featureTypesChanged, FeatureStoreProviderServices storeServices) { |
49 |
super(helper, table, featureType, deleteds, inserteds, updateds, featureTypesChanged, storeServices);
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void performUpdateTable(JDBCConnection conn) throws DataException { |
54 |
|
55 |
FeatureType currentFeatureTypeDB = getCurrentFeatureTypeDB(); |
56 |
Map<String, Bitmask> changes = getUpdateTableChanges(currentFeatureTypeDB); |
57 |
List<String> sqls = getUpdateTableSQLs(currentFeatureTypeDB, changes); |
58 |
|
59 |
if( !CollectionUtils.isEmpty(sqls) ) {
|
60 |
Statement st = null; |
61 |
String currentsql = null; |
62 |
try {
|
63 |
st = conn.createStatement(); |
64 |
for (String sql : sqls) { |
65 |
currentsql = sql; |
66 |
if( !StringUtils.isBlank(sql) ) {
|
67 |
JDBCUtils.execute(st, sql); |
68 |
} |
69 |
} |
70 |
} catch (SQLException e) { |
71 |
throw new JDBCSQLException(e,currentsql); |
72 |
} finally {
|
73 |
JDBCUtils.closeQuietly(st); |
74 |
} |
75 |
} |
76 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
77 |
if( attrTarget.isComputed() ) {
|
78 |
continue;
|
79 |
} |
80 |
Bitmask changesAttr = changes.get(attrTarget.getName()); |
81 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
82 |
if( changesAttr.isSetBit(ALTER_COLUMN_CREATE_INDEX) ) {
|
83 |
populateSpatialIndex(attrTarget.getName()); |
84 |
} |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
private void populateSpatialIndex(String name) { |
90 |
PreparedStatement insertStmt = null; |
91 |
Statement selectStmt = null; |
92 |
ResultSet rs = null; |
93 |
try {
|
94 |
String insertSql = String.format("INSERT OR REPLACE INTO \"rtree_%s_%s\" VALUES (?,?,?,?,?)", |
95 |
this.table.getTable(),
|
96 |
name |
97 |
); |
98 |
insertStmt = this.getConnection().prepareStatement(insertSql);
|
99 |
FeatureAttributeDescriptor attrId = this.featureTypeTarget.getPrimaryKey()[0]; |
100 |
String selectSql = String.format("SELECT \"%s\", \"%s\" FROM \"%s\"", attrId.getName(), name, this.table.getTable()); |
101 |
selectStmt = this.getConnection().createStatement(selectSql);
|
102 |
rs = selectStmt.executeQuery(selectSql); |
103 |
SQLiteConnection sqlConn = (SQLiteConnection) this.getConnection().get();
|
104 |
while(rs.next()){
|
105 |
String id = rs.getString(1); |
106 |
byte[] geomBytes = rs.getBytes(2); |
107 |
|
108 |
GeopackageGeometryParser parser = GeopackageUtils.createGeometryParser(); |
109 |
|
110 |
Geometry geom1 = parser.parseToGeometry(sqlConn, geomBytes); |
111 |
if (geom1 == null) { |
112 |
continue;
|
113 |
} |
114 |
Envelope envelope = geom1.getEnvelope(); |
115 |
insertStmt.setString(1, id);
|
116 |
insertStmt.setDouble(2, envelope.getMinimum(Geometry.DIMENSIONS.X));
|
117 |
insertStmt.setDouble(3, envelope.getMaximum(Geometry.DIMENSIONS.X));
|
118 |
insertStmt.setDouble(4, envelope.getMinimum(Geometry.DIMENSIONS.Y));
|
119 |
insertStmt.setDouble(5, envelope.getMaximum(Geometry.DIMENSIONS.Y));
|
120 |
insertStmt.execute(); |
121 |
} |
122 |
} catch (Exception ex) { |
123 |
LOGGER.warn("Can't populate index for column '"+name+"' table '"+this.table.getTable()+"'." , ex); |
124 |
} finally {
|
125 |
JDBCUtils.closeQuietly(insertStmt); |
126 |
JDBCUtils.closeQuietly(rs); |
127 |
JDBCUtils.closeQuietly(selectStmt); |
128 |
} |
129 |
} |
130 |
|
131 |
|
132 |
|
133 |
} |