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 SelectByExampleWithBLOBsElementGenerator extends
30          AbstractXmlElementGenerator {
31  
32      public SelectByExampleWithBLOBsElementGenerator() {
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          answer
42                  .addAttribute(new Attribute(
43                          "id", introspectedTable.getSelectByExampleWithBLOBsStatementId())); //$NON-NLS-1$
44          answer.addAttribute(new Attribute(
45                  "resultMap", introspectedTable.getResultMapWithBLOBsId())); //$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  
65          answer.addElement(getBaseColumnListElement());
66          answer.addElement(new TextElement(",")); //$NON-NLS-1$
67          answer.addElement(getBlobColumnListElement());
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          answer.addElement(getExampleIncludeElement());
75  
76          ifElement = new XmlElement("if"); //$NON-NLS-1$
77          ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
78          ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
79          answer.addElement(ifElement);
80  
81          if (context.getPlugins()
82                  .sqlMapSelectByExampleWithBLOBsElementGenerated(answer,
83                          introspectedTable)) {
84              parentElement.addElement(answer);
85          }
86      }
87  }