Example usage for org.springframework.jdbc.core SqlParameterValue getSqlType

List of usage examples for org.springframework.jdbc.core SqlParameterValue getSqlType

Introduction

In this page you can find the example usage for org.springframework.jdbc.core SqlParameterValue getSqlType.

Prototype

public int getSqlType() 

Source Link

Document

Return the SQL type of the parameter.

Usage

From source file:org.hxzon.util.db.springjdbc.StatementCreatorUtils.java

/**
 * Set the value for a parameter. The method used is based on the SQL type
 * of the parameter and we can handle complex types like arrays and LOBs.
 * @param ps the prepared statement or callable statement
 * @param paramIndex index of the parameter we are setting
 * @param sqlType the SQL type of the parameter
 * @param typeName the type name of the parameter
 * (optional, only used for SQL NULL and SqlTypeValue)
 * @param scale the number of digits after the decimal point
 * (for DECIMAL and NUMERIC types)/*  w w  w  .j  a v  a 2 s . c o  m*/
 * @param inValue the value to set (plain value or a SqlTypeValue)
 * @throws SQLException if thrown by PreparedStatement methods
 * @see SqlTypeValue
 */
private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType,
        String typeName, Integer scale, Object inValue) throws SQLException {

    String typeNameToUse = typeName;
    int sqlTypeToUse = sqlType;
    Object inValueToUse = inValue;

    // override type info?
    if (inValue instanceof SqlParameterValue) {
        SqlParameterValue parameterValue = (SqlParameterValue) inValue;
        if (logger.isDebugEnabled()) {
            logger.debug("Overriding type info with runtime info from SqlParameterValue: column index "
                    + paramIndex + ", SQL type " + parameterValue.getSqlType() + ", type name "
                    + parameterValue.getTypeName());
        }
        if (parameterValue.getSqlType() != SqlTypeValue.TYPE_UNKNOWN) {
            sqlTypeToUse = parameterValue.getSqlType();
        }
        if (parameterValue.getTypeName() != null) {
            typeNameToUse = parameterValue.getTypeName();
        }
        inValueToUse = parameterValue.getValue();
    }

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

    if (inValueToUse == null) {
        setNull(ps, paramIndex, sqlTypeToUse, typeNameToUse);
    } else {
        setValue(ps, paramIndex, sqlTypeToUse, typeNameToUse, scale, inValueToUse);
    }
}

From source file:org.springframework.jdbc.core.StatementCreatorUtils.java

/**
 * Set the value for a parameter. The method used is based on the SQL type
 * of the parameter and we can handle complex types like arrays and LOBs.
 * @param ps the prepared statement or callable statement
 * @param paramIndex index of the parameter we are setting
 * @param sqlType the SQL type of the parameter
 * @param typeName the type name of the parameter
 * (optional, only used for SQL NULL and SqlTypeValue)
 * @param scale the number of digits after the decimal point
 * (for DECIMAL and NUMERIC types)/*from  w  w  w  . j av  a  2s . c  om*/
 * @param inValue the value to set (plain value or a SqlTypeValue)
 * @throws SQLException if thrown by PreparedStatement methods
 * @see SqlTypeValue
 */
private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType,
        @Nullable String typeName, @Nullable Integer scale, @Nullable Object inValue) throws SQLException {

    String typeNameToUse = typeName;
    int sqlTypeToUse = sqlType;
    Object inValueToUse = inValue;

    // override type info?
    if (inValue instanceof SqlParameterValue) {
        SqlParameterValue parameterValue = (SqlParameterValue) inValue;
        if (logger.isDebugEnabled()) {
            logger.debug("Overriding type info with runtime info from SqlParameterValue: column index "
                    + paramIndex + ", SQL type " + parameterValue.getSqlType() + ", type name "
                    + parameterValue.getTypeName());
        }
        if (parameterValue.getSqlType() != SqlTypeValue.TYPE_UNKNOWN) {
            sqlTypeToUse = parameterValue.getSqlType();
        }
        if (parameterValue.getTypeName() != null) {
            typeNameToUse = parameterValue.getTypeName();
        }
        inValueToUse = parameterValue.getValue();
    }

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

    if (inValueToUse == null) {
        setNull(ps, paramIndex, sqlTypeToUse, typeNameToUse);
    } else {
        setValue(ps, paramIndex, sqlTypeToUse, typeNameToUse, scale, inValueToUse);
    }
}