org.springframework.jdbc.core.StatementCreatorUtils Java Examples

The following examples show how to use org.springframework.jdbc.core.StatementCreatorUtils. 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 File: SpringJdbcAccess.java    From snakerflow with 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 #2
Source File: SpringJdbcAccess.java    From snakerflow with 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 #3
Source File: AbstractJdbcInsert.java    From lams with 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 #4
Source File: AbstractJdbcInsert.java    From effectivejava with 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 #5
Source File: BeanPropertySqlParameterSource.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * 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 File: SqlParameterSourceBuilder.java    From SimpleFlatMapper with 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 #7
Source File: ShardJdbcTemplate.java    From compass with 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 #8
Source File: AbstractJdbcInsert.java    From spring4-understanding with 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 #9
Source File: BeanPropertySqlParameterSource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 File: BeanPropertySqlParameterSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #11
Source File: AbstractJdbcInsert.java    From java-technology-stack with 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 #12
Source File: BeanPropertySqlParameterSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 #13
Source File: AbstractJdbcInsert.java    From spring-analysis-note with 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 #14
Source File: BeanPropertySqlParameterSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 #15
Source File: RdbEventRecordLoader.java    From DataLink with 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 #16
Source File: ArgumentTypePreparedStatementSetter.java    From sqlhelper with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void cleanupParameters() {
    StatementCreatorUtils.cleanupParameters(this.args);
}
 
Example #17
Source File: ArgumentTypePreparedStatementSetter.java    From sqlhelper with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}