Changeset: ca8f1b66b1ed for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=ca8f1b66b1ed
Modified Files:
        java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
Branch: default
Log Message:

Replace in all getXyz(int colIndex) methods the getString(colIndex) call by 
inlining its code.
This eliminates the method call overhead for each of the getXyz() methods.
As ResultSet.getXyz() methods are called very often (#columns * #rows * 
#resultsets)
this will reduce execution time.


diffs (truncated from 779 to 300 lines):

diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java 
b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
--- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -436,10 +436,16 @@ public class MonetResultSet extends Mone
         */
        @Override
        public Reader getCharacterStream(int columnIndex) throws SQLException {
-               String tmp = getString(columnIndex);
-               if (tmp == null)
-                       return null;
-               return new StringReader(tmp);
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
+                       return new StringReader(val);
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
+               }
        }
 
        /**
@@ -506,12 +512,16 @@ public class MonetResultSet extends Mone
         * @throws SQLException if a database access error occurs
         */
        @Override
-       public Blob getBlob(int i) throws SQLException {
-               String tmp = getString(i);
-               if (tmp == null) {
-                       return null;
-               } else {
-                       return MonetBlob.create(tmp);
+       public Blob getBlob(int columnIndex) throws SQLException {
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
+                       return MonetBlob.create(val);
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
        }
 
@@ -542,12 +552,16 @@ public class MonetResultSet extends Mone
         * @throws SQLException if a database access error occurs
         */
        @Override
-       public Clob getClob(int i) throws SQLException {
-               String tmp = getString(i);
-               if (tmp == null) {
-                       return null;
-               } else {
-                       return new MonetClob(tmp);
+       public Clob getClob(int columnIndex) throws SQLException {
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
+                       return new MonetClob(val);
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
        }
 
@@ -613,15 +627,19 @@ public class MonetResultSet extends Mone
         */
        @Override
        public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
-               String decimal = getString(columnIndex);
-               if (decimal == null) {
-                       return null;
-               } else {
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
                        try {
-                               return new BigDecimal(decimal);
+                               return new BigDecimal(val);
                        } catch (NumberFormatException e) {
                                return BigDecimal.ZERO;
                        }
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
        }
 
@@ -640,18 +658,21 @@ public class MonetResultSet extends Mone
        public BigDecimal getBigDecimal(int columnIndex, int scale)
                throws SQLException
        {
-               String decimal = getString(columnIndex);
-               if (decimal == null) {
-                       return null;
-               } else {
-                       BigDecimal bd;
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
                        try {
-                               bd = new BigDecimal(decimal);
+                               BigDecimal bd = new BigDecimal(val);
                                bd.setScale(scale);
+                               return bd;
                        } catch (NumberFormatException e) {
-                               bd = BigDecimal.ZERO;
+                               return BigDecimal.ZERO;
                        }
-                       return bd;
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
        }
 
@@ -699,42 +720,59 @@ public class MonetResultSet extends Mone
         */
        @Override
        public boolean getBoolean(int columnIndex) throws SQLException {
-               switch (getJavaType(types[columnIndex - 1])) {
-                       case Types.TINYINT:
-                       case Types.SMALLINT:
-                       case Types.INTEGER:
-                               if (getInt(columnIndex) == 0) {
-                                       return false;
-                               }
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return false;   // if the value is SQL NULL, 
the value returned is false
+
+                       // match common cases first
+                       if ("false".equalsIgnoreCase(val) || "0".equals(val))
+                               return false;
+                       if ("true".equalsIgnoreCase(val) || "1".equals(val))
                                return true;
-                       case Types.BIGINT:
-                               if (getLong(columnIndex) == 0L) {
-                                       return false;
-                               }
-                               return true;
-                       case Types.DOUBLE:
-                       case Types.FLOAT:
-                       case Types.REAL:
-                               if (getDouble(columnIndex) == 0.0) {
-                                       return false;
-                               }
-                               return true;
-                       case Types.DECIMAL:
-                       case Types.NUMERIC:
-                               if 
(getBigDecimal(columnIndex).compareTo(BigDecimal.ZERO) == 0) {
-                                       return false;
-                               }
-                               return true;
-                       case Types.BOOLEAN:
-                       case Types.BIT: // MonetDB doesn't use type BIT, it's 
here for completeness
-                       case Types.CHAR:
-                       case Types.VARCHAR:
-                       case Types.LONGVARCHAR: // MonetDB doesn't use type 
LONGVARCHAR, it's here for completeness
-                       case Types.CLOB:
-                               // check if string value equals "true" (case 
insensitive) or not
-                               return 
(Boolean.valueOf(getString(columnIndex))).booleanValue();
-                       default:
-                               throw new SQLException("Conversion from " + 
types[columnIndex - 1] + " to boolean type not supported", "M1M05");
+
+                       // match type specific values
+                       switch (getJavaType(types[columnIndex - 1])) {
+                               case Types.BOOLEAN:
+                               case Types.CHAR:
+                               case Types.VARCHAR:
+                               case Types.LONGVARCHAR: // MonetDB doesn't use 
type LONGVARCHAR, it's here for completeness
+                               case Types.CLOB:
+                                       // check if string value equals "true" 
(case insensitive) or not
+                                       return Boolean.parseBoolean(val);
+                               case Types.BIT: // MonetDB doesn't use type 
BinaryDigit, it's here for completeness
+                               case Types.TINYINT:
+                               case Types.SMALLINT:
+                               case Types.INTEGER:
+                                       if (getInt(columnIndex) == 0) {
+                                               return false;
+                                       }
+                                       return true;
+                               case Types.BIGINT:
+                                       if (getLong(columnIndex) == 0L) {
+                                               return false;
+                                       }
+                                       return true;
+                               case Types.DOUBLE:
+                               case Types.FLOAT:
+                               case Types.REAL:
+                                       if (getDouble(columnIndex) == 0.0) {
+                                               return false;
+                                       }
+                                       return true;
+                               case Types.DECIMAL:
+                               case Types.NUMERIC:
+                                       if 
(getBigDecimal(columnIndex).compareTo(BigDecimal.ZERO) == 0) {
+                                               return false;
+                                       }
+                                       return true;
+                               default:
+                                       throw new SQLException("Conversion from 
" + types[columnIndex - 1] + " to boolean type not supported", "M1M05");
+                       }
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
        }
 
@@ -763,12 +801,21 @@ public class MonetResultSet extends Mone
         */
        @Override
        public byte getByte(int columnIndex) throws SQLException {
-               byte ret = 0;
-               String val = getString(columnIndex);
-               if (val != null) {
-                       ret = Byte.parseByte(val);
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return (byte) 0;
+                       try {
+                               return Byte.parseByte(val);
+                       } catch (NumberFormatException e) {
+                               // ignore parse error, return the default: 0
+                               return (byte) 0;
+                       }
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
                }
-               return ret;
        }
 
        /**
@@ -798,28 +845,33 @@ public class MonetResultSet extends Mone
         */
        @Override
        public byte[] getBytes(int columnIndex) throws SQLException {
-               // According to Table B-6, getBytes() only operates on BINARY
-               // types
-               switch (getJavaType(types[columnIndex - 1])) {
-                       case Types.BINARY:
-                       case Types.VARBINARY:
-                       case Types.LONGVARBINARY:
-                               // pass
-                               break;
-                       default:
-                               throw new SQLException("Cannot operate on " +
-                                               types[columnIndex - 1] + " 
type", "M1M05");
-               }
-               String tmp = getString(columnIndex);
-               if (tmp == null) {
-                       return null;
-               } else {
-                       // unpack the HEX (BLOB) notation to real bytes
-                       int len = tmp.length() / 2;
-                       byte[] buf = new byte[len];
-                       for (int j = 0; j < len; j++)
-                               buf[j] = (byte)Integer.parseInt(tmp.substring(2 
* j, (2 * j) + 2), 16);
-                       return buf;
+               try {
+                       String val = tlp.values[columnIndex - 1];
+                       // the lastColumnRead must be updated for the wasNull() 
to work properly!
+                       lastColumnRead = columnIndex - 1;
+                       if (val == null)
+                               return null;
+
+                       // According to Table B-6, getBytes() only operates on 
BINARY types
+                       switch (getJavaType(types[columnIndex - 1])) {
+                               case Types.BLOB:
+                               case Types.BINARY:
+                               case Types.VARBINARY:
+                               case Types.LONGVARBINARY:
+                                       // unpack the HEX (BLOB) notation to 
real bytes
+                                       int len = val.length() / 2;
+                                       byte[] buf = new byte[len];
+                                       int offset;
+                                       for (int j = 0; j < len; j++) {
+                                               offset = j * 2;
+                                               buf[j] = 
(byte)Integer.parseInt(val.substring(offset, offset + 2), 16);
+                                       }
+                                       return buf;
+                               default:
+                                       throw new SQLException("Cannot operate 
on " + types[columnIndex - 1] + " type", "M1M05");
+                       }
+               } catch (IndexOutOfBoundsException e) {
+                       throw new SQLException("No such column " + columnIndex, 
"M1M05");
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to