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 UpdateByExampleSelectiveElementGenerator extends
30 AbstractXmlElementGenerator {
31
32 public UpdateByExampleSelectiveElementGenerator() {
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.getUpdateByExampleSelectiveStatementId()));
43
44 context.getCommentGenerator().addComment(answer);
45
46 StringBuilder sb = new StringBuilder();
47
48 sb.append("update ");
49 sb.append(introspectedTable
50 .getAliasedFullyQualifiedTableNameAtRuntime());
51 answer.addElement(new TextElement(sb.toString()));
52
53 XmlElement dynamicElement = new XmlElement("dynamic");
54 dynamicElement.addAttribute(new Attribute("prepend", "set"));
55 answer.addElement(dynamicElement);
56
57 for (IntrospectedColumn introspectedColumn : introspectedTable
58 .getAllColumns()) {
59 XmlElement isNotNullElement = new XmlElement("isNotNull");
60 isNotNullElement.addAttribute(new Attribute("prepend", ","));
61 isNotNullElement.addAttribute(new Attribute(
62 "property", introspectedColumn.getJavaProperty("record.")));
63 dynamicElement.addElement(isNotNullElement);
64
65 sb.setLength(0);
66 sb.append(Ibatis2FormattingUtilities
67 .getAliasedEscapedColumnName(introspectedColumn));
68 sb.append(" = ");
69 sb.append(Ibatis2FormattingUtilities.getParameterClause(
70 introspectedColumn, "record."));
71
72 isNotNullElement.addElement(new TextElement(sb.toString()));
73 }
74
75 XmlElement isParameterPresentElement = new XmlElement(
76 "isParameterPresent");
77 answer.addElement(isParameterPresentElement);
78
79 XmlElement includeElement = new XmlElement("include");
80 includeElement.addAttribute(new Attribute("refid",
81 introspectedTable.getIbatis2SqlMapNamespace()
82 + "." + introspectedTable.getExampleWhereClauseId()));
83 isParameterPresentElement.addElement(includeElement);
84
85 if (context.getPlugins()
86 .sqlMapUpdateByExampleSelectiveElementGenerated(answer,
87 introspectedTable)) {
88 parentElement.addElement(answer);
89 }
90 }
91 }