Revision 165 trunk/org.gvsig.mssqlserver/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerHelper.java

View differences:

MSSQLServerHelper.java
23 23

  
24 24
import org.gvsig.mssqlserver.dal.operations.MSSQLServerOperationsFactory;
25 25
import java.sql.Connection;
26
import java.sql.ResultSet;
26 27
import java.sql.SQLException;
28
import java.sql.Statement;
27 29
import java.text.MessageFormat;
30
import java.util.ArrayList;
31
import java.util.List;
28 32
import org.apache.commons.dbcp.BasicDataSource;
29 33
import org.apache.commons.lang3.StringUtils;
30
import org.cresques.cts.IProjection;
31 34
import org.gvsig.fmap.dal.DataParameters;
32 35
import org.gvsig.fmap.dal.DataTypes;
33 36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
......
39 42
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
40 43
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
41 44
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
45
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
46
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
42 47
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
43 48
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
44 49
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.primitive.Envelope;
45 52
import org.slf4j.Logger;
46 53
import org.slf4j.LoggerFactory;
47 54

  
......
246 253
        this.lastUsedSpatialType = null;
247 254
        this.lastUsedFeatureType = featureType;
248 255
    }
256

  
257
    public void createOrUpdateSpatialIndex(
258
            Connection conn,
259
            String database,
260
            String schema,
261
            String table,
262
            String columnName
263
        ) throws JDBCSQLException {
264
        List<String> sqls = this.createOrUpdateSpatialIndexSql(
265
                conn, database, schema, table, columnName
266
        );
267
        Statement st = null;
268
        try {
269
            st = conn.createStatement();
270
            for (String sql : sqls) {
271
                JDBCUtils.execute(st, sql);
272
            }
273
        } catch (SQLException ex) {
274
            throw new JDBCSQLException(ex);
275
        } finally {
276
            JDBCUtils.closeQuietly(st);
277
        }
278
        
279
    }
249 280
    
281
    public List<String> createOrUpdateSpatialIndexSql(
282
            Connection conn,
283
            String database,
284
            String schema,
285
            String table,
286
            String columnName
287
        ) throws JDBCSQLException {
288
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
289
        MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder create_index = (MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder) sqlbuilder.create_index();
290

  
291
        sqlbuilder.create_index().spatial();
292
        create_index.setBoundingBox(
293
                this.getBoundingBox(
294
                        conn,
295
                        database,
296
                        schema,
297
                        table,
298
                        columnName
299
                )
300
        );
301
        create_index.if_not_exist();
302
        create_index.name("idx_" + table + "_" + columnName);
303
        create_index.column(columnName);
304
        create_index.table().database(database).schema(schema).name(table);
305

  
306
        return create_index.toStrings();
307
    }    
308
    
309
    public Envelope getBoundingBox(
310
            Connection conn,
311
            String database,
312
            String schema,
313
            String table,
314
            String columnName
315
        ) throws JDBCSQLException {
316
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
317
        sqlbuilder.select().column().value(
318
            sqlbuilder.getAsGeometry(
319
                sqlbuilder.ST_ExtentAggregate(
320
                        sqlbuilder.column(columnName)
321
                )
322
            )
323
        );
324
        sqlbuilder.select().from().table().database(database).schema(schema).name(table);
325

  
326
        String sql = sqlbuilder.select().toString();
327
        Statement st = null;
328
        ResultSet rs = null;
329
        try {
330
            st = conn.createStatement();
331
            rs = JDBCUtils.executeQuery(st, sql);
332
            if (!rs.next()) {
333
                return null;
334
            }
335
            Geometry geom = this.getGeometryFromColumn(rs, 1);
336
            if (geom == null) {
337
                return null;
338
            }
339
            return geom.getEnvelope();
340

  
341
        } catch (Exception ex) {
342
            throw new JDBCSQLException(ex);
343
        } finally {
344
            JDBCUtils.closeQuietly(st);
345
            JDBCUtils.closeQuietly(rs);
346
        }        
347
    }
348
    
250 349
    @Override
251 350
    public OperationsFactory getOperations() {
252 351
        if (this.operationsFactory == null) {

Also available in: Unified diff