-
-
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.
- at the moment, only one lambda parameter is supported, sorry Signed-off-by: Andreas Reichel <[email protected]>
- Loading branch information
1 parent
1cd576b
commit 236793a
Showing
8 changed files
with
154 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
57 changes: 57 additions & 0 deletions
57
src/main/java/net/sf/jsqlparser/expression/LambdaExpression.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,57 @@ | ||
package net.sf.jsqlparser.expression; | ||
|
||
import net.sf.jsqlparser.parser.ASTNodeAccessImpl; | ||
|
||
import java.util.List; | ||
|
||
public class LambdaExpression extends ASTNodeAccessImpl implements Expression { | ||
private List<String> identifiers; | ||
private Expression expression; | ||
|
||
public LambdaExpression(List<String> identifiers, Expression expression) { | ||
this.identifiers = identifiers; | ||
this.expression = expression; | ||
} | ||
|
||
public List<String> getIdentifiers() { | ||
return identifiers; | ||
} | ||
|
||
public LambdaExpression setIdentifiers(List<String> identifiers) { | ||
this.identifiers = identifiers; | ||
return this; | ||
} | ||
|
||
public Expression getExpression() { | ||
return expression; | ||
} | ||
|
||
public LambdaExpression setExpression(Expression expression) { | ||
this.expression = expression; | ||
return this; | ||
} | ||
|
||
public StringBuilder appendTo(StringBuilder builder) { | ||
if (identifiers.size() == 1) { | ||
builder.append(identifiers.get(0)); | ||
} else { | ||
int i = 0; | ||
builder.append("( "); | ||
for (String s : identifiers) { | ||
builder.append(i++ > 0 ? ", " : "").append(s); | ||
} | ||
builder.append(" )"); | ||
} | ||
return builder.append(" -> ").append(expression); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return appendTo(new StringBuilder()).toString(); | ||
} | ||
|
||
@Override | ||
public void accept(ExpressionVisitor expressionVisitor) { | ||
expressionVisitor.visit(this); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/test/java/net/sf/jsqlparser/expression/LambdaExpressionTest.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,29 @@ | ||
package net.sf.jsqlparser.expression; | ||
|
||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.test.TestUtils; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class LambdaExpressionTest { | ||
|
||
@Test | ||
void testLambdaFunctionSingleParameter() throws JSQLParserException { | ||
String sqlStr = "select list_transform( split('test', ''), x -> unicode(x) )"; | ||
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true); | ||
} | ||
|
||
@Disabled | ||
@Test | ||
// wip, right now the Grammar works but collides with Multi Value Lists | ||
void testLambdaFunctionMultipleParameter() throws JSQLParserException { | ||
String sqlStr = "SELECT list_transform(\n" + | ||
" [1, 2, 3],\n" + | ||
" x -> list_reduce([4, 5, 6], (a, b) -> a + b) + x\n" + | ||
" )"; | ||
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true); | ||
} | ||
|
||
} |