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 SelectByPrimaryKeyElementGenerator extends AbstractXmlElementGenerator {
25  
26      public SelectByPrimaryKeyElementGenerator() {
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          if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
37              answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
38                      introspectedTable.getResultMapWithBLOBsId()));
39          } else {
40              answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
41                      introspectedTable.getBaseResultMapId()));
42          }
43  
44          String parameterType;
45          if (introspectedTable.getRules().generatePrimaryKeyClass()) {
46              parameterType = introspectedTable.getPrimaryKeyType();
47          } else {
48              // PK fields are in the base class. If more than on PK
49              // field, then they are coming in a map.
50              if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
51                  parameterType = "map"; //$NON-NLS-1$
52              } else {
53                  parameterType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
54              }
55          }
56  
57          answer.addAttribute(new Attribute("parameterType", parameterType)); //$NON-NLS-1$
58  
59          context.getCommentGenerator().addComment(answer);
60  
61          StringBuilder sb = new StringBuilder();
62          sb.append("select "); //$NON-NLS-1$
63  
64          if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
65              sb.append('\'');
66              sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
67              sb.append("' as QUERYID,"); //$NON-NLS-1$
68          }
69          answer.addElement(new TextElement(sb.toString()));
70          answer.addElement(getBaseColumnListElement());
71          if (introspectedTable.hasBLOBColumns()) {
72              answer.addElement(new TextElement(",")); //$NON-NLS-1$
73              answer.addElement(getBlobColumnListElement());
74          }
75  
76          sb.setLength(0);
77          sb.append("from "); //$NON-NLS-1$
78          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
79          answer.addElement(new TextElement(sb.toString()));
80  
81          buildPrimaryKeyWhereClause().forEach(answer::addElement);
82  
83          if (context.getPlugins().sqlMapSelectByPrimaryKeyElementGenerated(answer, introspectedTable)) {
84              parentElement.addElement(answer);
85          }
86      }
87  }