All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hecloud.runtime.database.builder.BatchInsertBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package com.hecloud.runtime.database.builder;

import com.alibaba.fastjson.JSONObject;
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.*;

/**
 * 批量插入SQL语句构造器
 *
 * @author LoveinBJ
 */
@Data
public class BatchInsertBuilder implements BatchSQLBuilder {
    /**
     * 主键策略
     */
    private Strategy strategy;
    private List columnFields;
    private String dialect;
    /**
     * 表名
     */
    private String table;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public BatchInsertBuilder(Class clazz, String dialect) {
        super();
        dialect = Optional.ofNullable(dialect).orElse("mysql");
        EntityLoader loader = new EntityLoader(dialect);
        this.table = loader.loadTableName(clazz);
        this.strategy = loader.loadStrategy(clazz);
        this.columnFields = loader.loadColumnField(clazz);
    }

    @Override
    public String buildValues(List objects) {
        return this.buildInsertValues(objects);
    }

    /**
     * 构建批量插入的values 后面的语句
     *
     * @param objects 待插入数据
     * @return 插入语句
     */
    private String buildInsertValues(List objects) {
        objects = Optional.ofNullable(objects).orElse(new ArrayList<>());
        StringBuilder builder = new StringBuilder();
        objects.stream().filter(Objects::nonNull).forEach(object -> {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
            builder.append("(");
            columnFields.stream().filter(ColumnField::isInsert)
                    .filter(columnField -> !columnField.isPk() || !strategy.equals(Strategy.AUTO))
                    .forEach(columnField -> {
                        String field = columnField.getFieldName();
                        if (columnField.getFieldType().equals(String.class)) {
                            builder.append(around(jsonObject.getString(field)));
                        } else if (columnField.getFieldType().equals(Date.class)) {
                            if (Objects.isNull(jsonObject.getDate(field))) {
                                if (columnField.isNullable()) {
                                    builder.append("null");
                                } else {
                                    builder.append(around(dateFormat.format(new Date())));
                                }
                            } else {
                                builder.append(around(dateFormat.format(jsonObject.getDate(field))));
                            }
                        } else if (columnField.getFieldType().isEnum()) {
                            builder.append(around(jsonObject.getString(field)));
                        } else {
                            builder.append(jsonObject.get(field));
                        }
                        builder.append(",");
                    });
            builder.deleteCharAt(builder.length() - 1);
            builder.append("),");
        });
        builder.deleteCharAt(builder.length() - 1);
        return builder.toString();
    }

    /**
     * 构建插入语句后缀
     *
     * @return 构建前置语句
     */
    @Override
    public String buildPrefix() {
        StringBuilder builder = new StringBuilder();
        builder.append("insert into ").append(table).append(" (");
        columnFields.stream().filter(ColumnField::isInsert)
                .filter(columnField -> !columnField.isPk() || !strategy.equals(Strategy.AUTO))
                .forEach(columnField -> builder.append(columnField.getColumnName()).append(","));
        builder.deleteCharAt(builder.length() - 1);
        builder.append(") values ");
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy