Skip to content
Snippets Groups Projects
Commit e132401e authored by vforchi's avatar vforchi
Browse files

Merge remote-tracking branch 'upstream/master'

parents d99d3735 61063906
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ package adql.query.operand.function.geometry; ...@@ -16,7 +16,7 @@ package adql.query.operand.function.geometry;
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>. * along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS), * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI) * Astronomisches Rechen Institut (ARI)
*/ */
...@@ -36,7 +36,7 @@ import adql.query.operand.ADQLOperand; ...@@ -36,7 +36,7 @@ import adql.query.operand.ADQLOperand;
* </i></p> * </i></p>
* *
* @author Gr&eacute;gory Mantelet (CDS;ARI) * @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 1.4 (06/2015) * @version 1.4 (02/2017)
*/ */
public class CentroidFunction extends GeometryFunction { public class CentroidFunction extends GeometryFunction {
...@@ -80,7 +80,7 @@ public class CentroidFunction extends GeometryFunction { ...@@ -80,7 +80,7 @@ public class CentroidFunction extends GeometryFunction {
@Override @Override
public boolean isNumeric(){ public boolean isNumeric(){
return true; return false;
} }
@Override @Override
...@@ -90,7 +90,7 @@ public class CentroidFunction extends GeometryFunction { ...@@ -90,7 +90,7 @@ public class CentroidFunction extends GeometryFunction {
@Override @Override
public boolean isGeometry(){ public boolean isGeometry(){
return false; return true;
} }
@Override @Override
......
...@@ -16,7 +16,7 @@ package adql.translator; ...@@ -16,7 +16,7 @@ package adql.translator;
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>. * along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS), * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI) * Astronomisches Rechen Institut (ARI)
*/ */
...@@ -34,6 +34,7 @@ import adql.query.constraint.Comparison; ...@@ -34,6 +34,7 @@ import adql.query.constraint.Comparison;
import adql.query.constraint.ComparisonOperator; import adql.query.constraint.ComparisonOperator;
import adql.query.operand.function.geometry.AreaFunction; import adql.query.operand.function.geometry.AreaFunction;
import adql.query.operand.function.geometry.BoxFunction; import adql.query.operand.function.geometry.BoxFunction;
import adql.query.operand.function.geometry.CentroidFunction;
import adql.query.operand.function.geometry.CircleFunction; import adql.query.operand.function.geometry.CircleFunction;
import adql.query.operand.function.geometry.ContainsFunction; import adql.query.operand.function.geometry.ContainsFunction;
import adql.query.operand.function.geometry.DistanceFunction; import adql.query.operand.function.geometry.DistanceFunction;
...@@ -48,7 +49,7 @@ import adql.query.operand.function.geometry.PolygonFunction; ...@@ -48,7 +49,7 @@ import adql.query.operand.function.geometry.PolygonFunction;
* The other functions are managed by {@link PostgreSQLTranslator}.</p> * The other functions are managed by {@link PostgreSQLTranslator}.</p>
* *
* @author Gr&eacute;gory Mantelet (CDS;ARI) * @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 1.4 (07/2015) * @version 1.4 (07/2017)
*/ */
public class PgSphereTranslator extends PostgreSQLTranslator { public class PgSphereTranslator extends PostgreSQLTranslator {
...@@ -174,6 +175,13 @@ public class PgSphereTranslator extends PostgreSQLTranslator { ...@@ -174,6 +175,13 @@ public class PgSphereTranslator extends PostgreSQLTranslator {
return str.toString(); return str.toString();
} }
@Override
public String translate(CentroidFunction centroidFunction) throws TranslationException{
StringBuffer str = new StringBuffer("center(");
str.append(translate(centroidFunction.getParameter(0))).append(")");
return str.toString();
}
@Override @Override
public String translate(ContainsFunction fct) throws TranslationException{ public String translate(ContainsFunction fct) throws TranslationException{
StringBuffer str = new StringBuffer("("); StringBuffer str = new StringBuffer("(");
...@@ -624,7 +632,8 @@ public class PgSphereTranslator extends PostgreSQLTranslator { ...@@ -624,7 +632,8 @@ public class PgSphereTranslator extends PostgreSQLTranslator {
end(); end();
// Build the STC Box region: // Build the STC Box region:
double width = Math.abs(northeast[0] - southwest[0]), height = Math.abs(northeast[1] - southwest[1]); double width = Math.abs(northeast[0] - southwest[0]),
height = Math.abs(northeast[1] - southwest[1]);
double[] center = new double[]{northeast[0] - width / 2,northeast[1] - height / 2}; double[] center = new double[]{northeast[0] - width / 2,northeast[1] - height / 2};
return new Region(null, center, width, height); return new Region(null, center, width, height);
} }
......
...@@ -16,13 +16,16 @@ package tap.data; ...@@ -16,13 +16,16 @@ package tap.data;
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with TAPLibrary. If not, see <http://www.gnu.org/licenses/>. * along with TAPLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2014-2016 - Astronomisches Rechen Institut (ARI) * Copyright 2014-2017 - Astronomisches Rechen Institut (ARI)
*/ */
import java.sql.Date;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import adql.db.DBColumn; import adql.db.DBColumn;
...@@ -43,7 +46,7 @@ import uws.ISO8601Format; ...@@ -43,7 +46,7 @@ import uws.ISO8601Format;
* </i></p> * </i></p>
* *
* @author Gr&eacute;gory Mantelet (ARI) * @author Gr&eacute;gory Mantelet (ARI)
* @version 2.1 (09/2016) * @version 2.1 (02/2017)
* @since 2.0 * @since 2.0
*/ */
public class ResultSetTableIterator implements TableIterator { public class ResultSetTableIterator implements TableIterator {
...@@ -73,6 +76,11 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -73,6 +76,11 @@ public class ResultSetTableIterator implements TableIterator {
/** Index of the last read column (=0 just after {@link #nextRow()} and before {@link #nextCol()}, ={@link #nbColumns} after the last column has been read). */ /** Index of the last read column (=0 just after {@link #nextRow()} and before {@link #nextCol()}, ={@link #nbColumns} after the last column has been read). */
private int colIndex; private int colIndex;
/** Formatter to use in order to format java.sql.Date values. */
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/** Formatter to use in order to format java.sql.Time values. */
private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
/** /**
* <p>Build a TableIterator able to read rows and columns of the given ResultSet.</p> * <p>Build a TableIterator able to read rows and columns of the given ResultSet.</p>
* *
...@@ -687,7 +695,7 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -687,7 +695,7 @@ public class ResultSetTableIterator implements TableIterator {
* *
* <p>By default, the following function performs the following formatting:</p> * <p>By default, the following function performs the following formatting:</p>
* <ul> * <ul>
* <li><b>If {@link Timestamp}:</b> the date-time is converted into a string with the ISO8601 format (see {@link ISO8601Format}).</li> * <li><b>If {@link Timestamp}, {@link Date} or {@link Time}:</b> the date-time is converted into a string with the ISO8601 format (see {@link ISO8601Format}).</li>
* <li><b>If a single CHAR is declared and a String is given:</b> only the first character is returned as a {@link Character} object.</li> * <li><b>If a single CHAR is declared and a String is given:</b> only the first character is returned as a {@link Character} object.</li>
* <li><b>If the value is declared as a Geometry:</b> the geometry is formatted as a STC-S expression.</li> * <li><b>If the value is declared as a Geometry:</b> the geometry is formatted as a STC-S expression.</li>
* </ul> * </ul>
...@@ -703,9 +711,16 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -703,9 +711,16 @@ public class ResultSetTableIterator implements TableIterator {
protected Object formatColValue(Object colValue) throws DataReadException{ protected Object formatColValue(Object colValue) throws DataReadException{
if (colValue != null){ if (colValue != null){
DBType colType = getColType(); DBType colType = getColType();
// if the column value is a Timestamp object, format it in ISO8601: // if the column value is a java.sql.Time object, format it into an ISO8601 time (i.e. with the format: HH:mm:ss):
if (colValue instanceof Timestamp) if (colValue instanceof java.sql.Time)
colValue = ISO8601Format.format(((Timestamp)colValue).getTime()); colValue = timeFormat.format((java.sql.Time)colValue);
// if the column value is a java.sql.Date object, format it into an ISO8601 date (i.e. with the format: yyyy-MM-dd):
else if (colValue instanceof java.sql.Date)
colValue = dateFormat.format((java.sql.Date)colValue);
// if the column value is a Timestamp (or java.util.Date) object, format it into an ISO8601 date-time:
// note: java.sql.Timestamp extends java.util.Date. That's why the next condition also works for java.sql.Timestamp.
else if (colValue instanceof java.util.Date)
colValue = ISO8601Format.format((java.util.Date)colValue);
// if the type is Integer but it is declared as a SMALLINT cast the value (absolutely required for the FITS format): // if the type is Integer but it is declared as a SMALLINT cast the value (absolutely required for the FITS format):
else if (colValue instanceof Integer && colType != null && colValue != null && colType.type == DBDatatype.SMALLINT) else if (colValue instanceof Integer && colType != null && colValue != null && colType.type == DBDatatype.SMALLINT)
colValue = new Short(((Integer)colValue).shortValue()); colValue = new Short(((Integer)colValue).shortValue());
......
package adql.query.operand.function.geometry;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import adql.query.operand.NumericConstant;
import adql.query.operand.StringConstant;
import adql.query.operand.function.geometry.GeometryFunction.GeometryValue;
public class TestCentroidFunction {
@Test
public void testIsGeometry(){
try{
CentroidFunction centfc = new CentroidFunction(new GeometryValue<GeometryFunction>(new CircleFunction(new StringConstant("ICRS"), new NumericConstant(128.23), new NumericConstant(0.53), new NumericConstant(2))));
assertTrue(centfc.isGeometry());
assertFalse(centfc.isNumeric());
assertFalse(centfc.isString());
}catch(Throwable t){
t.printStackTrace(System.err);
fail("An error occured while building a simple CentroidFunction! (see the console for more details)");
}
}
}
...@@ -19,6 +19,12 @@ import adql.db.DBType; ...@@ -19,6 +19,12 @@ import adql.db.DBType;
import adql.db.DBType.DBDatatype; import adql.db.DBType.DBDatatype;
import adql.db.STCS.Region; import adql.db.STCS.Region;
import adql.parser.ParseException; import adql.parser.ParseException;
import adql.query.operand.NumericConstant;
import adql.query.operand.StringConstant;
import adql.query.operand.function.geometry.CentroidFunction;
import adql.query.operand.function.geometry.CircleFunction;
import adql.query.operand.function.geometry.GeometryFunction;
import adql.query.operand.function.geometry.GeometryFunction.GeometryValue;
public class TestPgSphereTranslator { public class TestPgSphereTranslator {
...@@ -34,6 +40,18 @@ public class TestPgSphereTranslator { ...@@ -34,6 +40,18 @@ public class TestPgSphereTranslator {
@After @After
public void tearDown() throws Exception{} public void tearDown() throws Exception{}
@Test
public void testTranslateCentroidFunction(){
try{
PgSphereTranslator translator = new PgSphereTranslator();
CentroidFunction centfc = new CentroidFunction(new GeometryValue<GeometryFunction>(new CircleFunction(new StringConstant("ICRS"), new NumericConstant(128.23), new NumericConstant(0.53), new NumericConstant(2))));
assertEquals("center(scircle(spoint(radians(128.23),radians(0.53)),radians(2)))", translator.translate(centfc));
}catch(Throwable t){
t.printStackTrace(System.err);
fail("An error occured while building a simple CentroidFunction! (see the console for more details)");
}
}
@Test @Test
public void testConvertTypeFromDB(){ public void testConvertTypeFromDB(){
PgSphereTranslator translator = new PgSphereTranslator(); PgSphereTranslator translator = new PgSphereTranslator();
......
...@@ -6,6 +6,7 @@ import static org.junit.Assert.fail; ...@@ -6,6 +6,7 @@ import static org.junit.Assert.fail;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.GregorianCalendar;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
...@@ -124,4 +125,44 @@ public class ResultSetTableIteratorTest { ...@@ -124,4 +125,44 @@ public class ResultSetTableIteratorTest {
assertEquals(ex.getClass().getName(), "tap.data.DataReadException"); assertEquals(ex.getClass().getName(), "tap.data.DataReadException");
} }
} }
@Test
public void testDateFormat(){
ResultSet rs = null;
try{
// create a valid ResultSet:
rs = DBTools.select(conn, "SELECT * FROM gums LIMIT 1;");
// Create the iterator:
ResultSetTableIterator rsit = new ResultSetTableIterator(rs);
assertTrue(rsit.nextRow());
assertTrue(rsit.hasNextCol());
rsit.nextCol();
// Set a date-time:
GregorianCalendar cal = new GregorianCalendar();
cal.set(2017, GregorianCalendar.FEBRUARY, 1, 15, 13, 56); // 1st Feb. 2017 - 15:13:56 CET
// Try to format it from a java.SQL.Timestamp into a ISO8601 date-time:
assertEquals("2017-02-01T14:13:56Z", rsit.formatColValue(new java.sql.Timestamp(cal.getTimeInMillis())));
// Try to format it from a java.UTIL.Date into an ISO8601 date-time:
assertEquals("2017-02-01T14:13:56Z", rsit.formatColValue(cal.getTime()));
// Try to format it from a java.SQL.Date into a simple date (no time indication):
assertEquals("2017-02-01", rsit.formatColValue(new java.sql.Date(cal.getTimeInMillis())));
// Try to format it into a simple time (no date indication):
assertEquals("15:13:56", rsit.formatColValue(new java.sql.Time(cal.getTimeInMillis())));
}catch(Exception ex){
ex.printStackTrace(System.err);
fail("An exception occurs while formatting dates/times.");
}finally{
if (rs != null){
try{
rs.close();
}catch(Exception ex){}
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment