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 static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  
20  import org.mybatis.generator.api.dom.xml.Attribute;
21  import org.mybatis.generator.api.dom.xml.TextElement;
22  import org.mybatis.generator.api.dom.xml.XmlElement;
23  
24  public class SelectByExampleWithoutBLOBsElementGenerator extends AbstractXmlElementGenerator {
25  
26      public SelectByExampleWithoutBLOBsElementGenerator() {
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("id", //$NON-NLS-1$
35                  introspectedTable.getSelectByExampleStatementId()));
36          answer.addAttribute(new Attribute(
37                  "resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
38          answer.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType())); //$NON-NLS-1$
39  
40          context.getCommentGenerator().addComment(answer);
41  
42          answer.addElement(new TextElement("select")); //$NON-NLS-1$
43          XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
44          ifElement.addAttribute(new Attribute("test", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
45          ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
46          answer.addElement(ifElement);
47  
48          StringBuilder sb = new StringBuilder();
49          if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
50              sb.append('\'');
51              sb.append(introspectedTable.getSelectByExampleQueryId());
52              sb.append("' as QUERYID,"); //$NON-NLS-1$
53              answer.addElement(new TextElement(sb.toString()));
54          }
55          answer.addElement(getBaseColumnListElement());
56  
57          sb.setLength(0);
58          sb.append("from "); //$NON-NLS-1$
59          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
60          answer.addElement(new TextElement(sb.toString()));
61          answer.addElement(getExampleIncludeElement());
62  
63          ifElement = new XmlElement("if"); //$NON-NLS-1$
64          ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
65          ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
66          answer.addElement(ifElement);
67  
68          if (context.getPlugins().sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer, introspectedTable)) {
69              parentElement.addElement(answer);
70          }
71      }
72  }