Revision 44376 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/operations/H2SpatialFetchFeatureTypeOperation.java

View differences:

H2SpatialFetchFeatureTypeOperation.java
15 15
import org.gvsig.fmap.dal.exception.DataException;
16 16
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
17 17
import org.gvsig.fmap.dal.feature.EditableFeatureType;
18
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
18 19
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
19 20
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
20 21
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
......
79 80
        super(helper, featureType, table, primaryKeys, defaultGeometryColumn, crs);
80 81
    }            
81 82

  
82
    @Override
83
    public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException {
84
        geometry_column = new HashMap<>();
85
        try {
86
            //
87
            // https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
88
            //
89
            StringBuilder where = null;
90
            if( table.hasDatabase() ) {
91
                if( where == null ) {
92
                    where = new StringBuilder();
93
                } else {
94
                    where.append(" AND ");
83
    private GeometryColumnInfo getGeometryColumnInfo(String name) {
84
        if( geometry_column==null ) {
85
            geometry_column = new HashMap<>();
86
            try {
87
                //
88
                // https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view
89
                //
90
                StringBuilder where = null;
91
                if( table.hasDatabase() ) {
92
                    if( where == null ) {
93
                        where = new StringBuilder();
94
                    } else {
95
                        where.append(" AND ");
96
                    }
97
                    where.append("UPPER(F_TABLE_CATALOG) = '");
98
                    where.append(table.getDatabase().toUpperCase());
99
                    where.append("'");
95 100
                }
96
                where.append("UPPER(F_TABLE_CATALOG) = '");
97
                where.append(table.getDatabase().toUpperCase());
98
                where.append("'");
99
            }
100
            if( table.hasSchema()) {
101
                if( where == null ) {
102
                    where = new StringBuilder();
103
                } else {
104
                    where.append(" AND ");
101
                if( table.hasSchema()) {
102
                    if( where == null ) {
103
                        where = new StringBuilder();
104
                    } else {
105
                        where.append(" AND ");
106
                    }
107
                    where.append("UPPER(F_TABLE_SCHEMA) = '");
108
                    where.append(table.getSchema().toUpperCase());
109
                    where.append("'");
105 110
                }
106
                where.append("UPPER(F_TABLE_SCHEMA) = '");
107
                where.append(table.getSchema().toUpperCase());
108
                where.append("'");
109
            }
110
            if( table.hasTable()) {
111
                if( where == null ) {
112
                    where = new StringBuilder();
113
                } else {
114
                    where.append(" AND ");
111
                if( table.hasTable()) {
112
                    if( where == null ) {
113
                        where = new StringBuilder();
114
                    } else {
115
                        where.append(" AND ");
116
                    }
117
                    where.append("UPPER(F_TABLE_NAME) = '");
118
                    where.append(table.getTable().toUpperCase());
119
                    where.append("'");
120
                }            
121
                String sql = "SELECT F_GEOMETRY_COLUMN, GEOMETRY_TYPE, COORD_DIMENSION, SRID, TYPE FROM GEOMETRY_COLUMNS WHERE " + where;
122
                Statement st = this.getConnection().createStatement();
123
                ResultSet rs = JDBCUtils.executeQuery(st,sql);
124
                while( rs.next() ) {
125
                    geometry_column.put(
126
                        rs.getString("F_GEOMETRY_COLUMN"), 
127
                        new GeometryColumnInfo(
128
                            rs.getString("F_GEOMETRY_COLUMN"), 
129
                            rs.getInt("GEOMETRY_TYPE"), 
130
                            rs.getString("TYPE"), 
131
                            rs.getInt("COORD_DIMENSION"), 
132
                            rs.getInt("SRID")
133
                        )
134
                    );
115 135
                }
116
                where.append("UPPER(F_TABLE_NAME) = '");
117
                where.append(table.getTable().toUpperCase());
118
                where.append("'");
119
            }            
120
            String sql = "SELECT F_GEOMETRY_COLUMN, GEOMETRY_TYPE, COORD_DIMENSION, SRID, TYPE FROM GEOMETRY_COLUMNS WHERE " + where;
121
            Statement st = this.getConnection().createStatement();
122
            ResultSet rs = JDBCUtils.executeQuery(st,sql);
123
            while( rs.next() ) {
124
                geometry_column.put(
125
                    rs.getString("F_GEOMETRY_COLUMN"), 
126
                    new GeometryColumnInfo(
127
                        rs.getString("F_GEOMETRY_COLUMN"), 
128
                        rs.getInt("GEOMETRY_TYPE"), 
129
                        rs.getString("TYPE"), 
130
                        rs.getInt("COORD_DIMENSION"), 
131
                        rs.getInt("SRID")
132
                    )
133
                );
136
            } catch (SQLException | AccessResourceException ex) {
137
                LOGGER.warn("Can't read metadata from table '"+table+"'.",ex);
134 138
            }
135
        } catch (SQLException ex) {
136
            LOGGER.warn("Can't read metadata from table '"+table+"'.",ex);
137 139
        }
140
        return this.geometry_column.get(name);
141
    }
142
    
143
    @Override
144
    public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException {
138 145
        super.fetch(featureType, conn, table, pks, defaultGeometryColumn, crs);
139 146
    }
140 147
        
......
146 153
        ) {
147 154
        try {
148 155
            if( attr.getType()==DataTypes.GEOMETRY ) {
149
                GeometryColumnInfo column_info = this.geometry_column.get(attr.getName());
156
                GeometryColumnInfo column_info = this.getGeometryColumnInfo(attr.getName());
150 157
                String type = "GEOMETRY";
151 158
                if( column_info!=null ) {
152 159
                    // H2GIS solo soporte 2D y 3D no soporta Ms

Also available in: Unified diff