public static String insertList()

in bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/sql/SQLBuilder.java [104:194]


    public static <Entity> String insertList(TableMetaData tableMetaData, List<Entity> entities, String databaseId) {
        if (entities == null || entities.isEmpty()) {
            throw new IllegalArgumentException("Entities list must not be null or empty");
        }

        Class<?> entityClass = entities.get(0).getClass();
        Map<String, String> fieldColumnMap = tableMetaData.getFieldColumnMap();

        SQL sql = new SQL();
        switch (DBType.toType(databaseId)) {
            case MYSQL: {
                sql.INSERT_INTO(keywordsFormat(tableMetaData.getTableName(), DBType.MYSQL));

                boolean firstRow = true;
                int idx = 0;
                for (Entity entity : entities) {
                    List<String> values = new ArrayList<>();
                    for (Map.Entry<String, String> entry : fieldColumnMap.entrySet()) {
                        // Ignore primary key
                        if (Objects.equals(entry.getKey(), tableMetaData.getPkProperty())) {
                            continue;
                        }
                        PropertyDescriptor ps = BeanUtils.getPropertyDescriptor(entityClass, entry.getKey());
                        if (ps == null || ps.getReadMethod() == null) {
                            continue;
                        }
                        Object value = ReflectionUtils.invokeMethod(ps.getReadMethod(), entity);
                        if (!ObjectUtils.isEmpty(value)) {
                            if (firstRow) {
                                sql.VALUES(
                                        keywordsFormat(entry.getValue(), DBType.MYSQL),
                                        getTokenParam("arg0[" + idx + "]." + entry.getKey()));
                            }
                            values.add(getTokenParam("arg0[" + idx + "]." + entry.getKey()));
                        }
                    }
                    if (firstRow) {
                        firstRow = false;
                    } else {
                        sql.ADD_ROW();
                        sql.INTO_VALUES(values.toArray(new String[0]));
                    }
                    idx++;
                }
                break;
            }
            case POSTGRESQL: {
                sql.INSERT_INTO(keywordsFormat(tableMetaData.getTableName(), DBType.POSTGRESQL));

                boolean firstRow = true;
                List<String> columns = new ArrayList<>();
                int idx = 0;
                for (Entity entity : entities) {
                    List<String> values = new ArrayList<>();
                    for (Map.Entry<String, String> entry : fieldColumnMap.entrySet()) {
                        // Ignore primary key
                        if (Objects.equals(entry.getKey(), tableMetaData.getPkProperty())) {
                            continue;
                        }
                        PropertyDescriptor ps = BeanUtils.getPropertyDescriptor(entityClass, entry.getKey());
                        if (ps == null || ps.getReadMethod() == null) {
                            continue;
                        }
                        Object value = ReflectionUtils.invokeMethod(ps.getReadMethod(), entity);
                        if (!ObjectUtils.isEmpty(value)) {
                            if (firstRow) {
                                sql.VALUES(
                                        keywordsFormat(entry.getValue(), DBType.POSTGRESQL),
                                        getTokenParam("arg0[" + idx + "]." + entry.getKey()));
                            }
                            values.add(getTokenParam("arg0[" + idx + "]." + entry.getKey()));
                        }
                    }
                    if (firstRow) {
                        firstRow = false;
                    } else {
                        sql.ADD_ROW();
                        sql.INTO_VALUES(values.toArray(new String[0]));
                    }
                    idx++;
                }
                break;
            }

            default: {
                log.error("Unsupported data source");
            }
        }

        return sql.toString();
    }