Skip to content

Commit 1211dcf

Browse files
committed
corrected merge conflict
2 parents 12cc405 + 5dcefce commit 1211dcf

File tree

6 files changed

+128
-4
lines changed

6 files changed

+128
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ To help JSqlParsers development you are encouraged to provide
4444
Also I would like to know about needed examples or documentation stuff.
4545

4646
## Extensions in the latest SNAPSHOT version 0.9.8
47+
48+
* support **FOR UPDATE WAIT**
4749
* support for simple expressions within case when
4850
* rewrite of SelectBody - production, reduce of needed lookaheads results in huge parser performance improvement
4951
* please test it due to possible changes in the parse tree

src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public class PlainSelect implements SelectBody {
5757
private boolean forUpdate = false;
5858
private Table forUpdateTable = null;
5959
private boolean useBrackets = false;
60-
60+
private Wait wait;
61+
6162
public boolean isUseBrackets() {
6263
return useBrackets;
6364
}
@@ -266,6 +267,25 @@ public OracleHint getOracleHint() {
266267
public void setOracleHint(OracleHint oracleHint) {
267268
this.oracleHint = oracleHint;
268269
}
270+
271+
/**
272+
* Sets the {@link Wait} for this SELECT
273+
*
274+
* @param wait
275+
* the {@link Wait} for this SELECT
276+
*/
277+
public void setWait(final Wait wait) {
278+
this.wait = wait;
279+
}
280+
281+
/**
282+
* Returns the value of the {@link Wait} set for this SELECT
283+
*
284+
* @return the value of the {@link Wait} set for this SELECT
285+
*/
286+
public Wait getWait() {
287+
return wait;
288+
}
269289

270290
@Override
271291
public String toString() {
@@ -344,6 +364,11 @@ public String toString() {
344364
if (forUpdateTable != null) {
345365
sql.append(" OF ").append(forUpdateTable);
346366
}
367+
368+
if (wait != null) {
369+
// Wait's toString will do the formatting for us
370+
sql.append(wait);
371+
}
347372
}
348373
} else {
349374
//without from
@@ -432,4 +457,4 @@ public static String getStringList(List<?> list, boolean useComma, boolean useBr
432457

433458
return ans.toString();
434459
}
435-
}
460+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* #%L JSQLParser library %% Copyright (C) 2004 - 2017 JSQLParser %% This program is free software:
3+
* you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
4+
* as published by the Free Software Foundation, either version 2.1 of the License, or (at your
5+
* option) any later version. This program is distributed in the hope that it will be useful, but
6+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
7+
* PARTICULAR PURPOSE. See the GNU General Lesser Public License for more details. You should have
8+
* received a copy of the GNU General Lesser Public License along with this program. If not, see
9+
* <http://www.gnu.org/licenses/lgpl-2.1.html>. #L%
10+
*/
11+
package net.sf.jsqlparser.statement.select;
12+
13+
/**
14+
* A timeout applied to SELECT to specify how long to wait for the row on the
15+
* lock to be released.
16+
*
17+
* @author janmonterrubio
18+
*/
19+
public class Wait {
20+
private long timeout;
21+
22+
/**
23+
* Returns the number of seconds specified for the WAIT command
24+
*
25+
* @return the number of seconds specified for the WAIT command
26+
*/
27+
public long getTimeout() {
28+
return timeout;
29+
}
30+
31+
/**
32+
* Sets the number of seconds to WAIT for this {@link Wait}
33+
*
34+
* @param timeout
35+
* the number of seconds to WAIT for this {@link Wait}
36+
*/
37+
public void setTimeout(long timeout) {
38+
this.timeout = timeout;
39+
}
40+
41+
/**
42+
* Returns a String containing the WAIT clause and its timeout, where
43+
* TIMEOUT is specified by {@link #getTimeout()}. The returned string will
44+
* be:<code>
45+
* &quot; WAIT &lt;TIMEOUT&gt;&quot;
46+
* </code>
47+
*/
48+
@Override
49+
public String toString() {
50+
return " WAIT " + timeout;
51+
}
52+
}

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ public void visit(PlainSelect plainSelect) {
168168
if (plainSelect.getForUpdateTable() != null) {
169169
buffer.append(" OF ").append(plainSelect.getForUpdateTable());
170170
}
171+
if (plainSelect.getWait() != null) {
172+
// wait's toString will do the formatting for us
173+
buffer.append(plainSelect.getWait());
174+
}
171175
}
172176
if (plainSelect.isUseBrackets()) {
173177
buffer.append(")");

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
255255
| <K_PRECISION : "PRECISION">
256256
| <K_TABLESPACE : "TABLESPACE">
257257
| <K_EXCLUDE : "EXCLUDE">
258+
| <K_WAIT : "WAIT">
258259
}
259260

260261
TOKEN : /* Stuff */
@@ -882,6 +883,7 @@ PlainSelect PlainSelect():
882883
OracleHierarchicalExpression oracleHierarchicalQueryClause = null;
883884
List<Table> intoTables = null;
884885
Table updateTable = null;
886+
Wait wait = null;
885887
}
886888
{
887889
<K_SELECT>
@@ -924,8 +926,9 @@ PlainSelect PlainSelect():
924926
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { plainSelect.setOffset(offset); } ]
925927
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { plainSelect.setFetch(fetch); } ]
926928

