1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.contrib.jdbc; |
16 |
| |
17 |
| import java.sql.Connection; |
18 |
| import java.sql.SQLException; |
19 |
| import java.sql.Timestamp; |
20 |
| import java.util.ArrayList; |
21 |
| import java.util.Calendar; |
22 |
| import java.util.Date; |
23 |
| import java.util.List; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| public class StatementAssembly |
54 |
| { |
55 |
| private StringBuffer _buffer = new StringBuffer(); |
56 |
| |
57 |
| private static final String NULL = "NULL"; |
58 |
| |
59 |
| public static final String SEP = ", "; |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| private List _parameters; |
67 |
| |
68 |
| private int _lineLength; |
69 |
| private int _maxLineLength = 80; |
70 |
| private int _indent = 5; |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
0
| public StatementAssembly()
|
78 |
| { |
79 |
| } |
80 |
| |
81 |
0
| public StatementAssembly(int maxLineLength, int indent)
|
82 |
| { |
83 |
0
| _maxLineLength = maxLineLength;
|
84 |
0
| _indent = indent;
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
0
| public void clear()
|
95 |
| { |
96 |
0
| _buffer.setLength(0);
|
97 |
0
| _lineLength = 0;
|
98 |
| |
99 |
0
| if (_parameters != null)
|
100 |
0
| _parameters.clear();
|
101 |
| } |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
0
| public int getMaxLineLength()
|
109 |
| { |
110 |
0
| return _maxLineLength;
|
111 |
| } |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
0
| public int getIndent()
|
119 |
| { |
120 |
0
| return _indent;
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
0
| public void add(String text)
|
134 |
| { |
135 |
0
| int textLength;
|
136 |
| |
137 |
0
| textLength = text.length();
|
138 |
| |
139 |
0
| if (_lineLength + textLength > _maxLineLength)
|
140 |
| { |
141 |
0
| _buffer.append('\n');
|
142 |
| |
143 |
0
| for (int i = 0; i < _indent; i++)
|
144 |
0
| _buffer.append(' ');
|
145 |
| |
146 |
0
| _lineLength = _indent;
|
147 |
| } |
148 |
| |
149 |
0
| _buffer.append(text);
|
150 |
0
| _lineLength += textLength;
|
151 |
| } |
152 |
| |
153 |
0
| public void add(short value)
|
154 |
| { |
155 |
0
| add(Short.toString(value));
|
156 |
| } |
157 |
| |
158 |
0
| public void add(int value)
|
159 |
| { |
160 |
0
| add(Integer.toString(value));
|
161 |
| } |
162 |
| |
163 |
0
| public void add(long value)
|
164 |
| { |
165 |
0
| add(Long.toString(value));
|
166 |
| } |
167 |
| |
168 |
0
| public void add(float value)
|
169 |
| { |
170 |
0
| add(Float.toString(value));
|
171 |
| } |
172 |
| |
173 |
0
| public void add(double value)
|
174 |
| { |
175 |
0
| add(Double.toString(value));
|
176 |
| } |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
0
| public void addParameter(Date date)
|
185 |
| { |
186 |
0
| if (date == null)
|
187 |
| { |
188 |
0
| add("NULL");
|
189 |
0
| return;
|
190 |
| } |
191 |
| |
192 |
0
| Calendar calendar = Calendar.getInstance();
|
193 |
| |
194 |
0
| calendar.setTime(date);
|
195 |
0
| calendar.set(Calendar.MILLISECOND, 0);
|
196 |
| |
197 |
0
| Date adjusted = calendar.getTime();
|
198 |
| |
199 |
0
| Timestamp timestamp = new Timestamp(adjusted.getTime());
|
200 |
| |
201 |
0
| addParameter(timestamp);
|
202 |
| } |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
0
| public void addSep(String text)
|
212 |
| { |
213 |
0
| _buffer.append(text);
|
214 |
0
| _lineLength += text.length();
|
215 |
| } |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
0
| public void newLine()
|
223 |
| { |
224 |
0
| if (_buffer.length() != 0)
|
225 |
0
| _buffer.append('\n');
|
226 |
| |
227 |
0
| _lineLength = 0;
|
228 |
| } |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
0
| public void newLine(String text)
|
236 |
| { |
237 |
0
| if (_buffer.length() != 0)
|
238 |
0
| _buffer.append('\n');
|
239 |
| |
240 |
0
| _buffer.append(text);
|
241 |
| |
242 |
0
| _lineLength = text.length();
|
243 |
| } |
244 |
| |
245 |
0
| public void addList(String[] items, String separator)
|
246 |
| { |
247 |
0
| for (int i = 0; i < items.length; i++)
|
248 |
| { |
249 |
0
| if (i > 0)
|
250 |
0
| addSep(separator);
|
251 |
| |
252 |
0
| add(items[i]);
|
253 |
| } |
254 |
| } |
255 |
| |
256 |
0
| public void addParameterList(int[] items, String separator)
|
257 |
| { |
258 |
0
| for (int i = 0; i < items.length; i++)
|
259 |
| { |
260 |
0
| if (i > 0)
|
261 |
0
| addSep(separator);
|
262 |
| |
263 |
0
| addParameter(items[i]);
|
264 |
| } |
265 |
| } |
266 |
| |
267 |
0
| public void addParameterList(Integer[] items, String separator)
|
268 |
| { |
269 |
0
| for (int i = 0; i < items.length; i++)
|
270 |
| { |
271 |
0
| if (i > 0)
|
272 |
0
| addSep(separator);
|
273 |
| |
274 |
0
| addParameter(items[i]);
|
275 |
| } |
276 |
| } |
277 |
| |
278 |
0
| public void addParameterList(long[] items, String separator)
|
279 |
| { |
280 |
0
| for (int i = 0; i < items.length; i++)
|
281 |
| { |
282 |
0
| if (i > 0)
|
283 |
0
| addSep(separator);
|
284 |
| |
285 |
0
| addParameter(items[i]);
|
286 |
| } |
287 |
| } |
288 |
| |
289 |
0
| public void addParameterList(Long[] items, String separator)
|
290 |
| { |
291 |
0
| for (int i = 0; i < items.length; i++)
|
292 |
| { |
293 |
0
| if (i > 0)
|
294 |
0
| addSep(separator);
|
295 |
| |
296 |
0
| addParameter(items[i]);
|
297 |
| } |
298 |
| } |
299 |
| |
300 |
0
| public void addParameterList(String[] items, String separator)
|
301 |
| { |
302 |
0
| for (int i = 0; i < items.length; i++)
|
303 |
| { |
304 |
0
| if (i > 0)
|
305 |
0
| addSep(separator);
|
306 |
| |
307 |
0
| addParameter(items[i]);
|
308 |
| } |
309 |
| } |
310 |
| |
311 |
0
| public void addParameterList(double[] items, String separator)
|
312 |
| { |
313 |
0
| for (int i = 0; i < items.length; i++)
|
314 |
| { |
315 |
0
| if (i > 0)
|
316 |
0
| addSep(separator);
|
317 |
| |
318 |
0
| addParameter(items[i]);
|
319 |
| } |
320 |
| } |
321 |
| |
322 |
0
| public void addParameter(Object value)
|
323 |
| { |
324 |
0
| if (value == null)
|
325 |
0
| add(NULL);
|
326 |
| else |
327 |
0
| addParameter(new ObjectParameter(value));
|
328 |
| } |
329 |
| |
330 |
0
| public void addParameter(Timestamp timestamp)
|
331 |
| { |
332 |
0
| if (timestamp == null)
|
333 |
0
| add(NULL);
|
334 |
| else |
335 |
0
| addParameter(new TimestampParameter(timestamp));
|
336 |
| } |
337 |
| |
338 |
0
| public void addParameter(String value)
|
339 |
| { |
340 |
0
| if (value == null)
|
341 |
0
| add(NULL);
|
342 |
| else |
343 |
0
| addParameter(new StringParameter(value));
|
344 |
| } |
345 |
| |
346 |
0
| public void addParameter(int value)
|
347 |
| { |
348 |
0
| addParameter(new IntegerParameter(value));
|
349 |
| } |
350 |
| |
351 |
0
| public void addParameter(Integer value)
|
352 |
| { |
353 |
0
| if (value == null)
|
354 |
0
| add(NULL);
|
355 |
| else |
356 |
0
| addParameter(value.intValue());
|
357 |
| } |
358 |
| |
359 |
0
| public void addParameter(long value)
|
360 |
| { |
361 |
0
| addParameter(new LongParameter(value));
|
362 |
| } |
363 |
| |
364 |
0
| public void addParameter(Long value)
|
365 |
| { |
366 |
0
| if (value == null)
|
367 |
0
| add(NULL);
|
368 |
| else |
369 |
0
| addParameter(value.longValue());
|
370 |
| } |
371 |
| |
372 |
0
| public void addParameter(float value)
|
373 |
| { |
374 |
0
| addParameter(new FloatParameter(value));
|
375 |
| } |
376 |
| |
377 |
0
| public void addParameter(Float value)
|
378 |
| { |
379 |
0
| if (value == null)
|
380 |
0
| add(NULL);
|
381 |
| else |
382 |
0
| addParameter(value.floatValue());
|
383 |
| } |
384 |
| |
385 |
0
| public void addParameter(double value)
|
386 |
| { |
387 |
0
| addParameter(new DoubleParameter(value));
|
388 |
| } |
389 |
| |
390 |
0
| public void addParameter(Double value)
|
391 |
| { |
392 |
0
| if (value == null)
|
393 |
0
| add(NULL);
|
394 |
| else |
395 |
0
| addParameter(value.doubleValue());
|
396 |
| } |
397 |
| |
398 |
0
| public void addParameter(short value)
|
399 |
| { |
400 |
0
| addParameter(new ShortParameter(value));
|
401 |
| } |
402 |
| |
403 |
0
| public void addParameter(Short value)
|
404 |
| { |
405 |
0
| if (value == null)
|
406 |
0
| add(NULL);
|
407 |
| else |
408 |
0
| addParameter(value.shortValue());
|
409 |
| } |
410 |
| |
411 |
0
| public void addParameter(boolean value)
|
412 |
| { |
413 |
0
| addParameter(value ? BooleanParameter.TRUE : BooleanParameter.FALSE);
|
414 |
| } |
415 |
| |
416 |
0
| public void addParameter(Boolean value)
|
417 |
| { |
418 |
0
| if (value == null)
|
419 |
0
| add(NULL);
|
420 |
| else |
421 |
0
| addParameter(value.booleanValue());
|
422 |
| } |
423 |
| |
424 |
0
| private void addParameter(IParameter parameter)
|
425 |
| { |
426 |
0
| if (_parameters == null)
|
427 |
0
| _parameters = new ArrayList();
|
428 |
| |
429 |
0
| _parameters.add(parameter);
|
430 |
| |
431 |
0
| add("?");
|
432 |
| } |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
0
| public IStatement createStatement(Connection connection) throws SQLException
|
441 |
| { |
442 |
0
| String sql = _buffer.toString();
|
443 |
| |
444 |
0
| if (_parameters == null || _parameters.isEmpty())
|
445 |
0
| return new SimpleStatement(sql, connection);
|
446 |
| |
447 |
0
| return new ParameterizedStatement(sql, connection, _parameters);
|
448 |
| } |
449 |
| |
450 |
0
| public String toString()
|
451 |
| { |
452 |
0
| StringBuffer buffer = new StringBuffer("StatementAssembly@");
|
453 |
| |
454 |
0
| buffer.append(Integer.toHexString(hashCode()));
|
455 |
0
| buffer.append("[SQL=\n<");
|
456 |
0
| buffer.append(_buffer);
|
457 |
0
| buffer.append("\n>");
|
458 |
| |
459 |
0
| if (_parameters != null)
|
460 |
| { |
461 |
0
| int count = _parameters.size();
|
462 |
0
| for (int i = 0; i < count; i++)
|
463 |
| { |
464 |
0
| Object parameter = _parameters.get(i);
|
465 |
| |
466 |
0
| buffer.append(" ?");
|
467 |
0
| buffer.append(i + 1);
|
468 |
0
| buffer.append('=');
|
469 |
| |
470 |
0
| buffer.append(parameter);
|
471 |
| } |
472 |
| } |
473 |
| |
474 |
0
| buffer.append(']');
|
475 |
| |
476 |
0
| return buffer.toString();
|
477 |
| } |
478 |
| } |