1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.mybatis3;
17
18 import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
19 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20
21 import org.mybatis.generator.api.IntrospectedColumn;
22
23 public class MyBatis3FormattingUtilities {
24
25 private MyBatis3FormattingUtilities() {
26 super();
27 }
28
29 public static String getParameterClause(IntrospectedColumn introspectedColumn) {
30 return getParameterClause(introspectedColumn, null);
31 }
32
33 public static String getParameterClause(IntrospectedColumn introspectedColumn, String prefix) {
34 StringBuilder sb = new StringBuilder();
35
36 sb.append("#{");
37 sb.append(introspectedColumn.getJavaProperty(prefix));
38 sb.append(",jdbcType=");
39 sb.append(introspectedColumn.getJdbcTypeName());
40
41 if (stringHasValue(introspectedColumn.getTypeHandler())) {
42 sb.append(",typeHandler=");
43 sb.append(introspectedColumn.getTypeHandler());
44 }
45
46 sb.append('}');
47
48 return sb.toString();
49 }
50
51
52
53
54
55
56
57
58
59
60 public static String getSelectListPhrase(IntrospectedColumn introspectedColumn) {
61 if (stringHasValue(introspectedColumn.getTableAlias())) {
62 StringBuilder sb = new StringBuilder();
63
64 sb.append(getAliasedEscapedColumnName(introspectedColumn));
65 sb.append(" as ");
66 if (introspectedColumn.isColumnNameDelimited()) {
67 sb.append(introspectedColumn.getContext().getBeginningDelimiter());
68 }
69 sb.append(introspectedColumn.getTableAlias());
70 sb.append('_');
71 sb.append(introspectedColumn.getActualColumnName());
72 if (introspectedColumn.isColumnNameDelimited()) {
73 sb.append(introspectedColumn.getContext().getEndingDelimiter());
74 }
75 return sb.toString();
76 } else {
77 return getEscapedColumnName(introspectedColumn);
78 }
79 }
80
81 public static String getEscapedColumnName(IntrospectedColumn introspectedColumn) {
82 StringBuilder sb = new StringBuilder();
83 sb.append(introspectedColumn.getActualColumnName());
84
85 if (introspectedColumn.isColumnNameDelimited()) {
86 sb.insert(0, introspectedColumn.getContext().getBeginningDelimiter());
87 sb.append(introspectedColumn.getContext().getEndingDelimiter());
88 }
89
90 return sb.toString();
91 }
92
93 public static String getAliasedEscapedColumnName(IntrospectedColumn introspectedColumn) {
94 if (stringHasValue(introspectedColumn.getTableAlias())) {
95 return introspectedColumn.getTableAlias() + '.' + getEscapedColumnName(introspectedColumn);
96 } else {
97 return getEscapedColumnName(introspectedColumn);
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static String getAliasedActualColumnName(IntrospectedColumn introspectedColumn) {
115 StringBuilder sb = new StringBuilder();
116 if (stringHasValue(introspectedColumn.getTableAlias())) {
117 sb.append(introspectedColumn.getTableAlias());
118 sb.append('.');
119 }
120
121 if (introspectedColumn.isColumnNameDelimited()) {
122 sb.append(escapeStringForJava(introspectedColumn.getContext().getBeginningDelimiter()));
123 }
124
125 sb.append(introspectedColumn.getActualColumnName());
126
127 if (introspectedColumn.isColumnNameDelimited()) {
128 sb.append(escapeStringForJava(introspectedColumn.getContext().getEndingDelimiter()));
129 }
130
131 return sb.toString();
132 }
133
134
135
136
137
138
139
140
141
142
143 public static String getRenamedColumnNameForResultMap(IntrospectedColumn introspectedColumn) {
144 if (stringHasValue(introspectedColumn.getTableAlias())) {
145 return introspectedColumn.getTableAlias() + '_' + introspectedColumn.getActualColumnName();
146 } else {
147 return introspectedColumn.getActualColumnName();
148 }
149 }
150 }