|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2019 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.util; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 14 | +import java.util.Set; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.schema.Table; |
| 17 | +import net.sf.jsqlparser.statement.update.Update; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
| 21 | + |
| 22 | +class UpdateJoinTablesTest { |
| 23 | + @ParameterizedTest |
| 24 | + @ValueSource(strings = { |
| 25 | + "UPDATE target t JOIN source s ON s.id IN (SELECT id FROM hidden) SET t.a = 1", |
| 26 | + "UPDATE target t LEFT JOIN source s ON EXISTS (SELECT 1 FROM hidden h WHERE h.id = s.id) SET t.a = 1", |
| 27 | + "UPDATE target SET a = 1 FROM source s JOIN target t ON s.id IN (SELECT id FROM hidden)", |
| 28 | + "WITH h AS (SELECT id FROM hidden) UPDATE target t JOIN source s ON s.id IN (SELECT id FROM h) SET t.a = 1"}) |
| 29 | + void includesTablesInsideJoinConditions(String sql) throws Exception { |
| 30 | + assertEquals(Set.of("target", "source", "hidden"), TablesNamesFinder.findTables(sql)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void preservesContextAndVisitsEachSourceOnce() throws Exception { |
| 35 | + Update update = (Update) CCJSqlParserUtil.parse( |
| 36 | + "UPDATE target t JOIN source s ON s.id IN (SELECT id FROM hidden) SET t.a = 1"); |
| 37 | + Object context = new Object(); |
| 38 | + java.util.List<String> seen = new java.util.ArrayList<>(); |
| 39 | + TablesNamesFinder<Void> finder = new TablesNamesFinder<Void>() { |
| 40 | + { |
| 41 | + init(false); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public <S> Void visit(Table table, S actual) { |
| 46 | + assertSame(context, actual); |
| 47 | + seen.add(table.getName()); |
| 48 | + return null; |
| 49 | + } |
| 50 | + }; |
| 51 | + update.accept(finder, context); |
| 52 | + assertEquals(java.util.List.of("target", "source", "hidden"), seen); |
| 53 | + } |
| 54 | +} |
0 commit comments