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 org.mybatis.generator.api.dom.xml.Attribute;
19  import org.mybatis.generator.api.dom.xml.TextElement;
20  import org.mybatis.generator.api.dom.xml.XmlElement;
21  import org.mybatis.generator.config.PropertyRegistry;
22  import org.mybatis.generator.internal.util.StringUtility;
23  
24  public class SimpleSelectAllElementGenerator extends AbstractXmlElementGenerator {
25  
26      public SimpleSelectAllElementGenerator() {
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.getSelectAllStatementId())); //$NON-NLS-1$
36          answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
37                  introspectedTable.getBaseResultMapId()));
38  
39          context.getCommentGenerator().addComment(answer);
40  
41          buildSelectList("select ", introspectedTable.getAllColumns()).forEach(answer::addElement); //$NON-NLS-1$
42  
43          StringBuilder sb = new StringBuilder();
44          sb.append("from "); //$NON-NLS-1$
45          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
46          answer.addElement(new TextElement(sb.toString()));
47  
48          String orderByClause = introspectedTable.getTableConfigurationProperty(
49                  PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
50          boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
51          if (hasOrderBy) {
52              sb.setLength(0);
53              sb.append("order by "); //$NON-NLS-1$
54              sb.append(orderByClause);
55              answer.addElement(new TextElement(sb.toString()));
56          }
57  
58          if (context.getPlugins().sqlMapSelectAllElementGenerated(answer, introspectedTable)) {
59              parentElement.addElement(answer);
60          }
61      }
62  }