Skip to content

Commit 905a623

Browse files
committed
allow complete type in cast expression
1 parent 1b3c507 commit 905a623

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/main/java/net/sf/jsqlparser/expression/CastExpression.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,23 @@
2121
*/
2222
package net.sf.jsqlparser.expression;
2323

24+
import net.sf.jsqlparser.statement.create.table.ColDataType;
25+
2426
/**
2527
*
2628
* @author tw
2729
*/
2830
public class CastExpression implements Expression {
2931

3032
private Expression leftExpression;
31-
private String typeName;
33+
private ColDataType type;
3234

33-
public String getTypeName() {
34-
return typeName;
35+
public ColDataType getType() {
36+
return type;
3537
}
3638

37-
public void setTypeName(String typeName) {
38-
this.typeName = typeName;
39+
public void setType(ColDataType type) {
40+
this.type = type;
3941
}
4042

4143
public Expression getLeftExpression() {
@@ -53,6 +55,6 @@ public void accept(ExpressionVisitor expressionVisitor) {
5355

5456
@Override
5557
public String toString() {
56-
return "CAST(" + leftExpression + " AS " + typeName + ")";
58+
return "CAST(" + leftExpression + " AS " + type.toString() + ")";
5759
}
5860
}

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public void visit(CastExpression cast) {
471471
buffer.append("CAST(");
472472
buffer.append(cast.getLeftExpression());
473473
buffer.append(" AS ");
474-
buffer.append(cast.getTypeName());
474+
buffer.append(cast.getType());
475475
buffer.append(")");
476476
}
477477

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,20 +1565,20 @@ ExtractExpression ExtractExpression() :
15651565
CastExpression CastExpression():
15661566
{
15671567
CastExpression retval = new CastExpression();
1568-
String typeName = null;
1568+
ColDataType type = null;
15691569
Expression expression = null;
15701570
}
15711571
{
15721572
<K_CAST>
15731573
"("
15741574
expression=SimpleExpression()
15751575
<K_AS>
1576-
typeName=RelObjectName()
1576+
type=ColDataType()
15771577
")"
15781578

15791579
{
15801580
retval.setLeftExpression(expression);
1581-
retval.setTypeName(typeName);
1581+
retval.setType(type);
15821582
return retval;
15831583
}
15841584
}

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ public void testCastInCast2() throws JSQLParserException {
589589
String stmt = "SELECT CAST('test' + CAST(assertEqual AS numeric) AS varchar) FROM tabelle1";
590590
assertSqlCanBeParsedAndDeparsed(stmt);
591591
}
592+
593+
public void testCastTypeProblem() throws JSQLParserException {
594+
String stmt = "SELECT CAST(col1 AS varchar (256)) FROM tabelle1";
595+
assertSqlCanBeParsedAndDeparsed(stmt);
596+
}
592597

593598
public void testCaseElseAddition() throws JSQLParserException {
594599
String stmt = "SELECT CASE WHEN 1 + 3 > 20 THEN 0 ELSE 1000 + 1 END AS d FROM dual";

0 commit comments

Comments
 (0)