|
| 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 net.sf.jsqlparser.JSQLParserException; |
| 14 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.Statement; |
| 17 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 18 | +import net.sf.jsqlparser.statement.create.table.TableOption; |
| 19 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
| 23 | + |
| 24 | +class IndexOptionBoundaryTest { |
| 25 | + @ParameterizedTest |
| 26 | + @ValueSource(strings = {"UNIQUE", "PRIMARY KEY"}) |
| 27 | + void tablespaceBelongsToConstraintOptions(String key) throws JSQLParserException { |
| 28 | + Alter alter = (Alter) parse("ALTER TABLE t ADD CONSTRAINT k " + key |
| 29 | + + " (id) USING INDEX TABLESPACE pg_default DEFERRABLE", Dialect.POSTGRESQL); |
| 30 | + Index index = alter.getAlterExpressions().get(0).getIndex(); |
| 31 | + assertEquals("pg_default", index.getTableSpace()); |
| 32 | + assertNull(index.getUsing()); |
| 33 | + assertTrue(index.getConstraintAttributes().getDeferrable()); |
| 34 | + index.setTableSpace("other_space"); |
| 35 | + assertEquals("ALTER TABLE t ADD CONSTRAINT k " + key |
| 36 | + + " (id) USING INDEX TABLESPACE other_space DEFERRABLE", alter.toString()); |
| 37 | + assertRoundTrip(alter, Dialect.POSTGRESQL); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @ValueSource(strings = {"SECONDARY_ENGINE_ATTRIBUTE = '{}'", |
| 42 | + "SECONDARY_ENGINE_ATTRIBUTE '{\"key\": \"value\"}'", |
| 43 | + "SECONDARY_ENGINE_ATTRIBUTE = '{}' VISIBLE COMMENT 'index'"}) |
| 44 | + void mysqlAddIndexAttributes(String options) throws JSQLParserException { |
| 45 | + Alter alter = (Alter) parse("ALTER TABLE t ADD INDEX ix (id) " + options, Dialect.MYSQL); |
| 46 | + assertEquals("ix", alter.getAlterExpressions().get(0).getIndex().getName()); |
| 47 | + assertEquals("ALTER TABLE t ADD INDEX ix (id) " + options, alter.toString()); |
| 48 | + assertRoundTrip(alter, Dialect.MYSQL); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void sharedTableAttributeRemainsMutable() throws JSQLParserException { |
| 53 | + Alter alter = |
| 54 | + (Alter) parse("ALTER TABLE t SECONDARY_ENGINE_ATTRIBUTE = '{}'", Dialect.MYSQL); |
| 55 | + AlterExpressionTableOption action = |
| 56 | + (AlterExpressionTableOption) alter.getAlterExpressions().get(0); |
| 57 | + TableOption option = action.getStructuredTableOption(); |
| 58 | + assertEquals(TableOption.Kind.SECONDARY_ENGINE_ATTRIBUTE, option.getKind()); |
| 59 | + option.setValue("'{\"key\":1}'"); |
| 60 | + assertEquals("ALTER TABLE t SECONDARY_ENGINE_ATTRIBUTE = '{\"key\":1}'", alter.toString()); |
| 61 | + assertRoundTrip(alter, Dialect.MYSQL); |
| 62 | + } |
| 63 | + |
| 64 | + private static Statement parse(String sql, Dialect dialect) throws JSQLParserException { |
| 65 | + return CCJSqlParserUtil.parse(sql, p -> p.withDialect(dialect)); |
| 66 | + } |
| 67 | + |
| 68 | + private static void assertRoundTrip(Statement statement, Dialect dialect) |
| 69 | + throws JSQLParserException { |
| 70 | + StringBuilder sql = new StringBuilder(); |
| 71 | + statement.accept(new StatementDeParser(sql), null); |
| 72 | + assertEquals(statement.toString(), sql.toString()); |
| 73 | + assertEquals(statement.toString(), parse(sql.toString(), dialect).toString()); |
| 74 | + } |
| 75 | +} |
0 commit comments