Coverage Report - liquibase.executor.jvm.StatementCreatorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StatementCreatorUtils
0%
0/71
0%
0/68
7.4
 
 1  
 package liquibase.executor.jvm;
 2  
 
 3  
 import java.io.StringWriter;
 4  
 import java.math.BigDecimal;
 5  
 import java.sql.PreparedStatement;
 6  
 import java.sql.SQLException;
 7  
 import java.sql.Types;
 8  
 import java.util.Calendar;
 9  
 
 10  
 /**
 11  
  * Utility methods for PreparedStatementSetter/Creator and CallableStatementCreator implementations, providing
 12  
  * sophisticated parameter management (including support for LOB values).
 13  
  * <p/>
 14  
  * <p>
 15  
  * Used by PreparedStatementCreatorFactory and CallableStatementCreatorFactory, but also available for direct use in
 16  
  * custom setter/creator implementations.
 17  
  * 
 18  
  * @author Spring Framework
 19  
  * @see PreparedStatementSetter
 20  
  * @see SqlParameter
 21  
  */
 22  0
 abstract class StatementCreatorUtils {
 23  
 
 24  
     /**
 25  
      * Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle
 26  
      * complex types like arrays and LOBs.
 27  
      * 
 28  
      * @param ps
 29  
      *            the prepared statement or callable statement
 30  
      * @param paramIndex
 31  
      *            index of the parameter we are setting
 32  
      * @param param
 33  
      *            the parameter as it is declared including type
 34  
      * @param inValue
 35  
      *            the value to set
 36  
      * @throws SQLException
 37  
      *             if thrown by PreparedStatement methods
 38  
      */
 39  
     public static void setParameterValue(PreparedStatement ps, int paramIndex, SqlParameter param, Object inValue)
 40  
             throws SQLException {
 41  
 
 42  0
         setParameterValueInternal(ps, paramIndex, param.getSqlType(), param.getTypeName(), param.getScale(), inValue);
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle
 47  
      * complex types like arrays and LOBs.
 48  
      * 
 49  
      * @param ps
 50  
      *            the prepared statement or callable statement
 51  
      * @param paramIndex
 52  
      *            index of the parameter we are setting
 53  
      * @param sqlType
 54  
      *            the SQL type of the parameter
 55  
      * @param inValue
 56  
      *            the value to set (plain value or a SqlTypeValue)
 57  
      * @throws SQLException
 58  
      *             if thrown by PreparedStatement methods
 59  
      */
 60  
     public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType, Object inValue)
 61  
             throws SQLException {
 62  
 
 63  0
         setParameterValueInternal(ps, paramIndex, sqlType, null, null, inValue);
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Set the value for a parameter. The method used is based on the SQL type of the parameter and we can handle
 68  
      * complex types like arrays and LOBs.
 69  
      * 
 70  
      * @param ps
 71  
      *            the prepared statement or callable statement
 72  
      * @param paramIndex
 73  
      *            index of the parameter we are setting
 74  
      * @param sqlType
 75  
      *            the SQL type of the parameter
 76  
      * @param typeName
 77  
      *            the type name of the parameter (optional, only used for SQL NULL and SqlTypeValue)
 78  
      * @param scale
 79  
      *            the number of digits after the decimal point (for DECIMAL and NUMERIC types)
 80  
      * @param inValue
 81  
      *            the value to set (plain value or a SqlTypeValue)
 82  
      * @throws SQLException
 83  
      *             if thrown by PreparedStatement methods
 84  
      */
 85  
     private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType, String typeName,
 86  
             Integer scale, Object inValue) throws SQLException {
 87  
 
 88  0
         if (inValue == null) {
 89  0
             if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
 90  0
                 boolean useSetObject = false;
 91  
                 try {
 92  0
                     useSetObject = (ps.getConnection().getMetaData().getDatabaseProductName().indexOf("Informix") != -1);
 93  0
                 } catch (Throwable ex) {
 94  
                     // logger.debug("Could not check database product name", ex);
 95  0
                 }
 96  0
                 if (useSetObject) {
 97  0
                     ps.setObject(paramIndex, null);
 98  
                 } else {
 99  0
                     ps.setNull(paramIndex, Types.NULL);
 100  
                 }
 101  0
             } else if (typeName != null) {
 102  0
                 ps.setNull(paramIndex, sqlType, typeName);
 103  
             } else {
 104  0
                 ps.setNull(paramIndex, sqlType);
 105  
             }
 106  
         } else { // inValue != null
 107  0
             if (inValue instanceof SqlTypeValue) {
 108  0
                 ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
 109  0
             } else if (sqlType == Types.VARCHAR || sqlType == -9) { // -9 is Types.NVARCHAR in java 1.6
 110  0
                 ps.setString(paramIndex, inValue.toString());
 111  0
             } else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
 112  0
                 if (inValue instanceof BigDecimal) {
 113  0
                     ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
 114  0
                 } else if (scale != null) {
 115  0
                     ps.setObject(paramIndex, inValue, sqlType, scale);
 116  
                 } else {
 117  0
                     ps.setObject(paramIndex, inValue, sqlType);
 118  
                 }
 119  0
             } else if (sqlType == Types.DATE) {
 120  0
                 if (inValue instanceof java.util.Date) {
 121  0
                     if (inValue instanceof java.sql.Date) {
 122  0
                         ps.setDate(paramIndex, (java.sql.Date) inValue);
 123  
                     } else {
 124  0
                         ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
 125  
                     }
 126  0
                 } else if (inValue instanceof Calendar) {
 127  0
                     Calendar cal = (Calendar) inValue;
 128  0
                     ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
 129  0
                 } else {
 130  0
                     ps.setObject(paramIndex, inValue, Types.DATE);
 131  
                 }
 132  0
             } else if (sqlType == Types.TIME) {
 133  0
                 if (inValue instanceof java.util.Date) {
 134  0
                     if (inValue instanceof java.sql.Time) {
 135  0
                         ps.setTime(paramIndex, (java.sql.Time) inValue);
 136  
                     } else {
 137  0
                         ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
 138  
                     }
 139  0
                 } else if (inValue instanceof Calendar) {
 140  0
                     Calendar cal = (Calendar) inValue;
 141  0
                     ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
 142  0
                 } else {
 143  0
                     ps.setObject(paramIndex, inValue, Types.TIME);
 144  
                 }
 145  0
             } else if (sqlType == Types.TIMESTAMP) {
 146  0
                 if (inValue instanceof java.util.Date) {
 147  0
                     if (inValue instanceof java.sql.Timestamp) {
 148  0
                         ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
 149  
                     } else {
 150  0
                         ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
 151  
                     }
 152  0
                 } else if (inValue instanceof Calendar) {
 153  0
                     Calendar cal = (Calendar) inValue;
 154  0
                     ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
 155  0
                 } else {
 156  0
                     ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
 157  
                 }
 158  0
             } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
 159  0
                 if (isStringValue(inValue)) {
 160  0
                     ps.setString(paramIndex, inValue.toString());
 161  0
                 } else if (isDateValue(inValue)) {
 162  0
                     ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
 163  0
                 } else if (inValue instanceof Calendar) {
 164  0
                     Calendar cal = (Calendar) inValue;
 165  0
                     ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()));
 166  0
                 } else {
 167  
                     // Fall back to generic setObject call without SQL type specified.
 168  0
                     ps.setObject(paramIndex, inValue);
 169  
                 }
 170  
             } else {
 171  
                 // Fall back to generic setObject call with SQL type specified.
 172  0
                 ps.setObject(paramIndex, inValue, sqlType);
 173  
             }
 174  
         }
 175  0
     }
 176  
 
 177  
     /**
 178  
      * Check whether the given value can be treated as a String value.
 179  
      */
 180  
     private static boolean isStringValue(Object inValue) {
 181  0
         return (inValue instanceof CharSequence || inValue instanceof StringWriter);
 182  
     }
 183  
 
 184  
     /**
 185  
      * Check whether the given value is a <code>java.util.Date</code> (but not one of the JDBC-specific subclasses).
 186  
      */
 187  
     private static boolean isDateValue(Object inValue) {
 188  0
         return (inValue instanceof java.util.Date && !(inValue instanceof java.sql.Date
 189  
                 || inValue instanceof java.sql.Time || inValue instanceof java.sql.Timestamp));
 190  
     }
 191  
 
 192  
 }