22
33import com .trigersoft .jaque .expression .BinaryExpression ;
44import com .trigersoft .jaque .expression .ConstantExpression ;
5+ import com .trigersoft .jaque .expression .DelegateExpression ;
56import com .trigersoft .jaque .expression .Expression ;
67import com .trigersoft .jaque .expression .ExpressionType ;
78import com .trigersoft .jaque .expression .ExpressionVisitor ;
@@ -61,6 +62,16 @@ private static String toSqlOp(int expressionType) {
6162 */
6263 @ Override
6364 public StringBuilder visit (BinaryExpression e ) {
65+ //Handling for null parameters
66+ if (e .getSecond () instanceof ParameterExpression && arguments .top ().get (((ParameterExpression ) e .getSecond ()).getIndex ()).getValue () == null ) {
67+ if (e .getExpressionType () == ExpressionType .Equal ) {
68+ return Expression .isNull (e .getFirst ()).accept (this );
69+ }
70+ if (e .getExpressionType () == ExpressionType .NotEqual ) {
71+ return Expression .unary (ExpressionType .LogicalNot , boolean .class , Expression .unary (ExpressionType .IsNull , boolean .class , e .getFirst ())).accept (this );
72+ }
73+ }
74+
6475 boolean quote = e != this .body && e .getExpressionType () == ExpressionType .LogicalOr ;
6576
6677 if (quote ) sb .append ('(' );
@@ -83,10 +94,12 @@ public StringBuilder visit(BinaryExpression e) {
8394 */
8495 @ Override
8596 public StringBuilder visit (ConstantExpression e ) {
97+ if (e .getValue () instanceof LambdaExpression ) {
98+ ((LambdaExpression ) e .getValue ()).getBody ().accept (this );
99+ return sb ;
100+ }
86101 if (e .getValue () == null ) {
87- sb .deleteCharAt (sb .length () - 1 );
88- sb .delete (sb .lastIndexOf (" " ), sb .length ());
89- return sb .append (" IS NULL" );
102+ return sb .append ("NULL" );
90103 }
91104 if (e .getValue () instanceof String ) {
92105 return sb .append ("'" ).append (e .getValue ().toString ()).append ("'" );
@@ -129,6 +142,11 @@ public StringBuilder visit(LambdaExpression<?> e) {
129142 return e .getBody ().accept (this );
130143 }
131144
145+ @ Override
146+ public StringBuilder visit (DelegateExpression e ) {
147+ return e .getDelegate ().accept (this );
148+ }
149+
132150 /**
133151 * An expression which represents a getter, and thus a field in a database, in the lambda expression.
134152 * For example:
@@ -158,6 +176,9 @@ public StringBuilder visit(MemberExpression e) {
158176 @ Override
159177 public StringBuilder visit (ParameterExpression e ) {
160178 arguments .top ().get (e .getIndex ()).accept (this );
179+ if (e .getIndex () == arguments .top ().size () - 1 ) {
180+ arguments .pop ();
181+ }
161182 return sb ;
162183 }
163184
@@ -172,6 +193,11 @@ public StringBuilder visit(ParameterExpression e) {
172193 */
173194 @ Override
174195 public StringBuilder visit (UnaryExpression e ) {
196+ if (e .getFirst () instanceof UnaryExpression && e .getExpressionType () == ExpressionType .LogicalNot ) {
197+ if (e .getFirst ().getExpressionType () == ExpressionType .IsNull ) {
198+ return ((UnaryExpression ) e .getFirst ()).getFirst ().accept (this ).append (" IS NOT NULL" );
199+ }
200+ }
175201 if (e .getExpressionType () == ExpressionType .IsNull ) {
176202 return e .getFirst ().accept (this ).append (" IS NULL" );
177203 }
0 commit comments