JSqlParser - Throw error if functionality is not supported

120 Views Asked by At

For a tiny database that I am building, I would like to add support for a very small subset of SQL. I am thinking about using the JqlParser library (instead of writing my own parser).

I was thinking about using the visitor pattern as described in the documentation. By default, if I just use the provided example which has visitors for Select, PlainSelect, Column, and EqualsTo, and the input is an insert statement or has group by, having, etc., the non-supported things are just getting ignored.

Is it possible to identify and reject SQL queries (e.g., throw an Exception) that use statements/features for which I didn't implement a visitor adapter? Or is there an alternative way of using the library that is better suited?

1

There are 1 best solutions below

0
Maurice Perry On

As you may have noticed, you often have a visitor interface, such as ExpressionVisitor as well as an adapter class, such as ExpressionVisitorAdapter that implements that interface, and provide a default implementation for each method; and most of them are empty. You could write your own adapter class that would implement each method by throwing an exception instead of doing nothing. That way if you don't override some method, you will get an exception whenever it's called.