Java Code Examples for org.springframework.jdbc.core.StatementCreatorUtils

The following examples show how to use org.springframework.jdbc.core.StatementCreatorUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: snakerflow   Source File: SpringJdbcAccess.java    License: Apache License 2.0 6 votes vote down vote up
public void saveProcess(final Process process) {
	super.saveProcess(process);
	if(process.getBytes() != null) {
		template.execute(PROCESS_UPDATE_BLOB, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
			
			protected void setValues(PreparedStatement ps, LobCreator lobCreator)
					throws SQLException, DataAccessException {
				try {
					lobCreator.setBlobAsBytes(ps, 1, process.getBytes());
					StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, process.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
 
Example 2
Source Project: snakerflow   Source File: SpringJdbcAccess.java    License: Apache License 2.0 6 votes vote down vote up
public void updateProcess(final Process process) {
	super.updateProcess(process);
	if(process.getBytes() != null) {
		template.execute(PROCESS_UPDATE_BLOB, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
			
			protected void setValues(PreparedStatement ps, LobCreator lobCreator)
					throws SQLException, DataAccessException {
				try {
					lobCreator.setBlobAsBytes(ps, 1, process.getBytes());
					StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, process.getId());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
 
Example 3
/**
 * 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);
}
 
Example 4
Source Project: spring-analysis-note   Source File: AbstractJdbcInsert.java    License: MIT License 5 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 5
/**
 * 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);
}
 
Example 6
Source Project: java-technology-stack   Source File: AbstractJdbcInsert.java    License: MIT License 5 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 7
/**
 * 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);
}
 
Example 8
Source Project: lams   Source File: AbstractJdbcInsert.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Internal implementation for setting parameter values
 * @param preparedStatement the PreparedStatement
 * @param values the values to be set
 */
private void setParameterValues(PreparedStatement preparedStatement, List<?> values, 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);
		}
	}
}
 
Example 9
/**
 * 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);
}
 
Example 10
Source Project: spring4-understanding   Source File: AbstractJdbcInsert.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Internal implementation for setting parameter values
 * @param preparedStatement the PreparedStatement
 * @param values the values to be set
 */
private void setParameterValues(PreparedStatement preparedStatement, List<?> values, 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);
		}
	}
}
 
Example 11
Source Project: compass   Source File: ShardJdbcTemplate.java    License: Apache License 2.0 5 votes vote down vote up
@Override
     public int[] call() throws Exception {
     	JdbcTemplate jdbcTemplate = createJdbcTemplate(shard.getTargetDataSource(), ShardJdbcTemplate.this);
         String interceptedSql = shardDataSource.getSqlInterceptor().intercept(sql, shard.getTableContext());
         
         return jdbcTemplate.batchUpdate(interceptedSql, new BatchPreparedStatementSetter() {
	
	@Override
	public void setValues(PreparedStatement ps, int statementIndex) throws SQLException {
		Object[] args = argsList.get(statementIndex);
		
		for (int i = 0; i < args.length; i++) {
			Object arg = args[i];
			if (arg instanceof SqlParameterValue) {
				SqlParameterValue paramValue = (SqlParameterValue) arg;
				StatementCreatorUtils.setParameterValue(ps, i + 1, paramValue, paramValue.getValue());
			}
			else {
				StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg);
			}
		}
	}
	
	@Override
	public int getBatchSize() {
		return argsList.size();
	}
});
     }
 
Example 12
Source Project: SimpleFlatMapper   Source File: SqlParameterSourceBuilder.java    License: MIT License 5 votes vote down vote up
private static int getParameterType(PropertyMapping<?, ?, JdbcColumnKey> pm) {
    Class<?> propertyType = TypeHelper.toClass(pm.getPropertyMeta().getPropertyType());
    
    if (pm.getColumnDefinition().has(SqlTypeColumnProperty.class)) {
        return pm.getColumnDefinition().lookFor(SqlTypeColumnProperty.class).getSqlType();
    }

    int t =  StatementCreatorUtils.javaTypeToSqlParameterType(propertyType);
    if (t == SqlTypeValue.TYPE_UNKNOWN) {
        //IFJAVA8_START
        if (propertyType.equals(ZonedDateTime.class) || propertyType.equals(OffsetDateTime.class)) {
            return Types.TIMESTAMP_WITH_TIMEZONE;
        }

        if (propertyType.equals(Instant.class) || propertyType.equals(LocalDateTime.class)) {
            return Types.TIMESTAMP;
        }

        if (propertyType.equals(LocalDate.class)) {
            return Types.DATE;
        }

        if (propertyType.equals(LocalTime.class)) {
            return Types.TIME;
        }

        //IFJAVA8_END
        return JdbcColumnKey.UNDEFINED_TYPE;
    }
    return t;
}
 
Example 13
/**
 * 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);
}
 
Example 14
Source Project: effectivejava   Source File: AbstractJdbcInsert.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Internal implementation for setting parameter values
 * @param preparedStatement the PreparedStatement
 * @param values the values to be set
 */
private void setParameterValues(PreparedStatement preparedStatement, List<Object> values, 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);
		}
	}
}
 
