1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.dao;
22
23 public class Filter {
24 public static final String LIKE = "LIKE";
25 public static final String BETWEEN = "BETWEEN";
26 public static final String IS_NOT_NULL = "IS NOT NULL";
27 public static final String IN = "IN";
28 public static final String SPECIFIC_SQL = "SPEC_SQL";
29 private static final byte B_NONE = 'n';
30 private static final byte B_SINGLE = 's';
31 private static final byte B_MULTIPLE = 'm';
32 public final String key;
33 public final String operand;
34 public final Object value;
35 private final int nbArgs;
36 private final byte specialOperand;
37
38 public Filter(final String key, final String operand,
39 final Object... values) {
40 this.key = key;
41 this.operand = operand;
42 nbArgs = values == null? 0 : values.length;
43 if (nbArgs == 0) {
44 this.value = null;
45 specialOperand = B_NONE;
46 } else if (nbArgs == 1) {
47 this.value = values[0];
48 specialOperand = B_SINGLE;
49 } else {
50 this.value = values;
51 specialOperand = B_MULTIPLE;
52 }
53 }
54
55
56
57
58 public final int nbAdditionnalParams() {
59 return nbArgs;
60 }
61
62
63
64
65
66
67
68
69 public final Object append(final StringBuilder builder) {
70 if (SPECIFIC_SQL.equalsIgnoreCase(operand)) {
71 builder.append(key);
72 } else if (nbArgs == 0) {
73 builder.append(key).append(' ').append(operand).append(" ");
74 } else if (nbArgs == 1) {
75 builder.append(key).append(' ').append(operand).append(" ? ");
76 } else {
77 if (BETWEEN.equalsIgnoreCase(operand)) {
78
79 builder.append(key).append(' ').append(BETWEEN).append(" ? AND ? ");
80 } else if (IN.equalsIgnoreCase((operand))) {
81
82 builder.append(key).append(' ').append(IN).append("(?");
83 for (int i = 1; i < nbArgs; i++) {
84 builder.append(", ?");
85 }
86 builder.append(") ");
87 } else {
88 throw new IllegalArgumentException(
89 "Command seems to not support multiple arguments: " + operand);
90 }
91 }
92 return value;
93 }
94 }