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