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 @ 47746

History | View | Annotate | Download (9.56 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.DataManager;
15
import org.gvsig.fmap.dal.DataTypes;
16
import org.gvsig.fmap.dal.exception.DataException;
17
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.EditableFeatureType;
19
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
20
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
21
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
22
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
23
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
24
import org.gvsig.fmap.geom.Geometry;
25
import org.gvsig.fmap.geom.GeometryUtils;
26
import org.gvsig.sqlite.dal.geopackage.GeopackageGeometryColumns;
27
import org.gvsig.sqlite.dal.geopackage.GeopackageGeometryColumns.GeopackageGeometryColumn;
28
import org.gvsig.sqlite.dal.geopackage.GeopackageUtils;
29
import org.gvsig.sqlite.dal.geopackage.index.GeopackageIndexManager;
30
import org.gvsig.tools.dataTypes.DataType;
31

    
32
@SuppressWarnings("UseSpecificCatch")
33
public class SQLiteFetchFeatureTypeOperation extends FetchFeatureTypeOperation {
34

    
35
    private Map<String,SQLiteColumnInfo> columns_info;
36

    
37
    public static class SQLiteColumnInfo {
38

    
39
        protected String table_name;
40
        protected String column_column;
41
        protected String column_def;
42

    
43
        public SQLiteColumnInfo() {
44
        }
45

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

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

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

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

    
86
    }
87

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

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

    
128
        EditableFeatureAttributeDescriptor attr = super.getAttributeFromMetadata(type, conn, rsMetadata, colIndex);
129
        switch (attr.getType()) {
130
            case DataTypes.STRING:
131
                if(attr.getSize() < 1){
132
                    attr.setSize(DataManager.RECOMENDED_SIZE_FOR_CLOB);
133
                }
134
        }
135
        return attr;
136
    }
137

    
138
    @Override
139
    protected int getDataTypeFromMetadata(
140
            ResultSetMetaData rsMetadata,
141
            int colIndex
142
        ) throws SQLException {
143

    
144
        String columnName = rsMetadata.getColumnName(colIndex);
145
        String typeName = rsMetadata.getColumnTypeName(colIndex);
146
        
147

    
148
        SQLiteColumnInfo columninfo = this.columns_info.get(columnName);
149
        if( columninfo!=null ) {
150
            if( columninfo instanceof GeopackageGeometryColumn ) {
151
                return DataTypes.GEOMETRY;
152
            }
153
            if( StringUtils.isNotBlank(columninfo.getColumn_def()) ) {
154
                int columnType = rsMetadata.getColumnType(colIndex);
155
                if( columnType == java.sql.Types.INTEGER && (
156
                    StringUtils.contains(columninfo.getColumn_def(), "date(") || 
157
                    StringUtils.contains(columninfo.getColumn_def(), "datetime(") || 
158
                    StringUtils.contains(columninfo.getColumn_def(), "strftime(") )
159
                    ) {
160
                    return DataTypes.DATE;
161
                }
162
            }
163
            if(StringUtils.equalsIgnoreCase(typeName, "TIME")) {
164
                return DataTypes.TIME;
165
            }
166
            if(StringUtils.equalsIgnoreCase(typeName, "DATETIME")) {
167
                return DataTypes.TIMESTAMP;
168
            }
169
            if(StringUtils.equalsIgnoreCase(typeName, "TIMESTAMP")) {
170
                return DataTypes.TIMESTAMP;
171
            }
172
        }
173
        return super.getDataTypeFromMetadata(rsMetadata, colIndex);
174
    }
175
        
176
    
177
    @Override
178
    protected void fetchGeometryTypeAndSRS(
179
            EditableFeatureAttributeDescriptor attr,
180
            ResultSetMetaData rsMetadata,
181
            int colIndex
182
        ) {
183
        
184
        if( attr.getType()!=DataTypes.GEOMETRY ) {
185
            return;
186
        }
187
        SQLiteColumnInfo columninfo = this.columns_info.get(attr.getName());
188
        if( !(columninfo instanceof GeopackageGeometryColumn) ) {
189
            return;
190
        }
191
        try {
192
            GeopackageGeometryColumn geominfo = (GeopackageGeometryColumn) columninfo;
193
            IProjection proj = this.helper.getSRSSolver().getProjection(conn, geominfo.getSrs_id());                
194
            int type = Geometry.TYPES.GEOMETRY;
195
            int subtype = Geometry.SUBTYPES.GEOM2D;
196

    
197
            if( ( geominfo.getZ()==1 || geominfo.getZ()==2 ) && ( geominfo.getM()==1  || geominfo.getM()==2)) {
198
                subtype = Geometry.SUBTYPES.GEOM3DM;
199
            } else if( geominfo.getZ()==1 || geominfo.getZ()==2 ) {
200
                subtype = Geometry.SUBTYPES.GEOM3D;
201
            } else if( geominfo.getM()==1  || geominfo.getM()==2) {
202
                subtype = Geometry.SUBTYPES.GEOM2DM;
203
            }
204
            if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "POINT") ) {
205
                type = Geometry.TYPES.POINT;
206
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "LINESTRING") ) {
207
                type = Geometry.TYPES.LINE;
208
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "POLYGON") ) {
209
                type = Geometry.TYPES.POLYGON;
210
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTIPOINT") ) {
211
                type = Geometry.TYPES.MULTIPOINT;
212
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTILINESTRING") ) {
213
                type = Geometry.TYPES.MULTILINE;
214
            } else if( StringUtils.containsIgnoreCase(geominfo.getGeometry_type_name(), "MULTIPOLYGON") ) {
215
                type = Geometry.TYPES.MULTIPOLYGON;
216
            }
217

    
218
            attr.setSRS(proj);
219
            attr.setGeometryType(GeometryUtils.getGeometryType(type, subtype));
220
            
221
            GeopackageUtils.getIndexManager().checkGeometryIndexes(conn, this.table.getTable(), attr);
222

    
223
        } catch (Exception ex) {
224
            LOGGER.debug("Can't get geometry type and srs from column '"+attr.getName()+"'.",ex);
225
        }
226
    }
227
    private void load_columns_def(Map<String,SQLiteColumnInfo> columns_info) throws AccessResourceException {
228
        try {
229
            ResultSet dbmetadata = this.getConnection().getMetaData().getColumns(null, null, table.getTable(), null);
230
            while( dbmetadata.next() ) {
231
                String columnname = dbmetadata.getString("COLUMN_NAME");
232
                SQLiteColumnInfo column_info = columns_info.get(columnname);
233
                if( column_info == null ) {
234
                    column_info = new SQLiteColumnInfo();
235
                }
236
                column_info.column_def = StringUtils.defaultString(dbmetadata.getString("COLUMN_DEF"));
237
                columns_info.put(columnname, column_info);
238
            }
239
        } catch (SQLException ex) {
240
            LOGGER.warn("Can't read metadata from table '"+table+"'.",ex);
241
        }
242
    }
243

    
244
    @Override
245
    protected List<String> getPrimaryKeysFromInformationSchema(JDBCConnection conn) throws SQLException {
246
        return Collections.EMPTY_LIST;
247
    }
248

    
249
    @Override
250
    public String getSQLToRetrievePrimaryKeysFromInformationSchema() throws SQLException {
251
        return null;
252
    }
253
    
254
    
255
    
256
    
257
}