Skip to content

Commit

Permalink
#94 Added CAST function support
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbraun committed Oct 21, 2020
1 parent 90cdea0 commit 54aea29
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 3 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.0](changes_4.3.0.md)
* [4.2.0](changes_4.2.0.md)
* [4.1.0](changes_4.1.0.md)
* [4.0.0](changes_4.0.0.md)
7 changes: 7 additions & 0 deletions doc/changes/changes_4.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SQL Statement Builder 4.3.0, released 2020-XX-XX

Code Name: CAST function support

## Features / Enhancements

* #94: Added CAST function support
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.exasol</groupId>
<artifactId>sql-statement-builder</artifactId>
<version>4.2.0</version>
<version>4.3.0</version>
<name>Exasol SQL Statement Builder</name>
<description>This module provides a Builder for SQL statements that helps creating the correct structure and
validates variable parts of the statements.
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/exasol/sql/dql/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import java.util.ArrayList;
import java.util.List;

import com.exasol.sql.*;
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.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 @@ -79,6 +84,32 @@ public Select function(final FunctionName functionName, final String derivedColu
return this;
}

/**
* Add a cast function.
*
* @param value value to cast
* @param type type to cast the value to
* @return <code>this</code> instance for fluent programming
*/
public Select cast(final ValueExpression value, final DataType type) {
return this.cast(value, type, "");
}

/**
* 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
* @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);
return this;
}

/**
* Add a User Defined Function.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +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;

Expand Down Expand Up @@ -41,4 +42,6 @@ public interface ValueExpressionVisitor {
public void visit(NullLiteral nullLiteral);

public void visit(BooleanExpression booleanExpression);

public void visit(ExasolCastFunction castFunction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.exasol.sql.expression.function.exasol;

import java.util.List;

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;

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

private ExasolCastFunction(final ValueExpression value, final DataType type) {
super(CAST_FUNCTION_NAME, List.of(value));
this.type = type;
}

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

/**
* Get the value to cast.
*
* @return value to cast
*/
public ValueExpression getValue() {
return this.valueExpressions.get(0);
}

/**
* Get the type to cast the value to.
*
* @return type to cast the value to
*/
public DataType getType() {
return this.type;
}

@Override
public boolean hasParenthesis() {
return true;
}

@Override
public void accept(final ValueExpressionVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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.rendering.ColumnsDefinitionRenderer;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void visit(final FloatLiteral literal) {
}

@Override
public void visit(BigDecimalLiteral literal) {
public void visit(final BigDecimalLiteral literal) {
appendCommaWhenNeeded(literal);
append(literal.toString());
setLastVisited(literal);
Expand Down Expand Up @@ -165,4 +166,16 @@ public void visit(final BooleanExpression booleanExpression) {
booleanExpression.accept(expressionRenderer);
append(expressionRenderer.render());
}

@Override
public void visit(final ExasolCastFunction castFunction) {
appendKeyword("CAST");
startParenthesis();
castFunction.getValue().accept(this);
appendKeyword(" AS");
final ColumnsDefinitionRenderer columnsDefinitionRenderer = new ColumnsDefinitionRenderer(this.config);
castFunction.getType().accept(columnsDefinitionRenderer);
append(columnsDefinitionRenderer.render());
endParenthesis();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.exasol.sql.expression.function.exasol;

import static com.exasol.hamcrest.SqlFragmentRenderResultMatcher.rendersTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

import com.exasol.datatype.type.Varchar;
import com.exasol.sql.StatementFactory;
import com.exasol.sql.dql.select.Select;
import com.exasol.sql.expression.NullLiteral;

class ExasolCastFunctionTest {

@Test
void testRendering() {
final Select select = StatementFactory.getInstance().select().cast(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");
assertThat(select, rendersTo("SELECT CAST(NULL AS VARCHAR(254)) TEST"));
}
}

0 comments on commit 54aea29

Please sign in to comment.