Skip to content

Commit

Permalink
#98 Refactored comparison and like class structure (#99)
Browse files Browse the repository at this point in the history
* #98 Refactored comparison and like class structure
  • Loading branch information
jakobbraun authored Oct 28, 2020
1 parent 4f6c592 commit a041d23
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 196 deletions.
1 change: 1 addition & 0 deletions doc/changes/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changes

* [4.3.1](changes_4.3.1.md)
* [4.3.0](changes_4.3.0.md)
* [4.2.0](changes_4.2.0.md)
* [4.1.0](changes_4.1.0.md)
Expand Down
9 changes: 9 additions & 0 deletions doc/changes/changes_4.3.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SQL Statement Builder 4.3.1, released 2020-XX-XX

Code Name: Refactoring

## Refactoring

* #98: Refactored comparison and like class structure
The refactoring changed the internal representation of the `Comparison`.
The public API from `BooleanTerm` did however not change.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.exasol.sql.expression;

import com.exasol.sql.expression.comparison.Comparison;

/**
* Visitor interface for a {@link BooleanTerm}
*/
Expand All @@ -12,19 +14,11 @@ public interface BooleanExpressionVisitor {

public void leave(Not not);

public void leave(BooleanLiteral literal);

public void leave(And and);

public void visit(Or or);

public void leave(Or or);

public void visit(Comparison comparison);

public void leave(Comparison comparison);

public void visit(Like like);

public void leave(Like like);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void acceptConcrete(final BooleanExpressionVisitor visitor) {

@Override
public void dismissConcrete(final BooleanExpressionVisitor visitor) {
visitor.leave(this);
// empty on purpose
}

/**
Expand Down
37 changes: 21 additions & 16 deletions src/main/java/com/exasol/sql/expression/BooleanTerm.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.exasol.sql.expression;

import com.exasol.sql.expression.comparison.LikeComparison;
import com.exasol.sql.expression.comparison.SimpleComparison;
import com.exasol.sql.expression.comparison.SimpleComparisonOperator;

// [impl->dsn~boolean-operators~1]
public abstract class BooleanTerm extends AbstractBooleanExpression {
private BooleanTerm() {
Expand Down Expand Up @@ -47,67 +51,68 @@ public static BooleanExpression or(final BooleanExpression... expressions) {
}

public static BooleanExpression like(final ValueExpression left, final ValueExpression right) {
return Like.builder().left(left).right(right).build();
return LikeComparison.builder().left(left).right(right).build();
}

public static BooleanExpression like(final ValueExpression left, final ValueExpression right, char escape) {
return Like.builder().left(left).right(right).escape(escape).build();
public static BooleanExpression like(final ValueExpression left, final ValueExpression right, final char escape) {
return LikeComparison.builder().left(left).right(right).escape(escape).build();
}

public static BooleanExpression notLike(final ValueExpression left, final ValueExpression right) {
return Like.builder().left(left).right(right).not().build();
return LikeComparison.builder().left(left).right(right).not().build();
}

public static BooleanExpression notLike(final ValueExpression left, final ValueExpression right, char escape) {
return Like.builder().left(left).right(right).not().escape(escape).build();
public static BooleanExpression notLike(final ValueExpression left, final ValueExpression right,
final char escape) {
return LikeComparison.builder().left(left).right(right).not().escape(escape).build();
}

// [impl->dsn~boolean-operation.comparison.constructing-from-strings~1]
public static BooleanExpression compare(final ValueExpression left, final String operatorSymbol,
final ValueExpression right) {
return new Comparison(ComparisonOperator.ofSymbol(operatorSymbol), left, right);
return new SimpleComparison(SimpleComparisonOperator.ofSymbol(operatorSymbol), left, right);
}

// [impl->dsn~boolean-operation.comparison.constructing-from-enum~1]
public static BooleanExpression compare(final ValueExpression left, final ComparisonOperator operator,
public static BooleanExpression compare(final ValueExpression left, final SimpleComparisonOperator operator,
final ValueExpression right) {
return new Comparison(operator, left, right);
return new SimpleComparison(operator, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression eq(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.EQUAL, left, right);
return new SimpleComparison(SimpleComparisonOperator.EQUAL, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression ne(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.NOT_EQUAL, left, right);
return new SimpleComparison(SimpleComparisonOperator.NOT_EQUAL, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression lt(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.LESS_THAN, left, right);
return new SimpleComparison(SimpleComparisonOperator.LESS_THAN, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression gt(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.GREATER_THAN, left, right);
return new SimpleComparison(SimpleComparisonOperator.GREATER_THAN, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression le(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.LESS_THAN_OR_EQUAL, left, right);
return new SimpleComparison(SimpleComparisonOperator.LESS_THAN_OR_EQUAL, left, right);
}

// [impl->dsn~comparison-operations~1]
public static BooleanExpression ge(final ValueExpression left, final ValueExpression right) {
return new Comparison(ComparisonOperator.GREATER_THAN_OR_EQUAL, left, right);
return new SimpleComparison(SimpleComparisonOperator.GREATER_THAN_OR_EQUAL, left, right);
}

/**
* Create a logical operation from an operator name and a list of operands
*
* @param operator name of the operator
* @param operator name of the operator
* @param expressions operands
* @return instance of either {@link And}, {@link Or} or {@link Not}
* @throws IllegalArgumentException if the operator is unknown or null
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/com/exasol/sql/expression/Comparison.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.exasol.sql.expression.comparison;

import com.exasol.sql.expression.AbstractBooleanExpression;
import com.exasol.sql.expression.BooleanExpressionVisitor;
import com.exasol.sql.expression.ValueExpression;

/**
* Abstract basis for comparisons.
*/
public abstract class AbstractComparison extends AbstractBooleanExpression implements Comparison {
protected final ComparisonOperator operator;
protected final ValueExpression leftOperand;
protected final ValueExpression rightOperand;

/**
* Create a new instance of {@link AbstractComparison}.
*
* @param comparisonOperator comparison operator
* @param leftOperand left-hand side operator of the comparison
* @param rightOperand right-hand side operator of the comparison
*/
protected AbstractComparison(final ComparisonOperator comparisonOperator, final ValueExpression leftOperand,
final ValueExpression rightOperand) {
this.operator = comparisonOperator;
this.leftOperand = leftOperand;
this.rightOperand = rightOperand;
}

@Override
public final void acceptConcrete(final BooleanExpressionVisitor visitor) {
visitor.visit(this);
}

@Override
public final void dismissConcrete(final BooleanExpressionVisitor visitor) {
// empty on purpose
}

/**
* Get the left-hand side operator of the comparison
*
* @return left operator
*/
@Override
public ValueExpression getLeftOperand() {
return this.leftOperand;
}

/**
* Get the right-hand side operator of the comparison
*
* @return right operator
*/
@Override
public ValueExpression getRightOperand() {
return this.rightOperand;
}

/**
* Get the comparison operator
*
* @return comparison operator
*/
@Override
public ComparisonOperator getOperator() {
return this.operator;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/exasol/sql/expression/comparison/Comparison.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.exasol.sql.expression.comparison;

import com.exasol.sql.expression.BooleanExpression;
import com.exasol.sql.expression.ValueExpression;

/**
* Interface for classes that implement comparisons between two columns.
*/
public interface Comparison extends BooleanExpression {

/**
* Get the left-hand side operator of the comparison
*
* @return left operator
*/
public ValueExpression getLeftOperand();

/**
* Get the right-hand side operator of the comparison
*
* @return right operator
*/
public ValueExpression getRightOperand();

/**
* Get the comparison operator
*
* @return comparison operator
*/
public ComparisonOperator getOperator();

/**
* Accept {@link ComparisonVisitor}.
*
* @param visitor visitor to accept
*/
public void accept(ComparisonVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.exasol.sql.expression.comparison;

/**
* Interface for comparison operators.
*/
public interface ComparisonOperator {
/**
* Returns the operator symbol that represents the comparison.
*
* @return operator symbol
*/
@Override
public String toString();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.exasol.sql.expression.comparison;

/**
* Visitor for {@link Comparison}.
*/
public interface ComparisonVisitor {
public void visit(SimpleComparison simpleComparison);

public void visit(LikeComparison like);
}
Loading

0 comments on commit a041d23

Please sign in to comment.