|
| 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.schema.Column; |
| 17 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 18 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; |
| 22 | + |
| 23 | +class AlterKeyAccessorsTest { |
| 24 | + private List<String> columns(AlterExpression action, boolean primary) { |
| 25 | + return primary ? action.getPkColumns() : action.getUkColumns(); |
| 26 | + } |
| 27 | + |
| 28 | + private void setColumns(AlterExpression action, boolean primary, List<String> columns) { |
| 29 | + if (primary) { |
| 30 | + action.setPkColumns(columns); |
| 31 | + } else { |
| 32 | + action.setUkColumns(columns); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private void assertDeparsed(Alter statement, String expected) { |
| 37 | + assertEquals(expected, statement.toString()); |
| 38 | + StringBuilder output = new StringBuilder(); |
| 39 | + statement.accept(new StatementDeParser(output), null); |
| 40 | + assertEquals(expected, output.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @ValueSource(booleans = {false, true}) |
| 45 | + void readsCurrentIndexAndAppliesSettersAndFluentAdditions(boolean primary) throws Exception { |
| 46 | + String prefix = "ALTER TABLE t ADD " + (primary ? "PRIMARY KEY" : "UNIQUE"); |
| 47 | + Alter statement = (Alter) CCJSqlParserUtil.parse(prefix + " (a)"); |
| 48 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 49 | + Index originalIndex = action.getIndex(); |
| 50 | + originalIndex.setColumnsNames(List.of("b")); |
| 51 | + List<String> snapshot = columns(action, primary); |
| 52 | + assertEquals(List.of("b"), snapshot); |
| 53 | + snapshot.set(0, "c"); |
| 54 | + assertEquals(List.of("b"), columns(action, primary)); |
| 55 | + assertDeparsed(statement, prefix + " (b)"); |
| 56 | + setColumns(action, primary, List.of("c")); |
| 57 | + assertDeparsed(statement, prefix + " (c)"); |
| 58 | + if (primary) { |
| 59 | + action.addPkColumns("d").addPkColumns(List.of("e")); |
| 60 | + } else { |
| 61 | + action.addUkColumns("d").addUkColumns(List.of("e")); |
| 62 | + } |
| 63 | + assertEquals(List.of("c", "d", "e"), columns(action, primary)); |
| 64 | + assertSame(originalIndex, action.getIndex()); |
| 65 | + assertDeparsed(statement, prefix + " (c, d, e)"); |
| 66 | + setColumns(action, primary, null); |
| 67 | + assertTrue(columns(action, primary).isEmpty()); |
| 68 | + assertTrue(originalIndex.getColumns().isEmpty()); |
| 69 | + } |
| 70 | + |
| 71 | + @ParameterizedTest |
| 72 | + @ValueSource(booleans = {false, true}) |
| 73 | + void replacingWithBareNamesDropsElementOptionsButKeepsIndexMetadata(boolean primary) |
| 74 | + throws Exception { |
| 75 | + String prefix = "ALTER TABLE t ADD CONSTRAINT key_t " |
| 76 | + + (primary ? "PRIMARY KEY" : "UNIQUE"); |
| 77 | + Alter statement = (Alter) CCJSqlParserUtil.parse(prefix + " (a DESC, b) DEFERRABLE"); |
| 78 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 79 | + Index index = action.getIndex(); |
| 80 | + Index.ColumnParams first = index.getColumns().get(0); |
| 81 | + assertEquals("a DESC", first.toString()); |
| 82 | + setColumns(action, primary, List.of("b", "a")); |
| 83 | + assertEquals(List.of("b", "a"), columns(action, primary)); |
| 84 | + assertNull(index.getColumns().get(1).getSortOrder()); |
| 85 | + assertNull(index.getColumns().get(1).getParams()); |
| 86 | + assertNotSame(first, index.getColumns().get(1)); |
| 87 | + assertSame(index, action.getIndex()); |
| 88 | + assertEquals("key_t", index.getName()); |
| 89 | + assertDeparsed(statement, prefix + " (b, a) DEFERRABLE"); |
| 90 | + } |
| 91 | + |
| 92 | + @ParameterizedTest |
| 93 | + @ValueSource(booleans = {false, true}) |
| 94 | + void settingAnUnchangedSnapshotPreservesSqlButRebuildsPlainElements(boolean primary) |
| 95 | + throws Exception { |
| 96 | + String prefix = "ALTER TABLE t ADD " + (primary ? "PRIMARY KEY" : "UNIQUE"); |
| 97 | + Alter statement = (Alter) CCJSqlParserUtil.parse(prefix + " (a DESC, b)"); |
| 98 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 99 | + Index index = action.getIndex(); |
| 100 | + Index.ColumnParams expression = new Index.ColumnParams(new Column("a")) |
| 101 | + .withExpressionParenthesized(false) |
| 102 | + .withSortOrder(Index.ColumnParams.SortOrder.DESC); |
| 103 | + index.getColumns().set(0, expression); |
| 104 | + setColumns(action, primary, new ArrayList<>(columns(action, primary))); |
| 105 | + Index.ColumnParams replacement = index.getColumns().get(0); |
| 106 | + assertNotSame(expression, replacement); |
| 107 | + assertEquals("a DESC", replacement.getColumnName()); |
| 108 | + assertNull(replacement.getExpression()); |
| 109 | + assertNull(replacement.getSortOrder()); |
| 110 | + assertDeparsed(statement, prefix + " (a DESC, b)"); |
| 111 | + } |
| 112 | + |
| 113 | + @ParameterizedTest |
| 114 | + @ValueSource(booleans = {false, true}) |
| 115 | + void structuredReorderingRetainsElementIdentityAndOptions(boolean primary) throws Exception { |
| 116 | + String prefix = "ALTER TABLE t ADD " + (primary ? "PRIMARY KEY" : "UNIQUE"); |
| 117 | + Alter statement = (Alter) CCJSqlParserUtil.parse(prefix + " (a DESC, b)"); |
| 118 | + AlterExpression action = statement.getAlterExpressions().get(0); |
| 119 | + Index index = action.getIndex(); |
| 120 | + Index.ColumnParams first = index.getColumns().get(0); |
| 121 | + Index.ColumnParams second = index.getColumns().get(1); |
| 122 | + index.setColumns(List.of(second, first)); |
| 123 | + assertSame(first, index.getColumns().get(1)); |
| 124 | + assertSame(second, index.getColumns().get(0)); |
| 125 | + assertEquals(Index.ColumnParams.SortOrder.DESC, first.getSortOrder()); |
| 126 | + assertEquals(List.of("b", "a DESC"), columns(action, primary)); |
| 127 | + assertDeparsed(statement, prefix + " (b, a DESC)"); |
| 128 | + } |
| 129 | + |
| 130 | + @ParameterizedTest |
| 131 | + @ValueSource(booleans = {false, true}) |
| 132 | + void supportsAnIndexWithoutColumnsAndFluentPopulation(boolean primary) { |
| 133 | + Index index = new Index().withType(primary ? "PRIMARY KEY" : "UNIQUE"); |
| 134 | + AlterExpression action = new AlterExpression().withOperation(AlterOperation.ADD) |
| 135 | + .withIndex(index); |
| 136 | + assertTrue(index.getColumnsNames().isEmpty()); |
| 137 | + assertTrue(columns(action, primary).isEmpty()); |
| 138 | + index.getColumnsNames().add("detached"); |
| 139 | + assertNull(index.getColumns()); |
| 140 | + if (primary) { |
| 141 | + action.addPkColumns("a"); |
| 142 | + } else { |
| 143 | + action.addUkColumns("a"); |
| 144 | + } |
| 145 | + assertEquals(List.of("a"), columns(action, primary)); |
| 146 | + index.setColumns(null); |
| 147 | + assertTrue(columns(action, primary).isEmpty()); |
| 148 | + setColumns(action, primary, List.of("b")); |
| 149 | + assertEquals(List.of("b"), index.getColumnsNames()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void retainsLegacyOnlyConstructionAndIgnoresUnrelatedIndexes() { |
| 154 | + AlterExpression action = new AlterExpression().withOperation(AlterOperation.ADD) |
| 155 | + .withPkColumns(new ArrayList<>(List.of("a"))); |
| 156 | + action.getPkColumns().add("b"); |
| 157 | + assertEquals("ADD PRIMARY KEY (a, b)", action.toString()); |
| 158 | + Index unrelated = new Index().withType("INDEX").withColumnsNames(List.of("other")); |
| 159 | + action.setIndex(unrelated); |
| 160 | + action.setPkColumns(List.of("pk")); |
| 161 | + action.setUkColumns(List.of("uk")); |
| 162 | + assertEquals(List.of("pk"), action.getPkColumns()); |
| 163 | + assertEquals(List.of("uk"), action.getUkColumns()); |
| 164 | + assertEquals(List.of("other"), unrelated.getColumnsNames()); |
| 165 | + } |
| 166 | +} |
0 commit comments