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 / operations / SQLiteFetchFeatureTypeOperation.java @ 47580

History | View | Annotate | Download (8.9 KB)

1

    
2
package org.gvsig.sqlite.dal.operations;
3

    
4
import java.sql.ResultSet;
5
import java.sql.ResultSetMetaData;
6
import java.sql.SQLException;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
import org.apache.commons.lang3.StringUtils;
13
import org.cresques.cts.IProjection;
14
import org.gvsig.fmap.dal.DataTypes;
15
import org.gvsig.fmap.dal.exception.DataException;
16
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.EditableFeatureType;
18
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
19
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
20
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
21
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
22
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
23
import org.gvsig.fmap.geom.Geometry;
24
import org.gvsig.fmap.geom.GeometryUtils;
25
import org.gvsig.sqlite.dal.geopackage.GeopackageGeometryColumns;
26
import org.gvsig.sqlite.dal.geopackage.GeopackageGeometryColumns.GeopackageGeometryColumn;
27
import org.gvsig.sqlite.dal.geopackage.GeopackageUtils;
28
import org.gvsig.sqlite.dal.geopackage.index.GeopackageIndexManager;
29

    
30
@SuppressWarnings("UseSpecificCatch")
31
public class SQLiteFetchFeatureTypeOperation extends FetchFeatureTypeOperation {
32

    
33
    private Map<String,SQLiteColumnInfo> columns_info;
34

    
35
    public static class SQLiteColumnInfo {
36

    
37
        protected String table_name;
38
        protected String column_column;
39
        protected String column_def;
40

    
41
        public SQLiteColumnInfo() {
42
        }
43

    
44
        public void set(
45
            String table_name,
46
            String column_column,
47
            String column_def
48
        ) {
49
            this.table_name = table_name;
50
            this.column_column = column_column;
51
            this.column_def = column_def;
52
        }
53

    
54
        public void copyfrom(SQLiteColumnInfo other) {
55
            if( other == null ) {
56
                return;
57
            }
58
            this.table_name = other.table_name;
59
            this.column_column = other.column_column;
60
            this.column_def = other.column_def;
61
        }
62
        
63
        /**
64
         * @return the table_name
65
         */
66
        public String getTable_name() {
67
            return table_name;
68
        }
69

    
70
        /**
71
         * @return the column_column
72
         */
73
        public String getColumn_column() {
74
            return column_column;
75
        }
76

    
77
        /**
78
         * @return the column_def
79
         */
80
        public String getColumn_def() {
81
            return column_def;
82
        }
83

    
84
    }
85

    
86
    
87
    public SQLiteFetchFeatureTypeOperation(
88
            JDBCHelper helper
89
        ) {
90
        super(helper);
91
    }
92
    
93
    public SQLiteFetchFeatureTypeOperation(
94
            JDBCHelper helper,
95
            EditableFeatureType featureType,
96
            OperationsFactory.TableReference table,
97
            List<String> primaryKeys,
98
            String defaultGeometryColumn,
99
            IProjection crs,
100
            int geometryType,
101
            int geometrySubtype            
102
        ) {
103
        super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs, geometryType, geometrySubtype);
104
    }            
105

    
106
    @Override
107
    public void fetch(JDBCConnection conn) throws DataException {
108
        this.columns_info = new HashMap<>();
109
        load_columns_def(columns_info);
110
        GeopackageUtils.load_gpkg_geometry_columns(
111
                columns_info, 
112
                conn, 
113
                this.table.getTable(), 
114
                this.createSQLBuilder()
115
        );
116
        super.fetch(conn);
117
    }
118
    
119
    @Override
120
    protected int getDataTypeFromMetadata(
121
            ResultSetMetaData rsMetadata,
122
            int colIndex
123
        ) throws SQLException {
124

    
125
        String columnName = rsMetadata.getColumnName(colIndex);
126
        String typeName = rsMetadata.getColumnTypeName(colIndex);
127
        
128

    
129
        SQLiteColumnInfo columninfo = this.columns_info.get(columnName);
130
        if( columninfo!=null ) {
131
            if( columninfo instanceof GeopackageGeometryColumn ) {
132
                return DataTypes.GEOMETRY;
133
            }
134
            if( StringUtils.isNotBlank(columninfo.getColumn_def()) ) {
135
                int columnType = rsMetadata.getColumnType(colIndex);
136
                if( columnType == java.sql.Types.INTEGER && (
137
                    StringUtils.contains(columninfo.getColumn_def(), "date(") || 
138
                    StringUtils.contains(columninfo.getColumn_def(), "datetime(") || 
139
                    StringUtils.contains(columninfo.getColumn_def(), "strftime(") )
140
                    ) {
141
                    return DataTypes.DATE;
142
                }
143
            }
144
            if(StringUtils.equalsIgnoreCase(typeName, "TIME")) {
145
                return DataTypes.TIME;
146
            }
147
            if(StringUtils.equalsIgnoreCase(typeName, "DATETIME")) {
148
                return DataTypes.TIMESTAMP;
149
            }
150
            if(StringUtils.equalsIgnoreCase(typeName, "TIMESTAMP")) {
151
                return DataTypes.TIMESTAMP;
152
            }
153
        }
154
        return super.getDataTypeFromMetadata(rsMetadata, colIndex);
155
    }
