|
| 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 | + |
| 14 | +import java.util.Set; |
| 15 | +import net.sf.jsqlparser.JSQLParserException; |
| 16 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 17 | +import net.sf.jsqlparser.statement.create.table.ConstraintAttributes; |
| 18 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 19 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 20 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | + |
| 25 | +class PostgreSqlConstraintAttributesChangeTest { |
| 26 | + @ParameterizedTest |
| 27 | + @ValueSource(strings = {"DEFERRABLE", "NOT DEFERRABLE", "INITIALLY DEFERRED", |
| 28 | + "INITIALLY IMMEDIATE", "DEFERRABLE INITIALLY DEFERRED", |
| 29 | + "INITIALLY DEFERRED DEFERRABLE", "ENFORCED", "NOT ENFORCED", |
| 30 | + "DEFERRABLE INITIALLY DEFERRED NOT ENFORCED", |
| 31 | + "NOT ENFORCED INITIALLY IMMEDIATE NOT DEFERRABLE"}) |
| 32 | + void sharesCreateAddAndAlterAttributes(String attributes) throws JSQLParserException { |
| 33 | + String sql = "ALTER TABLE t ALTER CONSTRAINT fk " + attributes + ", ADD COLUMN z INT"; |
| 34 | + Alter table = (Alter) parse(sql); |
| 35 | + AlterConstraintAttributes change = assertInstanceOf(AlterConstraintAttributes.class, |
| 36 | + table.getAlterExpressions().get(0)); |
| 37 | + assertEquals(AlterOperation.ALTER, change.getOperation()); |
| 38 | + assertEquals("CONSTRAINT", change.getConstraintType()); |
| 39 | + assertEquals("fk", change.getConstraintSymbol()); |
| 40 | + assertEquals("fk", change.getConstraintName()); |
| 41 | + String declaration = "CONSTRAINT fk FOREIGN KEY(id) REFERENCES p(id) " + attributes; |
| 42 | + CreateTable create = (CreateTable) parse("CREATE TABLE t(id INT, " |
| 43 | + + declaration + ")"); |
| 44 | + Alter add = (Alter) parse("ALTER TABLE t ADD " + declaration); |
| 45 | + assertEquals(create.getIndexes().get(0).getConstraintAttributes().toString(), |
| 46 | + change.getAttributes().toString()); |
| 47 | + assertEquals( |
| 48 | + add.getAlterExpressions().get(0).getIndex().getConstraintAttributes().toString(), |
| 49 | + change.getAttributes().toString()); |
| 50 | + assertEquals(2, table.getAlterExpressions().size()); |
| 51 | + roundTrip(table); |
| 52 | + assertEquals(2, CCJSqlParserUtil.parseStatements(sql + "; SELECT 1").size()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void legacyAndStructuredMutationsStayConsistent() throws JSQLParserException { |
| 57 | + Alter table = (Alter) parse("ALTER TABLE t ALTER CONSTRAINT fk ENFORCED"); |
| 58 | + AlterConstraintAttributes action = |
| 59 | + (AlterConstraintAttributes) table.getAlterExpressions().get(0); |
| 60 | + action.setConstraintName("\"new fk\""); |
| 61 | + assertEquals("\"new fk\"", action.getConstraintSymbol()); |
| 62 | + action.setEnforced(false); |
| 63 | + assertEquals(Boolean.FALSE, action.getAttributes().getEnforced()); |
| 64 | + action.getAttributes().setDeferrable(true); |
| 65 | + action.getAttributes().setInitially(ConstraintAttributes.Initially.DEFERRED); |
| 66 | + assertEquals( |
| 67 | + "ALTER TABLE t ALTER CONSTRAINT \"new fk\" DEFERRABLE INITIALLY DEFERRED NOT ENFORCED", |
| 68 | + table.toString()); |
| 69 | + action.getAttributes().setEnforced(true); |
| 70 | + assertTrue(action.isEnforced()); |
| 71 | + assertEquals(Set.of("t"), new TablesNamesFinder().getTables(table)); |
| 72 | + roundTrip(table); |
| 73 | + } |
| 74 | + |
| 75 | + @ParameterizedTest |
| 76 | + @ValueSource(strings = {"NOT VALID", "DEFERRABLE NOT VALID", "INITIALLY UNKNOWN", |
| 77 | + "DEFERRABLE NOT DEFERRABLE", "ENFORCED NOT ENFORCED", |
| 78 | + "INITIALLY DEFERRED INITIALLY IMMEDIATE", "DEFERRABLE,"}) |
| 79 | + void rejectsWrongOrDuplicateAlterAttributes(String attributes) { |
| 80 | + assertThrows(JSQLParserException.class, |
| 81 | + () -> parse("ALTER TABLE t ALTER CONSTRAINT fk " + attributes)); |
| 82 | + } |
| 83 | + |
| 84 | + private static net.sf.jsqlparser.statement.Statement parse(String sql) |
| 85 | + throws JSQLParserException { |
| 86 | + return CCJSqlParserUtil.parse(sql, |
| 87 | + parser -> parser.withDialect( |
| 88 | + net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect.POSTGRESQL)); |
| 89 | + } |
| 90 | + |
| 91 | + private static void roundTrip(Alter table) throws JSQLParserException { |
| 92 | + StringBuilder output = new StringBuilder(); |
| 93 | + table.accept(new StatementDeParser(output)); |
| 94 | + assertEquals(table.toString(), output.toString()); |
| 95 | + assertEquals(output.toString(), parse(output.toString()).toString()); |
| 96 | + } |
| 97 | +} |
0 commit comments