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;
17  
18  import static org.mybatis.generator.internal.util.messages.Messages.getString;
19  
20  import org.mybatis.generator.api.FullyQualifiedTable;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.Document;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.AbstractXmlGenerator;
25  import org.mybatis.generator.codegen.XmlConstants;
26  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;
27  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.BaseColumnListElementGenerator;
28  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.BlobColumnListElementGenerator;
29  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.CountByExampleElementGenerator;
30  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByExampleElementGenerator;
31  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByPrimaryKeyElementGenerator;
32  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ExampleWhereClauseElementGenerator;
33  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertElementGenerator;
34  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertSelectiveElementGenerator;
35  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithBLOBsElementGenerator;
36  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithoutBLOBsElementGenerator;
37  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByExampleWithBLOBsElementGenerator;
38  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByExampleWithoutBLOBsElementGenerator;
39  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByPrimaryKeyElementGenerator;
40  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleSelectiveElementGenerator;
41  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleWithBLOBsElementGenerator;
42  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleWithoutBLOBsElementGenerator;
43  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeySelectiveElementGenerator;
44  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeyWithBLOBsElementGenerator;
45  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeyWithoutBLOBsElementGenerator;
46  
47  public class XMLMapperGenerator extends AbstractXmlGenerator {
48  
49      public XMLMapperGenerator() {
50          super();
51      }
52  
53      protected XmlElement getSqlMapElement() {
54          FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
55          progressCallback.startTask(getString("Progress.12", table.toString())); //$NON-NLS-1$
56          XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
57          String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
58          answer.addAttribute(new Attribute("namespace", namespace)); //$NON-NLS-1$
59  
60          context.getCommentGenerator().addRootComment(answer);
61  
62          addResultMapWithoutBLOBsElement(answer);
63          addResultMapWithBLOBsElement(answer);
64          addExampleWhereClauseElement(answer);
65          addMyBatis3UpdateByExampleWhereClauseElement(answer);
66          addBaseColumnListElement(answer);
67          addBlobColumnListElement(answer);
68          addSelectByExampleWithBLOBsElement(answer);
69          addSelectByExampleWithoutBLOBsElement(answer);
70          addSelectByPrimaryKeyElement(answer);
71          addDeleteByPrimaryKeyElement(answer);
72          addDeleteByExampleElement(answer);
73          addInsertElement(answer);
74          addInsertSelectiveElement(answer);
75          addCountByExampleElement(answer);
76          addUpdateByExampleSelectiveElement(answer);
77          addUpdateByExampleWithBLOBsElement(answer);
78          addUpdateByExampleWithoutBLOBsElement(answer);
79          addUpdateByPrimaryKeySelectiveElement(answer);
80          addUpdateByPrimaryKeyWithBLOBsElement(answer);
81          addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
82  
83          return answer;
84      }
85  
86      protected void addResultMapWithoutBLOBsElement(XmlElement parentElement) {
87          if (introspectedTable.getRules().generateBaseResultMap()) {
88              AbstractXmlElementGenerator elementGenerator = new ResultMapWithoutBLOBsElementGenerator(false);
89              initializeAndExecuteGenerator(elementGenerator, parentElement);
90          }
91      }
92  
93      protected void addResultMapWithBLOBsElement(XmlElement parentElement) {
94          if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
95              AbstractXmlElementGenerator elementGenerator = new ResultMapWithBLOBsElementGenerator();
96              initializeAndExecuteGenerator(elementGenerator, parentElement);
97          }
98      }
99  
100     protected void addExampleWhereClauseElement(XmlElement parentElement) {
101         if (introspectedTable.getRules().generateSQLExampleWhereClause()) {
102             AbstractXmlElementGenerator elementGenerator = new ExampleWhereClauseElementGenerator(false);
103             initializeAndExecuteGenerator(elementGenerator, parentElement);
104         }
105     }
106 
107     protected void addMyBatis3UpdateByExampleWhereClauseElement(XmlElement parentElement) {
108         if (introspectedTable.getRules().generateMyBatis3UpdateByExampleWhereClause()) {
109             AbstractXmlElementGenerator elementGenerator = new ExampleWhereClauseElementGenerator(true);
110             initializeAndExecuteGenerator(elementGenerator, parentElement);
111         }
112     }
113 
114     protected void addBaseColumnListElement(XmlElement parentElement) {
115         if (introspectedTable.getRules().generateBaseColumnList()) {
116             AbstractXmlElementGenerator elementGenerator = new BaseColumnListElementGenerator();
117             initializeAndExecuteGenerator(elementGenerator, parentElement);
118         }
119     }
120 
121     protected void addBlobColumnListElement(XmlElement parentElement) {
122         if (introspectedTable.getRules().generateBlobColumnList()) {
123             AbstractXmlElementGenerator elementGenerator = new BlobColumnListElementGenerator();
124             initializeAndExecuteGenerator(elementGenerator, parentElement);
125         }
126     }
127 
128     protected void addSelectByExampleWithoutBLOBsElement(XmlElement parentElement) {
129         if (introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
130             AbstractXmlElementGenerator elementGenerator = new SelectByExampleWithoutBLOBsElementGenerator();
131             initializeAndExecuteGenerator(elementGenerator, parentElement);
132         }
133     }
134 
135     protected void addSelectByExampleWithBLOBsElement(XmlElement parentElement) {
136         if (introspectedTable.getRules().generateSelectByExampleWithBLOBs()) {
137             AbstractXmlElementGenerator elementGenerator = new SelectByExampleWithBLOBsElementGenerator();
138             initializeAndExecuteGenerator(elementGenerator, parentElement);
139         }
140     }
141 
142     protected void addSelectByPrimaryKeyElement(XmlElement parentElement) {
143         if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
144             AbstractXmlElementGenerator elementGenerator = new SelectByPrimaryKeyElementGenerator();
145             initializeAndExecuteGenerator(elementGenerator, parentElement);
146         }
147     }
148 
149     protected void addDeleteByExampleElement(XmlElement parentElement) {
150         if (introspectedTable.getRules().generateDeleteByExample()) {
151             AbstractXmlElementGenerator elementGenerator = new DeleteByExampleElementGenerator();
152             initializeAndExecuteGenerator(elementGenerator, parentElement);
153         }
154     }
155 
156     protected void addDeleteByPrimaryKeyElement(XmlElement parentElement) {
157         if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
158             AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator(false);
159             initializeAndExecuteGenerator(elementGenerator, parentElement);
160         }
161     }
162 
163     protected void addInsertElement(XmlElement parentElement) {
164         if (introspectedTable.getRules().generateInsert()) {
165             AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator(false);
166             initializeAndExecuteGenerator(elementGenerator, parentElement);
167         }
168     }
169 
170     protected void addInsertSelectiveElement(XmlElement parentElement) {
171         if (introspectedTable.getRules().generateInsertSelective()) {
172             AbstractXmlElementGenerator elementGenerator = new InsertSelectiveElementGenerator();
173             initializeAndExecuteGenerator(elementGenerator, parentElement);
174         }
175     }
176 
177     protected void addCountByExampleElement(XmlElement parentElement) {
178         if (introspectedTable.getRules().generateCountByExample()) {
179             AbstractXmlElementGenerator elementGenerator = new CountByExampleElementGenerator();
180             initializeAndExecuteGenerator(elementGenerator, parentElement);
181         }
182     }
183 
184     protected void addUpdateByExampleSelectiveElement(XmlElement parentElement) {
185         if (introspectedTable.getRules().generateUpdateByExampleSelective()) {
186             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleSelectiveElementGenerator();
187             initializeAndExecuteGenerator(elementGenerator, parentElement);
188         }
189     }
190 
191     protected void addUpdateByExampleWithBLOBsElement(XmlElement parentElement) {
192         if (introspectedTable.getRules().generateUpdateByExampleWithBLOBs()) {
193             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleWithBLOBsElementGenerator();
194             initializeAndExecuteGenerator(elementGenerator, parentElement);
195         }
196     }
197 
198     protected void addUpdateByExampleWithoutBLOBsElement(XmlElement parentElement) {
199         if (introspectedTable.getRules().generateUpdateByExampleWithoutBLOBs()) {
200             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleWithoutBLOBsElementGenerator();
201             initializeAndExecuteGenerator(elementGenerator, parentElement);
202         }
203     }
204 
205     protected void addUpdateByPrimaryKeySelectiveElement(XmlElement parentElement) {
206         if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
207             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeySelectiveElementGenerator();
208             initializeAndExecuteGenerator(elementGenerator, parentElement);
209         }
210     }
211 
212     protected void addUpdateByPrimaryKeyWithBLOBsElement(XmlElement parentElement) {
213         if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithBLOBs()) {
214             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeyWithBLOBsElementGenerator();
215             initializeAndExecuteGenerator(elementGenerator, parentElement);
216         }
217     }
218 
219     protected void addUpdateByPrimaryKeyWithoutBLOBsElement(XmlElement parentElement) {
220         if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithoutBLOBs()) {
221             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeyWithoutBLOBsElementGenerator(false);
222             initializeAndExecuteGenerator(elementGenerator, parentElement);
223         }
224     }
225 
226     protected void initializeAndExecuteGenerator(AbstractXmlElementGenerator elementGenerator,
227                                                  XmlElement parentElement) {
228         elementGenerator.setContext(context);
229         elementGenerator.setIntrospectedTable(introspectedTable);
230         elementGenerator.setProgressCallback(progressCallback);
231         elementGenerator.setWarnings(warnings);
232         elementGenerator.addElements(parentElement);
233     }
234 
235     @Override
236     public Document getDocument() {
237         Document document = new Document(XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
238                 XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
239         document.setRootElement(getSqlMapElement());
240 
241         if (!context.getPlugins().sqlMapDocumentGenerated(document, introspectedTable)) {
242             document = null;
243         }
244 
245         return document;
246     }
247 }