|
| 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.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | + |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 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.TablesNamesFinder; |
| 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 MySqlTableOptionsTest { |
| 28 | + @ParameterizedTest |
| 29 | + @ValueSource(strings = { |
| 30 | + "STATS_PERSISTENT=1, STATS_AUTO_RECALC=1", |
| 31 | + "STATS_PERSISTENT DEFAULT, STATS_AUTO_RECALC=0 STATS_SAMPLE_PAGES=32", |
| 32 | + "ENGINE=InnoDB, DEFAULT CHARSET=utf8mb4", |
| 33 | + "ENGINE=InnoDB, COMMENT='commas, in text', AUTO_INCREMENT=10", |
| 34 | + "UNION=(t1,t2) ENGINE=MRG_MyISAM", |
| 35 | + "ENGINE=MRG_MyISAM, UNION (db.t1, `db`.`t2`)", |
| 36 | + "UNION=() ENGINE=MRG_MyISAM", |
| 37 | + "ENGINE=InnoDB, STATS_PERSISTENT=1 PARTITION BY HASH(id) PARTITIONS 2", |
| 38 | + "ENGINE=InnoDB, STATS_AUTO_RECALC=DEFAULT AS SELECT 1 AS other" |
| 39 | + }) |
| 40 | + void parsesOptionSeparatorsAndValues(String options) throws JSQLParserException { |
| 41 | + CreateTable table = parse("CREATE TABLE t (id INT) " + options); |
| 42 | + assertRoundTrip(table); |
| 43 | + assertEquals(table.toString(), CCJSqlParserUtil.parse(table.toString()).toString()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void statisticsOptionsHaveIndependentValues() throws JSQLParserException { |
| 48 | + CreateTable table = parse("CREATE TABLE t (id INT) " |
| 49 | + + "STATS_PERSISTENT=1, STATS_AUTO_RECALC=DEFAULT"); |
| 50 | + TableOption persistent = |
| 51 | + table.getTableOption(TableOption.Kind.STATS_PERSISTENT).orElseThrow(); |
| 52 | + assertEquals("1", persistent.getValue()); |
| 53 | + persistent.setValue("0"); |
| 54 | + assertThat(table.toString()).contains("STATS_PERSISTENT = 0 STATS_AUTO_RECALC = DEFAULT"); |
| 55 | + assertRoundTrip(table); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void unionSourcesAreTraversableAndMutable() throws JSQLParserException { |
| 60 | + CreateTable table = |
| 61 | + parse("CREATE TABLE merged (id INT) UNION=(db.t1,t2) ENGINE=MRG_MyISAM"); |
| 62 | + TableOption union = table.getTableOption(TableOption.Kind.UNION).orElseThrow(); |
| 63 | + assertEquals(2, union.getUnionTables().size()); |
| 64 | + assertEquals("db", union.getUnionTables().get(0).getSchemaName()); |
| 65 | + union.getUnionTables().get(0).setName("changed"); |
| 66 | + assertEquals("(db.changed, t2)", union.getValue()); |
| 67 | + assertThat(table.getTableOptionsStrings()).contains("(db.changed, t2)"); |
| 68 | + assertThat(new TablesNamesFinder().getTables(table)) |
| 69 | + .containsExactlyInAnyOrder("merged", "db.changed", "t2"); |
| 70 | + assertRoundTrip(table); |
| 71 | + union.getUnionTables().clear(); |
| 72 | + assertEquals("()", union.getValue()); |
| 73 | + assertRoundTrip(table); |
| 74 | + union.setValue("(replacement)"); |
| 75 | + assertEquals("(replacement)", union.getValue()); |
| 76 | + assertRoundTrip(table); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest |
| 80 | + @ValueSource(strings = {", ENGINE=InnoDB", "ENGINE=InnoDB,", "ENGINE=InnoDB,, COMMENT='x'", |
| 81 | + "UNION=(t1,)", "UNION=", "STATS_PERSISTENT=2", "STATS_AUTO_RECALC=-1"}) |
| 82 | + void rejectsIncompleteListsAndInvalidBooleanOptions(String options) { |
| 83 | + assertThrows(JSQLParserException.class, () -> parse("CREATE TABLE t (id INT) " + options)); |
| 84 | + } |
| 85 | + |
| 86 | + private static CreateTable parse(String sql) throws JSQLParserException { |
| 87 | + return (CreateTable) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.MYSQL)); |
| 88 | + } |
| 89 | + |
| 90 | + private static void assertRoundTrip(CreateTable table) throws JSQLParserException { |
| 91 | + StringBuilder buffer = new StringBuilder(); |
| 92 | + table.accept(new StatementDeParser(buffer), null); |
| 93 | + assertEquals(table.toString(), buffer.toString()); |
| 94 | + assertEquals(table.toString(), parse(buffer.toString()).toString()); |
| 95 | + } |
| 96 | +} |
0 commit comments