|
| 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.statement.create; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import net.sf.jsqlparser.JSQLParserException; |
| 16 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 17 | +import net.sf.jsqlparser.schema.Table; |
| 18 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 19 | +import net.sf.jsqlparser.util.TableDefinitionTraversal; |
| 20 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 21 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | + |
| 26 | +class PostgreSqlTableInheritanceTest { |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = {"parent", "public.parent", "public.parent, other.parent2", |
| 29 | + "\"Odd.Schema\".\"Parent.Name\""}) |
| 30 | + void modelsAndVisitsParentTables(String parents) throws JSQLParserException { |
| 31 | + for (String definition : new String[] {"()", "(extra INT)", "(LIKE source, extra INT)"}) { |
| 32 | + CreateTable table = (CreateTable) CCJSqlParserUtil.parse( |
| 33 | + "CREATE TABLE child " + definition + " INHERITS (" + parents |
| 34 | + + ") WITH (fillfactor = 80)"); |
| 35 | + List<Table> inherited = table.getInherits(); |
| 36 | + assertEquals(parents.split(", ").length, inherited.size()); |
| 37 | + assertEquals(parents.split(", ")[0], inherited.get(0).getFullyQualifiedName()); |
| 38 | + List<Table> visited = new ArrayList<>(); |
| 39 | + TableDefinitionTraversal.visit(table, e -> { |
| 40 | + }, visited::add); |
| 41 | + for (Table parent : inherited) { |
| 42 | + assertTrue(visited.stream().anyMatch(t -> t == parent)); |
| 43 | + assertTrue(new TablesNamesFinder().getTables(table) |
| 44 | + .contains(parent.getFullyQualifiedName())); |
| 45 | + } |
| 46 | + roundTrip(table); |
| 47 | + inherited.set(0, new Table("replacement")); |
| 48 | + assertTrue(table.toString().contains("INHERITS (replacement")); |
| 49 | + roundTrip(table); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void preservesEmptyDefinitionsAndCanRemoveInheritance() throws JSQLParserException { |
| 55 | + CreateTable table = |
| 56 | + (CreateTable) CCJSqlParserUtil.parse("CREATE TABLE child () INHERITS (parent)"); |
| 57 | + assertNotNull(table.getTableElements()); |
| 58 | + assertTrue(table.getTableElements().isEmpty()); |
| 59 | + table.setInherits(null); |
| 60 | + assertEquals("CREATE TABLE child ()", table.toString()); |
| 61 | + roundTrip(table); |
| 62 | + assertEquals(2, CCJSqlParserUtil.parseStatements(table + "; SELECT 1").size()); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @ValueSource(strings = {"()", "(parent,)", "(parent AS p)", "(parent + 1)"}) |
| 67 | + void rejectsMalformedParentLists(String parents) { |
| 68 | + assertThrows(JSQLParserException.class, |
| 69 | + () -> CCJSqlParserUtil.parse("CREATE TABLE child () INHERITS " + parents)); |
| 70 | + } |
| 71 | + |
| 72 | + private static void roundTrip(CreateTable table) throws JSQLParserException { |
| 73 | + StringBuilder out = new StringBuilder(); |
| 74 | + table.accept(new StatementDeParser(out)); |
| 75 | + assertEquals(table.toString(), out.toString()); |
| 76 | + assertEquals(out.toString(), CCJSqlParserUtil.parse(out.toString()).toString()); |
| 77 | + } |
| 78 | +} |
0 commit comments