Skip to content
Snippets Groups Projects
Commit 6d4554c2 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[TAP] Improve support of BigDecimal and BigInteger while formatting a DB result.

_This commit fixes the GitHub issue #97 ._
parent 41a46294
Branches
No related tags found
No related merge requests found
...@@ -16,7 +16,8 @@ package tap.data; ...@@ -16,7 +16,8 @@ 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-2017 - Astronomisches Rechen Institut (ARI) * Copyright 2014-2019 - Astronomisches Rechen Institut (ARI),
* UDS/Centre de Données astronomiques de Strasbourg (CDS)
*/ */
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -47,8 +48,8 @@ import uws.ISO8601Format; ...@@ -47,8 +48,8 @@ import uws.ISO8601Format;
* {@link #getColType()} will return a TAP type based on the one declared in the {@link ResultSetMetaData} object. * {@link #getColType()} will return a TAP type based on the one declared in the {@link ResultSetMetaData} object.
* </i></p> * </i></p>
* *
* @author Gr&eacute;gory Mantelet (ARI) * @author Gr&eacute;gory Mantelet (ARI;CDS)
* @version 2.1 (07/2017) * @version 2.3 (03/2019)
* @since 2.0 * @since 2.0
*/ */
public class ResultSetTableIterator implements TableIterator { public class ResultSetTableIterator implements TableIterator {
...@@ -716,32 +717,59 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -716,32 +717,59 @@ 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 java.sql.Time object, format it into an ISO8601 time (i.e. with the format: HH:mm:ss): // 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 java.sql.Time) if (colValue instanceof java.sql.Time)
colValue = timeFormat.format((java.sql.Time)colValue); 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): // 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) else if (colValue instanceof java.sql.Date)
colValue = dateFormat.format((java.sql.Date)colValue); 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: // 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. // 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) else if (colValue instanceof java.util.Date)
colValue = ISO8601Format.format((java.util.Date)colValue); colValue = ISO8601Format.format((java.util.Date)colValue);
// if the type is a BigDecimal object (this is possible for instance with PostgreSQL "numeric" datatype,
// if the type is a BigDecimal object (this is possible for instance with PostgreSQL "numeric" datatype or Sybase IQ,
// but this type can not be supported in FITS and VOTable): // but this type can not be supported in FITS and VOTable):
else if (colValue instanceof BigDecimal) else if (colValue instanceof BigDecimal){
colValue = ((BigDecimal)colValue).doubleValue(); BigDecimal bd = (BigDecimal)colValue;
if (colType.type == DBDatatype.BIGINT)
colValue = bd.longValue();
else if (colType.type == DBDatatype.INTEGER)
colValue = bd.intValue();
else if (colType.type == DBDatatype.SMALLINT)
colValue = bd.shortValue();
else if (colType.type == DBDatatype.REAL)
colValue = bd.floatValue();
else
colValue = bd.doubleValue();
}
// if the type is a BigInteger object (as BigDecimal, this type can not be supported in FITS and VOTable): // if the type is a BigInteger object (as BigDecimal, this type can not be supported in FITS and VOTable):
else if (colValue instanceof BigInteger) else if (colValue instanceof BigInteger){
colValue = ((BigInteger)colValue).longValue(); BigInteger bi = (BigInteger)colValue;
if (colType.type == DBDatatype.INTEGER)
colValue = bi.intValue();
else if (colType.type == DBDatatype.SMALLINT)
colValue = bi.shortValue();
else
colValue = bi.longValue();
}
// 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());
// if the column value is a Boolean object, format it as a SMALLINT: // if the column value is a Boolean object, format it as a SMALLINT:
else if (colValue instanceof Boolean) else if (colValue instanceof Boolean)
colValue = ((Boolean)colValue) ? new Short((short)1) : new Short((short)0); colValue = ((Boolean)colValue) ? new Short((short)1) : new Short((short)0);
// if the column should be only a character: // if the column should be only a character:
else if (colType != null && colValue != null && colType.type == DBDatatype.CHAR && (colType.length == 1 || colType.length <= 0) && colValue instanceof String) else if (colType != null && colValue != null && colType.type == DBDatatype.CHAR && (colType.length == 1 || colType.length <= 0) && colValue instanceof String)
colValue = ((String)colValue).charAt(0); colValue = ((String)colValue).charAt(0);
// if the column value is a geometrical object, it must be serialized in STC-S: // if the column value is a geometrical object, it must be serialized in STC-S:
else if (translator != null && colType != null && colType.isGeometry()){ else if (translator != null && colType != null && colType.isGeometry()){
try{ try{
...@@ -753,6 +781,7 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -753,6 +781,7 @@ public class ResultSetTableIterator implements TableIterator {
} }
} }
} }
return colValue; return colValue;
} }
...@@ -858,7 +887,8 @@ public class ResultSetTableIterator implements TableIterator { ...@@ -858,7 +887,8 @@ public class ResultSetTableIterator implements TableIterator {
if (params != null && params.length > 0){ if (params != null && params.length > 0){
try{ try{
lengthParam = Integer.parseInt(params[0]); lengthParam = Integer.parseInt(params[0]);
}catch(NumberFormatException nfe){} }catch(NumberFormatException nfe){
}
} }
// CASE: SQLITE // CASE: SQLITE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment