Skip to content

Commit

Permalink
for update selects implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Oct 30, 2014
1 parent 32b0a67 commit 92efe5b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 0.9.2

* first support for *FOR UPDATE*

```sql
SELECT * FROM user_table FOR UPDATE
```

```sql
UPDATE mytable SET (col) = (SELECT a FROM mytable2)
```
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class PlainSelect implements SelectBody {
private Top top;
private OracleHierarchicalExpression oracleHierarchical = null;
private boolean oracleSiblings = false;
private boolean forUpdate = false;

/**
* The {@link FromItem} in this query
Expand Down Expand Up @@ -205,6 +206,14 @@ public void setOracleSiblings(boolean oracleSiblings) {
this.oracleSiblings = oracleSiblings;
}

public boolean isForUpdate() {
return forUpdate;
}

public void setForUpdate(boolean forUpdate) {
this.forUpdate = forUpdate;
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder("SELECT ");
Expand Down Expand Up @@ -260,6 +269,9 @@ public String toString() {
if (fetch != null) {
sql.append(fetch);
}
if (isForUpdate()) {
sql.append(" FOR UPDATE");
}
}
return sql.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void visit(PlainSelect plainSelect) {
if (plainSelect.getFetch() != null) {
deparseFetch(plainSelect.getFetch());
}
if (plainSelect.isForUpdate()) {
buffer.append(" FOR UPDATE");
}

}

Expand Down
2 changes: 2 additions & 0 deletions src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ PlainSelect PlainSelect():
[LOOKAHEAD(<K_OFFSET>) offset = Offset() { plainSelect.setOffset(offset); } ]
[LOOKAHEAD(<K_FETCH>) fetch = Fetch() { plainSelect.setFetch(fetch); } ]

[ <K_FOR> <K_UPDATE> { plainSelect.setForUpdate(true); } ]

{
plainSelect.setSelectItems(selectItems);
plainSelect.setFromItem(fromItem);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1616,4 +1616,9 @@ public void testJsonExpression() throws JSQLParserException {
public void testSelectInto1() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * INTO user_copy FROM user");
}

public void testSelectForUpdate() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM user_table FOR UPDATE");
}

}

0 comments on commit 92efe5b

Please sign in to comment.