/** * Set the value for the prepared statement's specified parameter position using the passed in * value and type. This method can be overridden by sub-classes if needed. * @param ps the PreparedStatement * @param parameterPosition index of the parameter position * @param argType the argument type * @param argValue the argument value * @throws SQLException if thrown by PreparedStatement methods */ protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue) throws SQLException { StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue); }
/** * Derives a default SQL type from the corresponding property type. * @see org.springframework.jdbc.core.StatementCreatorUtils#javaTypeToSqlParameterType */ @Override public int getSqlType(String paramName) { int sqlType = super.getSqlType(paramName); if (sqlType != TYPE_UNKNOWN) { return sqlType; } Class<?> propType = this.beanWrapper.getPropertyType(paramName); return StatementCreatorUtils.javaTypeToSqlParameterType(propType); }
@Override public void cleanupParameters() { StatementCreatorUtils.cleanupParameters(this.args); }
public PreparedStatement createPreparedStatement(Connection con) throws SQLException { log.debug("createPreparedStatement: {}", sql); for (String key : parameterMap.keySet()) { log.debug(" {} => {}", key, parameterMap.get(key)); } SqlAndParams sap = createSqlAndParams(); PreparedStatement ps = con.prepareStatement(sap.getSql()); for (int i = 0; i < sap.getParams().size(); i++) { Object paramValue = sap.getParams().get(i); int paramSqlType = paramValue != null ? StatementCreatorUtils.javaTypeToSqlParameterType(paramValue.getClass()) : SqlTypeValue.TYPE_UNKNOWN; StatementCreatorUtils.setParameterValue(ps, i + 1, paramSqlType, paramValue); } return ps; }
/** * 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 inValue the value to set (plain value or a SqlTypeValue) * @throws SQLException if thrown by PreparedStatement methods * @see SqlTypeValue */ public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, sqlType, null, null, inValue); }
/** * 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 inValue the value to set (plain value or a SqlTypeValue) * @throws SQLException if thrown by PreparedStatement methods * @see SqlTypeValue */ public static void setParameterValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, @Nullable Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, sqlType, typeName, null, inValue); }
/** * Internal implementation for setting parameter values. * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<?> values, @Nullable int... columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
@Override public void cleanupParameters() { StatementCreatorUtils.cleanupParameters(this.parameters); }
@Override public void processParameter(String parameterName, Object arg, Class<?> parameterType, MapSqlParameterSource ps) { ps.addValue(parameterName, arg, StatementCreatorUtils.javaTypeToSqlParameterType(parameterType)); }
/** * 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 param the parameter as it is declared including type * @param inValue the value to set * @throws SQLException if thrown by PreparedStatement methods */ public static void setParameterValue(PreparedStatement ps, int paramIndex, SqlParameter param, @Nullable Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, param.getSqlType(), param.getTypeName(), param.getScale(), inValue); }
/** * Set the value for prepared statements specified parameter index using the passed in value. * This method can be overridden by sub-classes if needed. * @param ps the PreparedStatement * @param parameterPosition index of the parameter position * @param argValue the value to set * @throws SQLException if thrown by PreparedStatement methods */ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object argValue) throws SQLException { if (argValue instanceof SqlParameterValue) { SqlParameterValue paramValue = (SqlParameterValue) argValue; StatementCreatorUtils.setParameterValue(ps, parameterPosition, paramValue, paramValue.getValue()); } else { StatementCreatorUtils.setParameterValue(ps, parameterPosition, SqlTypeValue.TYPE_UNKNOWN, argValue); } }
@Override public void cleanupParameters() { StatementCreatorUtils.cleanupParameters(this.args); }
public static int getSqlType(Class<?> type) { int sqlType = StatementCreatorUtils.javaTypeToSqlParameterType(type); if (sqlType != SqlTypeValue.TYPE_UNKNOWN) { return sqlType; } if (LocalDate.class.isAssignableFrom(type)) { return Types.DATE; } if (LocalDateTime.class.isAssignableFrom(type)) { return Types.TIMESTAMP; } if (LocalTime.class.isAssignableFrom(type)) { return Types.TIME; } if (ZonedDateTime.class.isAssignableFrom(type)) { return Types.TIMESTAMP; } if (OffsetDateTime.class.isAssignableFrom(type)) { return Types.TIMESTAMP; } if (OffsetTime.class.isAssignableFrom(type)) { return Types.TIME; } return SqlTypeValue.TYPE_UNKNOWN; }
/** * 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 inValue the value to set (plain value or a SqlTypeValue) * @throws SQLException if thrown by PreparedStatement methods * @see SqlTypeValue */ public static void setParameterValue( PreparedStatement ps, int paramIndex, int sqlType, Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, sqlType, null, null, inValue); }
protected static void setStatementParameters(Object[] values, PreparedStatement ps, @Nullable int[] columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (value instanceof SqlParameterValue) { SqlParameterValue paramValue = (SqlParameterValue) value; StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue()); } else { int colType; if (columnTypes == null || columnTypes.length < colIndex) { colType = SqlTypeValue.TYPE_UNKNOWN; } else { colType = columnTypes[colIndex - 1]; } StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value); } } }
@Override public void cleanupParameters() { if (this.inParameters != null) { StatementCreatorUtils.cleanupParameters(this.inParameters.values()); } }
@Override public int getSqlType(String paramName) { if (!hasValue(paramName)) { return TYPE_UNKNOWN; } Object value = getValue(paramName); return StatementCreatorUtils.javaTypeToSqlParameterType(value.getClass()); }
/** * 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 inValue the value to set (plain value or a SqlTypeValue) * @throws SQLException if thrown by PreparedStatement methods * @see SqlTypeValue */ public static void setParameterValue( PreparedStatement ps, int paramIndex, int sqlType, String typeName, Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, sqlType, typeName, null, inValue); }
@Override public void setValues(PreparedStatement ps, int i) throws SQLException { Object[] values = batchArgs.get(i); int colIndex = 0; for (Object value : values) { colIndex++; if (value instanceof SqlParameterValue) { SqlParameterValue paramValue = (SqlParameterValue) value; StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue()); } else { int colType; if (argTypes.length < colIndex) { colType = SqlTypeValue.TYPE_UNKNOWN; } else { colType = argTypes[colIndex - 1]; } StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value); } } } @Override
/** * Clean up all resources held by parameter values which were passed to an * execute method. This is for example important for closing LOB values. * @param paramValues parameter values supplied. May be {@code null}. * @see DisposableSqlTypeValue#cleanup() * @see org.springframework.jdbc.core.support.SqlLobValue#cleanup() */ public static void cleanupParameters(@Nullable Object... paramValues) { if (paramValues != null) { cleanupParameters(Arrays.asList(paramValues)); } }