package lambda2sql;

import static com.trigersoft.jaque.expression.ExpressionType.Equal;
import static com.trigersoft.jaque.expression.ExpressionType.LogicalAnd;
import static com.trigersoft.jaque.expression.ExpressionType.LogicalOr;

import com.trigersoft.jaque.expression.*;

public class ToSqlVisitor implements ExpressionVisitor<StringBuilder> {

	private StringBuilder sb = new StringBuilder();
	private Expression body;

	@Override
	public StringBuilder visit(BinaryExpression e) {
		boolean quote = e != body && e.getExpressionType() == LogicalOr;

		if( quote ) sb.append('(');

		e.getFirst().accept(this);
		sb.append(' ').append(toSqlOp(e.getExpressionType())).append(' ');
		e.getSecond().accept(this);

		if( quote ) sb.append(')');

		return sb;
	}

	public static String toSqlOp(int expressionType) {
		switch(expressionType) {
			case Equal: return "=";
			case LogicalAnd: return "AND";
			case LogicalOr: return "OR";
		}
		return ExpressionType.toString(expressionType);
	}

	@Override
	public StringBuilder visit(ConstantExpression e) {
		return sb.append(e.getValue().toString());
	}

	@Override
	public StringBuilder visit(InvocationExpression e) {
		return e.getTarget().accept(this);
	}

	@Override
	public StringBuilder visit(LambdaExpression<?> e) {
		this.body = e.getBody();
		return body.accept(this);
	}

	@Override
	public StringBuilder visit(MemberExpression e) {
		String name = e.getMember().getName();
		name = name.replaceAll("^(get|is)", "").toLowerCase();
		return sb.append(name);
	}

	@Override
	public StringBuilder visit(ParameterExpression e) {
		return sb;
	}

	@Override
	public StringBuilder visit(UnaryExpression e) {
		sb.append(ExpressionType.toString(e.getExpressionType()));
		return e.getFirst().accept(this);
	}

}
@@ -1042,32 +1042,146 @@
 Expression e) %7B%0A
+%09%09if (e.getValue() instanceof String) %7B%0A%09%09%09return sb.append(%22'%22).append(e.getValue().toString()).append(%22'%22);%0A%09%09%7D%0A
 %09%09return sb.appe
@@ -1209,16 +1209,16 @@
 ing());%0A
-
 %09%7D%0A%0A%09@Ov
@@ -1579,31 +1579,78 @@
 (get
-%7Cis
 )%22, %22%22)
-.toLowerCase(
+%0A%09%09name = name.substring(0, 1).toLowerCase() + name.substring(1
 );%0A%09
@@ -1920,9 +1920,10 @@
 s);%0A%09%7D%0A%0A
-
 %7D
+%0A

Update ToSqlVisitor.java