Skip to content

Commit

Permalink
Merge pull request #395 from ebocher/master
Browse files Browse the repository at this point in the history
Forgot some exceptions
  • Loading branch information
ebocher authored Jun 4, 2024
2 parents 0fd211a + c27119b commit 054240a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -220,7 +220,7 @@ private void dropIndexes() {
* methods.
*/
@Test
public void testIndexes() {
public void testIndexes() throws Exception {
dropIndexes();
testNoSpatialIndexes();
testNoIndexes();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand Down
134 changes: 41 additions & 93 deletions data/jdbc/src/main/java/org/orbisgis/data/jdbc/JdbcDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,155 +1193,103 @@ 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<String> 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<String> 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) {
return true;
}
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.");
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Changelog for v2.1.1

- Refactoring all data exceptions
- Refactoring to manage data exceptions

0 comments on commit 054240a

Please sign in to comment.