Skip to content

Commit

Permalink
ISpatialTable only implements ITable, move columns methods to ITable.
Browse files Browse the repository at this point in the history
  • Loading branch information
SPalominos committed Nov 27, 2019
1 parent c30d16d commit d510df3
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,5 +187,29 @@ default boolean save(String filePath) {
* @return The values of the first row in a {@link List}.
*/
List<Object> 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<String> columns);

/**
* Return true if the {@link ITable} is spatial.
*
* @return True if the {@link ITable} is spatial.
*/
boolean isSpatial();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ private void getPrivateMethod(){/*Does nothing*/}
@Override public Collection<String> getUniqueValues(String column) {return null;}
@Override public boolean save(String filePath, String encoding) {return false;}
@Override public List<Object> getFirstRow() {return null;}
@Override public ITable columns(String... columns) { return null;}
@Override public ITable columns(List<String> columns) { return null; }
@Override public IConditionOrOptionBuilder where(String condition) {return null;}
@Override public IOptionBuilder groupBy(String... fields) {return null;}
@Override public IOptionBuilder orderBy(Map<String, Order> orderByMap) {return null;}
Expand All @@ -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;}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ public int getRowCount() {
@Override public Collection<String> getUniqueValues(String column) {return null;}
@Override public boolean save(String filePath, String encoding) {return true;}
@Override public List<Object> getFirstRow() {return null;}
@Override public ITable columns(String... columns) { return null; }
@Override public ITable columns(List<String> 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;}
Expand Down
23 changes: 20 additions & 3 deletions data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,33 @@ public boolean save(String filePath, String encoding) {
}

private String getQuery(){
return "SELECT * FROM " + getTableLocation().getTable().toUpperCase();
return baseQuery.trim();
}

private String getQuery(String ... columns){
return "SELECT " + String.join(", ", columns) + " FROM " + getTableLocation().getTable().toUpperCase();
}

@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<String> columns){
WhereBuilder builder = new WhereBuilder(getQuery(columns.toArray(new String[0])), getJdbcDataSource());
if(isSpatial()){
return builder.getSpatialTable();
}
else {
return builder.getTable();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void querySpatialTableWhere() throws SQLException {
"insert into h2gis values (1, 'POINT(10 10)'::GEOMETRY), (2, 'POINT(1 1)'::GEOMETRY);");

ArrayList<String> 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());
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit d510df3

Please sign in to comment.