1
2
3 package org.tentackle.persist.wurblet;
4
5 import org.tentackle.wurblet.*;
6 import org.tentackle.buildsupport.*;
7 import java.util.*;
8 import java.io.*;
9 import org.tentackle.common.*;
10 import org.wurbelizer.wurbel.*;
11 import org.tentackle.sql.*;
12 import org.tentackle.model.*;
13 import org.wurbelizer.wurblet.*;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class DbDeleteBy extends DbModelWurblet {
31
32 @Override
33 public void run() throws WurbelException {
34 super.run();
35 try {
36 wurbel();
37 }
38 catch (Throwable t) {
39 if (t instanceof WurbelException) {
40 throw (WurbelException) t;
41 }
42 throw new WurbelException("wurblet " + this + " failed", t);
43 }
44 }
45
46
47
48 private void wurbel() throws WurbelException, ModelException {
49
50 assertNoOptionalArguments();
51
52 String className = getClassName();
53 String methodName = getMethodName();
54
55 String classVar = null;
56 String append = null;
57 String scope = "public";
58
59 for (String arg: getOptionArgs()) {
60 if (arg.equals("private")) {
61 scope = "private";
62 setRemote(false);
63 }
64 else if (arg.startsWith("append=")) {
65 append = arg.substring(7);
66 }
67 else if (arg.startsWith("classvar=")) {
68 classVar = arg.substring(9);
69 }
70 }
71
72 if (classVar == null) {
73 classVar = "";
74 }
75 else {
76 classVar += ".";
77 }
78
79 String params = buildMethodParameters();
80 String iparms = buildInvocationParameters();
81 String statementId = createStatementId();
82
83 out.print(source[0]);
84 out.print(scope);
85 out.print(source[1]);
86 out.print(methodName);
87 out.print(source[2]);
88 out.print(params);
89 out.print(source[3]);
90 if (isRemote()) {
91
92 RemoteIncludes genInc = new RemoteIncludes(this);
93 PrintStream implOut = genInc.getImplementationStream();
94 PrintStream remoteOut = genInc.getInterfaceStream();
95 out.print(source[4]);
96 remoteOut.print(source[5]);
97 remoteOut.print(methodName);
98 remoteOut.print(source[6]);
99 remoteOut.print(params);
100 remoteOut.print(source[7]);
101 implOut.print(source[8]);
102 implOut.print(methodName);
103 implOut.print(source[9]);
104 implOut.print(params);
105 implOut.print(source[10]);
106 implOut.print(source[11]);
107 implOut.print(methodName);
108 implOut.print(source[12]);
109 implOut.print(iparms);
110 implOut.print(source[13]);
111 out.print(source[14]);
112 out.print(methodName);
113 out.print(source[15]);
114 out.print(iparms);
115 out.print(source[16]);
116 }
117 out.print(source[17]);
118 out.print(classVar);
119 out.print(statementId);
120 out.print(source[18]);
121 if (getEntity().getSuperEntity() != null && getEntity().getHierarchyInheritanceType() == InheritanceType.SINGLE) {
122 out.print(source[19]);
123 }
124
125 boolean andPrepended = false;
126 if (getContextAttribute() != null) {
127 andPrepended = true;
128 out.print(source[20]);
129 out.print(getColumnNameConstant(getContextAttribute(), 0));
130 out.print(source[21]);
131 }
132
133 boolean needParentheses = false;
134
135 if (!getExpressionArguments().isEmpty()) {
136 needParentheses = getExpression().needParenthesesAfterAndOperator();
137 if (andPrepended || needParentheses) {
138 out.print(source[22]);
139 }
140 else {
141 out.print(source[23]);
142 if (needParentheses && getEntity().getHierarchyInheritanceType() == InheritanceType.SINGLE) {
143 out.print(source[24]);
144 }
145 }
146
147
148 String code = getExpression().toCode(new WhereClauseGenerator(this));
149 out.print(StringHelper.trimRight(code, '\n'));
150 out.print(source[25]);
151
152 if (andPrepended || needParentheses) {
153 out.print(source[26]);
154 }
155 else if (needParentheses && getEntity().getHierarchyInheritanceType() == InheritanceType.SINGLE) {
156 out.print(source[27]);
157 }
158
159 if (isWithOptionalArguments()) {
160
161
162 out.print(source[28]);
163 }
164 }
165
166 String orderByStr = createOrderBy();
167 if (orderByStr != null) {
168 out.print(source[29]);
169 out.print(orderByStr);
170 out.print(source[30]);
171 }
172 if (append != null) {
173 out.print(source[31]);
174 out.print(append);
175 out.print(source[32]);
176 }
177 out.print(source[33]);
178 if (isWithJoins()) {
179 out.print(createJoinSetPars());
180 }
181
182 if (getContextAttribute() != null) {
183 out.print(source[34]);
184 out.print(createJdbcSetterName(getContextAttribute().getDataType()));
185 out.print(source[35]);
186 out.print(getContextAttribute().getName());
187 out.print(source[36]);
188 }
189 for (WurbletArgument par: getMethodArguments()) {
190
191 String clauseCondition = par.getExistsArguments() == null ? null :
192 createOptionalCondition(par.getExistsArguments());
193 String indent = clauseCondition == null ? "" : " ";
194 if (par.isPath()) {
195
196 Set<Relation> existsRels = par.getExistsRelations();
197 boolean withValueArgs = false;
198 if (existsRels != null) {
199 for (Relation rel: existsRels) {
200 List<MethodArgument> methodArgs = rel.getMethodArgs();
201 for (MethodArgument methodArg: methodArgs.subList(1, methodArgs.size())) {
202 if (methodArg.isValue()) {
203 withValueArgs = true;
204 }
205 }
206 }
207 }
208 if (withValueArgs) {
209 if (clauseCondition != null) {
210 out.print(source[37]);
211 out.print(clauseCondition);
212 out.print(source[38]);
213 }
214 for (Relation rel: existsRels) {
215 List<MethodArgument> methodArgs = rel.getMethodArgs();
216 for (MethodArgument methodArg: methodArgs.subList(1, methodArgs.size())) {
217 if (methodArg.isValue()) {
218 Attribute attr = methodArg.getForeignAttribute();
219 out.print(source[39]);
220 out.print(indent);
221 out.print(createWhereSetPars(attr, attr.toMethodArgument(methodArg.getValue())));
222 out.print(source[40]);
223 }
224 }
225 }
226 if (clauseCondition != null) {
227 out.print(source[41]);
228 }
229 }
230 }
231
232 if (!par.isValueLiterally()) {
233
234 String optCondition = par.isOptional() ? par.getOptionalName() : null;
235 indent = optCondition == null ? "" : " ";
236 if (optCondition != null) {
237 out.print(source[42]);
238 out.print(optCondition);
239 out.print(source[43]);
240 }
241 out.print(source[44]);
242 out.print(indent);
243 out.print(createWhereSetPars(par));
244 out.print(source[45]);
245 if (optCondition != null) {
246 out.print(source[46]);
247 }
248 }
249 }
250 out.print(source[47]);
251 if (classVar.isEmpty()) {
252 out.print(source[48]);
253 out.print(statementId);
254 out.print(source[49]);
255 }
256 }
257
258
259
260 @Override
261 public String process(String code) {
262 if (isWithOptionalArguments()) {
263
264 return code;
265 }
266 int ref = code.lastIndexOf("ndx");
267 int pos = code.lastIndexOf("(ndx++,");
268 if (pos >= 0 && ref == pos + 1) {
269
270 code = code.substring(0, pos + 4) + code.substring(pos + 6);
271 }
272 else {
273
274 pos = code.lastIndexOf("ndx += st.set(");
275 if (pos >= 0 && ref > pos) {
276 code = code.substring(0, pos) + code.substring(pos + 7);
277 }
278 else {
279 pos = code.lastIndexOf(", ndx++, ");
280 if (pos >= 0 && ref > pos) {
281 code = code.substring(0, pos + 5) + code.substring(pos + 7);
282 }
283 }
284 }
285 return code;
286 }
287 }