/
BaseQueryBuilder.java
108 lines (88 loc) · 3.47 KB
/
BaseQueryBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.communication.column;
import static java.util.Arrays.asList;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
abstract class BaseQueryBuilder {
protected String name;
protected boolean negate;
protected boolean and;
protected ColumnCondition condition;
protected <T> void eqImpl(T value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.eq(Column.of(name, value));
appendCondition(newCondition);
}
protected <T> void gtImpl(T value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.gt(Column.of(name, value));
appendCondition(newCondition);
}
protected void likeImpl(String value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.like(Column.of(name, value));
appendCondition(newCondition);
}
protected <T> void ltImpl(T value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.lt(Column.of(name, value));
appendCondition(newCondition);
}
protected <T> void lteImpl(T value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.lte(Column.of(name, value));
appendCondition(newCondition);
}
protected <T> void gteImpl(T value) {
requireNonNull(value, "value is required");
ColumnCondition newCondition = ColumnCondition.gte(Column.of(name, value));
appendCondition(newCondition);
}
protected <T> void betweenImpl(T valueA, T valueB) {
requireNonNull(valueA, "valueA is required");
requireNonNull(valueB, "valueB is required");
ColumnCondition newCondition = ColumnCondition.between(Column.of(name, asList(valueA, valueB)));
appendCondition(newCondition);
}
protected <T> void inImpl(Iterable<T> values) {
requireNonNull(values, "values is required");
ColumnCondition newCondition = ColumnCondition.in(Column.of(name, values));
appendCondition(newCondition);
}
protected void appendCondition(ColumnCondition newCondition) {
ColumnCondition columnCondition = getColumnCondition(newCondition);
if (nonNull(condition)) {
if (and) {
this.condition = condition.and(columnCondition);
} else {
this.condition = condition.or(columnCondition);
}
} else {
this.condition = columnCondition;
}
this.negate = false;
this.name = null;
}
private ColumnCondition getColumnCondition(ColumnCondition newCondition) {
if (negate) {
return newCondition.negate();
} else {
return newCondition;
}
}
}