View Javadoc
1   /*
2    *    Copyright 2006-2023 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.generator.codegen.mybatis3.xmlmapper.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  
20  import org.mybatis.generator.api.dom.xml.Attribute;
21  import org.mybatis.generator.api.dom.xml.TextElement;
22  import org.mybatis.generator.api.dom.xml.XmlElement;
23  
24  public class SimpleSelectByPrimaryKeyElementGenerator extends AbstractXmlElementGenerator {
25  
26      public SimpleSelectByPrimaryKeyElementGenerator() {
27          super();
28      }
29  
30      @Override
31      public void addElements(XmlElement parentElement) {
32          XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
33  
34          answer.addAttribute(new Attribute(
35                  "id", introspectedTable.getSelectByPrimaryKeyStatementId())); //$NON-NLS-1$
36          answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
37                  introspectedTable.getBaseResultMapId()));
38  
39          String parameterType;
40          // PK fields are in the base class. If more than on PK
41          // field, then they are coming in a map.
42          if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
43              parameterType = "map"; //$NON-NLS-1$
44          } else {
45              parameterType = introspectedTable.getPrimaryKeyColumns().get(0)
46                      .getFullyQualifiedJavaType().toString();
47          }
48  
49          answer.addAttribute(new Attribute("parameterType", parameterType)); //$NON-NLS-1$
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          StringBuilder sb = new StringBuilder();
54          sb.append("select "); //$NON-NLS-1$
55  
56          if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
57              sb.append('\'');
58              sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
59              sb.append("' as QUERYID,"); //$NON-NLS-1$
60          }
61  
62          buildSelectList(sb.toString(), introspectedTable.getAllColumns()).forEach(answer::addElement);
63  
64          sb.setLength(0);
65          sb.append("from "); //$NON-NLS-1$
66          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
67          answer.addElement(new TextElement(sb.toString()));
68  
69          buildPrimaryKeyWhereClause().forEach(answer::addElement);
70  
71          if (context.getPlugins().sqlMapSelectByPrimaryKeyElementGenerated(answer, introspectedTable)) {
72              parentElement.addElement(answer);
73          }
74      }
75  }