All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pivotal.gemfirexd.internal.client.am.CrossConverters Maven / Gradle / Ivy

The newest version!
/*

   Derby - Class com.pivotal.gemfirexd.internal.client.am.CrossConverters

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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.

*/

/*
 * Changes for GemFireXD distributed data platform (some marked by "GemStone changes")
 *
 * Portions Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */

package com.pivotal.gemfirexd.internal.client.am;

import com.gemstone.gemfire.internal.shared.ClientSharedUtils;
import com.pivotal.gemfirexd.internal.shared.common.Converter;
import com.pivotal.gemfirexd.internal.shared.common.reference.SQLState;

// 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;
//

public final class CrossConverters implements Converter {

    /**
     * Value used to signal unknown length of data.
     */
    public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE;

    private final static java.math.BigDecimal bdMaxByteValue__ =
            java.math.BigDecimal.valueOf(Byte.MAX_VALUE);
    private final static java.math.BigDecimal bdMinByteValue__ =
            java.math.BigDecimal.valueOf(Byte.MIN_VALUE);
    private final static java.math.BigDecimal bdMaxShortValue__ =
            java.math.BigDecimal.valueOf(Short.MAX_VALUE);
    private final static java.math.BigDecimal bdMinShortValue__ =
            java.math.BigDecimal.valueOf(Short.MIN_VALUE);
    private final static java.math.BigDecimal bdMaxIntValue__ =
            java.math.BigDecimal.valueOf(Integer.MAX_VALUE);
    private final static java.math.BigDecimal bdMinIntValue__ =
            java.math.BigDecimal.valueOf(Integer.MIN_VALUE);
    private final static java.math.BigDecimal bdMaxLongValue__ =
            java.math.BigDecimal.valueOf(Long.MAX_VALUE);
    private final static java.math.BigDecimal bdMinLongValue__ =
            java.math.BigDecimal.valueOf(Long.MIN_VALUE);
    private final static java.math.BigDecimal bdMaxFloatValue__ =
            new java.math.BigDecimal(Float.MAX_VALUE);
    private final static java.math.BigDecimal bdMinFloatValue__ =
            new java.math.BigDecimal(-Float.MAX_VALUE);
    private final static java.math.BigDecimal bdMaxDoubleValue__ =
            new java.math.BigDecimal(Double.MAX_VALUE);
    private final static java.math.BigDecimal bdMinDoubleValue__ =
            new java.math.BigDecimal(-Double.MAX_VALUE);

    // Since BigDecimals are immutable, we can return pointers to these canned 0's and 1's.
    private final static java.math.BigDecimal bdZero__ = java.math.BigDecimal.valueOf(0);
    private final static java.math.BigDecimal bdOne__ = java.math.BigDecimal.valueOf(1);

    // ---------------------- state ----------------------------------------------

    Agent agent_;

    // ----------------------constructors/finalizer-------------------------------

    CrossConverters(Agent agent) {
        agent_ = 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 SqlException {
        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:
// GemStone changes BEGIN
            return source;
            /* (original code)
            return new Short(source);
            */
// GemStone changes END

        case Types.INTEGER:
// GemStone changes BEGIN
            return (int)source;
            /* (original code)
            return new Integer(source);
            */
// GemStone changes END

        case Types.BIGINT:
// GemStone changes BEGIN
            return (long)source;
            /* (original code)
            return new Long(source);
            */
// GemStone changes END

        case Types.REAL:
            return (float)source;

        case Types.DOUBLE:
            return (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_, 
                new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
                "byte", Types.getTypeString(targetType), (String)null);
        }
    }

    // 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_, String.valueOf(source));
            }
// GemStone changes BEGIN
            return (short)source;
            /* (original code)
            return new Short((short) source);
            */
// GemStone changes END

        case Types.INTEGER:
// GemStone changes BEGIN
            return source;
            /* (original code)
            return new Integer(source);
            */
// GemStone changes END

        case Types.BIGINT:
// GemStone changes BEGIN
            return (long)source;
            /* (original code)
            return new Long(source);
            */
