com.hecloud.runtime.database.builder.BatchUpdateBuilder Maven / Gradle / Ivy
package com.hecloud.runtime.database.builder;
import com.alibaba.fastjson.JSONObject;
import com.hecloud.runtime.common.collections.Arrays;
import com.hecloud.runtime.database.emuns.ColumnField;
import com.hecloud.runtime.database.emuns.Strategy;
import com.hecloud.runtime.database.utils.EntityLoader;
import lombok.Data;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* 批量更新SQL构造器
*
* @author LoveinBJ
*/
@Data
public class BatchUpdateBuilder implements BatchSQLBuilder {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 主键策略
*/
private Strategy strategy;
private List columnFields;
private String dialect;
/**
* 表名
*/
private String tableName;
/**
* 需要更新的列
*/
private String[] fields;
public BatchUpdateBuilder(Class clazz, String[] fields, String dialect) {
super();
dialect = Optional.ofNullable(dialect).orElse("mysql");
EntityLoader loader = new EntityLoader(dialect);
this.strategy = loader.loadStrategy(clazz);
this.tableName = loader.loadTableName(clazz);
this.processColumnFields(loader.loadColumnField(clazz));
this.processEntityFields(fields);
}
@Override
public String buildValues(List> objects) {
objects = Optional.ofNullable(objects).orElse(new ArrayList<>());
StringBuilder builder = new StringBuilder();
objects.stream().filter(Objects::nonNull).forEach(object -> {
JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(object));
builder.append("(");
columnFields.forEach(columnField -> {
String field = columnField.getFieldName();
if (columnField.getFieldType().equals(String.class)) {
builder.append(around(json.getString(field))).append(",");
} else if (columnField.getFieldType().equals(Date.class)) {
Date date = Optional.ofNullable(json.getDate(field)).orElse(new Date());
if (columnField.isNullable()) {
builder.append("null").append(",");
} else {
builder.append(around(dateFormat.format(date))).append(",");
}
} else if (columnField.getFieldType().isEnum()) {
builder.append(around(json.getString(field)));
} else {
builder.append(json.get(field)).append(",");
}
});
builder.delete(builder.length() - 1, builder.length());
builder.append("),");
});
builder.delete(builder.length() - 1, builder.length());
builder.append(" ON DUPLICATE KEY UPDATE ");
columnFields.stream().filter(columnField -> Arrays.exits(columnField.getFieldName(), this.getFields()))
.forEach(columnField -> builder.append(columnField.getColumnName())
.append("=VALUES(`").append(columnField.getColumnName()).append("`),"));
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
@Override
public String buildPrefix() {
StringBuilder builder = new StringBuilder();
builder.append("insert into ").append(tableName).append(" (`");
columnFields.stream().filter(columnField -> columnField.isInsert() || columnField.isUpdate())
.forEach(columnField -> builder.append(columnField.getColumnName()).append("`,`"));
builder.delete(builder.length() - 3, builder.length() - 1);
builder.append(") values ");
return builder.toString();
}
private void processColumnFields(List columnFields) {
this.columnFields = Optional.ofNullable(columnFields).orElse(new ArrayList<>()).stream()
.filter(columnField -> columnField.isUpdate() || columnField.isInsert()).collect(Collectors.toList());
}
/**
*
*/
private void processEntityFields(String[] fields) {
if (Arrays.isEmpty(fields)) {
List updateFields = new ArrayList<>();
columnFields.stream().filter(columnField -> columnField.isInsert() || columnField.isUpdate())
.forEach(columnField -> updateFields.add(columnField.getFieldName()));
this.fields = new String[updateFields.size()];
for (int i = 0; i < updateFields.size(); i++) {
this.fields[i] = updateFields.get(i);
}
} else {
this.fields = fields;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy