|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 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.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import net.sf.jsqlparser.JSQLParserException; |
| 20 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 21 | +import net.sf.jsqlparser.statement.LikeClause; |
| 22 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
| 23 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 24 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 25 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 26 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +class TableElementMutationTest { |
| 30 | + private static final String SQL = "CREATE TABLE t (LIKE parent INCLUDING DEFAULTS, " |
| 31 | + + "a INT, CONSTRAINT c CHECK (a > 0), b INT)"; |
| 32 | + |
| 33 | + @Test |
| 34 | + void legacyListEditsUpdateOrderedElements() throws JSQLParserException { |
| 35 | + CreateTable table = parse(); |
| 36 | + table.getColumnDefinitions().remove(1); |
| 37 | + table.getIndexes().clear(); |
| 38 | + table.getColumnDefinitions().add(0, column("first")); |
| 39 | + table.getColumnDefinitions().set(1, column("last")); |
| 40 | + assertSql(table, "CREATE TABLE t (LIKE parent INCLUDING DEFAULTS, first INT, last INT)"); |
| 41 | + assertEquals(3, table.getTableElements().size()); |
| 42 | + assertTrue(new TablesNamesFinder().getTables(table).contains("parent")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void orderedListEditsRemainVisibleThroughExistingViews() throws JSQLParserException { |
| 47 | + CreateTable table = parse(); |
| 48 | + List<ColumnDefinition> columns = table.getColumnDefinitions(); |
| 49 | + table.getTableElements().remove(1); |
| 50 | + assertEquals("b", columns.get(0).getColumnName()); |
| 51 | + table.getTableElements().add(column("extra")); |
| 52 | + assertEquals(2, columns.size()); |
| 53 | + columns.clear(); |
| 54 | + assertEquals(2, table.getTableElements().size()); |
| 55 | + assertEquals(1, table.getIndexes().size()); |
| 56 | + assertEquals(1, table.getTableElements(LikeClause.class).size()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void replacingColumnsPreservesOtherElementsAndTheirPositions() throws JSQLParserException { |
| 61 | + CreateTable table = parse(); |
| 62 | + table.setColumnDefinitions(new ArrayList<>(table.getColumnDefinitions())); |
| 63 | + assertSql(table, SQL); |
| 64 | + table.setColumnDefinitions(Collections.singletonList(column("replacement"))); |
| 65 | + assertSql(table, "CREATE TABLE t (LIKE parent INCLUDING DEFAULTS, replacement INT, " |
| 66 | + + "CONSTRAINT c CHECK (a > 0))"); |
| 67 | + table.setIndexes(null); |
| 68 | + table.setColumnDefinitions(null); |
| 69 | + assertSql(table, "CREATE TABLE t (LIKE parent INCLUDING DEFAULTS)"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void fluentAddersCanPassTheirOwnViewsToSetters() throws JSQLParserException { |
| 74 | + CreateTable table = parse(); |
| 75 | + table.addColumnDefinitions(column("extra")); |
| 76 | + assertEquals(3, table.getColumnDefinitions().size()); |
| 77 | + assertEquals(1, table.getTableElements(LikeClause.class).size()); |
| 78 | + table.setIndexes(table.getIndexes()); |
| 79 | + assertEquals(1, table.getIndexes().size()); |
| 80 | + assertSql(table, SQL.substring(0, SQL.length() - 1) + ", extra INT)"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void filteredListHonorsIndexBounds() throws JSQLParserException { |
| 85 | + List<ColumnDefinition> columns = parse().getColumnDefinitions(); |
| 86 | + assertThrows(IndexOutOfBoundsException.class, () -> columns.get(-1)); |
| 87 | + assertThrows(IndexOutOfBoundsException.class, () -> columns.get(2)); |
| 88 | + assertThrows(IndexOutOfBoundsException.class, () -> columns.add(3, column("bad"))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void clearingAllElementsKeepsBothRenderersConsistent() throws JSQLParserException { |
| 93 | + CreateTable table = parse(); |
| 94 | + table.getTableElements().clear(); |
| 95 | + assertSql(table, "CREATE TABLE t ()"); |
| 96 | + assertTrue(table.getColumnDefinitions().isEmpty()); |
| 97 | + assertTrue(table.getIndexes().isEmpty()); |
| 98 | + } |
| 99 | + |
| 100 | + private static CreateTable parse() throws JSQLParserException { |
| 101 | + return (CreateTable) CCJSqlParserUtil.parse(SQL); |
| 102 | + } |
| 103 | + |
| 104 | + private static ColumnDefinition column(String name) { |
| 105 | + return new ColumnDefinition(name, new ColDataType("INT")); |
| 106 | + } |
| 107 | + |
| 108 | + private static void assertSql(CreateTable table, String expected) { |
| 109 | + assertEquals(expected, table.toString()); |
| 110 | + StringBuilder buffer = new StringBuilder(); |
| 111 | + table.accept(new StatementDeParser(buffer), null); |
| 112 | + assertEquals(expected, buffer.toString()); |
| 113 | + } |
| 114 | +} |
0 commit comments