// GemStone changes END

        case Types.REAL:
            return (float)source;

        case Types.DOUBLE:
            return (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_, 
                new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
                    "int", Types.getTypeString(targetType), (String)null);
        }
    }

    // This method is used in lieu of setObject(targetType, sourceObject) because we
    // don't support the BIT/BOOLEAN as underlying DERBY targetTypes.
    final boolean setBooleanFromObject(Object source, int sourceType) throws 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_,
                Types.getTypeString(sourceType), "boolean");
        }
    }

    // This method is used in lieu of setObject(targetType, sourceObject) because we
    // don't support the BIT/BOOLEAN as underlying DERBY targetTypes.
    final byte setByteFromObject(Object source, int sourceType) throws SqlException {
        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_,
                Types.getTypeString(sourceType), "byte");
        }
    }

    // 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_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Short.valueOf() if possible
            return (short)source;
            /* (original code)
            return new Short((short) source);
            */
// GemStone changes END

        case Types.INTEGER:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Integer.valueOf() if possible
            return (int)source;
            /* (original code)
            return new Integer((int) source);
            */
// GemStone changes END

        case Types.BIGINT:
// GemStone changes BEGIN
            // changed to use Long.valueOf() if possible
            return source;
            /* (original code)
            return new Long(source);
            */
// GemStone changes END

        case Types.REAL:
            return (float)source;

        case Types.DOUBLE:
            return (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_, 
                new ClientMessageId (SQLState.LANG_DATA_TYPE_SET_MISMATCH),
                "long", Types.getTypeString(targetType), (String)null);
        }
    }

    // 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_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Short.valueOf() if possible
            return (short)source;
            /* (original code)
            return new Short((short) source);
            */
// GemStone changes END

        case Types.INTEGER:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Integer.valueOf() if possible
            return (int)source;
            /* (original code)
            return new Integer((int) source);
            */
// GemStone changes END

        case Types.BIGINT:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Short.valueOf() if possible
            return (long)source;
            /* (original code)
            return new Long((long) source);
            */
// GemStone changes END

        case Types.REAL:
            if (Configuration.rangeCheckCrossConverters &&
                    // change the check from (source > Float.MAX_VALUE || source < -Float.MIN_VALUE))
                    // to the following:
                    //-----------------------------------------------------------------------------------
                    //   -infinity                             0                            +infinity
                    //           |__________________________|======|________________________|
                    //  <-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_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Short.valueOf() if possible
            return (short)source;
            /* (original code)
            return new Short((short) source);
            */
// GemStone changes END

        case Types.INTEGER:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Integer.MAX_VALUE || source < Integer.MIN_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Integer.valueOf() if possible
            return (int)source;
            /* (original code)
            return new Integer((int) source);
            */
// GemStone changes END

        case Types.BIGINT:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Long.MAX_VALUE || source < Long.MIN_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
            }
// GemStone changes BEGIN
            // changed to use Integer.valueOf() if possible
            return (long)source;
            /* (original code)
            return new Long((long) source);
            */
