Search code examples
javasearchsql-parser

Usage of SQL parsers for java for search scenarios


Can anyone suggest some good implementation examples or usage scenarios where SQL parsers can be used for java.

I have an application where we need to filter data to be presented on UI based on certain parameters, sort criteria etc. I have some doubts regarding this:

1)Can this be an ideal solution for this?

2)How can UI play an role for providing query to the Java layer?


Solution

  • Do you want to construct sql query dynamically in your java app and then fetch data using this sql? Let's say you have a sql like this:

    select salary from emp where dept='sales'
    

    after users pick up some other filters from UI, such as age > 40, then sql will be like this:

    select salary from emp where dept='sales' and age > 40
    

    Of course, you maybe want to add more complicated filters or add sort clause as well.

    In order to achieve this, a full SQL Parser is helpful, here is a demo that illustrate how to Deconstruct, Modify, Rebuild a SQL statement based on a Java SQL Parser.