View Javadoc
1   /**
2    *    Copyright 2006-2015 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    *       http://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.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.TextElement;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
25  
26  /**
27   * 
28   * @author Jeff Butler
29   * 
30   */
31  public class SelectByPrimaryKeyElementGenerator extends
32          AbstractXmlElementGenerator {
33  
34      public SelectByPrimaryKeyElementGenerator() {
35          super();
36      }
37  
38      @Override
39      public void addElements(XmlElement parentElement) {
40          XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
41  
42          answer.addAttribute(new Attribute(
43                  "id", introspectedTable.getSelectByPrimaryKeyStatementId())); //$NON-NLS-1$
44          if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
45              answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
46                      introspectedTable.getResultMapWithBLOBsId()));
47          } else {
48              answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
49                      introspectedTable.getBaseResultMapId()));
50          }
51  
52          String parameterType;
53          if (introspectedTable.getRules().generatePrimaryKeyClass()) {
54              parameterType = introspectedTable.getPrimaryKeyType();
55          } else {
56              // PK fields are in the base class. If more than on PK
57              // field, then they are coming in a map.
58              if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
59                  parameterType = "map"; //$NON-NLS-1$
60              } else {
61                  parameterType = introspectedTable.getPrimaryKeyColumns().get(0)
62                          .getFullyQualifiedJavaType().toString();
63              }
64          }
65  
66          answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
67                  parameterType));
68  
69          context.getCommentGenerator().addComment(answer);
70  
71          StringBuilder sb = new StringBuilder();
72          sb.append("select "); //$NON-NLS-1$
73  
74          if (stringHasValue(introspectedTable
75                  .getSelectByPrimaryKeyQueryId())) {
76              sb.append('\'');
77              sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
78              sb.append("' as QUERYID,"); //$NON-NLS-1$
79          }
80          answer.addElement(new TextElement(sb.toString()));
81          answer.addElement(getBaseColumnListElement());
82          if (introspectedTable.hasBLOBColumns()) {
83              answer.addElement(new TextElement(",")); //$NON-NLS-1$
84              answer.addElement(getBlobColumnListElement());
85          }
86  
87          sb.setLength(0);
88          sb.append("from "); //$NON-NLS-1$
89          sb.append(introspectedTable
90                  .getAliasedFullyQualifiedTableNameAtRuntime());
91          answer.addElement(new TextElement(sb.toString()));
92  
93          boolean and = false;
94          for (IntrospectedColumn introspectedColumn : introspectedTable
95                  .getPrimaryKeyColumns()) {
96              sb.setLength(0);
97              if (and) {
98                  sb.append("  and "); //$NON-NLS-1$
99              } else {
100                 sb.append("where "); //$NON-NLS-1$
101                 and = true;
102             }
103 
104             sb.append(MyBatis3FormattingUtilities
105                     .getAliasedEscapedColumnName(introspectedColumn));
106             sb.append(" = "); //$NON-NLS-1$
107             sb.append(MyBatis3FormattingUtilities
108                     .getParameterClause(introspectedColumn));
109             answer.addElement(new TextElement(sb.toString()));
110         }
111 
112         if (context.getPlugins()
113                 .sqlMapSelectByPrimaryKeyElementGenerated(answer,
114                         introspectedTable)) {
115             parentElement.addElement(answer);
116         }
117     }
118 }