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.ibatis2.sqlmap.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.ibatis2.Ibatis2FormattingUtilities;
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              // select by primary key, but no primary key class. Fields
57              // must be in the base record
58              parameterType = introspectedTable.getBaseRecordType();
59          }
60  
61          answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
62                  parameterType));
63  
64          context.getCommentGenerator().addComment(answer);
65  
66          StringBuilder sb = new StringBuilder();
67          sb.append("select "); //$NON-NLS-1$
68  
69          if (stringHasValue(introspectedTable
70                  .getSelectByPrimaryKeyQueryId())) {
71              sb.append('\'');
72              sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
73              sb.append("' as QUERYID,"); //$NON-NLS-1$
74          }
75          answer.addElement(new TextElement(sb.toString()));
76          answer.addElement(getBaseColumnListElement());
77          if (introspectedTable.hasBLOBColumns()) {
78              answer.addElement(new TextElement(",")); //$NON-NLS-1$
79              answer.addElement(getBlobColumnListElement());
80          }
81  
82          sb.setLength(0);
83          sb.append("from "); //$NON-NLS-1$
84          sb.append(introspectedTable
85                  .getAliasedFullyQualifiedTableNameAtRuntime());
86          answer.addElement(new TextElement(sb.toString()));
87  
88          boolean and = false;
89          for (IntrospectedColumn introspectedColumn : introspectedTable
90                  .getPrimaryKeyColumns()) {
91              sb.setLength(0);
92              if (and) {
93                  sb.append("  and "); //$NON-NLS-1$
94              } else {
95                  sb.append("where "); //$NON-NLS-1$
96                  and = true;
97              }
98  
99              sb.append(Ibatis2FormattingUtilities
100                     .getAliasedEscapedColumnName(introspectedColumn));
101             sb.append(" = "); //$NON-NLS-1$
102             sb.append(Ibatis2FormattingUtilities
103                     .getParameterClause(introspectedColumn));
104             answer.addElement(new TextElement(sb.toString()));
105         }
106 
107         if (context.getPlugins()
108                 .sqlMapSelectByPrimaryKeyElementGenerated(answer,
109                         introspectedTable)) {
110             parentElement.addElement(answer);
111         }
112     }
113 }