BatchInsertSqlBuilder implements InsertSqlBuilder { @Override public SqlRequest build(InsertOperatorParameter parameter) { PrepareSqlFragments fragments = beforeBuild(parameter, PrepareSqlFragments.of()).addSql("("); Map<Integer, RDBColumnMetadata> indexMapping = new HashMap<>(); Map<Integer, SqlFragments> functionValues = new HashMap<>(); int index = 0; Set<InsertColumn> columns = parameter.getColumns(); for (InsertColumn column : columns) { RDBColumnMetadata columnMetadata = ofNullable(column.getColumn()) .flatMap(table::getColumn) .orElse(null); if (columnMetadata != null && columnMetadata.isInsertable()) { if (indexMapping.size() != 0) { fragments.addSql(","); } fragments.addSql(columnMetadata.getQuoteName()); indexMapping.put(index, columnMetadata); SqlFragments functionFragments = Optional.of(column) .flatMap(insertColumn -> ofNullable(insertColumn.getFunction()) .flatMap(function -> columnMetadata.findFeature(FunctionFragmentBuilder.createFeatureId(function))) .map(builder -> builder.create(columnMetadata.getName(), columnMetadata, insertColumn.getOpts()))) .orElse(EmptySqlFragments.INSTANCE); if (functionFragments.isNotEmpty()) { functionValues.put(index, functionFragments); } } index++; } if (indexMapping.isEmpty()) { throw new IllegalArgumentException("No operable columns"); } fragments.addSql(") values "); index = 0; for (List<Object> values : parameter.getValues()) { if (index++ != 0) { fragments.addSql(","); } fragments.addSql("("); int valueLen = values.size(); int vIndex = 0; for (Map.Entry<Integer, RDBColumnMetadata> entry : indexMapping.entrySet()) { int valueIndex = entry.getKey(); if (vIndex++ != 0) { fragments.addSql(","); } SqlFragments function = functionValues.get(valueIndex); if (null != function) { fragments.addFragments(function); continue; } RDBColumnMetadata column = entry.getValue(); Object value = valueLen <= valueIndex ? null : values.get(valueIndex); if ((value == null || value instanceof NullValue) && column.getDefaultValue() instanceof RuntimeDefaultValue) { value = column.getDefaultValue().get(); } if (value instanceof NativeSql) { fragments .addSql(((NativeSql) value).getSql()) .addParameter(((NativeSql) value).getParameters()); continue; } if (value == null) { value = NullValue.of(column.getType()); } fragments.addSql("?").addParameter(column.encode(value)); } fragments.addSql(")"); afterValues(columns, values, fragments); } return afterBuild(columns, parameter, fragments).toRequest(); }  static BatchInsertSqlBuilder of(RDBTableMetadata table); @Override SqlRequest build(InsertOperatorParameter parameter);  }
@Test public void test() { InsertOperatorParameter insert = new InsertOperatorParameter(); { InsertColumn column = new InsertColumn(); column.setColumn("id"); insert.getColumns().add(column); } { InsertColumn column = new InsertColumn(); column.setColumn("name"); insert.getColumns().add(column); } insert.getValues().add(Arrays.asList("1", "2")); insert.getValues().add(Arrays.asList("3", "4")); SqlRequest request = builder.build(insert); Assert.assertArrayEquals(request.getParameters(), new Object[]{"1", "2", "3", "4"}); System.out.println(request); }