Package GenericDBMS

Source Code of GenericDBMS.PreparedStatementSetterWithNulls

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package GenericDBMS;

import java.io.StringWriter;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.ParameterDisposer;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;

import Framework.BinaryData;
import Framework.BooleanData;
import Framework.DataValue;
import Framework.DateTimeData;
import Framework.DecimalData;
import Framework.DoubleData;
import Framework.IntegerData;
import Framework.TextData;
import Framework.UsageException;

/**
* This class maps input values onto a prepared statement, catering for nulls and nullable classes
* @author tfaulkes
*
*/
public class PreparedStatementSetterWithNulls implements PreparedStatementSetter, ParameterDisposer {

    private static Logger logger = Logger.getLogger(PreparedStatementSetterWithNulls.class);
    private Object[] args;

    public PreparedStatementSetterWithNulls() {
        super();
    }

    public PreparedStatementSetterWithNulls(Object[] args) {
        this();
        this.args = args;
    }

    public void setValues(PreparedStatement ps) throws SQLException {
        if (this.args != null) {
            for (int i = 0; i < this.args.length; i++) {
                this.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, null, this.args[i]);
            }
        }
    }

    public void cleanupParameters() {
        StatementCreatorUtils.cleanupParameters(this.args);
    }

    public void setParameterValue(
        PreparedStatement ps, int paramIndex, int sqlType, String typeName, Object inValue)
        throws SQLException {

        if (logger.isDebugEnabled()) {
            logger.debug("Setting SQL statement parameter value: column index " + paramIndex +
                    ", parameter value [" + inValue +
                    "], value class [" + (inValue != null ? inValue.getClass().getName() : "null") +
                    "], SQL type " + (sqlType == SqlTypeValue.TYPE_UNKNOWN ? "unknown" : Integer.toString(sqlType)));
        }

        if (inValue == null) {
            if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
                ps.setNull(paramIndex, Types.NULL);
            }
            else if (typeName != null) {
                ps.setNull(paramIndex, sqlType, typeName);
            }
            else {
                ps.setNull(paramIndex, sqlType);
            }
        }
        else if (inValue instanceof DataValue) {
            // work out the correct type to call
            DataValue value = (DataValue)inValue;
            if (value instanceof TextData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.VARCHAR);
                }
                else {
                    ps.setString(paramIndex, inValue.toString());
                }
            }
            else if (value instanceof IntegerData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.INTEGER);
                }
                else {
                    ps.setInt(paramIndex, ((IntegerData)value).intValue());
                }
            }
            else if (value instanceof DecimalData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.DOUBLE);
                }
                else {
                    ps.setDouble(paramIndex, ((DecimalData)value).doubleValue());
                }
            }
            else if (value instanceof DoubleData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.DOUBLE);
                }
                else {
                    ps.setDouble(paramIndex, ((DoubleData)value).doubleValue());
                }
            }
            else if (value instanceof DateTimeData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.DATE);
                }
                else {
                    java.util.Date theDate = ((DateTimeData)value).asDate();
                    ps.setTimestamp(paramIndex, new java.sql.Timestamp(theDate.getTime()));
                }
            }
            else if (value instanceof BinaryData) {
                if (value.isNull()) {
                    ps.setNull(paramIndex, Types.BINARY);
                }
                else {
                    ps.setBytes(paramIndex, ((BinaryData)inValue).toByteArray());
                }
            }
            // TF:20/3/08:Added a missing case for inserting boolean datas into the database
            else if (value instanceof BooleanData) {
              if (sqlType == SqlTypeValue.TYPE_UNKNOWN){ //PM:23/01/2009: special case to handle the BooleanData into column type of number(1)
                if (value.isNull()) {
                  ps.setNull(paramIndex, Types.NULL);
                }
                else {
                  int numericBoolean = ((((BooleanData)inValue).getValue()) ? 1 : 0);
                  ps.setInt(paramIndex, numericBoolean);
                }

              } else {
                if (value.isNull()) {
                  ps.setNull(paramIndex, Types.BOOLEAN);
                }
                else {
                  ps.setBoolean(paramIndex, ((BooleanData)inValue).getValue());
                }
                }
            }
            else {
                UsageException e = new UsageException("setParameterValue could not handle a " + inValue.getClass().getName() + ". Please determine how to map this type to the database.");
                logger.error(e.getMessage(), e);
                throw e;
            }
        }
        else // inValue != null
            if (inValue instanceof SqlTypeValue) {
                ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
            }
            else if (sqlType == Types.VARCHAR) {
                ps.setString(paramIndex, inValue.toString());
            }
            else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
                if (inValue instanceof BigDecimal) {
                    ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
                }
                else {
                    ps.setObject(paramIndex, inValue, sqlType);
                }
            }
            else if (sqlType == Types.DATE) {
                if (inValue instanceof java.util.Date) {
                    if (inValue instanceof java.sql.Date) {
                        ps.setDate(paramIndex, (java.sql.Date) inValue);
                    }
                    else {
                        ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
                    }
                }
                else if (inValue instanceof Calendar) {
                    Calendar cal = (Calendar) inValue;
                    ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
                }
                else {
                    ps.setObject(paramIndex, inValue, Types.DATE);
                }
            }
            else if (sqlType == Types.TIME) {
                if (inValue instanceof java.util.Date) {
                    if (inValue instanceof java.sql.Time) {
                        ps.setTime(paramIndex, (java.sql.Time) inValue);
                    }
                    else {
                        ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
                    }
                }
                else if (inValue instanceof Calendar) {
                    Calendar cal = (Calendar) inValue;
                    ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
                }
                else {
                    ps.setObject(paramIndex, inValue, Types.TIME);
                }
            }
            else if (sqlType == Types.TIMESTAMP) {
                if (inValue instanceof java.util.Date) {
                    if (inValue instanceof java.sql.Timestamp) {
                        ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
                    }
                    else {
                        ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
                    }
                }
                else if (inValue instanceof Calendar) {
                    Calendar cal = (Calendar) inValue;
                    ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
                }
                else {
                    ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
                }
            }
            else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
              if (inValue instanceof String || inValue instanceof StringBuffer || inValue instanceof StringBuilder || inValue instanceof StringWriter) {
                    ps.setString(paramIndex, inValue.toString());
                }
                else if ((inValue instanceof java.util.Date) && !(inValue instanceof java.sql.Date ||
                        inValue instanceof java.sql.Time || inValue instanceof java.sql.Timestamp)) {
                    ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
                }
                else if (inValue instanceof Calendar) {
                    Calendar cal = (Calendar) inValue;
                    ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()));
                }
                else {
                    // Fall back to generic setObject call without SQL type specified.
                    ps.setObject(paramIndex, inValue);
                }
            }
            else {
                // Fall back to generic setObject call with SQL type specified.
                ps.setObject(paramIndex, inValue, sqlType);
            }
        }
    }
}
TOP

Related Classes of GenericDBMS.PreparedStatementSetterWithNulls

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.