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.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.XmlElement;
23  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
24  
25  /**
26   * 
27   * @author Jeff Butler
28   * 
29   */
30  public class ResultMapWithBLOBsElementGenerator extends
31          AbstractXmlElementGenerator {
32  
33      public ResultMapWithBLOBsElementGenerator() {
34          super();
35      }
36  
37      @Override
38      public void addElements(XmlElement parentElement) {
39          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
40  
41          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
42                  introspectedTable.getResultMapWithBLOBsId()));
43  
44          String returnType;
45          if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
46              returnType = introspectedTable.getRecordWithBLOBsType();
47          } else {
48              // table has BLOBs, but no BLOB class - BLOB fields must be
49              // in the base class
50              returnType = introspectedTable.getBaseRecordType();
51          }
52  
53          answer.addAttribute(new Attribute("type", //$NON-NLS-1$
54                  returnType));
55  
56          if (!introspectedTable.isConstructorBased()) {
57              answer.addAttribute(new Attribute("extends", //$NON-NLS-1$
58                  introspectedTable.getBaseResultMapId()));
59          }
60  
61          context.getCommentGenerator().addComment(answer);
62  
63          if (introspectedTable.isConstructorBased()) {
64              addResultMapConstructorElements(answer);
65          } else {
66              addResultMapElements(answer);
67          }
68  
69          if (context.getPlugins()
70                  .sqlMapResultMapWithBLOBsElementGenerated(answer,
71                          introspectedTable)) {
72              parentElement.addElement(answer);
73          }
74      }
75  
76      private void addResultMapElements(XmlElement answer) {
77          for (IntrospectedColumn introspectedColumn : introspectedTable
78                  .getBLOBColumns()) {
79              XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$
80  
81              resultElement
82                      .addAttribute(new Attribute(
83                              "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
84              resultElement.addAttribute(new Attribute(
85                      "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
86              resultElement.addAttribute(new Attribute(
87                      "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$
88  
89              if (stringHasValue(introspectedColumn
90                      .getTypeHandler())) {
91                  resultElement.addAttribute(new Attribute(
92                          "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
93              }
94  
95              answer.addElement(resultElement);
96          }
97      }
98  
99      private void addResultMapConstructorElements(XmlElement answer) {
100         XmlElement constructor = new XmlElement("constructor"); //$NON-NLS-1$
101         
102         for (IntrospectedColumn introspectedColumn : introspectedTable
103                 .getPrimaryKeyColumns()) {
104             XmlElement resultElement = new XmlElement("idArg"); //$NON-NLS-1$
105 
106             resultElement
107                     .addAttribute(new Attribute(
108                             "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
109             resultElement.addAttribute(new Attribute(
110                     "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$
111             resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
112                     introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
113 
114             if (stringHasValue(introspectedColumn
115                     .getTypeHandler())) {
116                 resultElement.addAttribute(new Attribute(
117                         "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
118             }
119 
120             constructor.addElement(resultElement);
121         }
122         
123         for (IntrospectedColumn introspectedColumn : introspectedTable
124                 .getNonPrimaryKeyColumns()) {
125             XmlElement resultElement = new XmlElement("arg"); //$NON-NLS-1$
126 
127             resultElement
128                     .addAttribute(new Attribute(
129                             "column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
130             resultElement.addAttribute(new Attribute(
131                     "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$
132 
133             if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
134                 // need to use the MyBatis type alias for a primitive byte
135                 StringBuilder sb = new StringBuilder();
136                 sb.append('_');
137                 sb.append(introspectedColumn.getFullyQualifiedJavaType().getShortName());
138                 resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
139                         sb.toString()));
140                 
141             } else if ("byte[]".equals(introspectedColumn.getFullyQualifiedJavaType() //$NON-NLS-1$
142                     .getFullyQualifiedName())) {
143                 // need to use the MyBatis type alias for a primitive byte arry
144                 resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
145                         "_byte[]")); //$NON-NLS-1$
146             } else {
147                 resultElement.addAttribute(new Attribute("javaType", //$NON-NLS-1$
148                         introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
149             }
150             
151             if (stringHasValue(introspectedColumn
152                     .getTypeHandler())) {
153                 resultElement.addAttribute(new Attribute(
154                         "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
155             }
156 
157             constructor.addElement(resultElement);
158         }
159 
160         answer.addElement(constructor);
161     }
162 }