11package com .robin .core .sql .util ;
22
33import com .robin .core .base .dao .util .AnnotationRetriever ;
4- import com .robin .core .base .dao .util .FieldContent ;
54import com .robin .core .base .dao .util .PropertyFunction ;
5+ import com .robin .core .base .exception .MissingConfigException ;
66import com .robin .core .base .model .BaseObject ;
77import com .robin .core .base .util .Const ;
88import lombok .Data ;
@@ -25,14 +25,21 @@ public class FilterCondition {
2525 private Const .LINKOPERATOR linkOper = Const .LINKOPERATOR .LINK_AND ;
2626 private Class <? extends BaseObject > mappingClass ;
2727 private String orderByStr ;
28- private Map <Class <? extends BaseObject >,String > aliasMap =new HashMap <>();
28+ private Map <Class <? extends BaseObject >, String > aliasMap = new HashMap <>();
29+ private Object leftColumn ;
30+ private SqlBuilder sqlBuilder ;
2931
3032
3133 public FilterCondition (String columnCode , Const .OPERATOR operator ) {
3234 this .columnCode = columnCode ;
3335 this .operator = operator ;
3436 }
3537
38+ public FilterCondition (Object leftColumn , Const .OPERATOR operator ) {
39+ this .leftColumn = leftColumn ;
40+ this .operator = operator ;
41+ }
42+
3643 public FilterCondition (String columnCode , String columnType , Const .OPERATOR operator ) {
3744 this .columnCode = columnCode ;
3845 this .columnType = columnType ;
@@ -81,13 +88,13 @@ public FilterCondition(Class<? extends BaseObject> clazz, Const.LINKOPERATOR lin
8188 this .conditions = conditions ;
8289 this .mappingClass = clazz ;
8390 }
91+
8492 public FilterCondition (Const .LINKOPERATOR linkOper , List <FilterCondition > conditions ) {
8593 this .linkOper = linkOper ;
8694 this .conditions = conditions ;
8795 }
8896
8997
90-
9198 public <T extends BaseObject > FilterCondition (PropertyFunction <T , ?> function , Const .OPERATOR operator , List <FilterCondition > values ) {
9299 this .columnCode = AnnotationRetriever .getFieldColumnName (function );
93100 this .mappingClass = AnnotationRetriever .getFieldOwnedClass (function );
@@ -133,20 +140,34 @@ public FilterCondition(Const.OPERATOR operator, List<FilterCondition> values, Cl
133140
134141 public String toPreparedSQLPart (List <Object > params ) {
135142 StringBuilder sbSQLStr = new StringBuilder ();
136- String realColumn = Optional .ofNullable (aliasMap .get (mappingClass )).map (f ->f +"." +columnCode ).orElse (columnCode );
137- if (ObjectUtils .isEmpty (value ) && ObjectUtils .isEmpty (values ) && !CollectionUtils .isEmpty (getConditions ()) && getConditions ().size ()>1 ) {
138- if (Const .LINKOPERATOR .LINK_OR .equals (getLinkOper ())) {
139- sbSQLStr .append ("(" );
143+ String realColumn = null ;
144+ if (!ObjectUtils .isEmpty (columnCode )) {
145+ realColumn = Optional .ofNullable (aliasMap .get (mappingClass )).map (f -> f + "." + columnCode ).orElse (columnCode );
146+ } else {
147+ if (leftColumn != null ) {
148+ if (FunctionCall .class .isAssignableFrom (leftColumn .getClass ())) {
149+ ((FunctionCall ) leftColumn ).setSqlBuilder (sqlBuilder );
150+ realColumn = ((FunctionCall ) leftColumn ).getFormula (false );
151+ } else {
152+ throw new MissingConfigException ("type " + leftColumn .getClass ().getName () + " not allowed" );
153+ }
140154 }
155+ }
156+ if (ObjectUtils .isEmpty (value ) && ObjectUtils .isEmpty (values ) && !CollectionUtils .isEmpty (getConditions ()) && getConditions ().size () > 1 ) {
157+
141158 for (int i = 0 ; i < getConditions ().size (); i ++) {
159+ if (Const .LINKOPERATOR .LINK_OR .equals (getLinkOper ())) {
160+ sbSQLStr .append ("(" );
161+ }
142162 sbSQLStr .append (getConditions ().get (i ).toPreparedSQLPart (params ));
163+ if (Const .LINKOPERATOR .LINK_OR .equals (getLinkOper ())) {
164+ sbSQLStr .append (")" );
165+ }
143166 if (i < getConditions ().size () - 1 ) {
144- sbSQLStr .append (getConditions (). get ( i + 1 ). getLinkOper ().getSignal ());
167+ sbSQLStr .append (getLinkOper ().getSignal ());
145168 }
146169 }
147- if (Const .LINKOPERATOR .LINK_OR .equals (getLinkOper ())) {
148- sbSQLStr .append (")" );
149- }
170+
150171 } else {
151172 switch (operator ) {
152173 case BETWEEN :
@@ -178,8 +199,24 @@ public String toPreparedSQLPart(List<Object> params) {
178199 case GE :
179200 Assert .notNull (value , "" );
180201 sbSQLStr .append (realColumn );
181- sbSQLStr .append (operator .getSignal ()).append ("?" );
182- params .add (getValue ());
202+ sbSQLStr .append (operator .getSignal ());
203+ if (FunctionCall .class .isAssignableFrom (value .getClass ())) {
204+ FunctionCall call = (FunctionCall ) value ;
205+ call .setSqlBuilder (sqlBuilder );
206+ sbSQLStr .append (call .getFormula (false ));
207+ } else if (PropertyFunction .class .isAssignableFrom (value .getClass ())) {
208+ Class <? extends BaseObject > clazz = AnnotationRetriever .getFieldClass ((PropertyFunction <? extends Object , ?>) value );
209+ String fieldName = AnnotationRetriever .getFieldName ((PropertyFunction <? extends Object , ?>) value );
210+ if (sqlBuilder != null ) {
211+ if (sqlBuilder .getTabAliasMap ().containsKey (clazz )) {
212+ sbSQLStr .append (sqlBuilder .getTabAliasMap ().get (clazz )).append ("." );
213+ }
214+ sqlBuilder .arithmetic (sqlBuilder .getFieldMap ().get (clazz ).get (fieldName ).getFieldName ());
215+ }
216+ } else {
217+ sbSQLStr .append ("?" );
218+ params .add (getValue ());
219+ }
183220 break ;
184221 case NOTNULL :
185222 sbSQLStr .append (realColumn );
@@ -242,7 +279,7 @@ public String toPreparedSQLPart(List<Object> params) {
242279
243280 private void parseConditions (StringBuilder sbSQLStr , List <Object > params ) {
244281 if (!CollectionUtils .isEmpty (getConditions ())) {
245- if (getConditions ().size ()> 1 ) {
282+ if (getConditions ().size () > 1 ) {
246283 for (int i = 0 ; i < getConditions ().size (); i ++) {
247284 if (Const .LINKOPERATOR .LINK_OR .equals (getConditions ().get (i ).getLinkOper ())) {
248285 sbSQLStr .append (" OR (" );
@@ -255,13 +292,13 @@ private void parseConditions(StringBuilder sbSQLStr, List<Object> params) {
255292 sbSQLStr .append ("(" );
256293 }
257294 }
258- }else {
259- FilterCondition condition = getConditions ().get (0 );
295+ } else {
296+ FilterCondition condition = getConditions ().get (0 );
260297 sbSQLStr .append (Const .SQL_SELECT ).append (condition .getColumnCode ()).append (Const .SQL_FROM );
261298 AnnotationRetriever .EntityContent tableDef = AnnotationRetriever .getMappingTableByCache (condition .getMappingClass ());
262299 sbSQLStr .append (tableDef .getTableName ()).append (Const .SQL_WHERE );
263- if (!CollectionUtils .isEmpty (condition .getConditions ())){
264- for (FilterCondition condition1 : condition .getConditions ()){
300+ if (!CollectionUtils .isEmpty (condition .getConditions ())) {
301+ for (FilterCondition condition1 : condition .getConditions ()) {
265302 sbSQLStr .append (condition1 .toPreparedSQLPart (params ));
266303 }
267304 }
0 commit comments