1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.ibatis2.sqlmap.elements;
17
18 import org.mybatis.generator.api.IntrospectedColumn;
19 import org.mybatis.generator.api.dom.xml.Attribute;
20 import org.mybatis.generator.api.dom.xml.TextElement;
21 import org.mybatis.generator.api.dom.xml.XmlElement;
22 import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
23
24
25
26
27
28
29 public class UpdateByPrimaryKeySelectiveElementGenerator extends
30 AbstractXmlElementGenerator {
31
32 public UpdateByPrimaryKeySelectiveElementGenerator() {
33 super();
34 }
35
36 @Override
37 public void addElements(XmlElement parentElement) {
38 XmlElement answer = new XmlElement("update");
39
40 answer
41 .addAttribute(new Attribute(
42 "id", introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()));
43
44 String parameterType;
45
46 if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
47 parameterType = introspectedTable.getRecordWithBLOBsType();
48 } else {
49 parameterType = introspectedTable.getBaseRecordType();
50 }
51
52 answer.addAttribute(new Attribute("parameterClass",
53 parameterType));
54
55 context.getCommentGenerator().addComment(answer);
56
57 StringBuilder sb = new StringBuilder();
58
59 sb.append("update ");
60 sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
61 answer.addElement(new TextElement(sb.toString()));
62
63 XmlElement dynamicElement = new XmlElement("dynamic");
64 dynamicElement.addAttribute(new Attribute("prepend", "set"));
65 answer.addElement(dynamicElement);
66
67 for (IntrospectedColumn introspectedColumn : introspectedTable
68 .getNonPrimaryKeyColumns()) {
69 XmlElement isNotNullElement = new XmlElement("isNotNull");
70 isNotNullElement.addAttribute(new Attribute("prepend", ","));
71 isNotNullElement.addAttribute(new Attribute(
72 "property", introspectedColumn.getJavaProperty()));
73 dynamicElement.addElement(isNotNullElement);
74
75 sb.setLength(0);
76 sb.append(Ibatis2FormattingUtilities
77 .getEscapedColumnName(introspectedColumn));
78 sb.append(" = ");
79 sb.append(Ibatis2FormattingUtilities
80 .getParameterClause(introspectedColumn));
81
82 isNotNullElement.addElement(new TextElement(sb.toString()));
83 }
84
85 boolean and = false;
86 for (IntrospectedColumn introspectedColumn : introspectedTable
87 .getPrimaryKeyColumns()) {
88 sb.setLength(0);
89 if (and) {
90 sb.append(" and ");
91 } else {
92 sb.append("where ");
93 and = true;
94 }
95
96 sb.append(Ibatis2FormattingUtilities
97 .getEscapedColumnName(introspectedColumn));
98 sb.append(" = ");
99 sb.append(Ibatis2FormattingUtilities
100 .getParameterClause(introspectedColumn));
101 answer.addElement(new TextElement(sb.toString()));
102 }
103
104 if (context.getPlugins()
105 .sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer,
106 introspectedTable)) {
107 parentElement.addElement(answer);
108 }
109 }
110 }