Permalink
Browse files

Merge branch 'expressions'

  • Loading branch information...
detinho committed Apr 19, 2012
2 parents 6725d4b + de9786c commit d516795b4bf3cf88190db8f3a9896d107ca8b7b0
@@ -0,0 +1,23 @@
+package br.com.detinho.sqlbuilder.expressions;
+
+import br.com.detinho.sqlbuilder.Expression;
+
+public class ArithmeticExpression implements Expression {
+ private final Expression operand1;
+ private final String operator;
+ private final Expression operand2;
+
+ public ArithmeticExpression(Expression operand1, String operator, Expression operand2) {
+ this.operand1 = operand1;
+ this.operator = operator;
+ this.operand2 = operand2;
+ }
+
+ @Override
+ public String expression() {
+ if (operand1 instanceof EmptyExpression) {
+ return operand2.expression();
+ }
+ return operand1.expression() + operator + operand2.expression();
+ }
+}
@@ -0,0 +1,12 @@
+package br.com.detinho.sqlbuilder.expressions;
+
+import br.com.detinho.sqlbuilder.Expression;
+
+public class EmptyExpression implements Expression {
+
+ @Override
+ public String expression() {
+ return "";
+ }
+
+}
@@ -0,0 +1,76 @@
+package br.com.detinho.sqlbuilder.expressions;
+
+import java.util.Set;
+
+import br.com.detinho.sqlbuilder.Expression;
+import br.com.detinho.sqlbuilder.Selectable;
+import br.com.detinho.sqlbuilder.StringUtils;
+import br.com.detinho.sqlbuilder.Table;
+import br.com.detinho.sqlbuilder.Writable;
+
+public class ExpressionBuilder implements Writable {
+
+ private Expression expr = new EmptyExpression();
+
+ public ExpressionBuilder(Expression operand) {
+ this.expr = operand;
+ }
+
+ public void add(Expression operand) {
+ this.expr = new ArithmeticExpression(expr, "+", operand);
+ }
+
+ @Override
+ public String write() {
+ return expr.expression();
+ }
+
+ public void sub(Expression operand) {
+ this.expr = new ArithmeticExpression(expr, "-", operand);
+ }
+
+ public void times(Expression operand) {
+ this.expr = new ArithmeticExpression(expr, "*", operand);
+ }
+
+ public void div(Expression operand) {
+ this.expr = new ArithmeticExpression(expr, "/", operand);
+ }
+
+ public Selectable selectable(final String alias) {
+ return new SelectableExpression(expr, alias);
+ }
+
+ public Selectable selectable() {
+ return selectable("");
+ }
+
+ private static final class SelectableExpression implements Selectable {
+ private final String alias;
+ private final Expression expr;
+
+ private SelectableExpression(Expression expr, String alias) {
+ this.expr = expr;
+ this.alias = alias;
+ }
+
+ @Override
+ public String write() {
+ if (StringUtils.nullToStr(alias).equals(""))
+ return expr.expression();
+
+ return "(" + expr.expression() + ") AS " + alias;
+ }
+
+ @Override
+ public String alias() {
+ return alias;
+ }
+
+ @Override
+ public void addTable(Set<Table> tables) {
+
+ }
+ }
+
+}
@@ -3,7 +3,8 @@
import static org.junit.Assert.*;
import org.junit.Test;
-import expressions.ExpressionBuilder;
+import br.com.detinho.sqlbuilder.expressions.ExpressionBuilder;
+
import static br.com.detinho.sqlbuilder.SqlBuilder.*;

0 comments on commit d516795

Please sign in to comment.