Replies: 3 comments 4 replies
-
Greetings. Maybe this explanations will help: https://manticore-projects.com/JSQLParser/usage.html#build-a-sql-statement |
Beta Was this translation helpful? Give feedback.
-
Ok, now I understand at least what you ask for: For the AST Node "Function" your want to access the parent branch and/or replace the AST Node. I actually have done something similar here: http://jsqlformatter.manticore-projects.com/jsqlformatter/demo.html?args=-c%20MoUQMiDCAqAE0HsD6BVAdgSwB5OhgtgKYDOALgIb4AOAFLAEQDGCANgIz2wCUAUAGIAlAPIBZWPgCeFAEYtCPANw8eoCDFh0mrDrAC08ZABFypQnQDkbAJwB2AAy67bR23MAaWOYneJu-Pl0AE0DzbjCAKlgADgA2ABY7O1gWRSA In this example, you can get the Node from a XSLT Path (and so can get the parent Object). |
Beta Was this translation helpful? Give feedback.
-
Greetings. This example may be useful, although it depends on a small, unpublished amendment of the Grammar, linking the @Test
public void testXPathReplace() throws Exception {
String sqlStr = "select func1( func2( func3( x ) ) ) from tablename where a=b and b=c;";
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
Function func1 = (Function) select.getSelectItem(0).getExpression();
// pretend knowing only `func2` and trying to get its parent
Function func2 = (Function) func1.getParameters().get(0);
SimpleNode node = (SimpleNode) func2.getASTNode().jjtGetParent();
while (node.jjtGetValue()==null) {
node = (SimpleNode) node.jjtGetParent();
}
Function parentFunction = (Function) node.jjtGetValue();
Assertions.assertEquals(func1, parentFunction);
} |
Beta Was this translation helpful? Give feedback.
-
I'm using JsqlParser to parse SQL script, which is always select statement and is written in Spark SQL syntax.
My goal is to travel through all functions and replace them with correct analog to target database (for example postgres).
Using visitor is great feature. but I can only change existing Function object, change function name, change parameters and so on.
But in some cases I need to replace this Function with other Expression, for example with Subtraction.
Look at this example:
SELECT to_unix_timestamp("col1") from mytable; -> this is valid spark sql
SELECT ("col1" - to_date('1970-01-01', 'yyyy-mm-dd')) * 86400 -> this is valid oracle sql
Here i need to change to_unix_timestamp function with Multiplication.
I wish there was an option to get parentExpression on my Function in a visitor and call something like replaceWith(multiplication)
Beta Was this translation helpful? Give feedback.
All reactions