View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.insert.render;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.stream.Collector;
23  import java.util.stream.Collectors;
24  import java.util.stream.IntStream;
25  
26  public class FieldAndValueCollector {
27      final List<FieldAndValueAndParameters> fieldsAndValues = new ArrayList<>();
28  
29      public FieldAndValueCollector() {
30          super();
31      }
32  
33      public void add(FieldAndValueAndParameters fieldAndValueAndParameters) {
34          fieldsAndValues.add(fieldAndValueAndParameters);
35      }
36  
37      public FieldAndValueCollector merge(FieldAndValueCollector other) {
38          fieldsAndValues.addAll(other.fieldsAndValues);
39          return this;
40      }
41  
42      public boolean isEmpty() {
43          return fieldsAndValues.isEmpty();
44      }
45  
46      public String columnsPhrase() {
47          return fieldsAndValues.stream()
48                  .map(FieldAndValueAndParameters::fieldName)
49                  .collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
50      }
51  
52      public String valuesPhrase() {
53          return fieldsAndValues.stream()
54                  .map(FieldAndValueAndParameters::valuePhrase)
55                  .collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
56      }
57  
58      public String multiRowInsertValuesPhrase(int rowCount) {
59          return IntStream.range(0, rowCount)
60                  .mapToObj(this::toSingleRowOfValues)
61                  .collect(Collectors.joining(", ", "values ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62      }
63  
64      private String toSingleRowOfValues(int row) {
65          return fieldsAndValues.stream()
66                  .map(FieldAndValueAndParameters::valuePhrase)
67                  .map(s -> String.format(s, row))
68                  .collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69      }
70  
71      public Map<String, Object> parameters() {
72          return fieldsAndValues.stream()
73                  .map(FieldAndValueAndParameters::parameters)
74                  .collect(HashMap::new, HashMap::putAll, HashMap::putAll);
75      }
76  
77      public static Collector<FieldAndValueAndParameters, FieldAndValueCollector, FieldAndValueCollector> collect() {
78          return Collector.of(FieldAndValueCollector::new,
79                  FieldAndValueCollector::add,
80                  FieldAndValueCollector::merge);
81      }
82  }