From d510df3e885338337f10dcfb3be28f0cd537ff40 Mon Sep 17 00:00:00 2001 From: Sylvain Palominos Date: Wed, 27 Nov 2019 11:50:37 +0100 Subject: [PATCH] ISpatialTable only implements ITable, move columns methods to ITable. --- .../datamanagerapi/dataset/IJdbcTable.java | 10 +--- .../datamanagerapi/dataset/ISpatialTable.java | 2 +- .../datamanagerapi/dataset/ITable.java | 27 ++++++++- .../datamanagerapi/dsl/IColumnsBuilder.java | 59 ------------------- .../dataset/IJdbcTableTest.java | 3 +- .../datamanagerapi/dataset/ITableTest.java | 3 + .../org/orbisgis/datamanager/JdbcTable.java | 23 +++++++- .../datamanager/dsl/BuilderResult.java | 5 +- .../orbisgis/datamanager/JdbcTableTest.java | 8 ++- .../datamanager/h2gis/H2GISTests.java | 6 +- 10 files changed, 66 insertions(+), 80 deletions(-) delete mode 100644 data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dsl/IColumnsBuilder.java diff --git a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/IJdbcTable.java b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/IJdbcTable.java index a9da5bcf..22378c56 100644 --- a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/IJdbcTable.java +++ b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/IJdbcTable.java @@ -37,7 +37,6 @@ package org.orbisgis.datamanagerapi.dataset; import groovy.lang.Closure; -import org.orbisgis.datamanagerapi.dsl.IColumnsBuilder; import org.orbisgis.datamanagerapi.dsl.IWhereBuilderOrOptionBuilder; import java.sql.ResultSet; @@ -49,7 +48,7 @@ * Extension of the {@link ITable} specially dedicated to the JDBC databases thanks to the extension of the * {@link ResultSet} interface. It also extends the {@link IWhereBuilderOrOptionBuilder} for the SQL requesting */ -public interface IJdbcTable extends ITable, ResultSet, IWhereBuilderOrOptionBuilder, IColumnsBuilder { +public interface IJdbcTable extends ITable, ResultSet, IWhereBuilderOrOptionBuilder { /** {@link String} name of the metadata property */ String META_PROPERTY = "meta"; @@ -71,13 +70,6 @@ public interface IJdbcTable extends ITable, ResultSet, IWhereBuilderOrOptionBuil @Override ResultSetMetaData getMetaData(); - /** - * Return true if the {@link ITable} is spatial. - * - * @return True if the {@link ITable} is spatial. - */ - boolean isSpatial(); - /** * Return true if the {@link ITable} is a linked one. * diff --git a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ISpatialTable.java b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ISpatialTable.java index dd7ef844..240d4b77 100644 --- a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ISpatialTable.java +++ b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ISpatialTable.java @@ -48,7 +48,7 @@ * @author Erwan Bocher (CNRS) * @author Sylvain PALOMINOS (UBS 2018-2019) */ -public interface ISpatialTable extends IJdbcTable { +public interface ISpatialTable extends ITable { Geometry getGeometry(int columnIndex); diff --git a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ITable.java b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ITable.java index c6201130..b39f250c 100644 --- a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ITable.java +++ b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dataset/ITable.java @@ -37,6 +37,7 @@ package org.orbisgis.datamanagerapi.dataset; import groovy.lang.Closure; +import org.orbisgis.datamanagerapi.dsl.IWhereBuilderOrOptionBuilder; import java.util.Collection; import java.util.List; @@ -186,5 +187,29 @@ default boolean save(String filePath) { * @return The values of the first row in a {@link List}. */ List getFirstRow(); - + + /** + * Indicates the columns use for the selection. + * + * @param columns Array of the columns use for the selection. + * + * @return Filtered {@link ITable}. + */ + ITable columns(String... columns); + + /** + * Indicates the columns use for the selection. + * + * @param columns List of the columns use for the selection. + * + * @return Filtered {@link ITable}. + */ + ITable columns(List columns); + + /** + * Return true if the {@link ITable} is spatial. + * + * @return True if the {@link ITable} is spatial. + */ + boolean isSpatial(); } diff --git a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dsl/IColumnsBuilder.java b/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dsl/IColumnsBuilder.java deleted file mode 100644 index 915f2d0e..00000000 --- a/data-manager-api/src/main/java/org/orbisgis/datamanagerapi/dsl/IColumnsBuilder.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Bundle DataManager API is part of the OrbisGIS platform - * - * OrbisGIS is a java GIS application dedicated to research in GIScience. - * OrbisGIS is developed by the GIS group of the DECIDE team of the - * Lab-STICC CNRS laboratory, see . - * - * The GIS group of the DECIDE team is located at : - * - * Laboratoire Lab-STICC – CNRS UMR 6285 - * Equipe DECIDE - * UNIVERSITÉ DE BRETAGNE-SUD - * Institut Universitaire de Technologie de Vannes - * 8, Rue Montaigne - BP 561 56017 Vannes Cedex - * - * DataManager API is distributed under GPL 3 license. - * - * Copyright (C) 2019 CNRS (Lab-STICC UMR CNRS 6285) - * - * - * DataManager API is free software: you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * DataManager API is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * DataManager API. If not, see . - * - * For more information, please consult: - * or contact directly: - * info_at_ orbisgis.org - */ -package org.orbisgis.datamanagerapi.dsl; - -import org.orbisgis.datamanagerapi.dataset.ITable; - -/** - * Interface defining methods for the SQL 'select ... from' building. The request construction can be continued thanks to the - * {@link IWhereBuilderOrOptionBuilder} or its result can be get calling 'eachRow' to iterate on the resultSet or - * 'as ITable' to get the {@link ITable} object - * - * @author Erwan Bocher (CNRS) - * @author Sylvain PALOMINOS (UBS 2019) - */ -public interface IColumnsBuilder { - - /** - * Indicates the columns use for the selection. - * - * @param columns Array of the columns use for the selection. - * - * @return {@link IWhereBuilderOrOptionBuilder} instance to continue building. - */ - IWhereBuilderOrOptionBuilder columns(String... columns); -} diff --git a/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/IJdbcTableTest.java b/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/IJdbcTableTest.java index 648073d1..12025ae3 100644 --- a/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/IJdbcTableTest.java +++ b/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/IJdbcTableTest.java @@ -446,6 +446,8 @@ private void getPrivateMethod(){/*Does nothing*/} @Override public Collection getUniqueValues(String column) {return null;} @Override public boolean save(String filePath, String encoding) {return false;} @Override public List getFirstRow() {return null;} + @Override public ITable columns(String... columns) { return null;} + @Override public ITable columns(List columns) { return null; } @Override public IConditionOrOptionBuilder where(String condition) {return null;} @Override public IOptionBuilder groupBy(String... fields) {return null;} @Override public IOptionBuilder orderBy(Map orderByMap) {return null;} @@ -455,6 +457,5 @@ private void getPrivateMethod(){/*Does nothing*/} @Override public Object asType(Class clazz) {return null;} @Override public ITable getTable() {return null;} @Override public ISpatialTable getSpatialTable() {return null;} - @Override public IWhereBuilderOrOptionBuilder columns(String... columns) {return null;} } } diff --git a/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/ITableTest.java b/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/ITableTest.java index b3423788..c35dad56 100644 --- a/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/ITableTest.java +++ b/data-manager-api/src/test/java/org/orbisgis/datamanagerapi/dataset/ITableTest.java @@ -290,6 +290,9 @@ public int getRowCount() { @Override public Collection getUniqueValues(String column) {return null;} @Override public boolean save(String filePath, String encoding) {return true;} @Override public List getFirstRow() {return null;} + @Override public ITable columns(String... columns) { return null; } + @Override public ITable columns(List columns) { return null; } + @Override public boolean isSpatial() { return false; } @Override public String getLocation() {return null;} @Override public String getName() {return null;} @Override public Object getMetaData() {return null;} diff --git a/data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java b/data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java index a97fb83e..cc5efbc2 100644 --- a/data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java +++ b/data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java @@ -375,7 +375,7 @@ public boolean save(String filePath, String encoding) { } private String getQuery(){ - return "SELECT * FROM " + getTableLocation().getTable().toUpperCase(); + return baseQuery.trim(); } private String getQuery(String ... columns){ @@ -383,8 +383,25 @@ private String getQuery(String ... columns){ } @Override - public IWhereBuilderOrOptionBuilder columns(String... columns){ - return new WhereBuilder(getQuery(columns), getJdbcDataSource()); + public ITable columns(String... columns){ + WhereBuilder builder = new WhereBuilder(getQuery(columns), getJdbcDataSource()); + if(isSpatial()){ + return builder.getSpatialTable(); + } + else { + return builder.getTable(); + } + } + + @Override + public ITable columns(List columns){ + WhereBuilder builder = new WhereBuilder(getQuery(columns.toArray(new String[0])), getJdbcDataSource()); + if(isSpatial()){ + return builder.getSpatialTable(); + } + else { + return builder.getTable(); + } } @Override diff --git a/data-manager/src/main/java/org/orbisgis/datamanager/dsl/BuilderResult.java b/data-manager/src/main/java/org/orbisgis/datamanager/dsl/BuilderResult.java index 47b060ab..cc55df3d 100644 --- a/data-manager/src/main/java/org/orbisgis/datamanager/dsl/BuilderResult.java +++ b/data-manager/src/main/java/org/orbisgis/datamanager/dsl/BuilderResult.java @@ -37,6 +37,7 @@ package org.orbisgis.datamanager.dsl; import groovy.lang.Closure; +import org.h2gis.utilities.wrapper.ConnectionWrapper; import org.h2gis.utilities.wrapper.StatementWrapper; import org.orbisgis.commons.printer.ICustomPrinter; import org.orbisgis.datamanager.JdbcDataSource; @@ -102,8 +103,8 @@ public Object asType(Class clazz) { default: case H2GIS: if(!(statement instanceof StatementWrapper)){ - LOGGER.error("The statement class not compatible with the database."); - break; + LOGGER.warn("The statement class not compatible with the database."); + statement = new StatementWrapper(statement, new ConnectionWrapper(getDataSource().getConnection())); } if(clazz == ISpatialTable.class) { return new H2gisSpatialTable(new TableLocation(getDataSource().getLocation().toString(), name), diff --git a/data-manager/src/test/java/org/orbisgis/datamanager/JdbcTableTest.java b/data-manager/src/test/java/org/orbisgis/datamanager/JdbcTableTest.java index 1a82e712..b47d1d8e 100644 --- a/data-manager/src/test/java/org/orbisgis/datamanager/JdbcTableTest.java +++ b/data-manager/src/test/java/org/orbisgis/datamanager/JdbcTableTest.java @@ -545,7 +545,13 @@ public void testSqlBuilding() { map.put("toto", IOptionBuilder.Order.ASC); map.put("tata", IOptionBuilder.Order.DESC); - assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", getTable().columns("TOTO", "tata", "TIti").where("toto").toString().trim()); + List columns = new ArrayList<>(); + columns.add("TOTO"); + columns.add("tata"); + columns.add("TIti"); + + assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", ((JdbcTable)getTable().columns("TOTO", "tata", "TIti")).where("toto").toString().trim()); + assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", ((JdbcTable)getTable().columns(columns)).where("toto").toString().trim()); assertEquals("SELECT * FROM ORBISGIS WHERE toto", getTable().where("toto").toString().trim()); assertEquals("SELECT * FROM ORBISGIS GROUP BY toto", getTable().groupBy("toto").toString().trim()); assertEquals("SELECT * FROM ORBISGIS GROUP BY toto,tata", getTable().groupBy("toto", "tata").toString().trim()); diff --git a/data-manager/src/test/java/org/orbisgis/datamanager/h2gis/H2GISTests.java b/data-manager/src/test/java/org/orbisgis/datamanager/h2gis/H2GISTests.java index 7ffb9839..26c6d998 100644 --- a/data-manager/src/test/java/org/orbisgis/datamanager/h2gis/H2GISTests.java +++ b/data-manager/src/test/java/org/orbisgis/datamanager/h2gis/H2GISTests.java @@ -158,7 +158,7 @@ public void querySpatialTableWhere() throws SQLException { "insert into h2gis values (1, 'POINT(10 10)'::GEOMETRY), (2, 'POINT(1 1)'::GEOMETRY);"); ArrayList values = new ArrayList<>(); - h2GIS.getSpatialTable("h2gis").where("id=2").eachRow(new Closure(null){ + ((IJdbcTable)h2GIS.getSpatialTable("h2gis")).where("id=2").eachRow(new Closure(null){ @Override public Object call(Object argument) { values.add(((ISpatialTable)argument).getGeometry().toString()); @@ -195,7 +195,7 @@ public void updateSpatialTable() throws SQLException { h2GIS.getSpatialTable("h2gis").eachRow(new Closure(null){ @Override public Object call(Object argument) { - ISpatialTable sp = ((ISpatialTable) argument); + IJdbcTable sp = ((IJdbcTable) argument); try { sp.updateInt(2, 3); sp.updateRow(); @@ -212,7 +212,7 @@ public Object call(Object argument) { @Override public Object call(Object argument) { try { - values.add(((ISpatialTable)argument).getInt(2)); + values.add(((IJdbcTable)argument).getInt(2)); } catch (SQLException e) { e.printStackTrace(); }