|
| 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.alter; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 18 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 19 | +import net.sf.jsqlparser.schema.Table; |
| 20 | +import net.sf.jsqlparser.statement.create.table.ConstraintUsingIndex; |
| 21 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 22 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 23 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 24 | +import net.sf.jsqlparser.util.validation.ValidationContext; |
| 25 | +import net.sf.jsqlparser.util.validation.metadata.Named; |
| 26 | +import net.sf.jsqlparser.util.validation.metadata.NamedObject; |
| 27 | +import net.sf.jsqlparser.util.validation.metadata.DatabaseMetaDataValidation; |
| 28 | +import net.sf.jsqlparser.util.validation.validator.AlterValidator; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.ValueSource; |
| 32 | + |
| 33 | +class PostgreSqlConstraintUsingIndexTest { |
| 34 | + @ParameterizedTest |
| 35 | + @ValueSource(strings = {"UNIQUE USING INDEX i", "PRIMARY KEY USING INDEX i", |
| 36 | + "CONSTRAINT uq UNIQUE USING INDEX i DEFERRABLE INITIALLY DEFERRED", |
| 37 | + "CONSTRAINT pk PRIMARY KEY USING INDEX i NOT DEFERRABLE INITIALLY IMMEDIATE", |
| 38 | + "CONSTRAINT \"Unique Name\" UNIQUE USING INDEX \"Index Name\""}) |
| 39 | + void roundTripsExistingIndexConstraints(String body) throws JSQLParserException { |
| 40 | + String sql = "ALTER TABLE t ADD " + body; |
| 41 | + Alter alter = parse(sql); |
| 42 | + ConstraintUsingIndex constraint = assertInstanceOf(ConstraintUsingIndex.class, |
| 43 | + alter.getAlterExpressions().get(0).getIndex()); |
| 44 | + assertNull(constraint.getColumns()); |
| 45 | + assertNull(constraint.getIndexName()); |
| 46 | + assertNull(constraint.getUsing()); |
| 47 | + assertEquals(sql, alter.toString()); |
| 48 | + roundTrip(alter); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void supportsMutationAndConstruction() throws JSQLParserException { |
| 53 | + Alter alter = parse("ALTER TABLE t ADD CONSTRAINT uq UNIQUE USING INDEX i DEFERRABLE"); |
| 54 | + ConstraintUsingIndex constraint = |
| 55 | + (ConstraintUsingIndex) alter.getAlterExpressions().get(0).getIndex(); |
| 56 | + assertEquals(Boolean.TRUE, constraint.getConstraintAttributes().getDeferrable()); |
| 57 | + constraint.setName("pk"); |
| 58 | + constraint.setType("PRIMARY KEY"); |
| 59 | + constraint.setExistingIndexName("other_index"); |
| 60 | + assertEquals(Index.Kind.PRIMARY_KEY, constraint.getKind()); |
| 61 | + assertEquals( |
| 62 | + "ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY USING INDEX other_index DEFERRABLE", |
| 63 | + alter.toString()); |
| 64 | + roundTrip(alter); |
| 65 | + Alter created = new Alter().withTable(new Table("t")); |
| 66 | + created.addAlterExpressions(new AlterExpression().withOperation(AlterOperation.ADD) |
| 67 | + .withIndex(new ConstraintUsingIndex().withName("uq").withExistingIndexName("i"))); |
| 68 | + assertEquals("ALTER TABLE t ADD CONSTRAINT uq UNIQUE USING INDEX i", created.toString()); |
| 69 | + roundTrip(created); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void distinguishesTableAndIndexAndNewConstraint() throws JSQLParserException { |
| 74 | + Alter alter = parse("ALTER TABLE t ADD CONSTRAINT uq UNIQUE USING INDEX i"); |
| 75 | + assertEquals(Set.of("t"), new TablesNamesFinder().getTables(alter)); |
| 76 | + List<Named> visited = new ArrayList<>(); |
| 77 | + DatabaseMetaDataValidation metadata = named -> { |
| 78 | + visited.add(named); |
| 79 | + return named.getNamedObject() != NamedObject.constraint; |
| 80 | + }; |
| 81 | + AlterValidator validator = new AlterValidator(); |
| 82 | + validator.setContext(new ValidationContext().setCapabilities(List.of(metadata))); |
| 83 | + validator.validate(alter); |
| 84 | + assertTrue(validator.getValidationErrors().isEmpty()); |
| 85 | + assertTrue(visited.stream() |
| 86 | + .anyMatch(n -> n.getNamedObject() == NamedObject.index && "i".equals(n.getFqn()))); |
| 87 | + assertTrue(visited.stream().anyMatch( |
| 88 | + n -> n.getNamedObject() == NamedObject.constraint && "uq".equals(n.getFqn()))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void preservesOrdinaryConstraintsAndActionBoundaries() throws JSQLParserException { |
| 93 | + roundTrip(parse("ALTER TABLE t ADD CONSTRAINT uq UNIQUE (id)")); |
| 94 | + Alter alter = parse("ALTER TABLE t ADD UNIQUE USING INDEX i, ADD COLUMN extra INT"); |
| 95 | + assertEquals(2, alter.getAlterExpressions().size()); |
| 96 | + roundTrip(alter); |
| 97 | + String oracle = "ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY (id) USING INDEX i"; |
| 98 | + assertEquals(oracle, CCJSqlParserUtil.parse(oracle).toString()); |
| 99 | + } |
| 100 | + |
| 101 | + private static Alter parse(String sql) throws JSQLParserException { |
| 102 | + return (Alter) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 103 | + } |
| 104 | + |
| 105 | + private static void roundTrip(Alter alter) throws JSQLParserException { |
| 106 | + StringBuilder sql = new StringBuilder(); |
| 107 | + alter.accept(new StatementDeParser(sql), null); |
| 108 | + assertEquals(alter.toString(), sql.toString()); |
| 109 | + assertEquals(sql.toString(), parse(sql.toString()).toString()); |
| 110 | + } |
| 111 | +} |
0 commit comments