// GemStone changes END

        case Types.REAL:
            if (Configuration.rangeCheckCrossConverters &&
                    (source > Float.MAX_VALUE || source < -Float.MAX_VALUE)) {
                throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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
                    //            |__________________________|======|________________________|
                    // <-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_, String.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_, String.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_, String.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_, String.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_, String.valueOf(source));
        }

        return (byte) source;
    }

    final byte getByteFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxByteValue__) == 1 || source.compareTo(bdMinByteValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId 
            		(SQLState.LANG_FORMAT_EXCEPTION), "byte", null, e);
        }
    }

    //---------------------------- getShort*() methods ---------------------------

    final short getShortFromInt(int source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source > java.lang.Short.MAX_VALUE || source < java.lang.Short.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
        }

        return (short) source;
    }

    final short getShortFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxShortValue__) == 1 || source.compareTo(bdMinShortValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId 
            		(SQLState.LANG_FORMAT_EXCEPTION), 
            		"short", null, e);
        }
    }

    //---------------------------- getInt*() methods -----------------------------

    final int getIntFromLong(long source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
        }

        return (int) source;
    }

    final int getIntFromFloat(float source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
        }

        return (int) source;
    }

    final int getIntFromDouble(double source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source > java.lang.Integer.MAX_VALUE || source < java.lang.Integer.MIN_VALUE)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
        }

        return (int) source;
    }

    final int getIntFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxIntValue__) == 1 || source.compareTo(bdMinIntValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
            		"int", null, e);
        }
    }

    //---------------------------- 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_, String.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_, String.valueOf(source));
        }

        return (long) source;
    }

    final long getLongFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxLongValue__) == 1 || source.compareTo(bdMinLongValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
        			"long", null, e);
        }
    }

    //---------------------------- getFloat*() methods ---------------------------

    final float getFloatFromDouble(double source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                Float.isInfinite((float)source)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.valueOf(source));
        }

        return (float) source;
    }

    final float getFloatFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxFloatValue__) == 1 || source.compareTo(bdMinFloatValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
                    "float", null, e);
        }
    }

    //---------------------------- getDouble*() methods --------------------------

    final double getDoubleFromBigDecimal(java.math.BigDecimal source) throws SqlException {
        if (Configuration.rangeCheckCrossConverters &&
                (source.compareTo(bdMaxDoubleValue__) == 1 || source.compareTo(bdMinDoubleValue__) == -1)) {
            throw new LossOfPrecisionConversionException(agent_.logWriter_, String.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_, 
            		new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
                    "double", null, e);
        }
    }

    //---------------------------- getBigDecimal*() methods ----------------------

    final java.math.BigDecimal getBigDecimalFromBoolean(boolean source) throws SqlException {
        return source ? bdOne__ : bdZero__;
    }

    final java.math.BigDecimal getBigDecimalFromString(String source) throws SqlException {
        try {
            // Unfortunately, the big decimal constructor calls java.lang.Long.parseLong(),
            // which doesn't like spaces, so we have to call trim() to get rid of the spaces from CHAR columns.
            return new java.math.BigDecimal(source.trim());
        } catch (java.lang.NumberFormatException e) {
            throw new SqlException(agent_.logWriter_,
            		new ClientMessageId (SQLState.LANG_FORMAT_EXCEPTION),
                    "java.math.BigDecimal", null, e);
        }
    }

    //---------------------------- getString*() methods --------------------------

    final String getStringFromBoolean(boolean source) throws SqlException {
        return source ? "1" : "0";
    }

    final String getStringFromBytes(byte[] bytes) throws SqlException {
// GemStone changes BEGIN
        return ClientSharedUtils.toHexString(bytes, 0, bytes.length);
        /* (original code)
        StringBuilder stringBuffer = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            String hexForByte = Integer.toHexString(bytes[i] & 0xff);
            // If the byte is x0-F, prepend a "0" in front to ensure 2 char representation
            if (hexForByte.length() == 1) {
                stringBuffer.append('0');
            }
            stringBuffer.append(hexForByte);
        }
        return stringBuffer.toString();
        */
// GemStone changes END
    }


    // All Numeric, and Date/Time types use String.valueOf (source)

    //---------------------------- getDate*() methods ----------------------------

    final java.sql.Date getDateFromString(String source) throws SqlException {
        try {
            return date_valueOf(source);
        } catch (java.lang.IllegalArgumentException e) { // subsumes NumberFormatException
            throw new SqlException(agent_.logWriter_, 
            		new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), null, e);
        }
    }

    final java.sql.Date getDateFromTime(java.sql.Time source) throws SqlException {
        return new java.sql.Date(source.getTime());
    }

    final java.sql.Date getDateFromTimestamp(java.sql.Timestamp source) throws SqlException {
        return new java.sql.Date(source.getTime());
    }

    //---------------------------- getTime*() methods ----------------------------

    final java.sql.Time getTimeFromString(String source) throws SqlException {
        try {
            return time_valueOf(source);
        } catch (java.lang.IllegalArgumentException e) { // subsumes NumberFormatException
            throw new SqlException(agent_.logWriter_, 
            		new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), null, e);
        }
    }

    final java.sql.Time getTimeFromTimestamp(java.sql.Timestamp source) throws SqlException {
        return new java.sql.Time(source.getTime());
    }

    //---------------------------- getTimestamp*() methods -----------------------

    final java.sql.Timestamp getTimestampFromString(String source) throws SqlException {
        try {
            return timestamp_valueOf(source);
        } catch (java.lang.IllegalArgumentException e) {  // subsumes NumberFormatException
            throw new SqlException(agent_.logWriter_, 
            		new ClientMessageId (SQLState.LANG_DATE_SYNTAX_EXCEPTION), null, e);
        }
    }

    final java.sql.Timestamp getTimestampFromTime(java.sql.Time source) throws SqlException {
        return new java.sql.Timestamp(source.getTime());
    }

    final java.sql.Timestamp getTimestampFromDate(java.sql.Date source) throws SqlException {
        return new java.sql.Timestamp(source.getTime());
    }

    final java.sql.Date date_valueOf(String s) throws java.lang.IllegalArgumentException {
        String formatError = "JDBC Date format must be yyyy-mm-dd";
        if (s == null) {
            throw new java.lang.IllegalArgumentException(formatError);
        }
        s = s.trim();
        return java.sql.Date.valueOf(s);
    }


    final java.sql.Time time_valueOf(String s) throws java.lang.IllegalArgumentException, NumberFormatException {
        String formatError = "JDBC Time format must be hh:mm:ss";
        if (s == null) {
            throw new java.lang.IllegalArgumentException();
        }
        s = s.trim();
        return java.sql.Time.valueOf(s);
    }

    final java.sql.Timestamp timestamp_valueOf(String s) throws java.lang.IllegalArgumentException, NumberFormatException {
        String formatError = "JDBC Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
        if (s == null) {
            throw new java.lang.IllegalArgumentException();
        }

        s = s.trim();
        return java.sql.Timestamp.valueOf(s);
    }

    private final byte parseByte(String s) throws NumberFormatException {
        int i = 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 = 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 == null) {
            throw new NumberFormatException("null");
        }

        int result = 0;
        boolean negative = false;
        int i = 0;
        int max = s.length();
        int limit;
        int multmin;
        int digit;

        if (max == 0) {
            throw new NumberFormatException(s);
        }

        if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / 10;
        // Special handle the first digit to get things started.
        if (i < max) {
            digit = Character.digit(s.charAt(i++), 10);
            if (digit < 0) {
                throw new NumberFormatException(s);
            } else {
                result = -digit;
            }
        }
        // Now handle all the subsequent digits or space padding.
        while (i < max) {
            char c = s.charAt(i++);
            if (c == ' ') {
                skipPadding(s, i, max);
                break;
            }
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(c, 10);
            if (digit < 0) {
                throw new NumberFormatException(s);
            }
            if (result < multmin) {
                throw new NumberFormatException(s);
            }
            result *= 10;
            if (result < limit + digit) {
                throw new NumberFormatException(s);
            }
            result -= 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 == null) {
            throw new NumberFormatException("null");
        }

        long result = 0;
        boolean negative = false;
        int i = 0, max = s.length();
        long limit;
        long multmin;
        int digit;

        if (max == 0) {
            throw new NumberFormatException(s);
        }

        if (s.charAt(0) == '-') {
            negative = true;
            limit = Long.MIN_VALUE;
            i++;
        } else {
            limit = -Long.MAX_VALUE;
        }
        multmin = limit / 10;
        if (i < max) {
            digit = Character.digit(s.charAt(i++), 10);
            if (digit < 0) {
                throw new NumberFormatException(s);
            } else {
                result = -digit;
            }
        }
        while (i < max) {
            char c = s.charAt(i++);
            if (c == ' ') {
                skipPadding(s, i, max);
                break;
            }
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(c, 10);
            if (digit < 0) {
                throw new NumberFormatException(s);
            }
            if (result < multmin) {
                throw new NumberFormatException(s);
            }
            result *= 10;
            if (result < limit + digit) {
                throw new NumberFormatException(s);
            }
            result -= 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 NumberFormatException {
        while (i < length) {
            if (s.charAt(i++) != ' ') {
                throw new NumberFormatException(s);
            }
        }
    }

// GemStone changes BEGIN
    public Object getJavaObjectOfType(int javaType, Object obj) {
      try {
        return setObject(javaType, obj, null);
      } catch (SqlException e) {
        throw ClientSharedUtils.newRuntimeException(e.getMessage(), e);
      }
    }
// GemStone changes END
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy