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 SelectByExampleWithBLOBsElementGenerator extends AbstractXmlElementGenerator {
25  
26      public SelectByExampleWithBLOBsElementGenerator() {
27          super();
28      }
29  
30      @Override
31      public void addElements(XmlElement parentElement) {
32  
33          XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
34          answer.addAttribute(new Attribute(
35                  "id", introspectedTable.getSelectByExampleWithBLOBsStatementId())); //$NON-NLS-1$
36          answer.addAttribute(new Attribute(
37                  "resultMap", introspectedTable.getResultMapWithBLOBsId())); //$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  
56          answer.addElement(getBaseColumnListElement());
57          answer.addElement(new TextElement(",")); //$NON-NLS-1$
58          answer.addElement(getBlobColumnListElement());
59  
60          sb.setLength(0);
61          sb.append("from "); //$NON-NLS-1$
62          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
63          answer.addElement(new TextElement(sb.toString()));
64          answer.addElement(getExampleIncludeElement());
65  
66          ifElement = new XmlElement("if"); //$NON-NLS-1$
67          ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
68          ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
69          answer.addElement(ifElement);
70  
71          if (context.getPlugins().sqlMapSelectByExampleWithBLOBsElementGenerated(answer, introspectedTable)) {
72              parentElement.addElement(answer);
73          }
74      }
75  }