|
10 | 10 | package net.sf.jsqlparser.statement.create; |
11 | 11 |
|
12 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
13 | 14 | import static org.junit.jupiter.api.Assertions.assertNull; |
14 | 15 | import static org.junit.jupiter.api.Assertions.assertSame; |
15 | 16 |
|
@@ -128,6 +129,63 @@ void replacementOptionsTakePrecedenceAndMissingOptionsSurviveDetachment() throws |
128 | 129 | + "NULLS NOT DISTINCT WITH (fillfactor = 90)", statement); |
129 | 130 | } |
130 | 131 |
|
| 132 | + @Test |
| 133 | + void detachedOptionSettersDoNotMutateTheRemovedIndex() throws Exception { |
| 134 | + CreateIndex statement = parse(SQL); |
| 135 | + Index removed = statement.getIndex(); |
| 136 | + statement.setIndex(null); |
| 137 | + |
| 138 | + assertNotSame(removed.getIncludeColumns(), statement.getIncludeColumns()); |
| 139 | + assertNotSame(removed.getStorageParameters(), statement.getStorageParameters()); |
| 140 | + assertSame(removed.getStorageParameters().get(0), statement.getStorageParameters().get(0)); |
| 141 | + List<String> detachedColumns = statement.getIncludeColumns(); |
| 142 | + List<Index.Option> detachedParameters = statement.getStorageParameters(); |
| 143 | + statement.setIndex(null); |
| 144 | + assertSame(detachedColumns, statement.getIncludeColumns()); |
| 145 | + assertSame(detachedParameters, statement.getStorageParameters()); |
| 146 | + statement.getIncludeColumns().add("extra"); |
| 147 | + assertEquals(List.of("payload", "extra"), statement.getIncludeColumns()); |
| 148 | + assertEquals(List.of("payload"), removed.getIncludeColumns()); |
| 149 | + |
| 150 | + statement.setTableSpace("detached_space"); |
| 151 | + statement.setNullsDistinct(true); |
| 152 | + statement.setIncludeColumns(List.of("detached_payload")); |
| 153 | + statement.setStorageParameters(List.of(option(90))); |
| 154 | + assertEquals("fast_space", removed.getTableSpace()); |
| 155 | + assertEquals(Boolean.FALSE, removed.getNullsDistinct()); |
| 156 | + assertEquals(List.of("payload"), removed.getIncludeColumns()); |
| 157 | + assertEquals("80", removed.getStorageParameters().get(0).getValue().toString()); |
| 158 | + |
| 159 | + removed.setTableSpace("external_space"); |
| 160 | + removed.setNullsDistinct(null); |
| 161 | + assertEquals("detached_space", statement.getTableSpace()); |
| 162 | + assertEquals(Boolean.TRUE, statement.getNullsDistinct()); |
| 163 | + statement.setIndex(new Index().withType("UNIQUE").withName("reattached") |
| 164 | + .withColumnsNames(List.of("id"))); |
| 165 | + assertRoundTrip("CREATE UNIQUE INDEX reattached ON t (id) INCLUDE (detached_payload) " |
| 166 | + + "NULLS DISTINCT WITH (fillfactor = 90) TABLESPACE detached_space", statement); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void optionsAreVisitedBeforeAttachmentAndAfterDetachment() throws Exception { |
| 171 | + for (CreateIndex statement : List.of(new CreateIndex(), parse(SQL))) { |
| 172 | + statement.setIndex(null); |
| 173 | + statement.setStorageParameters(List.of(option(80))); |
| 174 | + List<Long> values = new ArrayList<>(); |
| 175 | + ExpressionVisitorAdapter<Void> expressions = new ExpressionVisitorAdapter<Void>() { |
| 176 | + @Override |
| 177 | + public <S> Void visit(LongValue value, S context) { |
| 178 | + assertEquals("context", context); |
| 179 | + values.add(value.getValue()); |
| 180 | + return null; |
| 181 | + } |
| 182 | + }; |
| 183 | + statement.accept(new StatementVisitorAdapter<>(new SelectVisitorAdapter<>(expressions)), |
| 184 | + "context"); |
| 185 | + assertEquals(List.of(80L), values); |
| 186 | + } |
| 187 | + } |
| 188 | + |
131 | 189 | @Test |
132 | 190 | void indexOptionEditsAreVisitedOnceAndCanBeDeparsedThroughCustomVisitors() throws Exception { |
133 | 191 | CreateIndex statement = parse(SQL); |
|
0 commit comments