View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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     * @return the number of values for the operand
57     */
58    public final int nbAdditionnalParams() {
59      return nbArgs;
60    }
61  
62    /**
63     * Helper
64     *
65     * @param builder
66     *
67     * @return the associated value
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          // Object is a Object[2]
79          builder.append(key).append(' ').append(BETWEEN).append(" ? AND ? ");
80        } else if (IN.equalsIgnoreCase((operand))) {
81          // Object is a Object[n]
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  }