Question

I am trying to use ANTLR to extract information from a PLSQL file. I am using porcelli PLSQL grammar, using which ANTLR spits out AST on my input plsql file. I need to read the returned "CommonTree" class (which represents the AST) and obtain different information - say name of tables and related columns. I was thinking if it would make sense to use the visitor pattern to collect information about tables and related columns on a particular table. For instance, a query like this

SELECT s.name from students s, departments d WHERE d.did=10 and s.sid=d.did

will be shown in AST as

AST

Obtaining table name and related columns here will involve capturing aliases first from the FROM element and then matching with columns used in SELECT_LIST. Information about tables and columns is hidden deep in leaf nodes under repeatedly used elements such as "ANY_ELEMENT".

So, How to go about using a visitor pattern here? Would I end up with way too many visitors because there are potentially a lot of element types? Is Visitor pattern relevant here?

EDIT

After thinking over it for a while, I am nearing a conclusion that Visitor pattern wouldn't make sense in this scenario. Given the fact that the data structure that needs to be visited is a tree and there are potentially so many node types (select, update, insert, delete, from, where, into..), defining what should happen on visiting each of these node types for any given visitor could result in hundreds of methods per visitor class!

Was it helpful?

Solution

As updated in my last edit, I resolved this by not implementing a visitor pattern because such a pattern would require me to create all node types and for PL/SQL there will be too many.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top