1
1
package org .nlpcn .es4sql .parse ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .LinkedList ;
4
5
import java .util .List ;
5
6
19
20
*/
20
21
public class FieldMaker {
21
22
public static Field makeField (SQLExpr expr , String alias ,String tableAlias ) throws SqlParseException {
22
- if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr ) {
23
- String name = expr .toString ().replace ("`" , "" );
24
- if (tableAlias ==null ) return new Field (name , alias );
25
- else if (tableAlias !=null ){
26
- String aliasPrefix = tableAlias + "." ;
27
- if (name .startsWith (aliasPrefix ))
28
- {
29
- name = name .replaceFirst (aliasPrefix ,"" );
30
- return new Field (name , alias );
31
- }
32
- }
33
- return null ;
34
- } else if (expr instanceof SQLQueryExpr ) {
35
- throw new SqlParseException ("unknow field name : " + expr );
36
- } else if (expr instanceof SQLAllColumnExpr ) {
23
+ if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr ) {
24
+ return handleIdentifer (expr , alias , tableAlias );
25
+ } else if (expr instanceof SQLQueryExpr ) {
26
+ throw new SqlParseException ("unknow field name : " + expr );
27
+ } else if (expr instanceof SQLBinaryOpExpr ) {
28
+ //make a SCRIPT method field;
29
+ SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr ) expr ;
30
+ List <SQLExpr > params = new ArrayList <>();
31
+
32
+ String scriptFieldAlias ;
33
+ if (alias == null || alias .equals ("" ))
34
+ scriptFieldAlias = binaryExpr .toString ();
35
+ else
36
+ scriptFieldAlias = alias ;
37
+ params .add (new SQLCharExpr (scriptFieldAlias ));
38
+
39
+ Object left = getScriptValue (binaryExpr .getLeft ());
40
+ Object right = getScriptValue (binaryExpr .getRight ());
41
+ String script = String .format ("%s %s %s" , left ,binaryExpr .getOperator ().getName () , right );
42
+
43
+ params .add (new SQLCharExpr (script ));
44
+
45
+ return makeMethodField ("script" ,params ,null ,null );
46
+
47
+ } else if (expr instanceof SQLAllColumnExpr ) {
37
48
} else if (expr instanceof SQLMethodInvokeExpr ) {
38
49
SQLMethodInvokeExpr mExpr = (SQLMethodInvokeExpr ) expr ;
39
50
return makeMethodField (mExpr .getMethodName (), mExpr .getParameters (), null , alias );
@@ -46,7 +57,31 @@ else if(tableAlias!=null){
46
57
return null ;
47
58
}
48
59
49
- private static MethodField makeMethodField (String name , List <SQLExpr > arguments , SQLAggregateOption option , String alias ) throws SqlParseException {
60
+ private static Object getScriptValue (SQLExpr expr ) throws SqlParseException {
61
+ if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr ) {
62
+ return "doc['" + expr .toString () + "'].value" ;
63
+ }
64
+ else if (expr instanceof SQLValuableExpr ){
65
+ return ((SQLValuableExpr )expr ).getValue ();
66
+ }
67
+ throw new SqlParseException ("could not parse sqlBinaryOpExpr need to be identifier/valuable got" + expr .getClass ().getTypeName () + " with value:" +expr .toString () );
68
+ }
69
+
70
+ private static Field handleIdentifer (SQLExpr expr , String alias , String tableAlias ) {
71
+ String name = expr .toString ().replace ("`" , "" );
72
+ if (tableAlias ==null ) return new Field (name , alias );
73
+ else if (tableAlias !=null ){
74
+ String aliasPrefix = tableAlias + "." ;
75
+ if (name .startsWith (aliasPrefix ))
76
+ {
77
+ name = name .replaceFirst (aliasPrefix ,"" );
78
+ return new Field (name , alias );
79
+ }
80
+ }
81
+ return null ;
82
+ }
83
+
84
+ private static MethodField makeMethodField (String name , List <SQLExpr > arguments , SQLAggregateOption option , String alias ) throws SqlParseException {
50
85
List <KVValue > paramers = new LinkedList <>();
51
86
for (SQLExpr object : arguments ) {
52
87
if (object instanceof SQLBinaryOpExpr ) {
0 commit comments