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 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  /**
25   * 
26   * @author Jeff Butler
27   * 
28   */
29  public class SelectByExampleWithoutBLOBsElementGenerator extends
30          AbstractXmlElementGenerator {
31  
32      public SelectByExampleWithoutBLOBsElementGenerator() {
33          super();
34      }
35  
36      @Override
37      public void addElements(XmlElement parentElement) {
38          String fqjt = introspectedTable.getExampleType();
39  
40          XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
41  
42          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
43                  introspectedTable.getSelectByExampleStatementId()));
44          answer.addAttribute(new Attribute(
45                  "resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
46          answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$
47  
48          context.getCommentGenerator().addComment(answer);
49  
50          answer.addElement(new TextElement("select")); //$NON-NLS-1$
51          XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
52          ifElement.addAttribute(new Attribute("test", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
53          ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
54          answer.addElement(ifElement);
55  
56          StringBuilder sb = new StringBuilder();
57          if (stringHasValue(introspectedTable
58                  .getSelectByExampleQueryId())) {
59              sb.append('\'');
60              sb.append(introspectedTable.getSelectByExampleQueryId());
61              sb.append("' as QUERYID,"); //$NON-NLS-1$
62              answer.addElement(new TextElement(sb.toString()));
63          }
64          answer.addElement(getBaseColumnListElement());
65  
66          sb.setLength(0);
67          sb.append("from "); //$NON-NLS-1$
68          sb.append(introspectedTable
69                  .getAliasedFullyQualifiedTableNameAtRuntime());
70          answer.addElement(new TextElement(sb.toString()));
71          answer.addElement(getExampleIncludeElement());
72  
73          ifElement = new XmlElement("if"); //$NON-NLS-1$
74          ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
75          ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
76          answer.addElement(ifElement);
77  
78          if (context.getPlugins()
79                  .sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
80                          introspectedTable)) {
81              parentElement.addElement(answer);
82          }
83      }
84  }