Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 41730 invoked from network); 28 Apr 2005 19:14:30 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Apr 2005 19:14:30 -0000 Received: (qmail 88045 invoked by uid 500); 28 Apr 2005 19:14:51 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 87910 invoked by uid 500); 28 Apr 2005 19:14:49 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 87772 invoked by uid 500); 28 Apr 2005 19:14:46 -0000 Delivered-To: apmail-incubator-derby-cvs@incubator.apache.org Received: (qmail 87539 invoked by uid 99); 28 Apr 2005 19:14:41 -0000 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 28 Apr 2005 12:14:36 -0700 Received: (qmail 39135 invoked by uid 65534); 28 Apr 2005 19:06:04 -0000 Message-ID: <20050428190604.39133.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: svn commit: r165178 [5/26] - in /incubator/derby/code/trunk: ./ java/client/ java/client/org/ java/client/org/apache/ java/client/org/apache/derby/ java/client/org/apache/derby/client/ java/client/org/apache/derby/client/am/ java/client/org/apache/derby/client/net/ java/client/org/apache/derby/client/resources/ java/client/org/apache/derby/jdbc/ tools/ant/properties/ Date: Thu, 28 Apr 2005 19:05:45 -0000 To: derby-cvs@incubator.apache.org From: bandaram@apache.org X-Mailer: svnmailer-1.0.0-dev X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Cr= ossConverters.java URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/client/o= rg/apache/derby/client/am/CrossConverters.java?rev=3D165178&view=3Dauto =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Cross= Converters.java (added) +++ incubator/derby/code/trunk/java/client/org/apache/derby/client/am/Cross= Converters.java Thu Apr 28 12:05:42 2005 @@ -0,0 +1,1622 @@ +/* + + Derby - Class org.apache.derby.client.am.CrossConverters + + Copyright (c) 2001, 2005 The Apache Software Foundation or its licensor= s, where applicable. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +package org.apache.derby.client.am; + +// All currently supported derby types are mapped to one of the following = jdbc types: +// java.sql.Types.SMALLINT; +// java.sql.Types.INTEGER; +// java.sql.Types.BIGINT; +// java.sql.Types.REAL; +// java.sql.Types.DOUBLE; +// java.sql.Types.DECIMAL; +// java.sql.Types.DATE; +// java.sql.Types.TIME; +// java.sql.Types.TIMESTAMP; +// java.sql.Types.CHAR; +// java.sql.Types.VARCHAR; +// java.sql.Types.LONGVARCHAR; +// java.sql.Types.CLOB; +// java.sql.Types.BLOB; +// +final class CrossConverters +{ + private final static java.math.BigDecimal bdMaxByteValue__ =3D + java.math.BigDecimal.valueOf (Byte.MAX_VALUE); + private final static java.math.BigDecimal bdMinByteValue__ =3D + java.math.BigDecimal.valueOf (Byte.MIN_VALUE); + private final static java.math.BigDecimal bdMaxShortValue__ =3D + java.math.BigDecimal.valueOf (Short.MAX_VALUE); + private final static java.math.BigDecimal bdMinShortValue__ =3D + java.math.BigDecimal.valueOf (Short.MIN_VALUE); + private final static java.math.BigDecimal bdMaxIntValue__ =3D + java.math.BigDecimal.valueOf (Integer.MAX_VALUE); + private final static java.math.BigDecimal bdMinIntValue__ =3D + java.math.BigDecimal.valueOf (Integer.MIN_VALUE); + private final static java.math.BigDecimal bdMaxLongValue__ =3D + java.math.BigDecimal.valueOf (Long.MAX_VALUE); + private final static java.math.BigDecimal bdMinLongValue__ =3D + java.math.BigDecimal.valueOf (Long.MIN_VALUE); + private final static java.math.BigDecimal bdMaxFloatValue__ =3D + new java.math.BigDecimal (Float.MAX_VALUE); + private final static java.math.BigDecimal bdMinFloatValue__ =3D + new java.math.BigDecimal (-Float.MAX_VALUE); + private final static java.math.BigDecimal bdMaxDoubleValue__ =3D + new java.math.BigDecimal (Double.MAX_VALUE); + private final static java.math.BigDecimal bdMinDoubleValue__ =3D + new java.math.BigDecimal (-Double.MAX_VALUE); + + // Since BigDecimals are immutable, we can return pointers to these cann= ed 0's and 1's. + private final static java.math.BigDecimal bdZero__ =3D java.math.BigDeci= mal.valueOf (0); + private final static java.math.BigDecimal bdOne__ =3D java.math.BigDecim= al.valueOf (1); + + // ---------------------- state ----------------------------------------= ------ + + Agent agent_; + + // ----------------------constructors/finalizer-------------------------= ------ + + CrossConverters (Agent agent) + { + agent_ =3D agent; + } + + // ---------------------------------------------------------------------= ------ + // The following methods are used for input cross conversion. + // ---------------------------------------------------------------------= ------ + + //---------------------------- setObject() methods ---------------------= ------ + + // Convert from boolean source to target type. + // In support of PS.setBoolean(). + // See differences.html for DNC setBoolean() semantics. + final Object setObject (int targetType, boolean source) throws SqlExcept= ion + { + return setObject (targetType, (short) (source ? 1 : 0)); + } + + // Convert from byte source to target type + // In support of PS.setByte() + final Object setObject (int targetType, byte source) throws SqlException + { + return setObject (targetType, (short) source); + } + + // Convert from short source to target type + // In support of PS.setShort() + final Object setObject (int targetType, short source) throws SqlException + { + switch (targetType) { + case Types.SMALLINT: + return new Short (source); + + case Types.INTEGER: + return new Integer (source); + + case Types.BIGINT: + return new Long (source); + + case Types.REAL: + return new Float (source); + + case Types.DOUBLE: + return new Double (source); + + case Types.DECIMAL: + return java.math.BigDecimal.valueOf (source); + + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + return String.valueOf (source); + + default: + throw new SqlException (agent_.logWriter_, "Illegal Conversion"); + } + } + + // Convert from integer source to target type + // In support of PS.setInt() + final Object setObject (int targetType, int source) throws SqlException + { + switch (targetType) { + case Types.SMALLINT: + if (Configuration.rangeCheckCrossConverters && + (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Short ((short) source); + + case Types.INTEGER: + return new Integer (source); + + case Types.BIGINT: + return new Long (source); + + case Types.REAL: + return new Float (source); + + case Types.DOUBLE: + return new Double (source); + + case Types.DECIMAL: + return java.math.BigDecimal.valueOf (source); + + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + return String.valueOf (source); + + default: + throw new SqlException (agent_.logWriter_, "Illegal Conversion"); + } + } + + // This method is used in lieu of setObject(targetType, sourceObject) be= cause we + // don't support the BIT/BOOLEAN as underlying DERBY targetTypes. + final boolean setBooleanFromObject (Object source, int sourceType) throw= s SqlException + { + switch (sourceType) { + case Types.SMALLINT: + return getBooleanFromShort (((Short)source).shortValue()); + case Types.INTEGER: + return getBooleanFromInt (((Integer)source).intValue()); + case Types.BIGINT: + return getBooleanFromLong (((java.math.BigInteger)source).longValue(= )); + case Types.REAL: + return getBooleanFromFloat (((Float)source).floatValue()); + case Types.DOUBLE: + return getBooleanFromDouble (((Double)source).doubleValue()); + case Types.DECIMAL: + return getBooleanFromLong (((java.math.BigDecimal)source).longValue(= )); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + return getBooleanFromString ((String)source); + default: + throw new ColumnTypeConversionException (agent_.logWriter_); + } + } + + // This method is used in lieu of setObject(targetType, sourceObject) be= cause we + // don't support the BIT/BOOLEAN as underlying DERBY targetTypes. + final byte setByteFromObject (Object source, int sourceType) throws SqlE= xception + { + switch (sourceType) { + case Types.SMALLINT: + return getByteFromShort (((Short)source).shortValue()); + case Types.INTEGER: + return getByteFromInt (((Integer)source).intValue()); + case Types.BIGINT: + return getByteFromLong (((java.math.BigInteger)source).longValue()); + case Types.REAL: + return getByteFromFloat (((Float)source).floatValue()); + case Types.DOUBLE: + return getByteFromDouble (((Double)source).doubleValue()); + case Types.DECIMAL: + return getByteFromLong (((java.math.BigDecimal)source).longValue()); + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + return getByteFromString ((String)source); + default: + throw new ColumnTypeConversionException (agent_.logWriter_); + } + } + + // Convert from long source to target type + // In support of PS.setLong() + final Object setObject (int targetType, long source) throws SqlException + { + switch (targetType) { + case Types.SMALLINT: + if (Configuration.rangeCheckCrossConverters && + (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Short ((short) source); + + case Types.INTEGER: + if (Configuration.rangeCheckCrossConverters && + (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Integer ((int) source); + + case Types.BIGINT: + return new Long (source); + + case Types.REAL: + return new Float (source); + + case Types.DOUBLE: + return new Double (source); + + case Types.DECIMAL: + return java.math.BigDecimal.valueOf (source); + + case Types.CHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: + return String.valueOf (source); + + default: + throw new SqlException (agent_.logWriter_, "Illegal Conversion"); + } + } + + // Convert from floating point source to target type + // In support of PS.setFloat() + final Object setObject (int targetType, float source) throws SqlException + { + switch (targetType) { + case Types.SMALLINT: + if (Configuration.rangeCheckCrossConverters && + (source > Short.MAX_VALUE || source < Short.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Short ((short) source); + + case Types.INTEGER: + if (Configuration.rangeCheckCrossConverters && + (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Integer ((int) source); + + case Types.BIGINT: + if (Configuration.rangeCheckCrossConverters && + (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Long ((long) source); + + case Types.REAL: + if (Configuration.rangeCheckCrossConverters && + // change the check from (source > Float.MAX_VALUE || source < -= Float.MIN_VALUE)) + // to the following: + //--------------------------------------------------------------= --------------------- + // -infinity 0 = +infinity + // |__________________________|=3D=3D=3D=3D=3D=3D|____= ____________________| + // <-3.4E+38| | | = |>+3.4E+38 + // | | |________________= _ | + // | |-1.4E-45 +1.79E+308 + // | | |_______________= __ | + // | |-4.9E-324 Short.MAX_VALUE || source < Short.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Short ((short) source); + + case Types.INTEGER: + if (Configuration.rangeCheckCrossConverters && + (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Integer ((int) source); + + case Types.BIGINT: + if (Configuration.rangeCheckCrossConverters && + (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Long ((long) source); + + case Types.REAL: + if (Configuration.rangeCheckCrossConverters && + (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, S= tring.valueOf (source)); + return new Float ((float) source); + + case Types.DOUBLE: + if (Configuration.rangeCheckCrossConverters && + // change the check from (source > Double.MAX_VALUE || source < = -Double.MIN_VALUE)) + // to the following: + //--------------------------------------------------------------= ----------------------- + // -infinity 0 = +infinity + // |__________________________|=3D=3D=3D=3D=3D=3D|___= _____________________| + // <-1.79E+308| | | = |>+1.79E+308 + // | | |_______________= __ | + // | |-4.9E-324 java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (byte) source; + } + + final byte getByteFromInt (int source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (byte) source; + } + + final byte getByteFromLong (long source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (byte) source; + } + + final byte getByteFromFloat (float source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (byte) source; + } + + final byte getByteFromDouble (double source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Byte.MAX_VALUE || source < java.lang.Byte.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (byte) source; + } + + final byte getByteFromBigDecimal (java.math.BigDecimal source) throws Sq= lException + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxByteValue__) =3D=3D 1 || source.compareTo = (bdMinByteValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return (byte) source.intValue(); + } + + final byte getByteFromBoolean (boolean source) throws SqlException + { + return source ? (byte) 1 : (byte) 0; + } + + final byte getByteFromString (String source) throws SqlException + { + try { + return parseByte (source); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "byte."); + } + } + + //---------------------------- getShort*() methods ---------------------= ------ + + final short getShortFromInt (int source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MI= N_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (short) source; + } + + final short getShortFromLong (long source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MI= N_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (short) source; + } + + final short getShortFromFloat (float source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MI= N_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (short) source; + } + + final short getShortFromDouble (double source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MI= N_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (short) source; + } + + final short getShortFromBigDecimal (java.math.BigDecimal source) throws = SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxShortValue__) =3D=3D 1 || source.compareTo= (bdMinShortValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return (short) source.intValue(); + } + + final short getShortFromBoolean (boolean source) throws SqlException + { + return source ? (short) 1 : (short) 0; + } + + final short getShortFromString (String source) throws SqlException + { + try { + return parseShort (source); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "short."); + } + } + + //---------------------------- getInt*() methods -----------------------= ------ + + final int getIntFromLong (long source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Integer.MAX_VALUE || source < java.lang.Intege= r=2EMIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (int) source; + } + + final int getIntFromFloat (float source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Integer.MAX_VALUE || source < java.lang.Intege= r=2EMIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (int) source; + } + + final int getIntFromDouble (double source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Integer.MAX_VALUE || source < java.lang.Intege= r=2EMIN_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (int) source; + } + + final int getIntFromBigDecimal (java.math.BigDecimal source) throws SqlE= xception + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxIntValue__) =3D=3D 1 || source.compareTo (= bdMinIntValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return source.intValue(); + } + + final int getIntFromBoolean (boolean source) throws SqlException + { + return source ? (int) 1 : (int) 0; + } + + final int getIntFromString (String source) throws SqlException + { + try { + return parseInt (source); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "int."); + } + } + + //---------------------------- getLong*() methods ----------------------= ------ + + final long getLongFromFloat (float source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (long) source; + } + + final long getLongFromDouble (double source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > java.lang.Long.MAX_VALUE || source < java.lang.Long.MIN_= VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (long) source; + } + + final long getLongFromBigDecimal (java.math.BigDecimal source) throws Sq= lException + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxLongValue__) =3D=3D 1 || source.compareTo = (bdMinLongValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return source.longValue(); + } + + final long getLongFromBoolean (boolean source) throws SqlException + { + return source ? (long) 1 : (long) 0; + } + + final long getLongFromString (String source) throws SqlException + { + try { + return parseLong (source); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "long."); + } + } + + //---------------------------- getFloat*() methods ---------------------= ------ + + final float getFloatFromDouble (double source) throws SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + + return (float) source; + } + + final float getFloatFromBigDecimal (java.math.BigDecimal source) throws = SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxFloatValue__) =3D=3D 1 || source.compareTo= (bdMinFloatValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return source.floatValue(); + } + + final float getFloatFromBoolean (boolean source) throws SqlException + { + return source ? (float) 1 : (float) 0; + } + + final float getFloatFromString (String source) throws SqlException + { + try { + return Float.parseFloat (source.trim()); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "float."); + } + } + + //---------------------------- getDouble*() methods --------------------= ------ + + final double getDoubleFromBigDecimal (java.math.BigDecimal source) throw= s SqlException + { + if (Configuration.rangeCheckCrossConverters && + (source.compareTo (bdMaxDoubleValue__) =3D=3D 1 || source.compareT= o (bdMinDoubleValue__) =3D=3D -1)) + throw new LossOfPrecisionConversionException (agent_.logWriter_, Str= ing.valueOf (source)); + return source.doubleValue(); + } + + final double getDoubleFromBoolean (boolean source) throws SqlException + { + return source ? (double) 1 : (double) 0; + } + + final double getDoubleFromString (String source) throws SqlException + { + try { + return Double.parseDouble (source.trim()); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "double."); + } + } + + //---------------------------- getBigDecimal*() methods ----------------= ------ + + final java.math.BigDecimal getBigDecimalFromBoolean (boolean source) thr= ows SqlException + { + return source ? bdOne__ : bdZero__; + } + + final java.math.BigDecimal getBigDecimalFromString (String source) throw= s SqlException + { + try { + // Unfortunately, the big decimal constructor calls java.lang.Long.p= arseLong(), + // which doesn't like spaces, so we have to call trim() to get rid o= f the spaces from CHAR columns. + return new java.math.BigDecimal (source.trim()); + } + catch (java.lang.NumberFormatException e) { + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "java.math.BigDecimal."); + } + } + + //---------------------------- getString*() methods --------------------= ------ + + final String getStringFromBoolean (boolean source) throws SqlException + { + return source ? "1" : "0"; + } + + final String getStringFromBytes (byte[] bytes) throws SqlException + { + StringBuffer stringBuffer =3D new StringBuffer (bytes.length*2); + for (int i=3D0; i < bytes.length; i++) { + String hexForByte =3D Integer.toHexString (bytes[i] & 0xff); + // If the byte is x0-F, prepend a "0" in front to ensure 2 char repr= esentation + if (hexForByte.length() =3D=3D 1) stringBuffer.append ('0'); + stringBuffer.append (hexForByte); + } + return stringBuffer.toString(); + } + + + // All Numeric, and Date/Time types use String.valueOf (source) + + //---------------------------- getDate*() methods ----------------------= ------ + + final java.sql.Date getDateFromString (String source) throws SqlException + { + try { + // We override the JRE's Date.valueOf() in order to support a more E= xtended ISO format as requested by Toronto team + return date_valueOf (source); + } + catch (java.lang.IllegalArgumentException e) { // subsumes NumberForma= tException + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "java.sql.Date."); + } + } + + final java.sql.Date getDateFromTime (java.sql.Time source) throws SqlExc= eption + { + return new java.sql.Date (source.getTime()); + } + + final java.sql.Date getDateFromTimestamp (java.sql.Timestamp source) thr= ows SqlException + { + return new java.sql.Date (source.getTime()); + } + + //---------------------------- getTime*() methods ----------------------= ------ + + final java.sql.Time getTimeFromString (String source) throws SqlException + { + try { + // We override the JRE's Time.valueOf() in order to support a more E= xtended ISO format as requested by Toronto team + return time_valueOf (source); + } + catch (java.lang.IllegalArgumentException e) { // subsumes NumberForma= tException + throw new SqlException (agent_.logWriter_, e, "Invalid data conversi= on:" + + " Parameter instance " + source + + " is invalid for requested conversion to " += "java.sql.Time."); + } + } + + final java.sql.Time getTimeFromTimestamp (java.sql.Timestamp source) thr= ows SqlException + { + return new java.sql.Time (source.getTime()); + } + + //---------------------------- getTimestamp*() methods -----------------= ------ + + final java.sql.Timestamp getTimestampFromString (String source) throws S= qlException + { + try { + // We override the JRE's Timestamp.valueOf() in order to support a m= ore Extended ISO format as requested by Toronto team + return timestamp_valueOf (source); + } + catch (java.lang.IllegalArgumentException e) { // subsumes NumberForm= atException + throw new SqlException (agent_.logWriter_, e, + "Invalid data conversion:" + + " Parameter instance " + source + + " is invalid for requested conversion t= o " + "java.sql.Timestamp."); + } + } + + final java.sql.Timestamp getTimestampFromTime (java.sql.Time source) thr= ows SqlException + { + return new java.sql.Timestamp (source.getTime()); + } + + final java.sql.Timestamp getTimestampFromDate(java.sql.Date source) thro= ws SqlException + { + return new java.sql.Timestamp (source.getTime()); + } + + //-------------- Customized versions of java.lang parse methods --------= ------ + + // Converts a string in JDBC date "extended" ISO format to a Date value. + // yyyy-mm-dd with trailing blanks allowed. + final java.sql.Date date_valueOf (String s) throws java.lang.IllegalArgu= mentException + { + String formatError =3D "JDBC Date format must be yyyy-mm-dd"; + if (s =3D=3D null) throw new java.lang.IllegalArgumentException (forma= tError); + + int year =3D 1000*getDigit (s, 0); + year +=3D 100*getDigit (s, 1); + year +=3D 10*getDigit (s, 2); + year +=3D 1*getDigit (s, 3); + + char hyphen =3D s.charAt (4); + if (hyphen !=3D '-') throw new java.lang.IllegalArgumentException (for= matError); + + int month =3D0; + int day =3D 0; + int pos =3D0; + + hyphen =3D s.charAt (6); + if(hyphen =3D=3D '-') { //single digit month + month =3D getDigit (s, 5); + pos =3D 7; + } + else { //double digit month + month =3D 10 * getDigit (s, 5); + month +=3D 1*getDigit (s, 6); + pos =3D 8; + hyphen =3D s.charAt (7); + if (hyphen !=3D '-') throw new java.lang.IllegalArgumentException (for= matError); + } + + if(s.length() =3D=3D (pos + 1) || Character.digit(s.charAt(pos +1), 10= ) =3D=3D -1) { //single digit day + day =3D getDigit (s, pos); + pos ++; + } + else { //double digit day + day =3D 10 * getDigit (s, pos); + day +=3D 1* getDigit (s, pos + 1); + pos =3D pos + 2; + } + skipPadding (s, pos, s.length()); + return new java.sql.Date (year-1900, month-1, day); + } + + // Customized versions of java.util.Date/Time/Timestamp valueOf methods + // We override the JRE's Date/Time/Timestamp.valueOf() methods in order = to support a + // more Extended ISO format as requested by Toronto team. + // Also allows for space-padding of CHAR fields. + + private final int getDigit (String s, int index) throws java.lang.Illega= lArgumentException + { + String formatError =3D "Date/Time must be JDBC format"; + int digit =3D -1 ; + if (s !=3D null && s.length() >=3D 10) + digit =3D Character.digit (s.charAt (index), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + return digit; + } + + // Converts a string in JDBC time "extended" ISO format to a Time value. + // hh:mm:ss or hh.mm.ss with trailing blanks allowed. + final java.sql.Time time_valueOf (String s) throws java.lang.IllegalArgu= mentException, NumberFormatException + { + String formatError =3D "JDBC Time format must be hh:mm:ss"; + if (s =3D=3D null) throw new java.lang.IllegalArgumentException(); + + int hour =3D 10*getDigit (s, 0); + hour +=3D 1*getDigit (s, 1); + + char colon =3D s.charAt(2); + if (colon !=3D ':' && colon !=3D '.') throw new java.lang.IllegalArgum= entException (formatError); + + int minute =3D 10*getDigit (s, 3); + minute +=3D 1*getDigit (s, 4); + + colon =3D s.charAt(5); + if (colon !=3D ':' && colon !=3D '.') throw new java.lang.IllegalArgum= entException (formatError); + + int second =3D 10*getDigit (s, 6); + second +=3D 1*getDigit (s, 7); + + skipPadding (s, 8, s.length()); + return new java.sql.Time (hour, minute, second); + } + + // Converts a string in JDBC date "extended" ISO format to a Timestamp v= alue. + // yyyy-mm-dd hh:mm:ss[.[n[n[n[n[n[n[n[n[n]]]]]]]] or yyyy-mm-dd-hh.mm.s= s[.[n[n[n[n[n[n[n[n[n]]]]]]]] with trailing blanks allowed. + final java.sql.Timestamp timestamp_valueOf (String s) throws java.lang.I= llegalArgumentException, NumberFormatException + { + String formatError =3D "JDBC Timestamp format must be yyyy-mm-dd hh:mm= :ss.fffffffff"; + if (s =3D=3D null) throw new java.lang.IllegalArgumentException(); + + int year =3D 1000*getDigit (s, 0); + year +=3D 100*getDigit (s, 1); + year +=3D 10*getDigit (s, 2); + year +=3D 1*getDigit (s, 3); + + char hyphen =3D s.charAt (4); + if (hyphen !=3D '-') throw new java.lang.IllegalArgumentException (for= matError); + + int month =3D 10*getDigit (s, 5); + month +=3D 1*getDigit (s, 6); + + hyphen =3D s.charAt (7); + if (hyphen !=3D '-') throw new java.lang.IllegalArgumentException (for= matError); + + int day =3D 10*getDigit (s, 8); + day +=3D 1*getDigit (s, 9); + + char space =3D s.charAt (10); + if (space !=3D ' ' && space !=3D '-') throw new java.lang.IllegalArgum= entException (formatError); + + int hour =3D 10*getDigit (s, 11); + hour +=3D 1*getDigit (s, 12); + + char colon =3D s.charAt (13); + if (colon !=3D ':' && colon !=3D '.') throw new java.lang.IllegalArgum= entException (formatError); + + int minute =3D 10*getDigit (s, 14); + minute +=3D 1*getDigit (s, 15); + + colon =3D s.charAt(16); + if (colon !=3D ':' && colon !=3D '.') throw new java.lang.IllegalArgum= entException (formatError); + + int second =3D 10*getDigit (s, 17); + second +=3D 1*getDigit (s, 18); + + if (s.trim().length() =3D=3D 19) return new java.sql.Timestamp (year-1= 900, month-1, day, hour, minute, second, 0); + + char period =3D s.charAt (19); + if (period !=3D '.') throw new java.lang.IllegalArgumentException (for= matError); + if (s.length() =3D=3D 20 || s.charAt(20) =3D=3D ' ') { + skipPadding (s, 20, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, 0); + } + + // We can put the following into a while loop later.... + + int digit =3D Character.digit (s.charAt(20), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + int nanos =3D 100000000*digit; + if (s.length() =3D=3D 21 || s.charAt(21) =3D=3D ' ') { + skipPadding (s, 21, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(21), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 10000000*digit; + if (s.length() =3D=3D 22 || s.charAt(22) =3D=3D ' ') { + skipPadding (s, 22, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(22), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 1000000*digit; + if (s.length() =3D=3D 23 || s.charAt(23) =3D=3D ' ') { + skipPadding (s, 23, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(23), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 100000*digit; + if (s.length() =3D=3D 24 || s.charAt(24) =3D=3D ' ') { + skipPadding (s, 24, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(24), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 10000*digit; + if (s.length() =3D=3D 25 || s.charAt(25) =3D=3D ' ') { + skipPadding (s, 25, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(25), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 1000*digit; + if (s.length() =3D=3D 26 || s.charAt(26) =3D=3D ' ') { + skipPadding (s, 26, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(26), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 100*digit; + if (s.length() =3D=3D 27 || s.charAt(27) =3D=3D ' ') { + skipPadding (s, 27, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(27), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 10*digit; + if (s.length() =3D=3D 28 || s.charAt(28) =3D=3D ' ') { + skipPadding (s, 28, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + digit =3D Character.digit (s.charAt(28), 10); + if (digit =3D=3D -1) throw new java.lang.IllegalArgumentException (for= matError); + nanos +=3D 1*digit; + if (s.length() =3D=3D 29 || s.charAt(29) =3D=3D ' ') { + skipPadding (s, 29, s.length()); + return new java.sql.Timestamp (year-1900, month-1, day, hour, minute= , second, nanos); + } + + throw new java.lang.IllegalArgumentException (formatError); + } + + private final byte parseByte (String s) throws NumberFormatException + { + int i =3D parseInt (s); + if (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) + throw new NumberFormatException(); + return (byte) i; + } + + private final short parseShort (String s) throws NumberFormatException + { + int i =3D parseInt (s); + if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) + throw new NumberFormatException(); + return (short) i; + } + + // Custom version of java.lang.parseInt() that allows for space padding = of char fields. + private final int parseInt (String s) throws NumberFormatException + { + if (s =3D=3D null) + throw new NumberFormatException ("null"); + + int result =3D 0; + boolean negative =3D false; + int i =3D 0; + int max =3D s.length(); + int limit; + int multmin; + int digit; + + if (max =3D=3D 0) + throw new NumberFormatException (s); + + if (s.charAt (0) =3D=3D '-') { + negative =3D true; + limit =3D Integer.MIN_VALUE; + i++; + } + else { + limit =3D -Integer.MAX_VALUE; + } + multmin =3D limit / 10; + // Special handle the first digit to get things started. + if (i < max) { + digit =3D Character.digit (s.charAt (i++), 10); + if (digit < 0) { + throw new NumberFormatException (s); + } + else { + result =3D -digit; + } + } + // Now handle all the subsequent digits or space padding. + while (i < max) { + char c =3D s.charAt (i++); + if (c =3D=3D ' ') { + skipPadding (s, i, max); + break; + } + // Accumulating negatively avoids surprises near MAX_VALUE + digit =3D Character.digit (c, 10); + if (digit < 0) { + throw new NumberFormatException (s); + } + if (result < multmin) { + throw new NumberFormatException (s); + } + result *=3D 10; + if (result < limit + digit) { + throw new NumberFormatException (s); + } + result -=3D digit; + } + if (negative) { + if (i > 1) { + return result; + } + else { // Only got "-" + throw new NumberFormatException (s); + } + } + else { + return -result; + } + } + + private final long parseLong (String s) throws NumberFormatException + { + if (s =3D=3D null) { + throw new NumberFormatException ("null"); + } + + long result =3D 0; + boolean negative =3D false; + int i =3D 0, max =3D s.length(); + long limit; + long multmin; + int digit; + + if (max =3D=3D 0) + throw new NumberFormatException (s); + + if (s.charAt(0) =3D=3D '-') { + negative =3D true; + limit =3D Long.MIN_VALUE; + i++; + } + else { + limit =3D -Long.MAX_VALUE; + } + multmin =3D limit / 10; + if (i < max) { + digit =3D Character.digit (s.charAt (i++), 10); + if (digit < 0) { + throw new NumberFormatException(s); + } + else { + result =3D -digit; + } + } + while (i < max) { + char c =3D s.charAt (i++); + if (c =3D=3D ' ') { + skipPadding (s, i, max); + break; + } + // Accumulating negatively avoids surprises near MAX_VALUE + digit =3D Character.digit (c, 10); + if (digit < 0) { + throw new NumberFormatException (s); + } + if (result < multmin) { + throw new NumberFormatException (s); + } + result *=3D 10; + if (result < limit + digit) { + throw new NumberFormatException (s); + } + result -=3D digit; + } + if (negative) { + if (i > 1) { + return result; + } + else { // Only got "-" + throw new NumberFormatException (s); + } + } + else { + return -result; + } + } + + private final void skipPadding (String s, int i, int length) throws Numb= erFormatException + { + while (i < length) { + if (s.charAt (i++) !=3D ' ') + throw new NumberFormatException (s); + } + } +} Propchange: incubator/derby/code/trunk/java/client/org/apache/derby/client/= am/CrossConverters.java ---------------------------------------------------------------------------= --- svn:eol-style =3D native