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.ibatis2.sqlmap.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.isTrue;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  
21  import org.mybatis.generator.api.IntrospectedColumn;
22  import org.mybatis.generator.api.dom.xml.Attribute;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
25  import org.mybatis.generator.config.PropertyRegistry;
26  
27  /**
28   * 
29   * @author Jeff Butler
30   * 
31   */
32  public class ResultMapWithBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      public ResultMapWithBLOBsElementGenerator() {
36          super();
37      }
38  
39      @Override
40      public void addElements(XmlElement parentElement) {
41          boolean useColumnIndex = isTrue(introspectedTable
42                          .getTableConfigurationProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES));
43  
44          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
45  
46          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
47                  introspectedTable.getResultMapWithBLOBsId()));
48  
49          String returnType;
50          if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
51              returnType = introspectedTable.getRecordWithBLOBsType();
52          } else {
53              // table has BLOBs, but no BLOB class - BLOB fields must be
54              // in the base class
55              returnType = introspectedTable.getBaseRecordType();
56          }
57  
58          answer.addAttribute(new Attribute("class", //$NON-NLS-1$
59                  returnType));
60  
61          StringBuilder sb = new StringBuilder();
62          sb.append(introspectedTable.getIbatis2SqlMapNamespace());
63          sb.append('.');
64          sb.append(introspectedTable.getBaseResultMapId());
65          answer.addAttribute(new Attribute("extends", sb.toString())); //$NON-NLS-1$
66  
67          context.getCommentGenerator().addComment(answer);
68  
69          int i = introspectedTable.getNonBLOBColumnCount() + 1;
70          if (stringHasValue(introspectedTable
71                  .getSelectByPrimaryKeyQueryId())
72                  || stringHasValue(introspectedTable
73                          .getSelectByExampleQueryId())) {
74              i++;
75          }
76  
77          for (IntrospectedColumn introspectedColumn : introspectedTable
78                  .getBLOBColumns()) {
79              XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$
80  
81              if (useColumnIndex) {
82                  resultElement.addAttribute(new Attribute(
83                          "columnIndex", Integer.toString(i++))); //$NON-NLS-1$
84              } else {
85                  resultElement
86                          .addAttribute(new Attribute(
87                                  "column", Ibatis2FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
88              }
89              resultElement.addAttribute(new Attribute(
90                      "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
91              resultElement.addAttribute(new Attribute(
92                      "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$
93  
94              if (stringHasValue(introspectedColumn
95                      .getTypeHandler())) {
96                  resultElement.addAttribute(new Attribute(
97                          "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
98              }
99  
100             answer.addElement(resultElement);
101         }
102 
103         if (context.getPlugins()
104                 .sqlMapResultMapWithBLOBsElementGenerated(answer,
105                         introspectedTable)) {
106             parentElement.addElement(answer);
107         }
108     }
109 }