|
| 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.List; |
| 14 | +import net.sf.jsqlparser.JSQLParserException; |
| 15 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 16 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 17 | +import net.sf.jsqlparser.schema.Table; |
| 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 MySqlDropConstraintTest { |
| 24 | + @ParameterizedTest |
| 25 | + @ValueSource(strings = {"DROP FOREIGN KEY fk1", "DROP FOREIGN KEY `fk name`", |
| 26 | + "DROP CHECK c1", "DROP CHECK `check name`", |
| 27 | + "DROP FOREIGN KEY fk1, DROP CHECK c1", |
| 28 | + "DROP FOREIGN KEY fk1, ALGORITHM = INPLACE, LOCK = NONE"}) |
| 29 | + void roundTripsNamedDrops(String actions) throws JSQLParserException { |
| 30 | + Alter alter = parse("ALTER TABLE t " + actions); |
| 31 | + assertEquals("ALTER TABLE t " + actions, alter.toString()); |
| 32 | + assertRoundTrip(alter); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void namesAndKindsAreEditableWithoutColumnLists() throws JSQLParserException { |
| 37 | + Alter alter = parse("ALTER TABLE t DROP FOREIGN KEY fk1, DROP CHECK c1"); |
| 38 | + AlterExpression foreign = alter.getAlterExpressions().get(0); |
| 39 | + AlterExpression check = alter.getAlterExpressions().get(1); |
| 40 | + assertEquals(AlterOperation.DROP_FOREIGN_KEY, foreign.getOperation()); |
| 41 | + assertEquals("fk1", foreign.getConstraintName()); |
| 42 | + assertNull(foreign.getPkColumns()); |
| 43 | + assertEquals(AlterOperation.DROP_CHECK, check.getOperation()); |
| 44 | + assertEquals("c1", check.getConstraintName()); |
| 45 | + check.setConstraintName("`new check`"); |
| 46 | + foreign.setConstraintName("fk2"); |
| 47 | + assertEquals("ALTER TABLE t DROP FOREIGN KEY fk2, DROP CHECK `new check`", |
| 48 | + alter.toString()); |
| 49 | + assertRoundTrip(alter); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void constructedActionsUseTheSameRendererAsParsedActions() throws JSQLParserException { |
| 54 | + for (AlterOperation operation : List.of(AlterOperation.DROP_FOREIGN_KEY, |
| 55 | + AlterOperation.DROP_CHECK)) { |
| 56 | + Alter alter = new Alter(); |
| 57 | + alter.setTable(new Table("t")); |
| 58 | + alter.addAlterExpression(new AlterExpression().withOperation(operation) |
| 59 | + .withConstraintName("`target constraint`")); |
| 60 | + Alter parsed = parse(alter.toString()); |
| 61 | + assertEquals(operation, parsed.getAlterExpressions().get(0).getOperation()); |
| 62 | + assertEquals("`target constraint`", |
| 63 | + parsed.getAlterExpressions().get(0).getConstraintName()); |
| 64 | + assertRoundTrip(alter); |
| 65 | + // Editing the operation on either concrete class must have the same effect. |
| 66 | + parsed.getAlterExpressions().get(0).setOperation(AlterOperation.DROP_CHECK); |
| 67 | + assertEquals("ALTER TABLE t DROP CHECK `target constraint`", parsed.toString()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @ParameterizedTest |
| 72 | + @ValueSource(strings = {"DROP FOREIGN KEY (id)", "DROP UNIQUE (a, b)", |
| 73 | + "DROP PRIMARY KEY", "DROP CONSTRAINT IF EXISTS ck CASCADE", "DROP (a, b)", |
| 74 | + "DROP COLUMN c", "DROP INDEX idx"}) |
| 75 | + void preservesDefaultDropForms(String action) throws JSQLParserException { |
| 76 | + String sql = "ALTER TABLE t " + action; |
| 77 | + Alter alter = (Alter) CCJSqlParserUtil.parse(sql); |
| 78 | + assertEquals(sql, alter.toString().trim()); |
| 79 | + } |
| 80 | + |
| 81 | + private static Alter parse(String sql) throws JSQLParserException { |
| 82 | + return (Alter) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.MYSQL)); |
| 83 | + } |
| 84 | + |
| 85 | + private static void assertRoundTrip(Alter alter) throws JSQLParserException { |
| 86 | + StringBuilder sql = new StringBuilder(); |
| 87 | + alter.accept(new StatementDeParser(sql), null); |
| 88 | + assertEquals(alter.toString(), sql.toString()); |
| 89 | + assertEquals(alter.toString(), parse(sql.toString()).toString()); |
| 90 | + } |
| 91 | +} |
0 commit comments