MultiInsertSqlBuilder implements InsertSqlBuilder { @Override public SqlRequest build(InsertOperatorParameter parameter) { DefaultBatchSqlRequest sqlRequest = new DefaultBatchSqlRequest(); 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()) { indexMapping.put(index, columnMetadata); SqlFragments functionFragments = Optional.of(column) .flatMap(insertColumn -> Optional.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"); } for (List<Object> values : parameter.getValues()) { PrepareSqlFragments intoSql = PrepareSqlFragments.of(); PrepareSqlFragments valuesSql = PrepareSqlFragments.of(); intoSql.addSql("insert into") .addSql(table.getFullName()) .addSql("("); valuesSql.addSql("values ("); int valueLen = values.size(); int vIndex = 0; for (Map.Entry<Integer, RDBColumnMetadata> entry : indexMapping.entrySet()) { RDBColumnMetadata column = entry.getValue(); SqlFragments function = functionValues.get(vIndex); if (null != function) { valuesSql.addFragments(function); } Object value = valueLen <= vIndex ? null : values.get(vIndex); if (vIndex++ != 0) { intoSql.addSql(","); valuesSql.addSql(","); } intoSql.addSql(column.getQuoteName()); if ((value == null || value instanceof NullValue) && column.getDefaultValue() instanceof RuntimeDefaultValue) { value = ((RuntimeDefaultValue) column.getDefaultValue()).get(); } if (value instanceof NativeSql) { valuesSql .addSql(((NativeSql) value).getSql()) .addParameter(((NativeSql) value).getParameters()); continue; } if (value == null) { value = NullValue.of(column.getType()); } valuesSql.addSql("?").addParameter(column.encode(value)); } intoSql.addSql(")"); valuesSql.addSql(")"); sqlRequest.addBatch(intoSql.addFragments(valuesSql).toRequest()); } return sqlRequest; }  @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", NativeSql.of("1+1","4"))); SqlRequest request = builder.build(insert); System.out.println(request); Assert.assertTrue(request instanceof BatchSqlRequest); BatchSqlRequest batch = ((BatchSqlRequest) request); Assert.assertArrayEquals(batch.getParameters(),new Object[]{"1","2"}); Assert.assertArrayEquals(batch.getBatch().get(0).getParameters(),new Object[]{"3","4"}); }