|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2023 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 org.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.List; |
| 14 | +import net.sf.jsqlparser.JSQLParserException; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.Statement; |
| 17 | +import net.sf.jsqlparser.statement.ReferentialAction; |
| 18 | +import net.sf.jsqlparser.statement.alter.Alter; |
| 19 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 20 | +import net.sf.jsqlparser.statement.create.table.ForeignKeyIndex; |
| 21 | +import net.sf.jsqlparser.statement.create.table.ForeignKeyReference; |
| 22 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | + |
| 27 | +class PostgreSqlForeignKeyActionColumnsTest { |
| 28 | + @ParameterizedTest |
| 29 | + @ValueSource(strings = {"NULL", "DEFAULT"}) |
| 30 | + void sharesActionsAcrossCreateAndAlter(String value) throws JSQLParserException { |
| 31 | + for (String columns : new String[] {"author_id", "tenant_id, author_id", "\"Author Id\""}) { |
| 32 | + for (boolean updateFirst : new boolean[] {false, true}) { |
| 33 | + String delete = "ON DELETE SET " + value + " (" + columns + ")"; |
| 34 | + String actions = updateFirst ? "ON UPDATE CASCADE " + delete |
| 35 | + : delete + " ON UPDATE CASCADE"; |
| 36 | + for (String prefix : new String[] { |
| 37 | + "CREATE TABLE posts (tenant_id INT, author_id INT, ", |
| 38 | + "ALTER TABLE posts ADD "}) { |
| 39 | + String sql = prefix + "CONSTRAINT fk FOREIGN KEY (tenant_id, author_id) " |
| 40 | + + "REFERENCES users (tenant_id, id) MATCH SIMPLE " + actions |
| 41 | + + (prefix.startsWith("CREATE") ? ")" : ""); |
| 42 | + Statement statement = CCJSqlParserUtil.parse(sql); |
| 43 | + ForeignKeyIndex index = (ForeignKeyIndex) (statement instanceof CreateTable |
| 44 | + ? ((CreateTable) statement).getIndexes().get(0) |
| 45 | + : ((Alter) statement).getAlterExpressions().get(0).getIndex()); |
| 46 | + ReferentialAction action = |
| 47 | + index.getReferentialAction(ReferentialAction.Type.DELETE); |
| 48 | + assertEquals(List.of(columns.split(", ")), action.getColumnNames()); |
| 49 | + assertNull(index.getReferentialAction(ReferentialAction.Type.UPDATE) |
| 50 | + .getColumnNames()); |
| 51 | + roundTrip(statement); |
| 52 | + action.setColumnNames(List.of("author_id")); |
| 53 | + roundTrip(statement); |
| 54 | + assertTrue(statement.toString().contains("SET " + value + " (author_id)")); |
| 55 | + action.setColumnNames(null); |
| 56 | + roundTrip(statement); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void supportsColumnReferencesAndStatementBoundaries() throws JSQLParserException { |
| 64 | + CreateTable table = (CreateTable) CCJSqlParserUtil.parse( |
| 65 | + "CREATE TABLE posts (author_id INT REFERENCES users ON DELETE SET NULL (author_id) NOT NULL)"); |
| 66 | + ForeignKeyReference reference = table.getColumnDefinitions().get(0) |
| 67 | + .getColumnOptions().get(0).getForeignKeyReference(); |
| 68 | + assertEquals(List.of("author_id"), |
| 69 | + reference.getReferentialAction(ReferentialAction.Type.DELETE).getColumnNames()); |
| 70 | + roundTrip(table); |
| 71 | + assertEquals(2, CCJSqlParserUtil.parseStatements(table + "; SELECT 1").size()); |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @ValueSource(strings = {"ON UPDATE SET NULL (a)", "ON UPDATE SET DEFAULT (a)", |
| 76 | + "ON DELETE CASCADE (a)", "ON DELETE SET NULL ()", "ON DELETE SET NULL (a,)", |
| 77 | + "ON DELETE SET NULL (a + 1)"}) |
| 78 | + void rejectsInvalidActionColumns(String action) { |
| 79 | + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse( |
| 80 | + "CREATE TABLE t (a INT, FOREIGN KEY (a) REFERENCES p (a) " + action + ")")); |
| 81 | + } |
| 82 | + |
| 83 | + private static void roundTrip(Statement statement) throws JSQLParserException { |
| 84 | + StringBuilder out = new StringBuilder(); |
| 85 | + statement.accept(new StatementDeParser(out)); |
| 86 | + assertEquals(statement.toString(), out.toString()); |
| 87 | + assertEquals(out.toString(), CCJSqlParserUtil.parse(out.toString()).toString()); |
| 88 | + } |
| 89 | +} |
0 commit comments