Skip to content

Commit

Permalink
#94: Refactored the Cast function
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Oct 22, 2020
1 parent c940c39 commit 21814a2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 47 deletions.
35 changes: 14 additions & 21 deletions src/main/java/com/exasol/sql/dql/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import java.util.ArrayList;
import java.util.List;

import com.exasol.datatype.type.DataType;
import com.exasol.sql.AbstractFragment;
import com.exasol.sql.ColumnsDefinition;
import com.exasol.sql.DerivedColumn;
import com.exasol.sql.SqlStatement;
import com.exasol.sql.*;
import com.exasol.sql.expression.*;
import com.exasol.sql.expression.function.Function;
import com.exasol.sql.expression.function.FunctionName;
import com.exasol.sql.expression.function.exasol.ExasolCastFunction;

/**
* This class implements an SQL {@link Select} statement.
Expand Down Expand Up @@ -60,7 +55,7 @@ public Select field(final String... names) {
/**
* Add a function.
*
* @param functionName a name of function
* @param functionName name of function
* @param valueExpressions zero or more value expression
* @return <code>this</code> instance for fluent programming
*/
Expand All @@ -85,28 +80,26 @@ public Select function(final FunctionName functionName, final String derivedColu
}

/**
* Add a cast function.
* Add a function.
*
* @param value value to cast
* @param type type to cast the value to
* @param function function
* @param derivedColumnName name under which you can refer to the derived column
* @return <code>this</code> instance for fluent programming
*/
public Select cast(final ValueExpression value, final DataType type) {
return this.cast(value, type, "");
public Select function(final Function function, final String derivedColumnName) {
final DerivedColumn derivedColumn = new DerivedColumn(this, function, derivedColumnName);
this.derivedColumns.add(derivedColumn);
return this;
}

/**
* Add a cast function.
*
* @param value value to cast
* @param type type to cast the value to
* @param derivedColumnName name under which you can refer to the derived column
* Add a function.
*
* @param function function
* @return <code>this</code> instance for fluent programming
*/
public Select cast(final ValueExpression value, final DataType type, final String derivedColumnName) {
final Function castFunction = ExasolCastFunction.of(value, type);
final DerivedColumn derivedColumn = new DerivedColumn(this, castFunction, derivedColumnName);
this.derivedColumns.add(derivedColumn);
public Select function(final Function function) {
function(function, "");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.exasol.sql.expression;

import com.exasol.sql.UnnamedPlaceholder;
import com.exasol.sql.expression.function.exasol.ExasolCastFunction;
import com.exasol.sql.expression.function.exasol.ExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolUdf;
import com.exasol.sql.expression.function.exasol.*;

/**
* Visitor interface for a {@link BooleanTerm}.
Expand Down Expand Up @@ -43,5 +41,5 @@ public interface ValueExpressionVisitor {

public void visit(BooleanExpression booleanExpression);

public void visit(ExasolCastFunction castFunction);
public void visit(ExasolFunctionCast castFunction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* <a href="https://github.com/exasol/sql-statement-builder/issues/72"> github issue # 72</a>.
* </p>
*/
public enum ExasolAnalyticFunctions implements FunctionName {
public enum ExasolAnalyticFunction implements FunctionName {
ANY, EVERY, LISTAGG
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import com.exasol.datatype.type.DataType;
import com.exasol.sql.expression.ValueExpression;
import com.exasol.sql.expression.ValueExpressionVisitor;
import com.exasol.sql.expression.function.AbstractFunction;
Expand All @@ -14,7 +15,7 @@ public class ExasolFunction extends AbstractFunction {
private static final List<String> functionsWithoutParenthesis = Arrays.asList("SYSDATE", "CURRENT_SCHEMA",
"CURRENT_SESSION", "CURRENT_STATEMENT", "CURRENT_USER", "ROWNUM", "ROWID", "SCOPE_USER", "USER");

private ExasolFunction(final FunctionName functionName, final List<ValueExpression> valueExpressions) {
protected ExasolFunction(final FunctionName functionName, final List<ValueExpression> valueExpressions) {
super(functionName.toString(), valueExpressions);
}

Expand All @@ -31,14 +32,18 @@ public static ExasolFunction of(final FunctionName functionName) {
/**
* Create a new {@link ExasolFunction} instance.
*
* @param functionName name of the function
* @param functionName name of the function
* @param valueExpressions zero or more value expressions
* @return new {@link ExasolFunction}
*/
public static ExasolFunction of(final FunctionName functionName, final ValueExpression... valueExpressions) {
return new ExasolFunction(functionName, Arrays.asList(valueExpressions));
}

public static ExasolFunction castScalarFunction(final ValueExpression valueExpression, final DataType dataType) {
return ExasolFunctionCast.of(valueExpression, dataType);
}

@Override
public boolean hasParenthesis() {
return !functionsWithoutParenthesis.contains(this.functionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
import com.exasol.datatype.type.DataType;
import com.exasol.sql.expression.ValueExpression;
import com.exasol.sql.expression.ValueExpressionVisitor;
import com.exasol.sql.expression.function.AbstractFunction;
import com.exasol.sql.expression.function.FunctionName;

/**
* This class represents the Exasol CAST function.
*/
public class ExasolCastFunction extends AbstractFunction {
private static final String FUNCTION_NAME = "CAST";
public class ExasolFunctionCast extends ExasolFunction {
private final DataType type;

private ExasolCastFunction(final ValueExpression value, final DataType type) {
super(FUNCTION_NAME, List.of(value));
private ExasolFunctionCast(final ValueExpression value, final DataType type) {
super(ExasolScalarFunctionCast.CAST, List.of(value));
this.type = type;
}

/**
* Create a new {@link ExasolCastFunction} instance.
* Create a new {@link ExasolFunctionCast} instance.
*
* @param valueExpression value to cast
* @param type type to cast the value to
* @return new {@link ExasolCastFunction}
* @return new {@link ExasolFunctionCast}
*/
public static ExasolCastFunction of(final ValueExpression valueExpression, final DataType type) {
return new ExasolCastFunction(valueExpression, type);
public static ExasolFunctionCast of(final ValueExpression valueExpression, final DataType type) {
return new ExasolFunctionCast(valueExpression, type);
}

/**
Expand Down Expand Up @@ -57,4 +56,8 @@ public boolean hasParenthesis() {
public void accept(final ValueExpressionVisitor visitor) {
visitor.visit(this);
}

private enum ExasolScalarFunctionCast implements FunctionName {
CAST
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.exasol.sql.UnnamedPlaceholder;
import com.exasol.sql.expression.*;
import com.exasol.sql.expression.function.Function;
import com.exasol.sql.expression.function.exasol.ExasolCastFunction;
import com.exasol.sql.expression.function.exasol.ExasolFunction;
import com.exasol.sql.expression.function.exasol.ExasolUdf;
import com.exasol.sql.expression.function.exasol.*;
import com.exasol.sql.rendering.ColumnsDefinitionRenderer;
import com.exasol.sql.rendering.StringRendererConfig;

Expand Down Expand Up @@ -169,7 +167,7 @@ public void visit(final BooleanExpression booleanExpression) {
}

@Override
public void visit(final ExasolCastFunction castFunction) {
public void visit(final ExasolFunctionCast castFunction) {
appendKeyword("CAST");
startParenthesis();
castFunction.getValue().accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExasolAnalyticFunctionTest {
void testAggregateFunctionCoalesce() {
final Select select = StatementFactory.getInstance().select() //
.field("department") //
.function(ExasolAnalyticFunctions.ANY, " ANY_ ", BooleanTerm.lt(column("age"), integerLiteral(30)));
.function(ExasolAnalyticFunction.ANY, " ANY_ ", BooleanTerm.lt(column("age"), integerLiteral(30)));
select.from().table("employee_table");
select.groupBy(column("department"));
assertThat(select,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import com.exasol.sql.dql.select.Select;
import com.exasol.sql.expression.NullLiteral;

class ExasolCastFunctionTest {

class ExasolFunctionCastTest {
@Test
void testRendering() {
final Select select = StatementFactory.getInstance().select().cast(NullLiteral.nullLiteral(), new Varchar(254));
final Select select = StatementFactory.getInstance().select()
.function(ExasolFunction.castScalarFunction(NullLiteral.nullLiteral(), new Varchar(254)));
assertThat(select, rendersTo("SELECT CAST(NULL AS VARCHAR(254))"));
}

@Test
void testRenderingWithName() {
final Select select = StatementFactory.getInstance().select().cast(NullLiteral.nullLiteral(), new Varchar(254),
"TEST");
final Select select = StatementFactory.getInstance().select()
.function(ExasolFunction.castScalarFunction(NullLiteral.nullLiteral(), new Varchar(254)), "TEST");
assertThat(select, rendersTo("SELECT CAST(NULL AS VARCHAR(254)) TEST"));
}
}

0 comments on commit 21814a2

Please sign in to comment.