Skip to content

Commit 85b8513

Browse files
committed
Adjusted NOWAIT and WAIT-Setter
1 parent 16ccb75 commit 85b8513

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

src/main/java/net/sf/jsqlparser/statement/lock/LockStatement.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Statement to Lock a specific table.<br>
99
* Example:<br>
10-
* LOCK TABLE &lt;TABLE&gt; IN EXCLUSIVE MODE;<br>
10+
* LOCK TABLE t IN EXCLUSIVE MODE<br>
1111
* <br>
1212
*/
1313
public class LockStatement implements Statement {
@@ -59,37 +59,42 @@ public void setLockMode(LockMode lockMode) {
5959
this.lockMode = lockMode;
6060
}
6161

62+
/**
63+
* @return True if the statement has a NOWAIT clause
64+
*/
6265
public boolean isNoWait() {
6366
return noWait;
6467
}
6568

6669
/**
67-
* Sets the NOWAIT-Flag. Clears a WAIT-Timeout if one was set before.
70+
* Sets the NOWAIT-Flag.
6871
*
69-
* @param noWait The new value of the NOWAIT-Flag
72+
* @param noWait True if the statement should have the NOWAIT clause
7073
*/
7174
public void setNoWait(boolean noWait) {
72-
this.waitSeconds = null;
7375
this.noWait = noWait;
7476
checkValidState();
7577
}
7678

77-
public boolean isWait() {
78-
return waitSeconds != null;
79-
}
80-
8179
/**
8280
* Sets the WAIT-Timeout. If this value is set, the Statement is rendered with WAIT
83-
* &lt;timeoutSeconds&gt;
81+
* &lt;timeoutSeconds&gt;<br>
82+
* If the value is set to NULL, the WAIT-clause is skipped
8483
*
85-
* @param waitSeconds The number of seconds for the WAIT timeout
84+
* @param waitSeconds The number of seconds for the WAIT timeout or NULL to skip the WAIT clause
8685
*/
87-
public void setWaitSeconds(long waitSeconds) {
88-
this.noWait = false;
86+
public void setWaitSeconds(Long waitSeconds) {
8987
this.waitSeconds = waitSeconds;
9088
checkValidState();
9189
}
9290

91+
/**
92+
* @return The number of seconds in the WAIT clause, or NULL if the statement has no WAIT clause
93+
*/
94+
public Long getWaitSeconds() {
95+
return waitSeconds;
96+
}
97+
9398
@Override
9499
public String toString() {
95100
return "LOCK TABLE "
@@ -106,7 +111,4 @@ public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
106111
return statementVisitor.visit(this, context);
107112
}
108113

109-
public long getWaitTimeout() {
110-
return waitSeconds;
111-
}
112114
}

src/test/java/net/sf/jsqlparser/statement/lock/LockTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ void testWaitTimeout() throws JSQLParserException {
6363

6464
LockStatement ls = (LockStatement) statement;
6565
assertEquals(LockMode.Share, ls.getLockMode());
66-
assertTrue(ls.isWait());
67-
assertEquals(300, ls.getWaitTimeout());
66+
assertNotNull(ls.getWaitSeconds());
67+
assertEquals(300, ls.getWaitSeconds());
6868
}
6969

7070
@Test
@@ -80,15 +80,38 @@ void testCreateLockStatement() {
8080
ls.setNoWait(true);
8181
assertEquals("LOCK TABLE a IN SHARE MODE NOWAIT", ls.toString());
8282

83-
ls.setWaitSeconds(60);
83+
ls.setNoWait(false);
84+
ls.setWaitSeconds(60L);
8485
assertEquals("LOCK TABLE a IN SHARE MODE WAIT 60", ls.toString());
8586

86-
ls.setNoWait(false);
87+
ls.setWaitSeconds(null);
8788
assertEquals("LOCK TABLE a IN SHARE MODE", ls.toString());
8889

8990
ls.setTable(new Table("b"));
9091
assertEquals("LOCK TABLE b IN SHARE MODE", ls.toString());
9192
}
9293

94+
@Test
95+
void testIllegalStateWaitSeconds() {
96+
Table t = new Table("a");
97+
LockStatement ls = new LockStatement(t, LockMode.Exclusive);
98+
99+
assertThrows(IllegalStateException.class, () -> {
100+
ls.setNoWait(true);
101+
ls.setWaitSeconds(60L);
102+
});
103+
}
104+
105+
@Test
106+
void testIllegalStateNoWait() {
107+
Table t = new Table("a");
108+
LockStatement ls = new LockStatement(t, LockMode.Exclusive);
109+
110+
assertThrows(IllegalStateException.class, () -> {
111+
ls.setWaitSeconds(60L);
112+
ls.setNoWait(true);
113+
});
114+
}
115+
93116

94117
}

0 commit comments

Comments
 (0)