|
| 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.assertThrows; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import net.sf.jsqlparser.JSQLParserException; |
| 18 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 19 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 20 | +import net.sf.jsqlparser.statement.Statement; |
| 21 | +import net.sf.jsqlparser.statement.alter.Alter; |
| 22 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 23 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 24 | +import net.sf.jsqlparser.statement.create.table.NamedConstraint; |
| 25 | +import net.sf.jsqlparser.test.TestUtils; |
| 26 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.EnumSource; |
| 30 | + |
| 31 | +class SqlServerConstraintClusteringTest { |
| 32 | + private static Index constraint(Statement statement) { |
| 33 | + return statement instanceof CreateTable |
| 34 | + ? ((CreateTable) statement).getIndexes().get(0) |
| 35 | + : ((Alter) statement).getAlterExpressions().get(0).getIndex(); |
| 36 | + } |
| 37 | + |
| 38 | + private static Statement parse(String sql) throws JSQLParserException { |
| 39 | + return CCJSqlParserUtil.parse(sql, parser -> parser.withDialect(Dialect.SQLSERVER)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void parsesSakilaReproducerIssue1589() throws Exception { |
| 44 | + String sql = "CREATE TABLE actor (actor_id INT NOT NULL IDENTITY, " |
| 45 | + + "first_name VARCHAR (45) NOT NULL, last_name VARCHAR (45) NOT NULL, " |
| 46 | + + "last_update DATETIME NOT NULL, PRIMARY KEY NONCLUSTERED (actor_id))"; |
| 47 | + Index index = constraint(TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true, |
| 48 | + parser -> parser.withDialect(Dialect.SQLSERVER))); |
| 49 | + assertEquals(Index.Clustering.NONCLUSTERED, index.getClustering()); |
| 50 | + assertNull(((NamedConstraint) index).getIndexName()); |
| 51 | + assertEquals(List.of("actor_id"), index.getColumnsNames()); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @EnumSource(Index.Clustering.class) |
| 56 | + void sharesClusteringAcrossCreateAndAlter(Index.Clustering clustering) throws Exception { |
| 57 | + for (String type : List.of("PRIMARY KEY", "UNIQUE")) { |
| 58 | + for (String name : List.of("", "CONSTRAINT [key name] ")) { |
| 59 | + String definition = name + type + " " + clustering + " (id)"; |
| 60 | + for (String sql : List.of("CREATE TABLE t (id INT, " + definition + ")", |
| 61 | + "ALTER TABLE t ADD " + definition)) { |
| 62 | + Statement statement = TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true, |
| 63 | + parser -> parser.withDialect(Dialect.SQLSERVER)); |
| 64 | + Index index = constraint(statement); |
| 65 | + assertEquals(clustering, index.getClustering(), sql); |
| 66 | + assertEquals(type, index.getType()); |
| 67 | + assertEquals(name.isEmpty() ? null : "[key name]", index.getName()); |
| 68 | + if (index instanceof NamedConstraint) { |
| 69 | + assertNull(((NamedConstraint) index).getIndexName()); |
| 70 | + } |
| 71 | + StringBuilder output = new StringBuilder(); |
| 72 | + statement.accept(new StatementDeParser(output), null); |
| 73 | + assertEquals(clustering, constraint(parse(output.toString())).getClustering()); |
| 74 | + assertEquals(clustering, |
| 75 | + constraint(parse(statement.toString())).getClustering()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void preservesNamesOutsideSqlServerAndQuotedNames() throws Exception { |
| 83 | + for (String type : List.of("PRIMARY KEY", "UNIQUE")) { |
| 84 | + String sql = "CREATE TABLE t (id INT, " + type + " NONCLUSTERED (id))"; |
| 85 | + NamedConstraint defaultIndex = |
| 86 | + (NamedConstraint) constraint(CCJSqlParserUtil.parse(sql)); |
| 87 | + assertNull(defaultIndex.getClustering()); |
| 88 | + assertEquals("NONCLUSTERED", defaultIndex.getIndexName()); |
| 89 | + for (Dialect dialect : Dialect.values()) { |
| 90 | + if (dialect != Dialect.SQLSERVER) { |
| 91 | + NamedConstraint index = (NamedConstraint) constraint(CCJSqlParserUtil.parse(sql, |
| 92 | + parser -> parser.withDialect(dialect))); |
| 93 | + assertNull(index.getClustering(), dialect.name()); |
| 94 | + assertEquals("NONCLUSTERED", index.getIndexName()); |
| 95 | + } |
| 96 | + } |
| 97 | + NamedConstraint quoted = (NamedConstraint) constraint( |
| 98 | + parse("CREATE TABLE t (id INT, " + type + " [NONCLUSTERED] (id))")); |
| 99 | + assertNull(quoted.getClustering()); |
| 100 | + assertEquals("[NONCLUSTERED]", quoted.getIndexName()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void supportsMutationAndLeavesOmittedOptionUnspecified() throws Exception { |
| 106 | + Statement statement = parse("CREATE TABLE t (id INT, PRIMARY KEY (id))"); |
| 107 | + Index index = constraint(statement); |
| 108 | + assertNull(index.getClustering()); |
| 109 | + index.setClustering(Index.Clustering.CLUSTERED); |
| 110 | + TestUtils.assertDeparse(statement, "CREATE TABLE t (id INT, PRIMARY KEY CLUSTERED (id))"); |
| 111 | + index.setClustering(null); |
| 112 | + TestUtils.assertDeparse(statement, "CREATE TABLE t (id INT, PRIMARY KEY (id))"); |
| 113 | + NamedConstraint built = new NamedConstraint().withType("UNIQUE").withName("uq_t") |
| 114 | + .withClustering(Index.Clustering.NONCLUSTERED).withColumnsNames(List.of("id")); |
| 115 | + assertEquals("CONSTRAINT uq_t UNIQUE NONCLUSTERED (id)", built.toString()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void keepsFollowingConstraintsAndRejectsDuplicateModifiers() throws Exception { |
| 120 | + CreateTable table = (CreateTable) parse("CREATE TABLE t (id INT, other_id INT, " |
| 121 | + + "PRIMARY KEY NONCLUSTERED (id), UNIQUE (other_id))"); |
| 122 | + assertNull(table.getIndexes().get(1).getClustering()); |
| 123 | + for (String sql : List.of( |
| 124 | + "CREATE TABLE t (id INT, PRIMARY KEY NONCLUSTERED CLUSTERED (id))", |
| 125 | + "ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY CLUSTERED NONCLUSTERED (id)", |
| 126 | + "ALTER TABLE t ADD UNIQUE NONCLUSTERED")) { |
| 127 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments