Hi I want to find the Column Names[and their respective] in a SQL query just like the Table names,
The query can be complex, I mean involve sub queries etc...
Just like
SELECT * FROM MY_TABLE1, MY_TABLE2, (SELECT * FROM MY_TABLE3) LEFT OUTER JOIN MY_TABLE4 "+
" WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)
I am sorry for asking this, I am new to Java and dunno the Visitor Design pattern,
any help would be appreciated.
Thank you,
Ashwin.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is there any other method other then implementing Visior to get list of all columns from expression. I am trying to parse sql function. e.g. sum(t1.amount)/count(t1.name) and get all column names. i.e. {"t1.amount", "t1.name"} Thanks
Last edit: Nir Peterson 2018-01-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi I want to find the Column Names[and their respective] in a SQL query just like the Table names,
The query can be complex, I mean involve sub queries etc...
Just like
SELECT * FROM MY_TABLE1, MY_TABLE2, (SELECT * FROM MY_TABLE3) LEFT OUTER JOIN MY_TABLE4 "+
" WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)
I am sorry for asking this, I am new to Java and dunno the Visitor Design pattern,
any help would be appreciated.
Thank you,
Ashwin.
I forgot to mention that I need to get the column in the Group by, Order by or having clauses too.
--
Ashwin
Have a look at src\net\sf\jsqlparser\util\deparser\ExpressionDeParser.java.
Every column should pass through:
public void visit(Column tableColumn)
You can do whatever you want with tableColumn (for example putting that in a vector).
You should:
1) extend ExpressionDeParser.java -> MyExpressionDeParser.java extends ExpressionDeParser.java
2) pass it to the select deparser:
StringBuffer buffer = new StringBuffer();
CCJSqlParserManager parserManager = new CCJSqlParserManager();
String statement = "SELECT * FROM mytable WHERE mytable.col = 9 LIMIT 3, ?";
Select select = (Select) parserManager.parse(new StringReader(statement));
SelectDeParser selectDeParser = new SelectDeParser();
selectDeParser.setBuffer(buffer);
MyExpressionDeParser expressionDeParser = new MyExpressionDeParser(selectDeParser, buffer);
selectDeParser.setExpressionVisitor(expressionDeParser);
select.getSelectBody().accept(selectDeParser);
3) your MyExpressionDeParser.visit(Column tableColumn) method should be called for each column in the where clause
Is there any other method other then implementing Visior to get list of all columns from expression. I am trying to parse sql function. e.g.
sum(t1.amount)/count(t1.name)and get all column names. i.e. {"t1.amount", "t1.name"} ThanksLast edit: Nir Peterson 2018-01-18