Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to get Alias to Column/Expression map #1526

Answered by imro2
imro2 asked this question in Q&A
Discussion options

I have seen questions about getting a list of columns and I have looked at the TableNamesFinder class. What I am not able to get my head wrapped around is how would I go about creating a map of aliases to table columns or expressions. I want to create list of table columns used in a select, but have also the alias included. I am attempting to do column to schema validation in the visit column method, but by the time it gets to the column visit the alias seems not available.

You must be logged in to vote

I was able to do it by adding two maps. One would map object to SelectExcpressionItem and would be updated as the visitor descended through the structure so I am always able to tell which SelectExpressionItem is the root of the current branch and then a map to keep track of SelectExpressionItems to a list of columns it has encountered on the way. As noted elsewhere I had to implement retrieval of column names from schema for cases like AllColumns and AllTableColumns.

Replies: 2 comments

Comment options

Maybe you look into: https://github.com/JSQLParser/JSqlParser/wiki/Examples-of-SQL-parsing#apply-aliases-to-all-expressions. This AddAliasesVisitor extracts all expressions and columns and adds aliases. So you could learn from this to read it and create a list.

You must be logged in to vote
0 replies
Comment options

I was able to do it by adding two maps. One would map object to SelectExcpressionItem and would be updated as the visitor descended through the structure so I am always able to tell which SelectExpressionItem is the root of the current branch and then a map to keep track of SelectExpressionItems to a list of columns it has encountered on the way. As noted elsewhere I had to implement retrieval of column names from schema for cases like AllColumns and AllTableColumns.

You must be logged in to vote
0 replies
Answer selected by imro2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /