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.Optional;
19  
20  import org.mybatis.dynamic.sql.SqlColumn;
21  import org.mybatis.dynamic.sql.render.RenderingStrategy;
22  import org.mybatis.dynamic.sql.util.ConstantMapping;
23  import org.mybatis.dynamic.sql.util.InsertMappingVisitor;
24  import org.mybatis.dynamic.sql.util.NullMapping;
25  import org.mybatis.dynamic.sql.util.PropertyMapping;
26  import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
27  import org.mybatis.dynamic.sql.util.RowMapping;
28  import org.mybatis.dynamic.sql.util.StringConstantMapping;
29  import org.mybatis.dynamic.sql.util.StringUtilities;
30  
31  public class ValuePhraseVisitor extends InsertMappingVisitor<Optional<FieldAndValueAndParameters>> {
32  
33      protected final RenderingStrategy renderingStrategy;
34  
35      public ValuePhraseVisitor(RenderingStrategy renderingStrategy) {
36          this.renderingStrategy = renderingStrategy;
37      }
38  
39      @Override
40      public Optional<FieldAndValueAndParameters> visit(NullMapping mapping) {
41          return FieldAndValueAndParameters.withFieldName(mapping.columnName())
42                  .withValuePhrase("null") //$NON-NLS-1$
43                  .buildOptional();
44      }
45  
46      @Override
47      public Optional<FieldAndValueAndParameters> visit(ConstantMapping mapping) {
48          return FieldAndValueAndParameters.withFieldName(mapping.columnName())
49                  .withValuePhrase(mapping.constant())
50                  .buildOptional();
51      }
52  
53      @Override
54      public Optional<FieldAndValueAndParameters> visit(StringConstantMapping mapping) {
55          return FieldAndValueAndParameters.withFieldName(mapping.columnName())
56                  .withValuePhrase(StringUtilities.formatConstantForSQL(mapping.constant()))
57                  .buildOptional();
58      }
59  
60      @Override
61      public Optional<FieldAndValueAndParameters> visit(PropertyMapping mapping) {
62          return FieldAndValueAndParameters.withFieldName(mapping.columnName())
63                  .withValuePhrase(calculateJdbcPlaceholder(mapping.column(), mapping.property()))
64                  .buildOptional();
65      }
66  
67      @Override
68      public Optional<FieldAndValueAndParameters> visit(PropertyWhenPresentMapping mapping) {
69          if (mapping.shouldRender()) {
70              return visit((PropertyMapping) mapping);
71          } else {
72              return Optional.empty();
73          }
74      }
75  
76      @Override
77      public Optional<FieldAndValueAndParameters> visit(RowMapping mapping) {
78          return FieldAndValueAndParameters.withFieldName(mapping.columnName())
79                  .withValuePhrase(calculateJdbcPlaceholder(mapping.column()))
80                  .buildOptional();
81      }
82  
83      private String calculateJdbcPlaceholder(SqlColumn<?> column) {
84          return column.renderingStrategy().orElse(renderingStrategy)
85                  .getRecordBasedInsertBinding(column, "row"); //$NON-NLS-1$
86      }
87  
88      private String calculateJdbcPlaceholder(SqlColumn<?> column, String parameterName) {
89          return column.renderingStrategy().orElse(renderingStrategy)
90                  .getRecordBasedInsertBinding(column, "row", parameterName); //$NON-NLS-1$
91      }
92  }