156
        
157
    
158
    @Override
159
    protected void fetchGeometryTypeAndSRS(
160
            EditableFeatureAttributeDescriptor attr,
161
            ResultSetMetaData rsMetadata,
162
            int colIndex
163
        ) {
164
        
165
        if( attr.getType()!=DataTypes.GEOMETRY ) {
166
            return;
167
        }
168
        SQLiteColumnInfo columninfo = this.columns_info.get(attr.getName());
169
        if( !(columninfo instanceof GeopackageGeometryColumn) ) {
170
            return;
171
        }
172
        try {
173
            GeopackageGeometryColumn geominfo = (GeopackageGeometryColumn) columninfo;
174
            IProjection proj = this.helper.getSRSSolver().getProjection(conn, geominfo.getSrs_id());                
175
            int type = Geometry.TYPES.GEOMETRY;
176
            int subtype = Geometry.SUBTYPES.GEOM2D;
177

    
178
            if( ( geominfo.getZ()==1 || geominfo.getZ()==2 ) && ( geominfo.getM()==1  || geominfo.getM()==2)) {
179
                subtype = Geometry.SUBTYPES.GEOM3DM;
180
            } else if( geominfo.getZ()==1 || geominfo.getZ()==2 ) {
181
                subtype = Geometry.SUBTYPES.GEOM3D;
182
            } else if( geominfo.getM()==1  || geominfo.getM()==2) {
183
                subtype = Geometry.SUBTYPES.GEOM2DM;
184
            }
185
            if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "POINT") ) {
186
                type = Geometry.TYPES.POINT;
187
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "LINESTRING") ) {
188
                type = Geometry.TYPES.LINE;
189
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "POLYGON") ) {
190
                type = Geometry.TYPES.POLYGON;
191
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTIPOINT") ) {
192
                type = Geometry.TYPES.MULTIPOINT;
193
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTILINESTRING") ) {
194
                type = Geometry.TYPES.MULTILINE;
195
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTIPOLYGON") ) {
196
                type = Geometry.TYPES.MULTIPOLYGON;
197
            }
198

    
199
            attr.setSRS(proj);
200
            attr.setGeometryType(GeometryUtils.getGeometryType(type, subtype));
201
            
202
            GeopackageUtils.getIndexManager().checkGeometryIndexes(conn, this.table.getTable(), attr);
203

    
204
        } catch (Exception ex) {
205
            LOGGER.debug("Can't get geometry type and srs from column '"+attr.getName()+"'.",ex);
206
        }
207
    }
208
    private void load_columns_def(Map<String,SQLiteColumnInfo> columns_info) throws AccessResourceException {
209
        try {
210
            ResultSet dbmetadata = this.getConnection().getMetaData().getColumns(null, null, table.getTable(), null);
211
            while( dbmetadata.next() ) {
212
                String columnname = dbmetadata.getString("COLUMN_NAME");
213
                SQLiteColumnInfo column_info = columns_info.get(columnname);
214
                if( column_info == null ) {
215
                    column_info = new SQLiteColumnInfo();
216
                }
217
                column_info.column_def = StringUtils.defaultString(dbmetadata.getString("COLUMN_DEF"));
218
                columns_info.put(columnname, column_info);
219
            }
220
        } catch (SQLException ex) {
221
            LOGGER.warn("Can't read metadata from table '"+table+"'.",ex);
222
        }
223
    }
224

    
225
    @Override
226
    protected List<String> getPrimaryKeysFromInformationSchema(JDBCConnection conn) throws SQLException {
227
        return Collections.EMPTY_LIST;
228
    }
229

    
230
    @Override
231
    public String getSQLToRetrievePrimaryKeysFromInformationSchema() throws SQLException {
232
        return null;
233
    }
234
    
235
    
236
    
237
    
238
}