|
| 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 net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | + |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.statement.create.table.CheckConstraint; |
| 18 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 19 | +import net.sf.jsqlparser.statement.create.table.DefaultConstraint; |
| 20 | +import net.sf.jsqlparser.statement.create.table.ExcludeConstraint; |
| 21 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 22 | +import net.sf.jsqlparser.statement.create.table.Index.Kind; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.CsvSource; |
| 26 | +import org.junit.jupiter.params.provider.NullAndEmptySource; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
| 29 | +class IndexKindStateTest { |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @CsvSource({"PRIMARY KEY, PRIMARY_KEY", "unique key, UNIQUE", "UNIQUE INDEX, UNIQUE", |
| 33 | + "KEY, INDEX", "INDEX, INDEX", "FULLTEXT KEY, FULLTEXT", "SPATIAL INDEX, SPATIAL", |
| 34 | + "FOREIGN KEY, FOREIGN_KEY", "CHECK, CHECK", "EXCLUDE, EXCLUDE", "DEFAULT, DEFAULT", |
| 35 | + "BITMAP INDEX, INDEX"}) |
| 36 | + void replacingTypeRefreshesEveryPreviousClassification(String type, Kind expected) { |
| 37 | + for (Kind previous : Kind.values()) { |
| 38 | + Index index = new Index().withType("UNIQUE").withKind(previous); |
| 39 | + index.setType(type); |
| 40 | + assertEquals(type, index.getType()); |
| 41 | + assertEquals(expected, index.getKind(), previous + " -> " + type); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @ParameterizedTest |
| 46 | + @NullAndEmptySource |
| 47 | + @ValueSource(strings = {"CUSTOM", "UNIQUE_CUSTOM", "PRIMARY_CUSTOM", "MONKEY", "INDEXED"}) |
| 48 | + void clearingOrReplacingWithCustomTypeClearsStaleKind(String type) { |
| 49 | + Index index = new Index().withType("UNIQUE").withType(type); |
| 50 | + assertEquals(type, index.getType()); |
| 51 | + assertEquals(Kind.OTHER, index.getKind()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void classificationDoesNotRewriteSpellingOrDependOnLeadingWhitespace() { |
| 56 | + Index index = new Index().withType(" unique\tkey "); |
| 57 | + assertEquals(Kind.UNIQUE, index.getKind()); |
| 58 | + assertEquals(" unique\tkey ", index.getType()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void explicitMetadataSupportsOmittedTypeAndSpecializedConstraints() { |
| 63 | + Index index = new Index().withKind(Kind.INDEX); |
| 64 | + assertNull(index.getType()); |
| 65 | + assertEquals(Kind.INDEX, index.getKind()); |
| 66 | + index.setType("UNIQUE"); |
| 67 | + assertEquals(Kind.UNIQUE, index.getKind()); |
| 68 | + assertEquals(Kind.CHECK, new CheckConstraint().getKind()); |
| 69 | + assertEquals(Kind.EXCLUDE, new ExcludeConstraint().getKind()); |
| 70 | + assertEquals(Kind.DEFAULT, new DefaultConstraint().getKind()); |
| 71 | + } |
| 72 | + |
| 73 | + // Both the original and rewritten SQL execute on MySQL 8.4 and PostgreSQL 18. |
| 74 | + @ParameterizedTest |
| 75 | + @CsvSource({"UNIQUE, PRIMARY KEY, PRIMARY_KEY", "PRIMARY KEY, UNIQUE, UNIQUE"}) |
| 76 | + void rewritingPostgreSqlConstraintPreservesKindThroughDeparsing(String before, String after, |
| 77 | + Kind expected) throws JSQLParserException { |
| 78 | + CreateTable table = (CreateTable) assertSqlCanBeParsedAndDeparsed( |
| 79 | + "CREATE TABLE index_state (id INT, CONSTRAINT key_state " + before + " (id))"); |
| 80 | + Index index = table.getIndexes().get(0); |
| 81 | + index.setType(after); |
| 82 | + assertEquals(expected, index.getKind()); |
| 83 | + CreateTable reparsed = (CreateTable) assertSqlCanBeParsedAndDeparsed(table.toString()); |
| 84 | + assertEquals(expected, reparsed.getIndexes().get(0).getKind()); |
| 85 | + assertEquals("key_state", reparsed.getIndexes().get(0).getName()); |
| 86 | + } |
| 87 | + |
| 88 | + // MySQL KEY and INDEX are aliases; UNIQUE changes the constraint's meaning. |
| 89 | + @ParameterizedTest |
| 90 | + @CsvSource({"UNIQUE KEY, KEY, INDEX", "KEY, UNIQUE KEY, UNIQUE", |
| 91 | + "UNIQUE INDEX, INDEX, INDEX", "INDEX, UNIQUE INDEX, UNIQUE"}) |
| 92 | + void rewritingMySqlIndexPreservesKindThroughDeparsing(String before, String after, |
| 93 | + Kind expected) throws JSQLParserException { |
| 94 | + CreateTable table = (CreateTable) assertSqlCanBeParsedAndDeparsed( |
| 95 | + "CREATE TABLE index_state (id INT, " + before + " key_state (id))"); |
| 96 | + table.getIndexes().get(0).setType(after); |
| 97 | + assertEquals(expected, table.getIndexes().get(0).getKind()); |
| 98 | + CreateTable reparsed = (CreateTable) assertSqlCanBeParsedAndDeparsed(table.toString()); |
| 99 | + assertEquals(expected, reparsed.getIndexes().get(0).getKind()); |
| 100 | + } |
| 101 | +} |
0 commit comments