NoiseModelling icon indicating copy to clipboard operation
NoiseModelling copied to clipboard

Check if index exists before running scripts

Open Nitnelav opened this issue 5 years ago • 0 comments

Check if index exists before running scripts. If not create it. Assume indexes on Geometry fields are SPATIAL.

Here is a code exemple :

    logger.info("searching index on attenuation matrix IDSOURCE ... ")
    DatabaseMetaData dbMeta = connection.getMetaData();
    ResultSet rs = dbMeta.getIndexInfo(null, null, attenuationTable, false, false);
    boolean indexIDSOURCE = false;
    while (rs.next()) {
        String column = rs.getString("COLUMN_NAME");
        String pos = rs.getString("ORDINAL_POSITION");
        if (column == "IDSOURCE" && pos == "1") {
            indexIDSOURCE = true;
            logger.info("index on attenuation matrix IDSOURCE found")
            break;
        }
    }
    if (!indexIDSOURCE) {
        logger.info("index on attenuation matrix IDSOURCE, NOT found, creating one...")
        sql.execute("CREATE INDEX ON " + attenuationTable + " (IDSOURCE)");
    }

Nitnelav avatar Jan 28 '21 10:01 Nitnelav