1+ package com .cloudimpl .outstack .repo ;
2+
3+ import com .cloudimpl .rstack .dsl .restql .BinNode ;
4+ import com .cloudimpl .rstack .dsl .restql .ConstArrayNode ;
5+ import com .cloudimpl .rstack .dsl .restql .ConstBooleanArrayNode ;
6+ import com .cloudimpl .rstack .dsl .restql .ConstBooleanNode ;
7+ import com .cloudimpl .rstack .dsl .restql .ConstNode ;
8+ import com .cloudimpl .rstack .dsl .restql .ConstNumberArrayNode ;
9+ import com .cloudimpl .rstack .dsl .restql .ConstNumberNode ;
10+ import com .cloudimpl .rstack .dsl .restql .ConstStringArrayNode ;
11+ import com .cloudimpl .rstack .dsl .restql .ConstStringNode ;
12+ import com .cloudimpl .rstack .dsl .restql .FieldCheckNode ;
13+ import com .cloudimpl .rstack .dsl .restql .OrderByExpNode ;
14+ import com .cloudimpl .rstack .dsl .restql .OrderByNode ;
15+ import com .cloudimpl .rstack .dsl .restql .RelNode ;
16+ import com .cloudimpl .rstack .dsl .restql .RestQLNode ;
17+ import com .cloudimpl .rstack .dsl .restql .RestQLParser ;
18+ import com .google .gson .JsonObject ;
19+
20+ import java .util .stream .Collectors ;
21+
22+ /**
23+ *
24+ * @author nuwan
25+ */
26+ public class PostgresSqlNode implements RestQLNode {
27+
28+ @ Override
29+ public String eval (RestQLNode node ) {
30+ if (node instanceof ConstArrayNode ) {
31+ return ConstArrayNode .class .cast (node ).getVals ().toString ();
32+ } else if (node instanceof ConstStringNode ) {
33+ return String .valueOf (ConstStringNode .class .cast (node ).getVal ());
34+ } else if (node instanceof ConstNumberNode ) {
35+ return String .valueOf (ConstNumberNode .class .cast (node ).getVal ());
36+ } else if (node instanceof ConstBooleanNode ) {
37+ return String .valueOf (ConstBooleanNode .class .cast (node ).getVal ());
38+ } else if (node instanceof RelNode ) {
39+ RelNode rel = RelNode .class .cast (node );
40+ if (rel .getOp () == RelNode .Op .IN ){
41+ return this .inOperator (rel );
42+ } else {
43+ return castToType (convertToJsonField (rel .getFieldName ()), rel .getConstNode ()) + (rel .getOp () == RelNode .Op .LIKE ? " ILIKE " :(rel .getOp () == RelNode .Op .NOT_LIKE ?" NOT ILIKE " :rel .getOp ().getOp ())) + (String ) rel .getConstNode ().eval (this );
44+ }
45+ }else if (node instanceof FieldCheckNode ) {
46+ FieldCheckNode fieldNode = FieldCheckNode .class .cast (node );
47+ return convertToJsonField (fieldNode .getFieldName ()) + (fieldNode .isCheckExist () ? " is not null " : " is null " );
48+ } else if (node instanceof BinNode ) {
49+ BinNode binNode = BinNode .class .cast (node );
50+ return "(" + binNode .getLeft ().eval (this ) + binNode .getOp ().getOp () + binNode .getRight ().eval (this ) + ")" ;
51+ }else if (node instanceof OrderByNode ) {
52+ OrderByNode orderBy = OrderByNode .class .cast (node );
53+ return castToType (convertToJsonField (orderBy .getFieldName ()),orderBy .getDataType () )+ " " + orderBy .getOrder ();
54+ }else if (node instanceof OrderByExpNode )
55+ {
56+ OrderByExpNode expNode = OrderByExpNode .class .cast (node );
57+ return expNode .getOrderByList ().stream ().map (i ->(String )i .eval (this )).collect (Collectors .joining ("," ));
58+ }
59+ throw new RuntimeException ("unknown node :" + node .getClass ().getName ());
60+ }
61+
62+ @ Override
63+ public JsonObject toJson () {
64+ throw new UnsupportedOperationException ("Not supported yet." ); //To change body of generated methods, choose Tools | Templates.
65+ }
66+
67+ private String inOperator (RelNode relNode ){
68+ ConstNode constNode = relNode .getConstNode ();
69+ if (constNode instanceof ConstStringArrayNode ){
70+ return convertToJsonField ( relNode .getFieldName () ) + " IN (" + ConstStringArrayNode .class .cast (constNode ).getVals ().stream ().collect (Collectors .joining ("," )) + ")" ;
71+ } else if (constNode instanceof ConstNumberArrayNode ){
72+ return convertToJsonField ( relNode .getFieldName () ) + " IN (" + ConstNumberArrayNode .class .cast (constNode ).getVals ().stream ().map (v ->v .toString ()).collect (Collectors .joining ("," )) + ")" ;
73+ } else if (constNode instanceof ConstBooleanArrayNode ){
74+ return convertToJsonField ( relNode .getFieldName () ) + " IN (" + ConstBooleanArrayNode .class .cast (constNode ).getVals ().stream ().map (v ->v .toString ()).collect (Collectors .joining ("," )) + ")" ;
75+ } else {
76+ throw new RuntimeException ("unknown const array node :" + constNode .getClass ().getName ());
77+ }
78+ }
79+
80+ public static String convertToJsonField (String field ) {
81+ field = field .trim ();
82+ if (field .startsWith ("_" ))
83+ {
84+ return field .substring (1 );
85+ }
86+
87+ String [] fields = field .split ("\\ ." );
88+ if (fields .length == 1 ) {
89+ return "json->>'" + field + "'" ;
90+ } else if (fields .length == 2 ) {
91+ return "json->'" + fields [0 ] + "'->>'" + fields [1 ] + "'" ;
92+ } else if (fields .length > 2 ) {
93+ String param = "json" ;
94+ for (int i = 0 ; i < fields .length ; i ++) {
95+ if (i == fields .length - 1 ) {
96+ param = param + "->>'" + fields [i ] + "'" ;
97+ } else {
98+ param = param + "->'" + fields [i ]+ "'" ;
99+ }
100+ }
101+ return param ;
102+ }
103+ throw new RuntimeException ("invalid field format" );
104+ }
105+
106+ public static String castToType (String fieldName , ConstNode constNode ) {
107+ if (constNode instanceof ConstNumberNode ) {
108+ return "(" + fieldName + ")::numeric" ;
109+ } else if (constNode instanceof ConstBooleanNode ) {
110+ return "(" + fieldName + ")::bool" ;
111+ } else {
112+ return fieldName ;
113+ }
114+ }
115+
116+ public static String castToType (String fieldName ,OrderByNode .DataType dataType ) {
117+ switch (dataType )
118+ {
119+ case BOOL :
120+ {
121+ return "(" +fieldName +")::bool" ;
122+ }
123+ case NUMBER :{
124+ return "(" +fieldName +")::numeric" ;
125+ }
126+ case STRING :
127+ {
128+ return fieldName ;
129+ }
130+ default :
131+ {
132+ return fieldName ;
133+ }
134+ }
135+
136+ }
137+
138+ public static void main (String [] args ) {
139+ RestQLNode node = RestQLParser .parse ("_resourceType = 'com.restrata.db.repo.TestEntity'" );
140+ PostgresSqlNode sqlNode = new PostgresSqlNode ();
141+ System .out .println (sqlNode .eval (node ));
142+ }
143+ }
0 commit comments