927-
[ <K_FOR> <K_UPDATE> { plainSelect.setForUpdate(true); }
928-
[ <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ] ]
929+
[ <K_FOR> <K_UPDATE> { plainSelect.setForUpdate(true); }
930+
[ <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ]
931+
[ LOOKAHEAD(<K_WAIT>) wait = Wait() { plainSelect.setWait(wait); } ] ]
929932

930933
{
931934
plainSelect.setSelectItems(selectItems);
@@ -3209,3 +3212,18 @@ Alter AlterTable():
32093212
return alter;
32103213
}
32113214
}
3215+
3216+
Wait Wait():
3217+
{
3218+
Wait wait = new Wait();
3219+
Token token = null;
3220+
}
3221+
{
3222+
// sqlserver-oracle-> WAIT (TIMEOUT)
3223+
// https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm#i2126016
3224+
<K_WAIT> token=<S_LONG> { wait.setTimeout(Long.parseLong(token.image)); }
3225+
3226+
{
3227+
return wait;
3228+
}
3229+
}

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,29 @@ public void testKeyWorkReplaceIssue393() throws JSQLParserException {
24902490
assertSqlCanBeParsedAndDeparsed("SELECT replace(\"aaaabbb\", 4, 4, \"****\")");
24912491
}
24922492

2493+
/**
2494+
* Validates that a SELECT with FOR UPDATE WAIT <TIMEOUT> can be parsed and
2495+
* deparsed
2496+
*/
2497+
public void testForUpdateWaitParseDeparse() throws JSQLParserException {
2498+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable FOR UPDATE WAIT 60");
2499+
}
2500+
2501+
/**
2502+
* Validates that a SELECT with FOR UPDATE WAIT <TIMEOUT> correctly sets a
2503+
* {@link Wait} with the correct timeout value.
2504+
*/
2505+
public void testForUpdateWaitWithTimeout() throws JSQLParserException {
2506+
String statement = "SELECT * FROM mytable FOR UPDATE WAIT 60";
2507+
Select select = (Select) parserManager.parse(new StringReader(statement));
2508+
PlainSelect ps = (PlainSelect) select.getSelectBody();
2509+
Wait wait = ps.getWait();
2510+
assertNotNull("wait should not be null", wait);
2511+
2512+
long waitTime = wait.getTimeout();
2513+
assertEquals("wait time should be 60", waitTime, 60L);
2514+
}
2515+
24932516
// public void testSubSelectFailsIssue394() throws JSQLParserException {
24942517
// assertSqlCanBeParsedAndDeparsed("select aa.* , t.* from accenter.all aa, (select a.* from pacioli.emc_plan a) t");
24952518
// }

0 commit comments

Comments
 (0)