|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2022 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.test; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.expression.LongValue; |
| 14 | +import net.sf.jsqlparser.expression.StringValue; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.Statement; |
| 17 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 18 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 19 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +public class AssortedFeatureTests { |
| 23 | + |
| 24 | + static class ReplaceColumnAndLongValues extends ExpressionDeParser { |
| 25 | + |
| 26 | + @Override |
| 27 | + public void visit(StringValue stringValue) { |
| 28 | + this.getBuffer().append("?"); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void visit(LongValue longValue) { |
| 33 | + this.getBuffer().append("?"); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static String cleanStatement(String sql) throws JSQLParserException { |
| 38 | + StringBuilder buffer = new StringBuilder(); |
| 39 | + ExpressionDeParser expr = new ReplaceColumnAndLongValues(); |
| 40 | + |
| 41 | + SelectDeParser selectDeparser = new SelectDeParser(expr, buffer); |
| 42 | + expr.setSelectVisitor(selectDeparser); |
| 43 | + expr.setBuffer(buffer); |
| 44 | + |
| 45 | + StatementDeParser stmtDeparser = new StatementDeParser(expr, selectDeparser, buffer); |
| 46 | + |
| 47 | + Statement stmt = CCJSqlParserUtil.parse(sql); |
| 48 | + |
| 49 | + stmt.accept(stmtDeparser); |
| 50 | + return stmtDeparser.getBuffer().toString(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testIssue1608() throws JSQLParserException { |
| 55 | + System.out.println(cleanStatement("SELECT 'abc', 5 FROM mytable WHERE col='test'")); |
| 56 | + System.out.println(cleanStatement("UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'")); |
| 57 | + System.out.println(cleanStatement("INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')")); |
| 58 | + System.out.println(cleanStatement("DELETE FROM table1 where col=5 and col2=4")); |
| 59 | + } |
| 60 | +} |
0 commit comments