Example 15
@Override
public void cleanupParameters() {
    StatementCreatorUtils.cleanupParameters(this.args);
}
 
Example 16
Source Project: DataLink   Source File: RdbEventRecordLoader.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void fillPreparedStatement(PreparedStatement ps, LobCreator lobCreator, RdbEventRecord record, DbDialect dbDialect, TaskWriterContext context) throws SQLException {
    EventType type = record.getEventType();
    // 注意insert/update语句对应的字段数序都是将主键排在后面
    List<EventColumn> columns = new ArrayList<EventColumn>();
    if (type.isInsert()) {
        columns.addAll(record.getColumns()); // insert为所有字段
        columns.addAll(record.getKeys());
    } else if (type.isDelete()) {
        columns.addAll(record.getKeys());
    } else if (type.isUpdate()) {
        boolean existOldKeys = !CollectionUtils.isEmpty(record.getOldKeys());
        boolean hasAutoIncrementNotKeyColumns = dbDialect.hasAutoIncrementNotKeyColumns(record.getSchemaName(), record.getTableName());
        if (hasAutoIncrementNotKeyColumns) {
            columns.addAll(record.getUpdatedColumns());// 只更新带有isUpdate=true的字段
        } else {
            columns.addAll(record.getColumns());// update、upsert都更新所有字段
        }
        columns.addAll(record.getKeys());
        if (existOldKeys) {
            columns.addAll(record.getOldKeys());
        }
    }

    // 获取一下当前字段名的数据是否必填
    boolean isSyncAutoAddColumn = context.getWriterParameter().isSyncAutoAddColumn();
    Map<String, Boolean> isRequiredMap = buildRequiredMap(dbDialect, record, columns, isSyncAutoAddColumn);
    Table table = dbDialect.findTable(record.getSchemaName(), record.getTableName());

    for (int i = 0; i < columns.size(); i++) {
        int paramIndex = i + 1;
        EventColumn column = columns.get(i);
        Boolean isRequired = isRequiredMap.get(StringUtils.lowerCase(column.getColumnName()));

        int sqlType = getSqlType(column, table, record);
        Object param = null;
        if (dbDialect instanceof MysqlDialect
                && (sqlType == Types.TIME || sqlType == Types.TIMESTAMP || sqlType == Types.DATE)) {
            // 解决mysql的0000-00-00 00:00:00问题,直接依赖mysql
            // driver进行处理,如果转化为Timestamp会出错
            param = column.getColumnValue();
        } else {
            param = SqlUtils.stringToSqlValue(column.getColumnValue(),
                    sqlType,
                    isRequired,
                    dbDialect.isEmptyStringNulled());
        }

        try {
            switch (sqlType) {
                case Types.CLOB:
                    lobCreator.setClobAsString(ps, paramIndex, (String) param);
                    break;

                case Types.BLOB:
                    lobCreator.setBlobAsBytes(ps, paramIndex, param instanceof String ? ((String) param).getBytes() : (byte[]) param);
                    break;
                case Types.TIME:
                case Types.TIMESTAMP:
                case Types.DATE:
                    // 只处理mysql的时间类型,oracle的进行转化处理
                    if (dbDialect instanceof MysqlDialect) {
                        // 解决mysql的0000-00-00 00:00:00问题,直接依赖mysql
                        // driver进行处理,如果转化为Timestamp会出错
                        ps.setObject(paramIndex, param);
                    } else {
                        StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                    }
                    break;
                case Types.BIT:
                    // 只处理mysql的bit类型,bit最多存储64位,所以需要使用BigInteger进行处理才能不丢精度
                    // mysql driver将bit按照setInt进行处理,会导致数据越界
                    if (dbDialect instanceof MysqlDialect) {
                        StatementCreatorUtils.setParameterValue(ps, paramIndex, Types.DECIMAL, null, param);
                    } else if (dbDialect instanceof SqlServerDialect) {
                        StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param.toString());
                    } else {
                        StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                    }
                    break;
                default:
                    StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param);
                    break;
            }
        } catch (SQLException ex) {
            logger.error("SetParam error , [mappingId={}, sqltype={}, value={}]",
                    RecordMeta.mediaMapping(record).getId(), sqlType, param);
            throw ex;
        }
    }
}
 
Example 17
/**
 * 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);
}