Statistics
| Revision:

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 / geopackage / index / AbstractGeopackageIndex.java @ 47456

History | View | Annotate | Download (5.32 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.geopackage.index;
7

    
8
import java.sql.ResultSet;
9
import java.sql.SQLException;
10
import java.sql.Statement;
11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
14
import org.gvsig.fmap.geom.Geometry;
15
import org.gvsig.fmap.geom.GeometryLocator;
16
import org.gvsig.fmap.geom.GeometryManager;
17
import org.gvsig.fmap.geom.primitive.Envelope;
18
import org.gvsig.sqlite.dal.utils.TemplateUtils;
19
import org.sqlite.SQLiteException;
20

    
21
/**
22
 *
23
 * @author jjdelcerro
24
 */
25
@SuppressWarnings("UseSpecificCatch")
26
public abstract class AbstractGeopackageIndex implements GeopackageIndex {
27
    
28
    public AbstractGeopackageIndex() {
29

    
30
    }
31
    
32
    protected abstract String getGroup();
33

    
34
    protected abstract String getAttributeIndexFlagName();
35
    
36
    @Override
37
    public boolean hasIndex(JDBCConnection conn, String tablename, String fieldnamegeom) {
38
        String sql = TemplateUtils.getSqlTemplate(this.getGroup(), "has_index", tablename, fieldnamegeom);
39
        Statement st = null;
40
        ResultSet rs = null;
41
        try {
42
            st = conn.createStatement();
43
            rs = JDBCUtils.executeQuery(st, sql);
44
            if( !rs.next() ) {
45
                return false;
46
            }
47
            int n = rs.getInt(1);
48
            return n==1;
49
        } catch(SQLiteException ex) {
50
            if( ex.getMessage().contains("no such table") ) {
51
                return false;
52
            }
53
            throw new RuntimeException("Can't get information about "+this.getGroup()+" index",ex);
54
        } catch(SQLException ex) {
55
            throw new RuntimeException("Can't get information about "+this.getGroup()+" index",ex);
56
        } finally {
57
            JDBCUtils.closeQuietly(rs);
58
            JDBCUtils.closeQuietly(st);
59
        }
60
        
61
    }
62
    
63

    
64
    @Override
65
    public void createIndex(JDBCConnection conn, String tablename, String fieldnamegeom, String fieldnameid) throws SQLException {
66
        for (String sql : this.getCreateIndexSQL(tablename, fieldnamegeom, fieldnameid)) {
67
            JDBCUtils.execute(conn.get(), sql);
68
        }
69
    }
70

    
71
    @Override
72
    public String getBBoxIntersectsFilter(String tablename, String fieldnameid, String fieldnamegeom, double minX, double minY, double maxX, double maxY) {
73
        String sql = TemplateUtils.getSqlTemplate(getGroup(), "bboxintersects", tablename, fieldnameid, fieldnamegeom, minX, minY, maxX, maxY);
74
        return sql;
75
    }
76

    
77
    @Override
78
    public String getBBoxIntersectsFilter(String tablename, String fieldnameid, String fieldnamegeom, String minX, String minY, String maxX, String maxY) {
79
        String sql = TemplateUtils.getSqlTemplate(getGroup(), "bboxintersects", tablename, fieldnameid, minX, minY, maxX, maxY);
80
        return sql;
81
    }
82

    
83
    @Override
84
    public String getBBoxIntersectsFilter(String tablename, String fieldnameid, String fieldnamegeom, Envelope env) {
85
        String sql = this.getBBoxIntersectsFilter(
86
                tablename, 
87
                fieldnameid, 
88
                fieldnamegeom,
89
                env.getMinimum(Geometry.DIMENSIONS.X), 
90
                env.getMinimum(Geometry.DIMENSIONS.Y), 
91
                env.getMaximum(Geometry.DIMENSIONS.X), 
92
                env.getMaximum(Geometry.DIMENSIONS.Y)
93
        );
94
        return sql;
95
    }
96

    
97
    @Override
98
    public String getBBoxIntersectsFilter(String tablename, String fieldnameid, String fieldnamegeom, Geometry geom) {
99
        return this.getBBoxIntersectsFilter(tablename, fieldnameid, fieldnamegeom, geom.getEnvelope());
100
    }
101

    
102
    @Override
103
    public void setHasIndex(FeatureAttributeDescriptor attr, boolean b) {
104
        attr.getTags().set(this.getAttributeIndexFlagName(), b?1:0);
105
    }
106
    
107
    @Override
108
    public boolean hasIndex(FeatureAttributeDescriptor attr) {
109
        return attr.getTags().getInt(this.getAttributeIndexFlagName(), 0)==1;
110
    }
111
    
112
    @Override
113
    public Envelope getEnvelope(JDBCConnection conn, String tablename, String fieldnamegeom) throws SQLException {
114
        String sql = this.getEnvelopeSQL(tablename, fieldnamegeom);
115
        Statement st = null;
116
        ResultSet rs = null;
117
        try {
118
            st = conn.createStatement();
119
            rs = JDBCUtils.executeQuery(st, sql);
120
            if( !rs.next() ) {
121
                return null;
122
            }
123
            int minx = rs.getInt("minx");
124
            int miny = rs.getInt("miny");
125
            int maxx = rs.getInt("maxx");
126
            int maxy = rs.getInt("maxy");
127
            GeometryManager geommanager = GeometryLocator.getGeometryManager();
128
            Envelope env = geommanager.createEnvelope(minx, miny, maxx, maxy, Geometry.SUBTYPES.GEOM2D);
129
            return env;
130
        } catch(SQLiteException ex) {
131
            if( ex.getMessage().contains("no such table") ) {
132
                return null;
133
            }
134
            throw new RuntimeException("Can't get envelop of index for group "+this.getGroup(),ex);
135
        } catch(Throwable ex) {
136
            throw new RuntimeException("Can't get envelop of index for group "+this.getGroup(),ex);
137
        } finally {
138
            JDBCUtils.closeQuietly(rs);
139
            JDBCUtils.closeQuietly(st);
140
        }
141
    }
142
}