diff --git a/src/main/java/com/exasol/sql/dql/select/Select.java b/src/main/java/com/exasol/sql/dql/select/Select.java index 8fa4cf8a..5793eb39 100644 --- a/src/main/java/com/exasol/sql/dql/select/Select.java +++ b/src/main/java/com/exasol/sql/dql/select/Select.java @@ -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. @@ -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 this instance for fluent programming */ @@ -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 this 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 this 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; } diff --git a/src/main/java/com/exasol/sql/expression/ValueExpressionVisitor.java b/src/main/java/com/exasol/sql/expression/ValueExpressionVisitor.java index 9a52bbf3..ceeab4fd 100644 --- a/src/main/java/com/exasol/sql/expression/ValueExpressionVisitor.java +++ b/src/main/java/com/exasol/sql/expression/ValueExpressionVisitor.java @@ -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}. @@ -43,5 +41,5 @@ public interface ValueExpressionVisitor { public void visit(BooleanExpression booleanExpression); - public void visit(ExasolCastFunction castFunction); + public void visit(ExasolFunctionCast castFunction); } \ No newline at end of file diff --git a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctions.java b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunction.java similarity index 92% rename from src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctions.java rename to src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunction.java index 54d1d4f1..07342bba 100644 --- a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctions.java +++ b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunction.java @@ -14,6 +14,6 @@ * github issue # 72. *

*/ -public enum ExasolAnalyticFunctions implements FunctionName { +public enum ExasolAnalyticFunction implements FunctionName { ANY, EVERY, LISTAGG } diff --git a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunction.java b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunction.java index 7bed23dc..e5710c3f 100644 --- a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunction.java +++ b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunction.java @@ -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; @@ -14,7 +15,7 @@ public class ExasolFunction extends AbstractFunction { private static final List functionsWithoutParenthesis = Arrays.asList("SYSDATE", "CURRENT_SCHEMA", "CURRENT_SESSION", "CURRENT_STATEMENT", "CURRENT_USER", "ROWNUM", "ROWID", "SCOPE_USER", "USER"); - private ExasolFunction(final FunctionName functionName, final List valueExpressions) { + protected ExasolFunction(final FunctionName functionName, final List valueExpressions) { super(functionName.toString(), valueExpressions); } @@ -31,7 +32,7 @@ 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} */ @@ -39,6 +40,10 @@ public static ExasolFunction of(final FunctionName functionName, final ValueExpr 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); diff --git a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolCastFunction.java b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCast.java similarity index 65% rename from src/main/java/com/exasol/sql/expression/function/exasol/ExasolCastFunction.java rename to src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCast.java index 5a31a3ec..880ee576 100644 --- a/src/main/java/com/exasol/sql/expression/function/exasol/ExasolCastFunction.java +++ b/src/main/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCast.java @@ -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); } /** @@ -57,4 +56,8 @@ public boolean hasParenthesis() { public void accept(final ValueExpressionVisitor visitor) { visitor.visit(this); } + + private enum ExasolScalarFunctionCast implements FunctionName { + CAST + } } \ No newline at end of file diff --git a/src/main/java/com/exasol/sql/expression/rendering/ValueExpressionRenderer.java b/src/main/java/com/exasol/sql/expression/rendering/ValueExpressionRenderer.java index dc767dd2..cf43de05 100644 --- a/src/main/java/com/exasol/sql/expression/rendering/ValueExpressionRenderer.java +++ b/src/main/java/com/exasol/sql/expression/rendering/ValueExpressionRenderer.java @@ -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; @@ -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); diff --git a/src/test/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctionTest.java b/src/test/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctionTest.java index 390a6c55..5d4fd06c 100644 --- a/src/test/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctionTest.java +++ b/src/test/java/com/exasol/sql/expression/function/exasol/ExasolAnalyticFunctionTest.java @@ -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, diff --git a/src/test/java/com/exasol/sql/expression/function/exasol/ExasolCastFunctionTest.java b/src/test/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCastTest.java similarity index 74% rename from src/test/java/com/exasol/sql/expression/function/exasol/ExasolCastFunctionTest.java rename to src/test/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCastTest.java index 7c8c5b07..47278657 100644 --- a/src/test/java/com/exasol/sql/expression/function/exasol/ExasolCastFunctionTest.java +++ b/src/test/java/com/exasol/sql/expression/function/exasol/ExasolFunctionCastTest.java @@ -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")); } } \ No newline at end of file