Mercurial > hg > monetdb-java
changeset 103:a00241382675 embedded
Fixed ResultSet data fetching from the JDBC tests output.
author | Pedro Ferreira <pedro.ferreira@monetdbsolutions.com> |
---|---|
date | Mon, 16 Jan 2017 15:19:45 +0100 (2017-01-16) |
parents | 08bc9009d190 |
children | d4c6a01cc300 |
files | src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java src/main/java/nl/cwi/monetdb/mcl/responses/DataBlockResponse.java |
diffstat | 4 files changed, 301 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @@ -190,10 +190,9 @@ public abstract class MonetConnection ex * @param resultSetType the type of result sets to produce * @param resultSetConcurrency the concurrency of result sets to produce * @return A ResponseList instance - * @throws SQLException if an IO exception or a database error occurs */ public abstract ResponseList createResponseList(int fetchSize, int maxRows, int resultSetType, - int resultSetConcurrency) throws SQLException; + int resultSetConcurrency); /** * Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be @@ -1369,7 +1368,7 @@ public abstract class MonetConnection ex * @param rstype the type of result sets to produce * @param rsconcur the concurrency of result sets to produce */ - public ResponseList(int cachesize, int maxrows, int rstype, int rsconcur) throws SQLException { + public ResponseList(int cachesize, int maxrows, int rstype, int rsconcur) { this.cachesize = cachesize; this.maxrows = maxrows; this.rstype = rstype;
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java @@ -587,7 +587,50 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return null; } - return (BigDecimal) currentBlock.getObjectValue(columnIndex - 1); + BigDecimal val; + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.DECIMAL: + val = (BigDecimal) currentBlock.getObjectValue(columnIndex - 1); + break; + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + val = BigDecimal.valueOf(huge.longValue()); + break; + case Types.BOOLEAN: + byte bval = currentBlock.getBooleanValue(columnIndex - 1) ? (byte) 1 : (byte) 0; + val = new BigDecimal(bval); + break; + case Types.TINYINT: + val = new BigDecimal(currentBlock.getByteValue(columnIndex - 1)); + break; + case Types.SMALLINT: + val = new BigDecimal(currentBlock.getShortValue(columnIndex - 1)); + break; + case Types.INTEGER: + val = new BigDecimal(currentBlock.getIntValue(columnIndex - 1)); + break; + case Types.BIGINT: + val = new BigDecimal(currentBlock.getLongValue(columnIndex - 1)); + break; + case Types.REAL: + val = new BigDecimal(currentBlock.getFloatValue(columnIndex - 1)); + break; + case Types.DOUBLE: + val = new BigDecimal(currentBlock.getDoubleValue(columnIndex - 1)); + break; + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + val = new BigDecimal(currentBlock.getValueAsString(columnIndex - 1)); + break; + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } + return val; } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -612,7 +655,7 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return null; } - BigDecimal val = (BigDecimal) currentBlock.getObjectValue(columnIndex - 1); + BigDecimal val = getBigDecimal(columnIndex); val.setScale(scale); return val; } catch (ClassCastException ex) { @@ -671,33 +714,36 @@ public class MonetResultSet extends Mone switch (JdbcSQLTypes[columnIndex - 1]) { case Types.BOOLEAN: return currentBlock.getBooleanValue(columnIndex - 1); + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1) != 0; + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1) != 0; + case Types.INTEGER: + return currentBlock.getIntValue(columnIndex - 1) != 0; + case Types.BIGINT: + return currentBlock.getLongValue(columnIndex - 1) != 0L; + case Types.REAL: + return currentBlock.getFloatValue(columnIndex - 1) != 0.0f; + case Types.DOUBLE: + return currentBlock.getDoubleValue(columnIndex - 1) != 0.0d; case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: String val = currentBlock.getValueAsString(columnIndex - 1); if ("false".equalsIgnoreCase(val) || "0".equals(val)) return false; if ("true".equalsIgnoreCase(val) || "1".equals(val)) return true; throw newSQLInvalidColumnIndexException(columnIndex); - case Types.TINYINT: - return getByte(columnIndex) != 0; - case Types.SMALLINT: - return getShort(columnIndex) != 0; - case Types.INTEGER: - return getInt(columnIndex) != 0; - case Types.BIGINT: - return getLong(columnIndex) != 0L; - case Types.REAL: - return getFloat(columnIndex) != 0.0f; - case Types.DOUBLE: - return getDouble(columnIndex) != 0.0d; case Types.NUMERIC: BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); return huge.compareTo(BigInteger.ZERO) != 0; case Types.DECIMAL: - return getBigDecimal(columnIndex).compareTo(BigDecimal.ZERO) != 0; + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.compareTo(BigDecimal.ZERO) != 0; default: //OTHERS, BLOB, LONGVARBINARY, TIME... throw new SQLException("Conversion from " + types[columnIndex - 1] + " to boolean type not supported", "M1M05"); @@ -736,7 +782,38 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0; } - return currentBlock.getByteValue(columnIndex - 1); + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? (byte) 1 : (byte) 0; + case Types.SMALLINT: + return (byte) currentBlock.getShortValue(columnIndex - 1); + case Types.INTEGER: + return (byte) currentBlock.getIntValue(columnIndex - 1); + case Types.BIGINT: + return (byte) currentBlock.getLongValue(columnIndex - 1); + case Types.REAL: + return (byte) Math.round(currentBlock.getFloatValue(columnIndex - 1)); + case Types.DOUBLE: + return (byte) Math.round(currentBlock.getDoubleValue(columnIndex - 1)); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Byte.parseByte(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.byteValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.byteValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -780,6 +857,11 @@ public class MonetResultSet extends Mone case Types.LONGVARBINARY: // unpack the HEX (BLOB) notation to real bytes return (byte[]) currentBlock.getObjectValue(columnIndex - 1); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + return currentBlock.getValueAsString(columnIndex - 1).getBytes(); default: throw new SQLException("Cannot operate on " + types[columnIndex - 1] + " type", "M1M05"); } @@ -856,7 +938,38 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0.0d; } - return currentBlock.getDoubleValue(columnIndex - 1); + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.DOUBLE: + return currentBlock.getDoubleValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? 1.0d : 0.0d; + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1); + case Types.INTEGER: + return currentBlock.getIntValue(columnIndex - 1); + case Types.BIGINT: + return currentBlock.getLongValue(columnIndex - 1); + case Types.REAL: + return currentBlock.getFloatValue(columnIndex - 1); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Double.parseDouble(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.doubleValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.doubleValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -970,7 +1083,38 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0.0f; } - return currentBlock.getFloatValue(columnIndex - 1); + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.REAL: + return currentBlock.getFloatValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? 1.0f : 0.0f; + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1); + case Types.INTEGER: + return currentBlock.getIntValue(columnIndex - 1); + case Types.BIGINT: + return currentBlock.getLongValue(columnIndex - 1); + case Types.DOUBLE: + return (float) currentBlock.getDoubleValue(columnIndex - 1); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Float.parseFloat(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.floatValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.floatValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -1005,7 +1149,39 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0; } - return currentBlock.getIntValue(columnIndex - 1); + // match type specific values + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.INTEGER: + return currentBlock.getIntValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? 1 : 0; + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1); + case Types.BIGINT: + return (int) currentBlock.getLongValue(columnIndex - 1); + case Types.REAL: + return Math.round(currentBlock.getFloatValue(columnIndex - 1)); + case Types.DOUBLE: + return (int) Math.round(currentBlock.getDoubleValue(columnIndex - 1)); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Integer.parseInt(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.intValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.intValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -1040,7 +1216,38 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0L; } - return currentBlock.getLongValue(columnIndex - 1); + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.BIGINT: + return currentBlock.getLongValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? 1 : 0; + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1); + case Types.INTEGER: + return currentBlock.getIntValue(columnIndex - 1); + case Types.REAL: + return Math.round(currentBlock.getFloatValue(columnIndex - 1)); + case Types.DOUBLE: + return Math.round(currentBlock.getDoubleValue(columnIndex - 1)); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Long.parseLong(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.longValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.longValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -2126,7 +2333,38 @@ public class MonetResultSet extends Mone if(setLastNullValue(columnIndex - 1)) { return 0; } - return currentBlock.getShortValue(columnIndex - 1); + switch (JdbcSQLTypes[columnIndex - 1]) { + case Types.SMALLINT: + return currentBlock.getShortValue(columnIndex - 1); + case Types.BOOLEAN: + return currentBlock.getBooleanValue(columnIndex - 1) ? (short) 1 : (short) 0; + case Types.TINYINT: + return currentBlock.getByteValue(columnIndex - 1); + case Types.INTEGER: + return (short) currentBlock.getIntValue(columnIndex - 1); + case Types.BIGINT: + return (short) currentBlock.getLongValue(columnIndex - 1); + case Types.REAL: + return (short) Math.round(currentBlock.getFloatValue(columnIndex - 1)); + case Types.DOUBLE: + return (short) Math.round(currentBlock.getDoubleValue(columnIndex - 1)); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: + return Short.parseShort(currentBlock.getValueAsString(columnIndex - 1)); + case Types.NUMERIC: + BigInteger huge = (BigInteger) currentBlock.getValueAsObject(columnIndex - 1); + return huge.shortValue(); + case Types.DECIMAL: + BigDecimal bigdec = (BigDecimal) currentBlock.getValueAsObject(columnIndex - 1); + return bigdec.shortValue(); + default: //OTHERS, BLOB, LONGVARBINARY, TIME... + throw new SQLException("Conversion from " + types[columnIndex - 1] + + " to boolean type not supported", "M1M05"); + } } catch (ClassCastException ex) { throw new SQLException(ex.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -2290,16 +2528,18 @@ public class MonetResultSet extends Mone } Calendar res; switch (JdbcSQLTypes[columnIndex - 1]) { - case Types.CHAR : + case Types.DATE: + res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); + break; + case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: String value = currentBlock.getValueAsString(columnIndex - 1); res = GregorianCalendarParser.ParseDate(value, new ParsePosition(0)); break; - case Types.DATE: - res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); - break; default: throw new SQLException("Conversion from " + types[columnIndex - 1] + " to Date not supported", "M1M05"); @@ -2380,21 +2620,23 @@ public class MonetResultSet extends Mone } Calendar res; switch (JdbcSQLTypes[columnIndex - 1]) { - case Types.CHAR : + case Types.TIME: + res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); + res.setTimeZone(cal.getTimeZone()); + break; + case Types.TIME_WITH_TIMEZONE: + res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); + break; + case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: String value = currentBlock.getValueAsString(columnIndex - 1); res = GregorianCalendarParser.ParseTime(value, new ParsePosition(0), false); res.setTimeZone(cal.getTimeZone()); break; - case Types.TIME: - res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); - res.setTimeZone(cal.getTimeZone()); - break; - case Types.TIME_WITH_TIMEZONE: - res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); - break; default: throw new SQLException("Conversion from " + types[columnIndex - 1] + " to Time not supported", "M1M05"); @@ -2474,21 +2716,23 @@ public class MonetResultSet extends Mone } Calendar res; switch (JdbcSQLTypes[columnIndex - 1]) { - case Types.CHAR : + case Types.TIMESTAMP: + res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); + res.setTimeZone(cal.getTimeZone()); + break; + case Types.TIMESTAMP_WITH_TIMEZONE: + res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); + break; + case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: String value = currentBlock.getValueAsString(columnIndex - 1); res = GregorianCalendarParser.ParseTimestamp(value, new ParsePosition(0), false); res.setTimeZone(cal.getTimeZone()); break; - case Types.TIMESTAMP: - res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); - res.setTimeZone(cal.getTimeZone()); - break; - case Types.TIMESTAMP_WITH_TIMEZONE: - res = (Calendar) currentBlock.getValueAsObject(columnIndex - 1); - break; default: throw new SQLException("Conversion from " + types[columnIndex - 1] + " to Timestamp not supported", "M1M05"); @@ -2561,15 +2805,17 @@ public class MonetResultSet extends Mone return null; } switch(JdbcSQLTypes[columnIndex - 1]) { //if it's a string type, will attempt the conversion + case Types.OTHER: + if("url".equals(types[columnIndex - 1])) { + return new URL(currentBlock.getValueAsString(columnIndex - 1)); + } case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: + case Types.BLOB: + case Types.LONGVARBINARY: return new URL(currentBlock.getValueAsString(columnIndex - 1)); - case Types.OTHER: - if("url".equals(types[columnIndex - 1])) { - return new URL(currentBlock.getValueAsString(columnIndex - 1)); - } default: throw new SQLException("Cannot convert " + types[columnIndex - 1] + " to an url", "M1M05"); }
--- a/src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java +++ b/src/main/java/nl/cwi/monetdb/mcl/connection/mapi/MapiConnection.java @@ -262,11 +262,9 @@ public class MapiConnection extends Mone * @param resultSetType the type of result sets to produce * @param resultSetConcurrency the concurrency of result sets to produce * @return A ResponseList instance - * @throws SQLException if an IO exception or a database error occurs */ @Override - public ResponseList createResponseList(int fetchSize, int maxRows, int resultSetType, int resultSetConcurrency) - throws SQLException { + public ResponseList createResponseList(int fetchSize, int maxRows, int resultSetType, int resultSetConcurrency) { return new MonetConnection.ResponseList(fetchSize, maxRows, resultSetType, resultSetConcurrency); }
--- a/src/main/java/nl/cwi/monetdb/mcl/responses/DataBlockResponse.java +++ b/src/main/java/nl/cwi/monetdb/mcl/responses/DataBlockResponse.java @@ -306,6 +306,13 @@ public class DataBlockResponse implement */ public String getValueAsString(int column) { switch (this.jdbcSQLTypes[column]) { + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + case Types.OTHER: + return ((String[]) this.data[column])[this.blockLine]; + case Types.LONGVARBINARY: + return Arrays.toString(((byte[][]) this.data[column])[this.blockLine]); case Types.BOOLEAN: return ((byte[]) this.data[column])[this.blockLine] == 1 ? "true" : "false"; case Types.TINYINT: @@ -320,13 +327,6 @@ public class DataBlockResponse implement return Float.toString(((float[]) this.data[column])[this.blockLine]); case Types.DOUBLE: return Double.toString(((double[]) this.data[column])[this.blockLine]); - case Types.LONGVARBINARY: - return Arrays.toString(((byte[][]) this.data[column])[this.blockLine]); - case Types.CHAR: - case Types.VARCHAR: - case Types.LONGVARCHAR: - case Types.OTHER: - return ((String[]) this.data[column])[this.blockLine]; default: //BLOB, CLOB, BigDecimal, BigInteger, Time, Timestamp and Date return ((Object[]) this.data[column])[this.blockLine].toString(); }