|
| 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.expression; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.expression.SQLServerHints.LockHint; |
| 18 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 19 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 20 | +import net.sf.jsqlparser.schema.Table; |
| 21 | +import net.sf.jsqlparser.statement.Statement; |
| 22 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 23 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 24 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.EnumSource; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
| 29 | + |
| 30 | +class SQLServerHintsTest { |
| 31 | + @ParameterizedTest |
| 32 | + @EnumSource(LockHint.class) |
| 33 | + void parsesEachLockingHint(LockHint hint) throws Exception { |
| 34 | + Statement statement = parse("SELECT * FROM dbo.jobs WITH (" + hint + ")"); |
| 35 | + assertEquals(List.of(hint), hints(statement).getLockHints()); |
| 36 | + roundTrip(statement); |
| 37 | + } |
| 38 | + |
| 39 | + @ParameterizedTest |
| 40 | + @ValueSource(strings = {"SELECT * FROM dbo.jobs WITH (UPDLOCK, ROWLOCK)", |
| 41 | + "SELECT * FROM [dbo].[jobs] AS j WITH (INDEX ([ix_jobs]), UPDLOCK, ROWLOCK) WHERE j.id = 1", |
| 42 | + "SELECT * FROM jobs WITH (ROWLOCK, READPAST, UPDLOCK) ORDER BY id", |
| 43 | + "SELECT * FROM jobs j WITH (HOLDLOCK) JOIN workers w WITH (ROWLOCK, NOWAIT) ON j.id = w.id", |
| 44 | + "WITH q AS (SELECT * FROM jobs WITH (UPDLOCK, ROWLOCK)) SELECT * FROM q", |
| 45 | + "UPDATE j SET state = 1 FROM jobs j WITH (UPDLOCK, ROWLOCK) WHERE id = 1", |
| 46 | + "DELETE j FROM jobs j WITH (ROWLOCK) WHERE id = 1", |
| 47 | + "DELETE FROM jobs WITH (ROWLOCK) WHERE id = 1", |
| 48 | + "UPDATE jobs WITH (ROWLOCK, UPDLOCK) SET state = 1 WHERE id = 1", |
| 49 | + "SELECT * FROM jobs WITH (NOLOCK)", |
| 50 | + "SELECT * FROM jobs WITH (INDEX (ix_jobs), NOLOCK)"}) |
| 51 | + void retainsHintsAcrossAliasesJoinsCtesAndDml(String sql) throws Exception { |
| 52 | + roundTrip(parse(sql)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void exposesTypedHintsWithoutChangingLegacyAccessors() throws Exception { |
| 57 | + Statement statement = parse("SELECT * FROM jobs WITH (updlock, rowlock)"); |
| 58 | + SQLServerHints hints = hints(statement); |
| 59 | + assertEquals(List.of(LockHint.UPDLOCK, LockHint.ROWLOCK), hints.getLockHints()); |
| 60 | + hints.getLockHints().set(0, LockHint.READPAST); |
| 61 | + hints.setIndexName("ix_jobs"); |
| 62 | + assertEquals("SELECT * FROM jobs WITH (INDEX (ix_jobs), READPAST, ROWLOCK)", |
| 63 | + statement.toString()); |
| 64 | + assertThat(new TablesNamesFinder().getTables(statement)).containsExactly("jobs"); |
| 65 | + roundTrip(statement); |
| 66 | + |
| 67 | + SQLServerHints legacy = new SQLServerHints(); |
| 68 | + assertNull(legacy.getNoLock()); |
| 69 | + legacy.withNoLock().withIndexName("ix"); |
| 70 | + assertEquals(Boolean.TRUE, legacy.getNoLock()); |
| 71 | + assertEquals(" WITH (INDEX (ix), NOLOCK)", legacy.toString()); |
| 72 | + legacy.setNoLock(false); |
| 73 | + assertEquals(Boolean.FALSE, legacy.getNoLock()); |
| 74 | + assertEquals(" WITH (INDEX (ix))", legacy.toString()); |
| 75 | + legacy.withNoLock(null); |
| 76 | + assertNull(legacy.getNoLock()); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest |
| 80 | + @ValueSource(strings = {"SELECT * FROM jobs WITH ()", "SELECT * FROM jobs WITH (ROWLOCK,)", |
| 81 | + "SELECT * FROM jobs WITH (,UPDLOCK)", "SELECT * FROM jobs WITH (UPDLOCK(1))", |
| 82 | + "SELECT * FROM jobs WITH (ROWLOCK = 1)", "SELECT * FROM jobs WITH (UNKNOWN_HINT)", |
| 83 | + "SELECT * FROM jobs WITH ('ROWLOCK')", "SELECT * FROM jobs WITH (ROWLOCK"}) |
| 84 | + void rejectsMalformedOrUnknownHints(String sql) { |
| 85 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 86 | + } |
| 87 | + |
| 88 | + private static SQLServerHints hints(Statement statement) { |
| 89 | + return ((Table) ((PlainSelect) statement).getFromItem()).getSqlServerHints(); |
| 90 | + } |
| 91 | + |
| 92 | + private static Statement parse(String sql) throws JSQLParserException { |
| 93 | + return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.SQLSERVER)); |
| 94 | + } |
| 95 | + |
| 96 | + private static void roundTrip(Statement statement) throws Exception { |
| 97 | + StringBuilder output = new StringBuilder(); |
| 98 | + statement.accept(new StatementDeParser(output), null); |
| 99 | + assertEquals(statement.toString(), output.toString()); |
| 100 | + assertEquals(output.toString(), parse(output.toString()).toString()); |
| 101 | + } |
| 102 | +} |
0 commit comments