Skip to content

Commit d8af909

Browse files
committed
Add children support to conditions.
1 parent d09ebc6 commit d8af909

File tree

6 files changed

+344
-54
lines changed

6 files changed

+344
-54
lines changed

src/main/java/org/nlpcn/es4sql/domain/Condition.java

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
import com.google.common.collect.BiMap;
88
import com.google.common.collect.HashBiMap;
9+
910
import org.nlpcn.es4sql.exception.SqlParseException;
11+
import org.nlpcn.es4sql.parse.ChildrenType;
12+
import org.nlpcn.es4sql.parse.NestedType;
1013

1114
/**
1215
* 过滤条件
@@ -16,7 +19,7 @@
1619
public class Condition extends Where {
1720

1821
public enum OPEAR {
19-
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , TERM , IDS_QUERY,NESTED_COMPLEX , SCRIPT;
22+
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN, BETWEEN, NBETWEEN, GEO_INTERSECTS, GEO_BOUNDING_BOX, GEO_DISTANCE, GEO_DISTANCE_RANGE, GEO_POLYGON, GEO_CELL, IN_TERMS, TERM, IDS_QUERY, NESTED_COMPLEX, CHILDREN_COMPLEX, SCRIPT;
2023

2124
public static Map<String,OPEAR> methodNameToOpear;
2225

@@ -59,44 +62,85 @@ public OPEAR negative() throws SqlParseException {
5962

6063
private OPEAR opear;
6164

62-
private boolean isNested;
65+
private Object relationshipType;
6366

67+
private boolean isNested;
6468
private String nestedPath;
6569

70+
private boolean isChildren;
71+
private String childType;
72+
6673
public Condition(CONN conn, String field, String condition, Object obj) throws SqlParseException {
67-
this(conn, field, condition, obj, false, null);
74+
this(conn, field, condition, obj, null);
6875
}
6976
public Condition(CONN conn, String field, OPEAR condition, Object obj) throws SqlParseException {
70-
this(conn, field, condition, obj, false, null);
77+
this(conn, field, condition, obj, null);
7178
}
7279

73-
public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNested , String nestedPath) throws SqlParseException {
80+
public Condition(CONN conn, String name, OPEAR oper, Object value, Object relationshipType) throws SqlParseException {
7481
super(conn);
75-
this.opear = null;
7682

83+
this.opear = null;
7784
this.name = name;
78-
7985
this.value = value;
80-
8186
this.opear = oper ;
82-
83-
this.isNested = isNested;
84-
85-
this.nestedPath = nestedPath;
87+
this.relationshipType = relationshipType;
88+
89+
if(this.relationshipType != null) {
90+
if(this.relationshipType instanceof NestedType) {
91+
NestedType nestedType = (NestedType)relationshipType;
92+
93+
this.isNested = true;
94+
this.nestedPath = nestedType.path;
95+
this.isChildren = false;
96+
this.childType = "";
97+
} else if(relationshipType instanceof ChildrenType) {
98+
ChildrenType childrenType = (ChildrenType)relationshipType;
99+
100+
this.isNested = false;
101+
this.nestedPath = "";
102+
this.isChildren = true;
103+
this.childType = childrenType.childType;
104+
}
105+
} else {
106+
this.isNested = false;
107+
this.nestedPath = "";
108+
this.isChildren = false;
109+
this.childType = "";
110+
}
86111
}
87112

88-
public Condition(CONN conn, String name, String oper, Object value,boolean isNested,String nestedPath) throws SqlParseException {
113+
public Condition(CONN conn, String name, String oper, Object value, Object relationshipType) throws SqlParseException {
89114
super(conn);
90115

91-
this.isNested = isNested;
116+
this.opear = null;
117+
this.name = name;
118+
this.value = value;
92119

93-
this.nestedPath = nestedPath;
120+
this.relationshipType = relationshipType;
94121

95-
this.opear = null;
122+
if(this.relationshipType != null) {
123+
if(this.relationshipType instanceof NestedType) {
124+
NestedType nestedType = (NestedType)relationshipType;
96125

97-
this.name = name;
126+
this.isNested = true;
127+
this.nestedPath = nestedType.path;
128+
this.isChildren = false;
129+
this.childType = "";
130+
} else if(relationshipType instanceof ChildrenType) {
131+
ChildrenType childrenType = (ChildrenType)relationshipType;
98132

99-
this.value = value;
133+
this.isNested = false;
134+
this.nestedPath = "";
135+
this.isChildren = true;
136+
this.childType = childrenType.childType;
137+
}
138+
} else {
139+
this.isNested = false;
140+
this.nestedPath = "";
141+
this.isChildren = false;
142+
this.childType = "";
143+
}
100144

101145
// EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN
102146
switch (oper) {
@@ -166,6 +210,9 @@ public Condition(CONN conn, String name, String oper, Object value,boolean isNes
166210
case "NESTED":
167211
this.opear = OPEAR.NESTED_COMPLEX;
168212
break;
213+
case "CHILDREN":
214+
this.opear = OPEAR.CHILDREN_COMPLEX;
215+
break;
169216
case "SCRIPT":
170217
this.opear = OPEAR.SCRIPT;
171218
break;
@@ -198,6 +245,14 @@ public void setOpear(OPEAR opear) {
198245
this.opear = opear;
199246
}
200247

248+
public Object getRelationshipType() {
249+
return relationshipType;
250+
}
251+
252+
public void setRelationshipType(Object relationshipType) {
253+
this.relationshipType = relationshipType;
254+
}
255+
201256
public boolean isNested() {
202257
return isNested;
203258
}
@@ -214,27 +269,52 @@ public void setNestedPath(String nestedPath) {
214269
this.nestedPath = nestedPath;
215270
}
216271

272+
public boolean isChildren() {
273+
return isChildren;
274+
}
275+
276+
public void setChildren(boolean isChildren) {
277+
this.isChildren = isChildren;
278+
}
279+
280+
public String getChildType() {
281+
return childType;
282+
}
283+
284+
public void setChildType(String childType) {
285+
this.childType = childType;
286+
}
287+
217288
@Override
218289
public String toString() {
219290
String result = "";
291+
220292
if(this.isNested()){
221293
result = "nested condition ";
222294
if(this.getNestedPath()!=null){
223295
result+="on path:" + this.getNestedPath() + " ";
224296
}
297+
} else if(this.isChildren()) {
298+
result = "children condition ";
299+
300+
if(this.getChildType() != null){
301+
result+="on child: " + this.getChildType() + " ";
302+
}
225303
}
304+
226305
if (value instanceof Object[]) {
227306
result += this.conn + " " + this.name + " " + this.opear + " " + Arrays.toString((Object[]) value);
228307
} else {
229308
result += this.conn + " " + this.name + " " + this.opear + " " + this.value;
230309
}
310+
231311
return result;
232312
}
233313

234314
@Override
235315
public Object clone() throws CloneNotSupportedException {
236316
try {
237-
Condition clonedCondition = new Condition(this.getConn(),this.getName(),this.getOpear(),this.getValue(),this.isNested(),this.getNestedPath());
317+
Condition clonedCondition = new Condition(this.getConn(), this.getName(), this.getOpear(), this.getValue(), this.getRelationshipType());
238318
return clonedCondition;
239319
} catch (SqlParseException e) {
240320

src/main/java/org/nlpcn/es4sql/domain/Field.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.nlpcn.es4sql.domain;
22

3+
import org.nlpcn.es4sql.parse.ChildrenType;
34
import org.nlpcn.es4sql.parse.NestedType;
45

56
/**
@@ -8,23 +9,25 @@
89
* @author ansj
910
*
1011
*/
11-
public class Field implements Cloneable{
12+
public class Field implements Cloneable{
1213

1314
protected String name;
1415
private String alias;
1516
private NestedType nested;
16-
17+
private ChildrenType children;
1718

1819
public Field(String name, String alias) {
1920
this.name = name;
2021
this.alias = alias;
2122
this.nested = null;
23+
this.children = null;
2224
}
2325

24-
public Field(String name, String alias, NestedType nested) {
26+
public Field(String name, String alias, NestedType nested, ChildrenType children) {
2527
this.name = name;
2628
this.alias = alias;
2729
this.nested = nested;
30+
this.children = children;
2831
}
2932

3033
public String getName() {
@@ -44,22 +47,34 @@ public void setAlias(String alias) {
4447
}
4548

4649
public boolean isNested() {
47-
return this.nested!=null;
50+
return this.nested != null;
4851
}
52+
4953
public boolean isReverseNested() {
50-
return this.nested!=null && this.nested.isReverse();
54+
return this.nested != null && this.nested.isReverse();
5155
}
5256

5357
public void setNested(NestedType nested){
5458
this.nested = nested;
5559
}
5660

57-
5861
public String getNestedPath() {
5962
if(this.nested == null ) return null;
6063
return this.nested.path;
6164
}
6265

66+
public boolean isChildren() {
67+
return this.children != null;
68+
}
69+
70+
public void setChildren(ChildrenType children){
71+
this.children = children;
72+
}
73+
74+
public String getChildType() {
75+
if(this.children == null ) return null;
76+
return this.children.childType;
77+
}
6378

6479
@Override
6580
public String toString() {
@@ -80,6 +95,6 @@ public boolean equals(Object obj) {
8095

8196
@Override
8297
protected Object clone() throws CloneNotSupportedException {
83-
return new Field(new String(this.name),new String(this.alias));
98+
return new Field(new String(this.name), new String(this.alias));
8499
}
85100
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.nlpcn.es4sql.parse;
2+
3+
import java.util.List;
4+
5+
import org.nlpcn.es4sql.Util;
6+
import org.nlpcn.es4sql.domain.Where;
7+
import org.nlpcn.es4sql.exception.SqlParseException;
8+
9+
import com.alibaba.druid.sql.ast.SQLExpr;
10+
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
11+
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
12+
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
13+
import com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr;
14+
15+
/**
16+
* Created by Razma Tazz on 14/04/2016.
17+
*/
18+
public class ChildrenType {
19+
public String field;
20+
public String childType;
21+
public Where where;
22+
private boolean simple;
23+
24+
public boolean tryFillFromExpr(SQLExpr expr) throws SqlParseException {
25+
if (!(expr instanceof SQLMethodInvokeExpr)) return false;
26+
SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) expr;
27+
28+
String methodName = method.getMethodName();
29+
30+
if (!methodName.toLowerCase().equals("children")) return false;
31+
32+
List<SQLExpr> parameters = method.getParameters();
33+
34+
if (parameters.size() != 2)
35+
throw new SqlParseException("on children object only allowed 2 parameters (type, field)/(type, conditions...) ");
36+
37+
String type = Util.extendedToString(parameters.get(0));
38+
this.childType = type;
39+
40+
SQLExpr secondParameter = parameters.get(1);
41+
if(secondParameter instanceof SQLTextLiteralExpr || secondParameter instanceof SQLIdentifierExpr || secondParameter instanceof SQLPropertyExpr) {
42+
this.field = Util.extendedToString(secondParameter);
43+
this.simple = true;
44+
} else {
45+
Where where = Where.newInstance();
46+
new SqlParser().parseWhere(secondParameter,where);
47+
if(where.getWheres().size() == 0)
48+
throw new SqlParseException("unable to parse filter where.");
49+
this.where = where;
50+
simple = false;
51+
}
52+
53+
return true;
54+
}
55+
56+
public boolean isSimple() {
57+
return simple;
58+
}
59+
}

0 commit comments

Comments
 (0)