|
| 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.*; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.create.table.*; |
| 17 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.ValueSource; |
| 20 | + |
| 21 | +class KeyColumnSourceMutationTest { |
| 22 | + @ParameterizedTest |
| 23 | + @ValueSource(strings = {"INDEX ix (id DESC)", "PRIMARY KEY (id)", "UNIQUE (id DESC)", |
| 24 | + "FOREIGN KEY (id) REFERENCES parent (id)", "EXCLUDE USING btree (id WITH =)"}) |
| 25 | + void clearedNamesCanBeRepopulatedThroughBothAppendApisAndTheLiveList(String declaration) |
| 26 | + throws Exception { |
| 27 | + for (boolean nullClear : new boolean[] {true, false}) { |
| 28 | + for (boolean collectionAppend : new boolean[] {false, true}) { |
| 29 | + CreateTable table = (CreateTable) CCJSqlParserUtil.parse( |
| 30 | + "CREATE TABLE t (id INT, " + declaration + ")"); |
| 31 | + KeyColumnSource key = (KeyColumnSource) table.getTableElements().get(1); |
| 32 | + KeyElement element = key.getColumns().get(0); |
| 33 | + String expected = table.toString(); |
| 34 | + |
| 35 | + key.setColumnsNames(nullClear ? null : List.of()); |
| 36 | + assertNotNull(key.getColumns()); |
| 37 | + assertTrue(key.getColumns().isEmpty()); |
| 38 | + assertTrue(key.getColumnsNames().isEmpty()); |
| 39 | + append(key, element, collectionAppend); |
| 40 | + assertSame(element, key.getColumns().get(0)); |
| 41 | + assertSql(table, expected); |
| 42 | + |
| 43 | + // Clearing and editing through the shared structured API must also work. |
| 44 | + key.setColumnsNames(null); |
| 45 | + key.getColumns().add(element); |
| 46 | + assertSql(table, expected); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void append(KeyColumnSource key, KeyElement element, boolean collection) { |
| 52 | + if (key instanceof Index) { |
| 53 | + if (collection) { |
| 54 | + ((Index) key).addColumns(List.of(element)); |
| 55 | + } else { |
| 56 | + ((Index) key).addColumns(element); |
| 57 | + } |
| 58 | + } else if (key instanceof KeyConstraint) { |
| 59 | + if (collection) { |
| 60 | + ((KeyConstraint) key).addColumns(List.of(element)); |
| 61 | + } else { |
| 62 | + ((KeyConstraint) key).addColumns(element); |
| 63 | + } |
| 64 | + } else if (key instanceof ForeignKeyConstraint) { |
| 65 | + if (collection) { |
| 66 | + ((ForeignKeyConstraint) key).addColumns(List.of(element)); |
| 67 | + } else { |
| 68 | + ((ForeignKeyConstraint) key).addColumns(element); |
| 69 | + } |
| 70 | + } else if (key instanceof ExcludeConstraint) { |
| 71 | + if (collection) { |
| 72 | + ((ExcludeConstraint) key).addColumns(List.of(element)); |
| 73 | + } else { |
| 74 | + ((ExcludeConstraint) key).addColumns(element); |
| 75 | + } |
| 76 | + } else { |
| 77 | + fail("Unexpected key owner: " + key.getClass()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void assertSql(CreateTable table, String expected) throws Exception { |
| 82 | + assertEquals(expected, table.toString()); |
| 83 | + StringBuilder output = new StringBuilder(); |
| 84 | + table.accept(new StatementDeParser(output), null); |
| 85 | + assertEquals(expected, output.toString()); |
| 86 | + assertEquals(expected, CCJSqlParserUtil.parse(output.toString()).toString()); |
| 87 | + } |
| 88 | +} |
0 commit comments