Skip to content

Commit

Permalink
Merge pull request #165 from SPalominos/master
Browse files Browse the repository at this point in the history
ISpatialTable only implements ITable, move columns methods to ITable.
  • Loading branch information
SPalominos authored Nov 27, 2019
2 parents c30d16d + 40c0b48 commit 81bc467
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 84 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 @@ -186,5 +186,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 @@ -40,15 +40,14 @@
import org.junit.jupiter.api.Test;
import org.orbisgis.datamanagerapi.dsl.IConditionOrOptionBuilder;
import org.orbisgis.datamanagerapi.dsl.IOptionBuilder;
import org.orbisgis.datamanagerapi.dsl.IWhereBuilderOrOptionBuilder;

import javax.sql.rowset.RowSetMetaDataImpl;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.sql.Date;
import java.sql.*;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -446,6 +445,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 +456,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
24 changes: 20 additions & 4 deletions data-manager/src/main/java/org/orbisgis/datamanager/JdbcTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.orbisgis.datamanagerapi.dataset.*;
import org.orbisgis.datamanagerapi.dsl.IConditionOrOptionBuilder;
import org.orbisgis.datamanagerapi.dsl.IOptionBuilder;
import org.orbisgis.datamanagerapi.dsl.IWhereBuilderOrOptionBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -375,16 +374,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 @@ -37,9 +37,9 @@
package org.orbisgis.datamanager.postgis;

import org.h2gis.postgis_jts.StatementWrapper;
import org.orbisgis.datamanager.TableLocation;
import org.orbisgis.datamanager.JdbcDataSource;
import org.orbisgis.datamanager.JdbcTable;
import org.orbisgis.datamanager.TableLocation;
import org.orbisgis.datamanagerapi.dataset.DataBaseType;
import org.orbisgis.datamanagerapi.dataset.ISpatialTable;
import org.orbisgis.datamanagerapi.dataset.ITable;
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 All @@ -555,6 +561,25 @@ public void testSqlBuilding() {
assertEquals("SELECT * FROM ORBISGIS LIMIT 0", getTable().limit(0).toString().trim());
}

/**
* Test the {@link JdbcTable#columns(String...)} and {@link JdbcTable#columns(List)} methods.
*/
@Test
public void testColumns() {
JdbcTable table = getTable();
JdbcSpatialTable spatialTable = (JdbcSpatialTable)dataSource.getSpatialTable(TABLE_NAME);

List<String> columns = new ArrayList<>();
columns.add("TOTO");
columns.add("tata");
columns.add("TIti");

assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", ((JdbcTable)table.columns("TOTO", "tata", "TIti")).where("toto").toString().trim());
assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", ((JdbcTable)table.columns(columns)).where("toto").toString().trim());
assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto", ((JdbcTable)spatialTable.columns("TOTO", "tata", "TIti")).where("toto").toString().trim());
assertEquals("SELECT TOTO, tata, TIti FROM ORBISGIS WHERE toto",((JdbcTable) spatialTable.columns(columns)).where("toto").toString().trim());
}

/**
* Test the {@link JdbcTable#getTable()} and {@link JdbcTable#getSpatialTable()} methods.
*/
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 81bc467

Please sign in to comment.