|
| 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.alter; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 17 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 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 AlterKeyAccessorsTest { |
| 23 | + private List<String> columns(AlterExpression action, boolean primary) { |
| 24 | + return primary ? action.getPkColumns() : action.getUkColumns(); |
| 25 | + } |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(booleans = {false, true}) |
| 29 | + void keepsGettersSettersAndMutableListsConnectedToTheIndex(boolean primary) throws Exception { |
| 30 | + String prefix = "ALTER TABLE t ADD " + (primary ? "PRIMARY KEY" : "UNIQUE"); |
| 31 | + Alter statement = (Alter) CCJSqlParserUtil.parse(prefix + " (a)"); |
| 32 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 33 | + Index originalIndex = action.getIndex(); |
| 34 | + action.getIndex().setColumnsNames(List.of("b")); |
| 35 | + assertEquals(List.of("b"), columns(action, primary)); |
| 36 | + if (primary) { |
| 37 | + action.setPkColumns(List.of("c")); |
| 38 | + } else { |
| 39 | + action.setUkColumns(List.of("c")); |
| 40 | + } |
| 41 | + assertEquals(prefix + " (c)", statement.toString()); |
| 42 | + columns(action, primary).add("d"); |
| 43 | + columns(action, primary).set(0, "e"); |
| 44 | + assertEquals(List.of("e", "d"), action.getIndex().getColumnsNames()); |
| 45 | + assertEquals("e", columns(action, primary).remove(0)); |
| 46 | + if (primary) { |
| 47 | + action.addPkColumns("f"); |
| 48 | + } else { |
| 49 | + action.addUkColumns("f"); |
| 50 | + } |
| 51 | + assertEquals(prefix + " (d, f)", statement.toString()); |
| 52 | + assertSame(originalIndex, action.getIndex()); |
| 53 | + StringBuilder output = new StringBuilder(); |
| 54 | + statement.accept(new StatementDeParser(output), null); |
| 55 | + assertEquals(statement.toString(), output.toString()); |
| 56 | + columns(action, primary).clear(); |
| 57 | + assertTrue(action.getIndex().getColumns().isEmpty()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void preservesStructuredElementsAndIndexMetadataForUnchangedKeys() throws Exception { |
| 62 | + Alter statement = (Alter) CCJSqlParserUtil.parse( |
| 63 | + "ALTER TABLE t ADD UNIQUE (a, b) DEFERRABLE", |
| 64 | + parser -> parser.withDialect( |
| 65 | + net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect.POSTGRESQL)); |
| 66 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 67 | + Index index = action.getIndex(); |
| 68 | + index.setName("uq"); |
| 69 | + Index.ColumnParams expression = |
| 70 | + new Index.ColumnParams(new net.sf.jsqlparser.schema.Column("a")) |
| 71 | + .withExpressionParenthesized(false); |
| 72 | + index.getColumns().set(0, expression); |
| 73 | + String original = statement.toString(); |
| 74 | + action.setUkColumns(new ArrayList<>(action.getUkColumns())); |
| 75 | + assertSame(expression, index.getColumns().get(0)); |
| 76 | + assertEquals(original, statement.toString()); |
| 77 | + action.addUkColumns("c"); |
| 78 | + assertSame(expression, index.getColumns().get(0)); |
| 79 | + assertEquals("uq", index.getName()); |
| 80 | + assertEquals(original.replace("(a, b)", "(a, b, c)"), statement.toString()); |
| 81 | + assertTrue(statement.toString().endsWith("DEFERRABLE")); |
| 82 | + assertEquals(List.of("a", "b", "c"), action.getUkColumns()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void retainsLegacyOnlyConstructionAndAllowsClearingStructuredKeys() { |
| 87 | + AlterExpression action = new AlterExpression().withOperation(AlterOperation.ADD) |
| 88 | + .withPkColumns(new ArrayList<>(List.of("a"))); |
| 89 | + action.addPkColumns("b"); |
| 90 | + assertEquals("ADD PRIMARY KEY (a, b)", action.toString()); |
| 91 | + action.setIndex(new Index().withType("PRIMARY KEY").withColumnsNames(List.of("c"))); |
| 92 | + assertEquals(List.of("c"), action.getPkColumns()); |
| 93 | + action.setPkColumns(null); |
| 94 | + assertTrue(action.getPkColumns().isEmpty()); |
| 95 | + } |
| 96 | +} |
0 commit comments