|
| 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 org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import net.sf.jsqlparser.JSQLParserException; |
| 19 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 20 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 21 | +import net.sf.jsqlparser.statement.create.table.ColumnOption; |
| 22 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 23 | +import net.sf.jsqlparser.statement.create.table.ForeignKeyReference; |
| 24 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 25 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +class ColumnOptionMutationTest { |
| 29 | + @Test |
| 30 | + void clearingIdentityDoesNotRestoreRawSpecifications() throws JSQLParserException { |
| 31 | + CreateTable table = parse("id INT GENERATED ALWAYS AS IDENTITY"); |
| 32 | + ColumnDefinition column = table.getColumnDefinitions().get(0); |
| 33 | + column.getColumnOptions().clear(); |
| 34 | + assertTrue(column.getColumnSpecs().isEmpty()); |
| 35 | + assertSql(table, "CREATE TABLE t (id INT)"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void legacyAppendPreservesReferenceAndTraversal() throws JSQLParserException { |
| 40 | + CreateTable table = parse("id INT REFERENCES parent(id)"); |
| 41 | + ColumnDefinition column = table.getColumnDefinitions().get(0); |
| 42 | + ForeignKeyReference reference = column.getForeignKeyReference(); |
| 43 | + column.addColumnSpecs("NOT", "NULL"); |
| 44 | + column.addColumnSpecs(Arrays.asList("DEFAULT", "1")); |
| 45 | + assertSame(reference, column.getForeignKeyReference()); |
| 46 | + assertTrue(new TablesNamesFinder().getTables(table).contains("parent")); |
| 47 | + assertSql(table, "CREATE TABLE t (id INT REFERENCES parent(id) NOT NULL DEFAULT 1)"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void addingStructuredOptionPreservesExistingRawSpecifications() throws JSQLParserException { |
| 52 | + CreateTable table = parse("id INT NOT NULL"); |
| 53 | + ColumnDefinition column = table.getColumnDefinitions().get(0); |
| 54 | + assertNull(column.getColumnOptions()); |
| 55 | + ForeignKeyReference reference = parse("id INT REFERENCES parent(id)") |
| 56 | + .getColumnDefinitions().get(0).getForeignKeyReference(); |
| 57 | + column.addColumnOptions(ColumnOption.reference(reference)); |
| 58 | + assertSql(table, "CREATE TABLE t (id INT NOT NULL REFERENCES parent(id))"); |
| 59 | + assertTrue(new TablesNamesFinder().getTables(table).contains("parent")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void replacingOptionsWithNullClearsPreviousSpecifications() throws JSQLParserException { |
| 64 | + CreateTable table = parse("id INT GENERATED ALWAYS AS IDENTITY"); |
| 65 | + ColumnDefinition column = table.getColumnDefinitions().get(0); |
| 66 | + column.setColumnOptions(null); |
| 67 | + assertNull(column.getColumnSpecs()); |
| 68 | + assertSql(table, "CREATE TABLE t (id INT)"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void legacySetterStillReplacesStructuredOptions() throws JSQLParserException { |
| 73 | + CreateTable table = parse("id INT REFERENCES parent(id)"); |
| 74 | + ColumnDefinition column = table.getColumnDefinitions().get(0); |
| 75 | + column.setColumnSpecs(Arrays.asList("NOT", "NULL")); |
| 76 | + assertNull(column.getForeignKeyReference()); |
| 77 | + assertSql(table, "CREATE TABLE t (id INT NOT NULL)"); |
| 78 | + } |
| 79 | + |
| 80 | + private static CreateTable parse(String definition) throws JSQLParserException { |
| 81 | + return (CreateTable) CCJSqlParserUtil.parse("CREATE TABLE t (" + definition + ")"); |
| 82 | + } |
| 83 | + |
| 84 | + private static void assertSql(CreateTable table, String expected) throws JSQLParserException { |
| 85 | + assertEquals(expected, table.toString()); |
| 86 | + StringBuilder buffer = new StringBuilder(); |
| 87 | + table.accept(new StatementDeParser(buffer), null); |
| 88 | + assertEquals(expected, buffer.toString()); |
| 89 | + assertEquals(expected, CCJSqlParserUtil.parse(expected).toString()); |
| 90 | + } |
| 91 | +} |
0 commit comments