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 java.util.Iterator;
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  import org.mybatis.generator.config.PropertyRegistry;
26  import org.mybatis.generator.internal.util.StringUtility;
27  
28  /**
29   * 
30   * @author Jeff Butler
31   * 
32   */
33  public class SimpleSelectAllElementGenerator extends
34          AbstractXmlElementGenerator {
35  
36      public SimpleSelectAllElementGenerator() {
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.getSelectAllStatementId())); //$NON-NLS-1$
46          answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
47                  introspectedTable.getBaseResultMapId()));
48  
49          context.getCommentGenerator().addComment(answer);
50  
51          StringBuilder sb = new StringBuilder();
52          sb.append("select "); //$NON-NLS-1$
53          Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
54                  .iterator();
55          while (iter.hasNext()) {
56              sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
57                      .next()));
58  
59              if (iter.hasNext()) {
60                  sb.append(", "); //$NON-NLS-1$
61              }
62  
63              if (sb.length() > 80) {
64                  answer.addElement(new TextElement(sb.toString()));
65                  sb.setLength(0);
66              }
67          }
68  
69          if (sb.length() > 0) {
70              answer.addElement(new TextElement(sb.toString()));
71          }
72  
73          sb.setLength(0);
74          sb.append("from "); //$NON-NLS-1$
75          sb.append(introspectedTable
76                  .getAliasedFullyQualifiedTableNameAtRuntime());
77          answer.addElement(new TextElement(sb.toString()));
78          
79          String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
80          boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
81          if (hasOrderBy) {
82              sb.setLength(0);
83              sb.append("order by "); //$NON-NLS-1$
84              sb.append(orderByClause);
85              answer.addElement(new TextElement(sb.toString()));
86          }
87  
88          if (context.getPlugins().sqlMapSelectAllElementGenerated(
89                  answer, introspectedTable)) {
90              parentElement.addElement(answer);
91          }
92      }
93  }