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 java.util.Iterator;
19
20 import org.mybatis.generator.api.IntrospectedColumn;
21 import org.mybatis.generator.api.dom.OutputUtilities;
22 import org.mybatis.generator.api.dom.xml.Attribute;
23 import org.mybatis.generator.api.dom.xml.TextElement;
24 import org.mybatis.generator.api.dom.xml.XmlElement;
25 import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
26
27
28
29
30
31
32 public class UpdateByPrimaryKeyWithBLOBsElementGenerator extends
33 AbstractXmlElementGenerator {
34
35 public UpdateByPrimaryKeyWithBLOBsElementGenerator() {
36 super();
37 }
38
39 @Override
40 public void addElements(XmlElement parentElement) {
41 XmlElement answer = new XmlElement("update");
42
43 answer
44 .addAttribute(new Attribute(
45 "id", introspectedTable.getUpdateByPrimaryKeyWithBLOBsStatementId()));
46
47 String parameterType;
48
49 if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
50 parameterType = introspectedTable.getRecordWithBLOBsType();
51 } else {
52 parameterType = introspectedTable.getBaseRecordType();
53 }
54
55 answer.addAttribute(new Attribute("parameterClass",
56 parameterType));
57
58 context.getCommentGenerator().addComment(answer);
59
60 StringBuilder sb = new StringBuilder();
61
62 sb.append("update ");
63 sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
64 answer.addElement(new TextElement(sb.toString()));
65
66
67 sb.setLength(0);
68 sb.append("set ");
69
70 Iterator<IntrospectedColumn> iter = introspectedTable
71 .getNonPrimaryKeyColumns().iterator();
72 while (iter.hasNext()) {
73 IntrospectedColumn introspectedColumn = iter.next();
74
75 sb.append(Ibatis2FormattingUtilities
76 .getEscapedColumnName(introspectedColumn));
77 sb.append(" = ");
78 sb.append(Ibatis2FormattingUtilities
79 .getParameterClause(introspectedColumn));
80
81 if (iter.hasNext()) {
82 sb.append(',');
83 }
84
85 answer.addElement(new TextElement(sb.toString()));
86
87
88 if (iter.hasNext()) {
89 sb.setLength(0);
90 OutputUtilities.xmlIndent(sb, 1);
91 }
92 }
93
94 boolean and = false;
95 for (IntrospectedColumn introspectedColumn : introspectedTable
96 .getPrimaryKeyColumns()) {
97 sb.setLength(0);
98 if (and) {
99 sb.append(" and ");
100 } else {
101 sb.append("where ");
102 and = true;
103 }
104
105 sb.append(Ibatis2FormattingUtilities
106 .getEscapedColumnName(introspectedColumn));
107 sb.append(" = ");
108 sb.append(Ibatis2FormattingUtilities
109 .getParameterClause(introspectedColumn));
110 answer.addElement(new TextElement(sb.toString()));
111 }
112
113 if (context.getPlugins()
114 .sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(answer,
115 introspectedTable)) {
116 parentElement.addElement(answer);
117 }
118 }
119 }