|
| 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 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import net.sf.jsqlparser.JSQLParserException; |
| 18 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 19 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 20 | +import net.sf.jsqlparser.statement.create.table.TableOption; |
| 21 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +class TableOptionMutationTest { |
| 25 | + @Test |
| 26 | + void changingTypedOptionUpdatesLegacyTokensAndBothRenderers() throws JSQLParserException { |
| 27 | + CreateTable table = parse(); |
| 28 | + TableOption engine = table.getTableOption(TableOption.Kind.ENGINE).orElseThrow(); |
| 29 | + engine.setValue("MyISAM"); |
| 30 | + engine.setUseEquals(false); |
| 31 | + assertEquals(Arrays.asList("ENGINE", "MyISAM"), table.getTableOptionsStrings()); |
| 32 | + assertSql(table, "CREATE TABLE t (id INT) ENGINE MyISAM"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void addingAndClearingTypedOptionsUpdatesDeparser() throws JSQLParserException { |
| 37 | + CreateTable table = parse(); |
| 38 | + table.getTableOptions().add(TableOption.raw("ROW_FORMAT", "=", "DYNAMIC")); |
| 39 | + assertSql(table, "CREATE TABLE t (id INT) ENGINE = InnoDB ROW_FORMAT = DYNAMIC"); |
| 40 | + table.getTableOptions().remove(0); |
| 41 | + assertSql(table, "CREATE TABLE t (id INT) ROW_FORMAT = DYNAMIC"); |
| 42 | + table.getTableOptions().clear(); |
| 43 | + assertSql(table, "CREATE TABLE t (id INT)"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void rawOptionsRemainMutableAndCanReplaceTypedOptions() throws JSQLParserException { |
| 48 | + CreateTable table = parse(); |
| 49 | + table.setTableOptionsStrings(new ArrayList<>(Arrays.asList("ENGINE", "=", "CSV"))); |
| 50 | + assertNull(table.getTableOptions()); |
| 51 | + table.getTableOptionsStrings().set(2, "MyISAM"); |
| 52 | + assertSql(table, "CREATE TABLE t (id INT) ENGINE = MyISAM"); |
| 53 | + table.setTableOptions(null); |
| 54 | + assertNull(table.getTableOptionsStrings()); |
| 55 | + assertSql(table, "CREATE TABLE t (id INT)"); |
| 56 | + } |
| 57 | + |
| 58 | + private static CreateTable parse() throws JSQLParserException { |
| 59 | + return (CreateTable) CCJSqlParserUtil.parse("CREATE TABLE t (id INT) ENGINE=InnoDB"); |
| 60 | + } |
| 61 | + |
| 62 | + private static void assertSql(CreateTable table, String expected) throws JSQLParserException { |
| 63 | + assertEquals(expected, table.toString()); |
| 64 | + StringBuilder buffer = new StringBuilder(); |
| 65 | + table.accept(new StatementDeParser(buffer), null); |
| 66 | + assertEquals(expected, buffer.toString()); |
| 67 | + assertEquals(expected, CCJSqlParserUtil.parse(expected).toString()); |
| 68 | + } |
| 69 | +} |
0 commit comments