Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
optimize use way
  • Loading branch information
jxnu-liguobin committed Dec 16, 2023
commit 92597dcf61b643708974b0b69cb7aadfc766f135
34 changes: 34 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/ForMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2023 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

/**
* @author jxnu-liguobin
*/
public enum ForMode {

UPDATE("UPDATE"),

SHARE("SHARE"),

NO_KEY_UPDATE("NO KEY UPDATE"),

KEY_SHARE("KEY SHARE");

private final String value;

public String getValue() {
return value;
}

ForMode(String s) {
this.value = s;
}
}
60 changes: 10 additions & 50 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public class PlainSelect extends Select {
private Top top;
private OracleHierarchicalExpression oracleHierarchical = null;
private OracleHint oracleHint = null;
private boolean forUpdate = false;
private boolean forShare = false;
private boolean forNoKeyUpdate = false;
private boolean forKeyShare = false;
private ForMode forMode = null;
private Table forUpdateTable = null;
private boolean skipLocked;
private Wait wait;
Expand Down Expand Up @@ -314,12 +311,12 @@ public void setOracleHierarchical(OracleHierarchicalExpression oracleHierarchica
this.oracleHierarchical = oracleHierarchical;
}

public boolean isForUpdate() {
return forUpdate;
public ForMode getForMode() {
return forMode;
}

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

public Table getForUpdateTable() {
Expand Down Expand Up @@ -488,22 +485,9 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
if (emitChanges) {
builder.append(" EMIT CHANGES");
}
if (isForUpdate() || isForShare() || isForKeyShare() || isForNoKeyUpdate()) {
if (getForMode() != null) {
builder.append(" FOR ");
String type = null;
if (isForUpdate()) {
type = "UPDATE";
}
if (isForShare()) {
type = "SHARE";
}
if (isForKeyShare()) {
type = "KEY SHARE";
}
if (isForNoKeyUpdate()) {
type = "NO KEY UPDATE";
}
builder.append(type);
builder.append(getForMode().getValue());

if (forUpdateTable != null) {
builder.append(" OF ").append(forUpdateTable);
Expand Down Expand Up @@ -636,12 +620,12 @@ public PlainSelect withOracleSiblings(boolean oracleSiblings) {
return this;
}

public PlainSelect withForUpdate(boolean forUpdate) {
this.setForUpdate(forUpdate);
public PlainSelect withForMode(ForMode forUpdate) {
this.setForMode(forUpdate);
return this;
}

public PlainSelect withForUpdateTable(Table forUpdateTable) {
public PlainSelect withForModeTable(Table forUpdateTable) {
this.setForUpdateTable(forUpdateTable);
return this;
}
Expand Down Expand Up @@ -701,30 +685,6 @@ public PlainSelect addJoins(Collection<? extends Join> joins) {
return this.withJoins(collection);
}

public boolean isForShare() {
return forShare;
}

public void setForShare(boolean forShare) {
this.forShare = forShare;
}

public boolean isForNoKeyUpdate() {
return forNoKeyUpdate;
}

public void setForNoKeyUpdate(boolean forNoKeyUpdate) {
this.forNoKeyUpdate = forNoKeyUpdate;
}

public boolean isForKeyShare() {
return forKeyShare;
}

public void setForKeyShare(boolean forKeyShare) {
this.forKeyShare = forKeyShare;
}

public <E extends FromItem> E getFromItem(Class<E> type) {
return type.cast(getFromItem());
}
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,9 @@ public void visit(PlainSelect plainSelect) {
buffer.append(plainSelect.getWindowDefinitions().stream()
.map(WindowDefinition::toString).collect(joining(", ")));
}
if (plainSelect.isForUpdate() || plainSelect.isForShare() || plainSelect.isForNoKeyUpdate()
|| plainSelect.isForKeyShare()) {
if (plainSelect.getForMode() != null) {
buffer.append(" FOR ");

String type = null;
if (plainSelect.isForUpdate()) {
type = "UPDATE";
}
if (plainSelect.isForShare()) {
type = "SHARE";
}
if (plainSelect.isForKeyShare()) {
type = "KEY SHARE";
}
if (plainSelect.isForNoKeyUpdate()) {
type = "NO KEY UPDATE";
}
buffer.append(type);
buffer.append(plainSelect.getForMode().getValue());

if (plainSelect.getForUpdateTable() != null) {
buffer.append(" OF ").append(plainSelect.getForUpdateTable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
package net.sf.jsqlparser.util.validation.validator;

import java.util.List;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.MySQLIndexHint;
import net.sf.jsqlparser.expression.SQLServerHints;
import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.ExceptOp;
import net.sf.jsqlparser.statement.select.Fetch;
import net.sf.jsqlparser.statement.select.ForMode;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.IntersectOp;
import net.sf.jsqlparser.statement.select.Join;
Expand Down Expand Up @@ -86,12 +86,14 @@ public void visit(PlainSelect plainSelect) {
isNotEmpty(plainSelect.getOrderByElements()) && plainSelect.isOracleSiblings(),
Feature.oracleOrderBySiblings);

if (plainSelect.isForUpdate() || plainSelect.isForKeyShare()
|| plainSelect.isForNoKeyUpdate() || plainSelect.isForShare()) {
if (plainSelect.getForMode() != null) {
validateFeature(c, Feature.selectForUpdate);
validateFeature(c, plainSelect.isForKeyShare(), Feature.selectForKeyShare);
validateFeature(c, plainSelect.isForNoKeyUpdate(), Feature.selectForNoKeyUpdate);
validateFeature(c, plainSelect.isForShare(), Feature.selectForShare);
validateFeature(c, plainSelect.getForMode() == ForMode.KEY_SHARE,
Feature.selectForKeyShare);
validateFeature(c, plainSelect.getForMode() == ForMode.NO_KEY_UPDATE,
Feature.selectForNoKeyUpdate);
validateFeature(c, plainSelect.getForMode() == ForMode.SHARE,
Feature.selectForShare);

validateOptionalFeature(c, plainSelect.getForUpdateTable(),
Feature.selectForUpdateOfTable);
Expand Down
13 changes: 8 additions & 5 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2269,11 +2269,14 @@ PlainSelect PlainSelect() #PlainSelect:
[ LOOKAHEAD(<K_LIMIT>, { limit==null }) limit = LimitWithOffset() { plainSelect.setLimit(limit); } ]
[ LOOKAHEAD(<K_FETCH>) fetch = Fetch() { plainSelect.setFetch(fetch); } ]
[ LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { plainSelect.setIsolation(withIsolation); } ]
[ LOOKAHEAD(2) <K_FOR> (
<K_UPDATE> { plainSelect.setForUpdate(true); } | <K_SHARE> { plainSelect.setForShare(true); } |
(<K_NO> <K_KEY> <K_UPDATE> { plainSelect.setForNoKeyUpdate(true); }) |
(<K_KEY> <K_SHARE> { plainSelect.setForKeyShare(true); })
)
[ LOOKAHEAD(2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better, well done!

<K_FOR>
(
<K_UPDATE> { plainSelect.setForMode(ForMode.UPDATE); }
| <K_SHARE> { plainSelect.setForMode(ForMode.SHARE); }
| (<K_NO> <K_KEY> <K_UPDATE> { plainSelect.setForMode(ForMode.NO_KEY_UPDATE); })
| (<K_KEY> <K_SHARE> { plainSelect.setForMode(ForMode.KEY_SHARE); })
)
[ LOOKAHEAD(2) <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ]
[ LOOKAHEAD(<K_WAIT>) wait = Wait() { plainSelect.setWait(wait); } ]
[ LOOKAHEAD(2) (<K_NOWAIT> { plainSelect.setNoWait(true); }
Expand Down
20 changes: 17 additions & 3 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -4812,6 +4813,19 @@ public void testIssue1878() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM MY_TABLE1 FOR KEY SHARE");
}

@Test
public void testIssue1878ViaJava() throws JSQLParserException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

String expectedSQLStr = "SELECT * FROM MY_TABLE1 FOR SHARE";

// Step 1: generate the Java Object Hierarchy for
Table table = new Table().withName("MY_TABLE1");

PlainSelect select = new PlainSelect().addSelectItem(new AllColumns())
.withFromItem(table).withForMode(ForMode.KEY_SHARE).withForMode(ForMode.SHARE);

Assertions.assertEquals(expectedSQLStr, select.toString());
}

@Test
public void testKeyWordView() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
Expand Down Expand Up @@ -5572,7 +5586,7 @@ public void testSelectStatementWithForUpdateAndSkipLockedTokens() throws JSQLPar

Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) select;
assertTrue(plainSelect.isForUpdate());
assertSame(plainSelect.getForMode(), ForMode.UPDATE);
assertTrue(plainSelect.isSkipLocked());
}

Expand All @@ -5584,7 +5598,7 @@ public void testSelectStatementWithForUpdateButWithoutSkipLockedTokens()

Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) select;
assertTrue(plainSelect.isForUpdate());
assertSame(plainSelect.getForMode(), ForMode.UPDATE);
assertFalse(plainSelect.isSkipLocked());
}

Expand All @@ -5596,7 +5610,7 @@ public void testSelectStatementWithoutForUpdateAndSkipLockedTokens()

Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect plainSelect = (PlainSelect) select;
assertFalse(plainSelect.isForUpdate());
assertNotNull(plainSelect.getForMode());
assertFalse(plainSelect.isSkipLocked());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package net.sf.jsqlparser.util.validation.validator;

import java.util.Arrays;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.util.validation.ValidationTestAsserts;
Expand Down