diff --git a/data/data-api/src/main/java/org/orbisgis/data/api/dataset/IJdbcColumn.java b/data/data-api/src/main/java/org/orbisgis/data/api/dataset/IJdbcColumn.java index 1f827739..2ce421b5 100644 --- a/data/data-api/src/main/java/org/orbisgis/data/api/dataset/IJdbcColumn.java +++ b/data/data-api/src/main/java/org/orbisgis/data/api/dataset/IJdbcColumn.java @@ -63,7 +63,7 @@ public interface IJdbcColumn extends IColumn { * * @return True if the column has a spatial index, false otherwise. */ - boolean isSpatialIndexed(); + boolean isSpatialIndexed() throws Exception; /** * Create an index of the column. If the column already has an index, no new index is created. @@ -83,7 +83,7 @@ public interface IJdbcColumn extends IColumn { /** * Drop the index of the column if exists. */ - void dropIndex(); + void dropIndex() throws Exception; /** * Return the SRID code of the column if it is a spatial one, -1 otherwise. @@ -99,5 +99,5 @@ public interface IJdbcColumn extends IColumn { * * @return TRUE is the SRID is updated */ - boolean setSrid(int srid); + boolean setSrid(int srid) throws Exception; } diff --git a/data/data-api/src/main/java/org/orbisgis/data/api/datasource/IJdbcDataSource.java b/data/data-api/src/main/java/org/orbisgis/data/api/datasource/IJdbcDataSource.java index d0c93add..bccec433 100644 --- a/data/data-api/src/main/java/org/orbisgis/data/api/datasource/IJdbcDataSource.java +++ b/data/data-api/src/main/java/org/orbisgis/data/api/datasource/IJdbcDataSource.java @@ -1140,7 +1140,7 @@ default void setProperty(String propertyName, Object newValue) { * * @return True if the tableName is empty, false otherwise. */ - boolean isEmpty(String tableName); + boolean isEmpty(String tableName) throws Exception; /** diff --git a/data/h2gis/src/test/java/org/orbisgis/data/H2GISColumnTest.java b/data/h2gis/src/test/java/org/orbisgis/data/H2GISColumnTest.java index 8f2cbabb..70c2e198 100644 --- a/data/h2gis/src/test/java/org/orbisgis/data/H2GISColumnTest.java +++ b/data/h2gis/src/test/java/org/orbisgis/data/H2GISColumnTest.java @@ -175,7 +175,7 @@ public void testIsSpatial() { /** * Test that there is no spatial index */ - private void testNoSpatialIndexes() { + private void testNoSpatialIndexes() throws Exception { //Test no spatial index assertFalse(getColumn(COL_THE_GEOM).isSpatialIndexed()); assertFalse(getColumn(COL_THE_GEOM2).isSpatialIndexed()); @@ -203,7 +203,7 @@ private void testNoIndexes() { /** * Drop indexes on all columns */ - private void dropIndexes() { + private void dropIndexes() throws Exception { //Test drop index getColumn(COL_THE_GEOM).dropIndex(); getColumn(COL_THE_GEOM2).dropIndex(); @@ -220,7 +220,7 @@ private void dropIndexes() { * methods. */ @Test - public void testIndexes() { + public void testIndexes() throws Exception { dropIndexes(); testNoSpatialIndexes(); testNoIndexes(); @@ -288,7 +288,7 @@ public void testIndexes() { * Test the {@link JdbcColumn#getSrid()}, {@link JdbcColumn#setSrid(int)} methods. */ @Test - public void testGetSrid() { + public void testGetSrid() throws Exception { JdbcColumn column = getColumn(COL_THE_GEOM); assertEquals(0, column.getSrid()); column.setSrid(2121); diff --git a/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcColumn.java b/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcColumn.java index 0fa1130b..cb80b8a9 100644 --- a/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcColumn.java +++ b/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcColumn.java @@ -190,7 +190,7 @@ public boolean isIndexed() { } @Override - public boolean isSpatialIndexed() { + public boolean isSpatialIndexed() throws Exception { if(dataSource == null){ LOGGER.error("Unable to find a spatial index"); } @@ -214,7 +214,7 @@ public boolean createIndex() { } @Override - public void dropIndex() { + public void dropIndex() throws Exception { if( name == null || tableName == null){ LOGGER.error("Unable to drop index"); } @@ -240,7 +240,7 @@ public int getSrid() { } @Override - public boolean setSrid(int srid) { + public boolean setSrid(int srid) throws Exception { if( name == null || tableName == null){ LOGGER.error("Unable to set the srid"); } diff --git a/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcDataSource.java b/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcDataSource.java index 02290d35..ceed4b8a 100644 --- a/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcDataSource.java +++ b/data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcDataSource.java @@ -1193,141 +1193,90 @@ public boolean isIndexed(String tableName, String columnName) { } @Override - public boolean isSpatialIndexed(String tableName, String columnName) { - try { - return JDBCUtilities.isSpatialIndexed(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName); - } catch (SQLException e) { - LOGGER.error("Unable to check if the column '" + columnName + "' from the table '" + tableName + "' is indexed.\n" + - e.getLocalizedMessage()); - } - return false; + public boolean isSpatialIndexed(String tableName, String columnName) throws Exception{ + return JDBCUtilities.isSpatialIndexed(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName); } @Override - public boolean isSpatialIndexed(String tableName) { - try { + public boolean isSpatialIndexed(String tableName) throws Exception{ TableLocation table = TableLocation.parse(tableName, getDataBaseType()); String geomColumn = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(getConnection(), table).first(); if (geomColumn == null || geomColumn.isEmpty()) { return false; } return JDBCUtilities.isSpatialIndexed(getConnection(), tableName, geomColumn); - } catch (SQLException e) { - LOGGER.error("Unable to check if the table '" + tableName + "' has a spatial index.\n" + - e.getLocalizedMessage()); - } - return false; } @Override - public void dropIndex(String tableName, String columnName) { - if (columnName == null || tableName == null) { - LOGGER.error("Unable to drop index"); - return; - } - try { + public void dropIndex(String tableName, String columnName) throws Exception{ + if (columnName != null || tableName != null) { JDBCUtilities.dropIndex(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName); - } catch (SQLException e) { - LOGGER.error("Unable to drop the indexes of the column '" + columnName + "' in the table '" + tableName + "'.\n" + - e.getLocalizedMessage()); } } @Override - public void dropTable(String... tableName) { - if (tableName == null) { - LOGGER.error("Unable to drop the tables"); - return; - } - String query = Stream.of(tableName).filter(s -> s != null && !s.isEmpty()) - .collect(Collectors.joining(" , ")); - try { - if (!query.isEmpty()) { - execute("DROP TABLE IF EXISTS " + query); + public void dropTable(String... tableName) throws Exception{ + if (tableName != null) { + String query = Stream.of(tableName).filter(s -> s != null && !s.isEmpty()) + .collect(Collectors.joining(" , ")); + try { + if (!query.isEmpty()) { + execute("DROP TABLE IF EXISTS " + query); + } + } catch (SQLException e) { + throw new SQLException("Unable to drop the tables '" + query + "'." , e); } - } catch (SQLException e) { - LOGGER.error("Unable to drop the tables '" + query + "'.\n" + - e.getLocalizedMessage()); } } @Override - public void dropTable(List tableNames) { - if (tableNames == null) { - LOGGER.error("Unable to drop the tables"); - return; - } - String query = tableNames.stream().filter(s -> s != null && !s.isEmpty()) - .collect(Collectors.joining(",")); - try { - if (!query.isEmpty()) { - execute("DROP TABLE IF EXISTS " + query); + public void dropTable(List tableNames) throws Exception{ + if (tableNames != null) { + String query = tableNames.stream().filter(s -> s != null && !s.isEmpty()) + .collect(Collectors.joining(",")); + try { + if (!query.isEmpty()) { + execute("DROP TABLE IF EXISTS " + query); + } + } catch (SQLException e) { + throw new SQLException("Unable to drop the tables '" + query + "'." , + e); } - } catch (SQLException e) { - LOGGER.error("Unable to drop the tables '" + query + "'.\n" + - e.getLocalizedMessage()); } } - @Override - public boolean setSrid(String tableName, String columnName, int srid) { - try { - return GeometryTableUtilities.alterSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName, srid); - } catch (SQLException e) { - LOGGER.error("Unable to set the table SRID.", e); - } - return false; - } + public boolean setSrid(String tableName, String columnName, int srid) throws Exception{ + return GeometryTableUtilities.alterSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName, srid); + } @Override - public int getSrid(String tableName) { + public int getSrid(String tableName) throws Exception{ if (tableName == null) { - LOGGER.error("Unable to get the srid"); - return -1; + throw new IllegalArgumentException("Unable to get the srid"); } - try { - return GeometryTableUtilities.getSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType())); - } catch (SQLException e) { - LOGGER.error("Unable to get the table SRID.", e); - } - return -1; + return GeometryTableUtilities.getSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType())); } @Override - public int getSrid(String tableName, String columnName) { + public int getSrid(String tableName, String columnName) throws Exception{ if (tableName == null) { - LOGGER.error("Unable to get the srid"); - return -1; + throw new IllegalArgumentException("Unable to get the srid"); } - try { - return GeometryTableUtilities.getSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName); - } catch (SQLException e) { - LOGGER.error("Unable to get the table SRID.", e); - } - return -1; + return GeometryTableUtilities.getSRID(getConnection(), TableLocation.parse(tableName, getDataBaseType()), columnName); } @Override - public boolean setSrid(String tableName, int srid) { - try { + public boolean setSrid(String tableName, int srid) throws Exception{ String geomColumn = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(getConnection(), TableLocation.parse(tableName, getDataBaseType())).first(); if (geomColumn == null || geomColumn.isEmpty()) { - return false; + throw new IllegalArgumentException("Unable to get the srid"); } return GeometryTableUtilities.alterSRID(getConnection(), tableName, geomColumn, srid); - } catch (SQLException e) { - LOGGER.error("Unable to set the table SRID.", e); - } - return false; } @Override - public boolean isEmpty(String tableName) { - if (tableName == null) { - LOGGER.error("Unable to drop the tables"); - return false; - } + public boolean isEmpty(String tableName) throws Exception{ try { GroovyRowResult row = firstRow("SELECT * FROM " + tableName + " LIMIT 1 "); if (row == null) { @@ -1335,13 +1284,12 @@ public boolean isEmpty(String tableName) { } return row.isEmpty(); } catch (SQLException e) { - LOGGER.error("Unable to check if the table is empty.", e); + throw new SQLException("Unable to check if the table is empty.", e); } - return false; } @Override - public void print(String tableName) { + public void print(String tableName) throws Exception{ if (tableName == null || tableName.isEmpty()) { System.out.println("The table name is null or empty."); } @@ -1388,7 +1336,7 @@ public void print(String tableName) { count++; } } catch (Exception e) { - LOGGER.error("Error while reading the table '" + tableName + "'.\n" + e.getLocalizedMessage()); + throw new SQLException("Error while reading the table '" + tableName + "'.\n" + e.getLocalizedMessage()); } } if (tooManyRows) {