-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Zhang, Dequn <deqzhang@paypal.com>
- Loading branch information
Showing
8 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/net/sf/jsqlparser/expression/SafeCastExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/*- | ||
* #%L | ||
* JSQLParser library | ||
* %% | ||
* Copyright (C) 2004 - 2019 JSQLParser | ||
* %% | ||
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0 | ||
* #L% | ||
*/ | ||
package net.sf.jsqlparser.expression; | ||
|
||
import net.sf.jsqlparser.parser.ASTNodeAccessImpl; | ||
import net.sf.jsqlparser.statement.create.table.ColDataType; | ||
|
||
public class SafeCastExpression extends ASTNodeAccessImpl implements Expression { | ||
|
||
private Expression leftExpression; | ||
private ColDataType type; | ||
private RowConstructor rowConstructor; | ||
private boolean useCastKeyword = true; | ||
|
||
public RowConstructor getRowConstructor() { | ||
return rowConstructor; | ||
} | ||
|
||
public void setRowConstructor(RowConstructor rowConstructor) { | ||
this.rowConstructor = rowConstructor; | ||
this.type = null; | ||
} | ||
|
||
public SafeCastExpression withRowConstructor(RowConstructor rowConstructor) { | ||
setRowConstructor(rowConstructor); | ||
return this; | ||
} | ||
|
||
public ColDataType getType() { | ||
return type; | ||
} | ||
|
||
public void setType(ColDataType type) { | ||
this.type = type; | ||
this.rowConstructor = null; | ||
} | ||
|
||
public Expression getLeftExpression() { | ||
return leftExpression; | ||
} | ||
|
||
public void setLeftExpression(Expression expression) { | ||
leftExpression = expression; | ||
} | ||
|
||
@Override | ||
public void accept(ExpressionVisitor expressionVisitor) { | ||
expressionVisitor.visit(this); | ||
} | ||
|
||
public boolean isUseCastKeyword() { | ||
return useCastKeyword; | ||
} | ||
|
||
public void setUseCastKeyword(boolean useCastKeyword) { | ||
this.useCastKeyword = useCastKeyword; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (useCastKeyword) { | ||
return rowConstructor!=null | ||
? "SAFE_CAST(" + leftExpression + " AS " + rowConstructor.toString() + ")" | ||
: "SAFE_CAST(" + leftExpression + " AS " + type.toString() + ")"; | ||
} else { | ||
return leftExpression + "::" + type.toString(); | ||
} | ||
} | ||
|
||
public SafeCastExpression withType(ColDataType type) { | ||
this.setType(type); | ||
return this; | ||
} | ||
|
||
public SafeCastExpression withUseCastKeyword(boolean useCastKeyword) { | ||
this.setUseCastKeyword(useCastKeyword); | ||
return this; | ||
} | ||
|
||
public SafeCastExpression withLeftExpression(Expression leftExpression) { | ||
this.setLeftExpression(leftExpression); | ||
return this; | ||
} | ||
|
||
public <E extends Expression> E getLeftExpression(Class<E> type) { | ||
return type.cast(getLeftExpression()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/net/sf/jsqlparser/expression/SafeCastExpressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/*- | ||
* #%L | ||
* JSQLParser library | ||
* %% | ||
* Copyright (C) 2004 - 2022 JSQLParser | ||
* %% | ||
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0 | ||
* #L% | ||
*/ | ||
package net.sf.jsqlparser.expression; | ||
|
||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.test.TestUtils; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SafeCastExpressionTest { | ||
|
||
@Test | ||
public void testSafeCast() throws JSQLParserException { | ||
TestUtils.assertExpressionCanBeParsedAndDeparsed("SAFE_CAST(ROW(dataid, value, calcMark) AS ROW(datapointid CHAR, value CHAR, calcMark CHAR))", true); | ||
TestUtils.assertExpressionCanBeParsedAndDeparsed("SAFE_CAST(ROW(dataid, value, calcMark) AS testcol)", true); | ||
} | ||
} |