|
| 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; |
| 11 | + |
| 12 | +import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | + |
| 17 | +import java.util.stream.Stream; |
| 18 | +import net.sf.jsqlparser.JSQLParserException; |
| 19 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 20 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 21 | +import net.sf.jsqlparser.statement.alter.Alter; |
| 22 | +import net.sf.jsqlparser.statement.alter.AlterExpression; |
| 23 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
| 29 | + |
| 30 | +/** |
| 31 | + * Words that may start a table element or an ALTER action, but name a column, a constraint, an |
| 32 | + * index or a check where that element or action does not follow. |
| 33 | + */ |
| 34 | +public class ContextualKeywordNamesTest { |
| 35 | + |
| 36 | + private static Statement parse(String sql, Dialect dialect) throws JSQLParserException { |
| 37 | + return assertSqlCanBeParsedAndDeparsed(sql, true, parser -> { |
| 38 | + if (dialect != null) { |
| 39 | + parser.withDialect(dialect); |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private static AlterExpression alterExpression(String sql, Dialect dialect) |
| 45 | + throws JSQLParserException { |
| 46 | + return ((Alter) parse(sql, dialect)).getAlterExpressions().get(0); |
| 47 | + } |
| 48 | + |
| 49 | + // PostgreSQL has no inline index definition: KEY, INDEX, FULLTEXT and SPATIAL name columns. |
| 50 | + @ParameterizedTest |
| 51 | + @ValueSource(strings = {"key", "index", "fulltext", "spatial"}) |
| 52 | + void testPostgreSqlIndexWordsAsColumns(String word) throws JSQLParserException { |
| 53 | + CreateTable createTable = (CreateTable) parse( |
| 54 | + "CREATE TABLE kv (id INT, " + word + " VARCHAR (10) NOT NULL)", Dialect.POSTGRESQL); |
| 55 | + assertEquals(word, createTable.getColumnDefinitions().get(1).getColumnName()); |
| 56 | + assertEquals(2, createTable.getTableElements().size()); |
| 57 | + |
| 58 | + assertEquals(word, |
| 59 | + alterExpression("ALTER TABLE kv ADD " + word + " INT", Dialect.POSTGRESQL) |
| 60 | + .getColDataTypeList().get(0).getColumnName()); |
| 61 | + assertEquals(word, alterExpression("ALTER TABLE kv ALTER " + word + " TYPE BIGINT", |
| 62 | + Dialect.POSTGRESQL).getColDataTypeList().get(0).getColumnName()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testMySqlIndexDefinitionsStay() throws JSQLParserException { |
| 67 | + CreateTable createTable = (CreateTable) parse("CREATE TABLE t (a INT, KEY k (a)," |
| 68 | + + " CONSTRAINT UNIQUE KEY u (a), CONSTRAINT PRIMARY KEY (a))", Dialect.MYSQL); |
| 69 | + assertEquals("k", createTable.getIndexes().get(0).getName()); |
| 70 | + assertEquals(2, createTable.getTableConstraints().size()); |
| 71 | + assertNull(createTable.getTableConstraints().get(1).getName()); |
| 72 | + assertEquals("k", alterExpression("ALTER TABLE t ADD INDEX k (a)", Dialect.MYSQL).getIndex() |
| 73 | + .getName()); |
| 74 | + assertEquals("k", alterExpression("ALTER TABLE t ALTER INDEX k INVISIBLE", Dialect.MYSQL) |
| 75 | + .getIndex().getName()); |
| 76 | + // MySQL reserves KEY, so it cannot name a column there |
| 77 | + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse( |
| 78 | + "ALTER TABLE t ADD key INT", parser -> parser.withDialect(Dialect.MYSQL))); |
| 79 | + } |
| 80 | + |
| 81 | + // A word that could start a constraint is its name when the constraint kind follows it. |
| 82 | + public static Stream<Arguments> constraintNames() { |
| 83 | + return Stream.of(Arguments.of("key", Dialect.POSTGRESQL), |
| 84 | + Arguments.of("index", Dialect.POSTGRESQL), |
| 85 | + Arguments.of("fulltext", Dialect.POSTGRESQL), |
| 86 | + Arguments.of("spatial", Dialect.POSTGRESQL), |
| 87 | + Arguments.of("exclude", Dialect.POSTGRESQL), |
| 88 | + Arguments.of("exclude", Dialect.MYSQL), Arguments.of("key", null), |
| 89 | + Arguments.of("exclude", null)); |
| 90 | + } |
| 91 | + |
| 92 | + @ParameterizedTest(name = "{0} {1}") |
| 93 | + @MethodSource("constraintNames") |
| 94 | + void testConstraintNameBeforeConstraintKind(String word, Dialect dialect) |
| 95 | + throws JSQLParserException { |
| 96 | + CreateTable createTable = (CreateTable) parse( |
| 97 | + "CREATE TABLE t (a INT, CONSTRAINT " + word + " UNIQUE (a))", dialect); |
| 98 | + assertEquals(word, createTable.getTableConstraints().get(0).getName()); |
| 99 | + |
| 100 | + AlterExpression add = alterExpression( |
| 101 | + "ALTER TABLE t ADD CONSTRAINT " + word + " PRIMARY KEY (a)", dialect); |
| 102 | + assertEquals(word, add.getConstraint() != null ? add.getConstraint().getName() |
| 103 | + : add.getIndex().getName()); |
| 104 | + } |
| 105 | + |
| 106 | + // EXCLUDE starts an exclusion constraint only with USING, its elements or WHERE. |
| 107 | + @ParameterizedTest |
| 108 | + @MethodSource("excludeDialects") |
| 109 | + void testExcludeAsAddedColumn(Dialect dialect) throws JSQLParserException { |
| 110 | + assertEquals("exclude", alterExpression("ALTER TABLE t ADD exclude INT", dialect) |
| 111 | + .getColDataTypeList().get(0).getColumnName()); |
| 112 | + } |
| 113 | + |
| 114 | + public static Stream<Dialect> excludeDialects() { |
| 115 | + return Stream.of(null, Dialect.MYSQL, Dialect.POSTGRESQL); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testExclusionConstraintStays() throws JSQLParserException { |
| 120 | + AlterExpression exclude = alterExpression( |
| 121 | + "ALTER TABLE t ADD CONSTRAINT x EXCLUDE USING gist (a WITH =)", Dialect.POSTGRESQL); |
| 122 | + assertEquals("x", exclude.getConstraint().getName()); |
| 123 | + } |
| 124 | + |
| 125 | + // OPTIONS starts PostgreSQL's foreign-data options only before its parenthesized list. |
| 126 | + @Test |
| 127 | + void testOptionsAsIndexAndConstraintName() throws JSQLParserException { |
| 128 | + assertEquals("options", alterExpression("ALTER TABLE t ALTER INDEX options INVISIBLE", |
| 129 | + Dialect.MYSQL).getIndex().getName()); |
| 130 | + parse("ALTER TABLE t ALTER CHECK options NOT ENFORCED", Dialect.MYSQL); |
| 131 | + parse("ALTER TABLE t ALTER CONSTRAINT options ENFORCED", Dialect.MYSQL); |
| 132 | + parse("ALTER FOREIGN TABLE t ALTER COLUMN c OPTIONS (ADD x 'y')", Dialect.POSTGRESQL); |
| 133 | + } |
| 134 | +} |
0 commit comments