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.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          XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
39  
40          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
41                  introspectedTable.getSelectByExampleStatementId()));
42          answer.addAttribute(new Attribute(
43                  "resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
44          answer.addAttribute(new Attribute(
45                  "parameterClass", introspectedTable.getExampleType())); //$NON-NLS-1$
46  
47          context.getCommentGenerator().addComment(answer);
48  
49          answer.addElement(new TextElement("select")); //$NON-NLS-1$
50          XmlElement isParameterPresent = new XmlElement("isParameterPresent"); //$NON-NLS-1$
51          XmlElement isEqualElement = new XmlElement("isEqual"); //$NON-NLS-1$
52          isEqualElement.addAttribute(new Attribute("property", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
53          isEqualElement.addAttribute(new Attribute("compareValue", "true")); //$NON-NLS-1$ //$NON-NLS-2$
54          isEqualElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
55          isParameterPresent.addElement(isEqualElement);
56          answer.addElement(isParameterPresent);
57  
58          StringBuilder sb = new StringBuilder();
59          if (stringHasValue(introspectedTable
60                  .getSelectByExampleQueryId())) {
61              sb.append('\'');
62              sb.append(introspectedTable.getSelectByExampleQueryId());
63              sb.append("' as QUERYID,"); //$NON-NLS-1$
64              answer.addElement(new TextElement(sb.toString()));
65          }
66  
67          answer.addElement(getBaseColumnListElement());
68  
69          sb.setLength(0);
70          sb.append("from "); //$NON-NLS-1$
71          sb.append(introspectedTable
72                  .getAliasedFullyQualifiedTableNameAtRuntime());
73          answer.addElement(new TextElement(sb.toString()));
74  
75          XmlElement isParameterPresenteElement = new XmlElement(
76                  "isParameterPresent"); //$NON-NLS-1$
77          answer.addElement(isParameterPresenteElement);
78  
79          XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
80          includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
81                  introspectedTable.getIbatis2SqlMapNamespace()
82                          + "." + introspectedTable.getExampleWhereClauseId())); //$NON-NLS-1$
83          isParameterPresenteElement.addElement(includeElement);
84  
85          XmlElement isNotNullElement = new XmlElement("isNotNull"); //$NON-NLS-1$
86          isNotNullElement
87                  .addAttribute(new Attribute("property", "orderByClause")); //$NON-NLS-1$ //$NON-NLS-2$
88          isNotNullElement
89                  .addElement(new TextElement("order by $orderByClause$")); //$NON-NLS-1$
90          isParameterPresenteElement.addElement(isNotNullElement);
91  
92          if (context.getPlugins()
93                  .sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
94                          introspectedTable)) {
95              parentElement.addElement(answer);
96          }
97      }
98  }