@@ -24,13 +24,15 @@ this software and associated documentation files (the "Software"), to deal in
2424
2525package su .interference .sql ;
2626
27+ import su .interference .core .EntityContainer ;
2728import su .interference .persistent .Session ;
2829import su .interference .persistent .Table ;
2930import su .interference .sqlexception .*;
3031import su .interference .core .Types ;
3132import su .interference .exception .InternalException ;
3233
3334import java .io .IOException ;
35+ import java .lang .reflect .Method ;
3436import java .util .*;
3537import java .io .UnsupportedEncodingException ;
3638import java .lang .reflect .Field ;
@@ -53,7 +55,10 @@ public class NestedCondition extends Condition {
5355 private int type ; // 1 - AND, 2 - OR
5456 private boolean empty ;
5557
56- public boolean checkNC (Object o , int sqlcid , boolean last ) throws UnsupportedEncodingException , InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
58+ public boolean checkNC (Object o , int sqlcid , boolean last , Session s ) throws UnsupportedEncodingException , InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
59+ final Class cl = o .getClass ();
60+ final Class cl_ = Arrays .asList (o .getClass ().getInterfaces ()).contains (EntityContainer .class ) ? o .getClass ().getSuperclass () : o .getClass ();
61+ final boolean rs_ = !Arrays .asList (o .getClass ().getInterfaces ()).contains (EntityContainer .class );
5762 boolean res = false ;
5863 if (empty ) {
5964 return true ;
@@ -65,20 +70,20 @@ public boolean checkNC (Object o, int sqlcid, boolean last) throws UnsupportedEn
6570 boolean cres = false ;
6671 if (c .getClass ().getName ().equals ("su.interference.sql.NestedCondition" )) {
6772 final NestedCondition nc = (NestedCondition )c ;
68- cres = nc .checkNC (o , sqlcid , last );
73+ cres = nc .checkNC (o , sqlcid , last , s );
6974 }
7075 if (c .getClass ().getName ().equals ("su.interference.sql.JoinCondition" )) {
7176 JoinCondition jc = (JoinCondition )c ;
7277 if ((jc .getId ()==sqlcid )||(jc .getId ()==0 &&last )) {
73- cres = sqlEquals (o , jc );
78+ cres = sqlEquals (cl , cl_ , o , jc , rs_ , s );
7479 } else {
7580 cres = true ;
7681 }
7782 }
7883 if (c .getClass ().getName ().equals ("su.interference.sql.ValueCondition" )) {
7984 final ValueCondition vc = (ValueCondition )c ;
8085 if ((vc .getId ()==sqlcid )||(vc .getId ()==0 &&last )) {
81- cres = sqlEquals (o , vc );
86+ cres = sqlEquals (cl , cl_ , o , vc , rs_ , s );
8287 } else {
8388 cres = true ;
8489 }
@@ -93,28 +98,26 @@ public boolean checkNC (Object o, int sqlcid, boolean last) throws UnsupportedEn
9398 return res ;
9499 }
95100
96- public boolean sqlEquals (Object o , ValueCondition vc ) throws UnsupportedEncodingException , InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
97- final Class c = o .getClass ();
101+ private boolean sqlEquals (Class c , Class c_ , Object o , ValueCondition vc , boolean rs_ , Session s ) throws UnsupportedEncodingException , InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
98102 boolean res = false ;
99103
100- for (int j =0 ; j <c .getDeclaredFields ().length ; j ++) {
101- final Field f = c .getDeclaredFields ()[j ];
104+ for (int j =0 ; j <c_ .getDeclaredFields ().length ; j ++) {
105+ final Field f = c_ .getDeclaredFields ()[j ];
102106 if (f .getName ().equals (vc .getConditionColumn ().getAlias ())) {
103- res = sqlEquals (o , f , null , vc .getValues (), vc .getCondition ());
107+ res = sqlEquals (c , o , f , null , vc .getValues (), vc .getCondition (), rs_ , s );
104108 }
105109 }
106110
107111 return res ;
108112 }
109113
110- public boolean sqlEquals (Object o , JoinCondition jc ) throws InternalException , UnsupportedEncodingException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
111- final Class c = o .getClass ();
114+ private boolean sqlEquals (Class c , Class c_ , Object o , JoinCondition jc , boolean rs_ , Session s ) throws InternalException , UnsupportedEncodingException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
112115 boolean res = false ;
113116
114117 Field lf = null ;
115118 Field rf = null ;
116- for (int j =0 ; j <c .getDeclaredFields ().length ; j ++) {
117- Field f = c .getDeclaredFields ()[j ];
119+ for (int j =0 ; j <c_ .getDeclaredFields ().length ; j ++) {
120+ Field f = c_ .getDeclaredFields ()[j ];
118121 if (f .getName ().equals (jc .getConditionColumn ().getAlias ())) {
119122 lf = f ;
120123 }
@@ -123,16 +126,15 @@ public boolean sqlEquals (Object o, JoinCondition jc) throws InternalException,
123126 }
124127 }
125128 if (lf !=null &&rf !=null ) {
126- res = sqlEquals (o , lf , rf , null , jc .getCondition ());
129+ res = sqlEquals (c , o , lf , rf , null , jc .getCondition (), rs_ , s );
127130 }
128131
129132 return res ;
130133
131134 }
132135
133136 //rf used for join condition, ro - for value condition
134- public boolean sqlEquals (Object o , Field lf , Field rf , Object [] ro , int ctype ) throws InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
135- final Class c = o .getClass ();
137+ private boolean sqlEquals (Class c , Object o , Field lf , Field rf , Object [] ro , int ctype , boolean rs_ , Session s ) throws InternalException , IllegalAccessException , NoSuchMethodException , InvocationTargetException {
136138 final String t1 = lf .getType ().getName ();
137139 final String t2 = rf ==null ?t1 :rf .getType ().getName ();
138140
@@ -141,14 +143,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
141143 }
142144
143145 if (t1 .equals (Types .t_string )&&t2 .equals (Types .t_string )) {
144- final String d1 = (String )c . getMethod ( "get" + lf . getName (). substring ( 0 , 1 ). toUpperCase ()+ lf . getName (). substring ( 1 , lf .getName (). length ()), null ). invoke ( o , null );
146+ final String d1 = (String )invoke ( c , o , lf .getName (), rs_ , s );
145147 final String [] d2 = new String [rf ==null ?ro .length :1 ];
146148 if (rf ==null ) {
147149 for (int i =0 ;i <ro .length ;i ++) {
148150 d2 [i ] = (String )ro [i ];
149151 }
150152 } else {
151- d2 [0 ] = (String )c . getMethod ( "get" + rf . getName (). substring ( 0 , 1 ). toUpperCase ()+ rf . getName (). substring ( 1 , rf .getName (). length ()), null ). invoke ( o , null );
153+ d2 [0 ] = (String )invoke ( c , o , rf .getName (), rs_ , s );
152154 }
153155 if ((ctype ==Condition .C_EQUAL )||(ctype ==Condition .C_IN )) {
154156 for (int i =0 ; i <d2 .length ; i ++) {
@@ -184,14 +186,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
184186 }
185187 }
186188 if (t1 .equals (Types .t_date )&&t2 .equals (Types .t_date )) {
187- final Date d1 = (Date )c . getMethod ( "get" + lf . getName (). substring ( 0 , 1 ). toUpperCase ()+ lf . getName (). substring ( 1 , lf .getName (). length ()), null ). invoke ( o , null );
189+ final Date d1 = (Date )invoke ( c , o , lf .getName (), rs_ , s );
188190 final Date [] d2 = new Date [rf ==null ?ro .length :1 ];
189191 if (rf ==null ) {
190192 for (int i =0 ;i <ro .length ;i ++) {
191193 d2 [i ] = (Date )ro [i ];
192194 }
193195 } else {
194- d2 [0 ] = (Date )c . getMethod ( "get" + rf . getName (). substring ( 0 , 1 ). toUpperCase ()+ rf . getName (). substring ( 1 , rf .getName (). length ()), null ). invoke ( o , null );
196+ d2 [0 ] = (Date )invoke ( c , o , rf .getName (), rs_ , s );
195197 }
196198 if ((ctype ==Condition .C_EQUAL )||(ctype ==Condition .C_IN )) {
197199 for (int i =0 ; i <d2 .length ; i ++) {
@@ -215,14 +217,14 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
215217 Long d1 = new Long (0 );
216218 Long [] d2 = new Long [rf ==null ?ro .length :1 ];
217219 if ((t1 .equals (Types .p_int )||t1 .equals (Types .t_int )||t1 .equals (Types .c_int ))&&(t2 .equals (Types .p_int )||t2 .equals (Types .t_int )||t2 .equals (Types .c_int ))) {
218- Integer dd = (Integer )c . getMethod ( "get" + lf . getName (). substring ( 0 , 1 ). toUpperCase ()+ lf . getName (). substring ( 1 , lf .getName (). length ()), null ). invoke ( o , null );
220+ Integer dd = (Integer )invoke ( c , o , lf .getName (), rs_ , s );
219221 d1 = new Long (dd );
220222 if (rf ==null ) {
221223 for (int i =0 ;i <ro .length ;i ++) {
222224 d2 [i ] = new Long ((Integer )ro [i ]);
223225 }
224226 } else {
225- Integer dr = (Integer )c . getMethod ( "get" + rf . getName (). substring ( 0 , 1 ). toUpperCase ()+ rf . getName (). substring ( 1 , rf .getName (). length ()), null ). invoke ( o , null );
227+ Integer dr = (Integer )invoke ( c , o , rf .getName (), rs_ , s );
226228 d2 [0 ] = new Long (dr );
227229 }
228230 }
@@ -284,21 +286,21 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
284286 Double d1 = new Double (0 );
285287 Double [] d2 = new Double [rf ==null ?ro .length :1 ];
286288 if ((t1 .equals (Types .p_float )||t1 .equals (Types .t_float ))&&(t2 .equals (Types .p_float )||t2 .equals (Types .t_float ))) {
287- Float dd = (Float )c . getMethod ( "get" + lf . getName (). substring ( 0 , 1 ). toUpperCase ()+ lf . getName (). substring ( 1 , lf .getName (). length ()), null ). invoke ( o , null );
289+ Float dd = (Float )invoke ( c , o , lf .getName (), rs_ , s );
288290 d1 = new Double (dd );
289291 if (rf ==null ) {
290292 for (int i =0 ;i <ro .length ;i ++) {
291293 //RO object created by ValueCondition constructor, and already cast to 8-byte type (Double)
292294 d2 [i ] = (Double )ro [i ];
293295 }
294296 } else {
295- Float dr = (Float )c . getMethod ( "get" + rf . getName (). substring ( 0 , 1 ). toUpperCase ()+ rf . getName (). substring ( 1 , rf .getName (). length ()), null ). invoke ( o , null );
297+ Float dr = (Float )invoke ( c , o , rf .getName (), rs_ , s );
296298 d2 [0 ] = new Double (dr );
297299 }
298300 }
299301 if ((t1 .equals (Types .p_double )||t1 .equals (Types .t_double ))&&(t2 .equals (Types .p_double )||t2 .equals (Types .t_double ))) {
300- d1 = (Double )c . getMethod ( "get" + lf . getName (). substring ( 0 , 1 ). toUpperCase ()+ lf . getName (). substring ( 1 , lf .getName (). length ()), null ). invoke ( o , null );
301- d2 = rf ==null ?(Double [])ro :new Double []{(Double )c . getMethod ( "get" + rf . getName (). substring ( 0 , 1 ). toUpperCase ()+ rf . getName (). substring ( 1 , rf .getName (). length ()), null ). invoke ( o , null )};
302+ d1 = (Double )invoke ( c , o , lf .getName (), rs_ , s );
303+ d2 = rf ==null ?(Double [])ro :new Double []{(Double )invoke ( c , o , rf .getName (), rs_ , s )};
302304 }
303305 if ((ctype ==Condition .C_EQUAL )||(ctype ==Condition .C_IN )) {
304306 for (int i =0 ; i <d2 .length ; i ++) {
@@ -353,6 +355,12 @@ public boolean sqlEquals (Object o, Field lf, Field rf, Object[] ro, int ctype)
353355 return false ;
354356 }
355357
358+ private Object invoke (Class c , Object o , String fname , boolean rs_ , Session s ) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException {
359+ final String mtname = "get" +fname .substring (0 ,1 ).toUpperCase ()+fname .substring (1 ,fname .length ());
360+ final Method m = rs_ ? c .getMethod (mtname , null ) : c .getMethod (mtname , Session .class );
361+ return rs_ ? m .invoke (o , null ) : m .invoke (o , s );
362+ }
363+
356364 public NestedCondition (String cdd , SQLStatement sql , ArrayList <SQLTable > tables ) throws Exception {
357365 int nl = 0 ;
358366 String cc = "" ;
0 commit comments