Replace multi appearance in sql statement using jsqlparser

1.1k views Asked by At

I am using jsqlparser to parse a SQL string and replace table names in the string.
My input is

 SELECT id, test 
 FROM test1 JOIN test2 
 ON test1.aa = test2.bb 
 WHERE test1.conf = \"test\"
 LIMIT 10"

and my goal output is

SELECT id, test 
FROM test1_suffix 
JOIN test2_suffix 
ON test1_suffix.aa = test2_suffix.bb 
WHERE test1_suffix.conf = \"test\"
LIMIT 10"

And I managed to replace table name by extend the TablesNamesFinder, but it gave me this:

SELECT id, test
FROM test1_suffix 
JOIN test2_suffix 
ON test1.aa = test2.bb 
WHERE test1.conf = \"test\"
LIMIT 10

I say that's half of the job done, but how can I do the rest of my job?

2

There are 2 answers

1
wumpz On BEST ANSWER

So here is a complete (hopefully) example to replace all occurances of table names. The problem is, that JSqlParser does not differ between aliases and table names. There has to be some logic to skip aliases of your sqls, if you do not want to correct those.

The usage of TableNamesFinder does not do the full job, because it traverses the AST only as far as it is needed to find table names and stops then. That is why my example uses the deparsers.

This code transforms

select id, test from test where name = "test"

to

SELECT id, test FROM test_mytest WHERE name = "test"

and

select * from t2 join t1 on t1.aa = t2.bb where t1.a = "someCondition" limit 10       

to

SELECT * FROM t2_mytest JOIN t1_mytest ON t1_mytest.aa = t2_mytest.bb WHERE t1_mytest.a = "someCondition" LIMIT 10

I think that solves your problem.

public class SimpleSqlParser24 {

    public static void main(String args[]) throws JSQLParserException {
        replaceTableName("select id, test from test where name = \"test\"");
        replaceTableName("select * from t2 join t1 on t1.aa = t2.bb where t1.a = \"someCondition\" limit 10");
    }

    private static void replaceTableName(String sql) throws JSQLParserException {
        Select select = (Select) CCJSqlParserUtil.parse(sql);

        StringBuilder buffer = new StringBuilder();
        ExpressionDeParser expressionDeParser = new ExpressionDeParser() {
            @Override
            public void visit(Column tableColumn) {
                if (tableColumn.getTable() != null) {
                    tableColumn.getTable().setName(tableColumn.getTable().getName() + "_mytest");
                }
                super.visit(tableColumn);
            }

        };
        SelectDeParser deparser = new SelectDeParser(expressionDeParser, buffer) {
            @Override
            public void visit(Table tableName) {
                tableName.setName(tableName.getName() + "_mytest");
                super.visit(tableName);
            }
        };
        expressionDeParser.setSelectVisitor(deparser);
        expressionDeParser.setBuffer(buffer);
        select.getSelectBody().accept(deparser);

        System.out.println(buffer.toString());
    }
}
3
Joakim Danielson On

Add an alias when parsing that gets used instead of the table name.

SELECT * 
FROM test a
WHERE a.conf = 'something'

Then this should be changed to, that is the where clause can be the same

SELECT * 
FROM test_suffix a
WHERE a.conf = 'something'