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.java.FullyQualifiedJavaType;
20 import org.mybatis.generator.api.dom.xml.Attribute;
21 import org.mybatis.generator.api.dom.xml.TextElement;
22 import org.mybatis.generator.api.dom.xml.XmlElement;
23 import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
24 import org.mybatis.generator.config.GeneratedKey;
25
26
27
28
29
30
31 public class InsertSelectiveElementGenerator extends
32 AbstractXmlElementGenerator {
33
34 public InsertSelectiveElementGenerator() {
35 super();
36 }
37
38 @Override
39 public void addElements(XmlElement parentElement) {
40 XmlElement answer = new XmlElement("insert");
41
42 answer.addAttribute(new Attribute(
43 "id", introspectedTable.getInsertSelectiveStatementId()));
44
45 FullyQualifiedJavaType parameterType = introspectedTable.getRules()
46 .calculateAllFieldsClass();
47
48 answer.addAttribute(new Attribute("parameterClass",
49 parameterType.getFullyQualifiedName()));
50
51 context.getCommentGenerator().addComment(answer);
52
53 GeneratedKey gk = introspectedTable.getGeneratedKey();
54
55 if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
56 IntrospectedColumn introspectedColumn = introspectedTable
57 .getColumn(gk.getColumn());
58
59
60 if (introspectedColumn != null) {
61
62 answer.addElement(getSelectKey(introspectedColumn, gk));
63 }
64 }
65
66 StringBuilder sb = new StringBuilder();
67
68 sb.append("insert into ");
69 sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
70 answer.addElement(new TextElement(sb.toString()));
71
72 XmlElement insertElement = new XmlElement("dynamic");
73 insertElement.addAttribute(new Attribute("prepend", "("));
74 answer.addElement(insertElement);
75
76 answer.addElement(new TextElement("values"));
77
78 XmlElement valuesElement = new XmlElement("dynamic");
79 valuesElement.addAttribute(new Attribute("prepend", "("));
80 answer.addElement(valuesElement);
81
82 for (IntrospectedColumn introspectedColumn : introspectedTable
83 .getAllColumns()) {
84 if (introspectedColumn.isIdentity()) {
85
86 continue;
87 }
88
89 XmlElement insertNotNullElement = new XmlElement("isNotNull");
90 insertNotNullElement.addAttribute(new Attribute("prepend", ","));
91 insertNotNullElement.addAttribute(new Attribute(
92 "property", introspectedColumn.getJavaProperty()));
93 insertNotNullElement.addElement(new TextElement(
94 Ibatis2FormattingUtilities
95 .getEscapedColumnName(introspectedColumn)));
96 insertElement.addElement(insertNotNullElement);
97
98 XmlElement valuesNotNullElement = new XmlElement("isNotNull");
99 valuesNotNullElement.addAttribute(new Attribute("prepend", ","));
100 valuesNotNullElement.addAttribute(new Attribute(
101 "property", introspectedColumn.getJavaProperty()));
102 valuesNotNullElement.addElement(new TextElement(
103 Ibatis2FormattingUtilities
104 .getParameterClause(introspectedColumn)));
105 valuesElement.addElement(valuesNotNullElement);
106 }
107
108 insertElement.addElement(new TextElement(")"));
109 valuesElement.addElement(new TextElement(")"));
110
111 if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
112 IntrospectedColumn introspectedColumn = introspectedTable
113 .getColumn(gk.getColumn());
114
115
116 if (introspectedColumn != null) {
117
118 answer.addElement(getSelectKey(introspectedColumn, gk));
119 }
120 }
121
122 if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(
123 answer, introspectedTable)) {
124 parentElement.addElement(answer);
125 }
126 }
127 }