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.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.TextElement;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  
25  public class ExampleWhereClauseElementGenerator extends AbstractXmlElementGenerator {
26  
27      private final boolean isForUpdateByExample;
28  
29      public ExampleWhereClauseElementGenerator(boolean isForUpdateByExample) {
30          super();
31          this.isForUpdateByExample = isForUpdateByExample;
32      }
33  
34      @Override
35      public void addElements(XmlElement parentElement) {
36          XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$
37  
38          if (isForUpdateByExample) {
39              answer.addAttribute(new Attribute(
40                      "id", introspectedTable.getMyBatis3UpdateByExampleWhereClauseId())); //$NON-NLS-1$
41          } else {
42              answer.addAttribute(new Attribute(
43                      "id", introspectedTable.getExampleWhereClauseId())); //$NON-NLS-1$
44          }
45  
46          context.getCommentGenerator().addComment(answer);
47  
48          XmlElement whereElement = new XmlElement("where"); //$NON-NLS-1$
49          answer.addElement(whereElement);
50  
51          XmlElement outerForEachElement = new XmlElement("foreach"); //$NON-NLS-1$
52          if (isForUpdateByExample) {
53              outerForEachElement.addAttribute(new Attribute(
54                      "collection", "example.oredCriteria")); //$NON-NLS-1$ //$NON-NLS-2$
55          } else {
56              outerForEachElement.addAttribute(new Attribute(
57                      "collection", "oredCriteria")); //$NON-NLS-1$ //$NON-NLS-2$
58          }
59          outerForEachElement.addAttribute(new Attribute("item", "criteria")); //$NON-NLS-1$ //$NON-NLS-2$
60          outerForEachElement.addAttribute(new Attribute("separator", "or")); //$NON-NLS-1$ //$NON-NLS-2$
61          whereElement.addElement(outerForEachElement);
62  
63          XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
64          ifElement.addAttribute(new Attribute("test", "criteria.valid")); //$NON-NLS-1$ //$NON-NLS-2$
65          outerForEachElement.addElement(ifElement);
66  
67          XmlElement trimElement = new XmlElement("trim"); //$NON-NLS-1$
68          trimElement.addAttribute(new Attribute("prefix", "(")); //$NON-NLS-1$ //$NON-NLS-2$
69          trimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
70          trimElement.addAttribute(new Attribute("prefixOverrides", "and")); //$NON-NLS-1$ //$NON-NLS-2$
71  
72          ifElement.addElement(trimElement);
73  
74          trimElement.addElement(getMiddleForEachElement(null));
75  
76          for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
77              if (stringHasValue(introspectedColumn.getTypeHandler())) {
78                  trimElement.addElement(getMiddleForEachElement(introspectedColumn));
79              }
80          }
81  
82          if (context.getPlugins().sqlMapExampleWhereClauseElementGenerated(answer, introspectedTable)) {
83              parentElement.addElement(answer);
84          }
85      }
86  
87      private XmlElement getMiddleForEachElement(IntrospectedColumn introspectedColumn) {
88          StringBuilder sb = new StringBuilder();
89          String criteriaAttribute;
90          boolean typeHandled;
91          String typeHandlerString;
92          if (introspectedColumn == null) {
93              criteriaAttribute = "criteria.criteria"; //$NON-NLS-1$
94              typeHandled = false;
95              typeHandlerString = null;
96          } else {
97              sb.setLength(0);
98              sb.append("criteria."); //$NON-NLS-1$
99              sb.append(introspectedColumn.getJavaProperty());
100             sb.append("Criteria"); //$NON-NLS-1$
101             criteriaAttribute = sb.toString();
102 
103             typeHandled = true;
104 
105             sb.setLength(0);
106             sb.append(",typeHandler="); //$NON-NLS-1$
107             sb.append(introspectedColumn.getTypeHandler());
108             typeHandlerString = sb.toString();
109         }
110 
111         XmlElement middleForEachElement = new XmlElement("foreach"); //$NON-NLS-1$
112         middleForEachElement.addAttribute(new Attribute(
113                 "collection", criteriaAttribute)); //$NON-NLS-1$
114         middleForEachElement.addAttribute(new Attribute("item", "criterion")); //$NON-NLS-1$ //$NON-NLS-2$
115 
116         XmlElement chooseElement = new XmlElement("choose"); //$NON-NLS-1$
117         middleForEachElement.addElement(chooseElement);
118 
119         XmlElement when = new XmlElement("when"); //$NON-NLS-1$
120         when.addAttribute(new Attribute("test", "criterion.noValue")); //$NON-NLS-1$ //$NON-NLS-2$
121         when.addElement(new TextElement("and ${criterion.condition}")); //$NON-NLS-1$
122         chooseElement.addElement(when);
123 
124         when = new XmlElement("when"); //$NON-NLS-1$
125         when.addAttribute(new Attribute("test", "criterion.singleValue")); //$NON-NLS-1$ //$NON-NLS-2$
126         sb.setLength(0);
127         sb.append("and ${criterion.condition} #{criterion.value"); //$NON-NLS-1$
128         if (typeHandled) {
129             sb.append(typeHandlerString);
130         }
131         sb.append('}');
132         when.addElement(new TextElement(sb.toString()));
133         chooseElement.addElement(when);
134 
135         when = new XmlElement("when"); //$NON-NLS-1$
136         when.addAttribute(new Attribute("test", "criterion.betweenValue")); //$NON-NLS-1$ //$NON-NLS-2$
137         sb.setLength(0);
138         sb.append("and ${criterion.condition} #{criterion.value"); //$NON-NLS-1$
139         if (typeHandled) {
140             sb.append(typeHandlerString);
141         }
142         sb.append("} and #{criterion.secondValue"); //$NON-NLS-1$
143         if (typeHandled) {
144             sb.append(typeHandlerString);
145         }
146         sb.append('}');
147         when.addElement(new TextElement(sb.toString()));
148         chooseElement.addElement(when);
149 
150         when = new XmlElement("when"); //$NON-NLS-1$
151         when.addAttribute(new Attribute("test", "criterion.listValue")); //$NON-NLS-1$ //$NON-NLS-2$
152         when.addElement(new TextElement("and ${criterion.condition}")); //$NON-NLS-1$
153         XmlElement innerForEach = new XmlElement("foreach"); //$NON-NLS-1$
154         innerForEach.addAttribute(new Attribute("collection", "criterion.value")); //$NON-NLS-1$ //$NON-NLS-2$
155         innerForEach.addAttribute(new Attribute("item", "listItem")); //$NON-NLS-1$ //$NON-NLS-2$
156         innerForEach.addAttribute(new Attribute("open", "(")); //$NON-NLS-1$ //$NON-NLS-2$
157         innerForEach.addAttribute(new Attribute("close", ")")); //$NON-NLS-1$ //$NON-NLS-2$
158         innerForEach.addAttribute(new Attribute("separator", ",")); //$NON-NLS-1$ //$NON-NLS-2$
159         sb.setLength(0);
160         sb.append("#{listItem"); //$NON-NLS-1$
161         if (typeHandled) {
162             sb.append(typeHandlerString);
163         }
164         sb.append('}');
165         innerForEach.addElement(new TextElement(sb.toString()));
166         when.addElement(innerForEach);
167         chooseElement.addElement(when);
168 
169         return middleForEachElement;
170     }
171 }