You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The task here is to build a map, where the alias is the key and the expression to this alias the value of one Map entry.
Select stmt = (Select) CCJSqlParserUtil.parse("SELECT col1 AS a, col2 AS b, col3 AS c FROM table WHERE col1 = 10 AND col2 = 20 AND col3 = 30");
Map<String, Expression> map = new HashMap<>();
for (SelectItem selectItem : ((PlainSelect)stmt.getSelectBody()).getSelectItems()) {
selectItem.accept(new SelectItemVisitorAdapter() {
@Override
public void visit(SelectExpressionItem item) {
map.put(item.getAlias().getName(), item.getExpression());
}
});
}
System.out.println("map " + map);
Selected items of a SQL could be of different type. We are now interested in so called SelectExpressionItems. These are those consisting of an expression optionally named by an alias. So with a SelectItemVisitor we are filtering those items. Directly from our SelectExpressionItem we can get the alias and the expression.