Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support FOR SHARE #1922

Merged
merged 5 commits into from
Dec 16, 2023
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
16 changes: 16 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/feature/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ public enum Feature {
* "FOR UPDATE"
*/
selectForUpdate,

/**
* "FOR SHARE"
*/
selectForShare,

/**
* "FOR KEY SHARE"
*/
selectForKeyShare,

/**
* "NO KEY UPDATE"
*/
selectForNoKeyUpdate,

/**
* "FOR UPDATE OF table"
*/
Expand Down
60 changes: 50 additions & 10 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
*/
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHierarchicalExpression;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.expression.WindowDefinition;
import net.sf.jsqlparser.schema.Table;
import static java.util.stream.Collectors.joining;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -23,8 +18,12 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.joining;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHierarchicalExpression;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.expression.WindowDefinition;
import net.sf.jsqlparser.schema.Table;

@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class PlainSelect extends Select {
Expand All @@ -47,6 +46,9 @@ public class PlainSelect extends Select {
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 Table forUpdateTable = null;
private boolean skipLocked;
private Wait wait;
Expand Down Expand Up @@ -486,8 +488,22 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
if (emitChanges) {
builder.append(" EMIT CHANGES");
}
if (isForUpdate()) {
builder.append(" FOR UPDATE");
if (isForUpdate() || isForShare() || isForKeyShare() || isForNoKeyUpdate()) {
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);

if (forUpdateTable != null) {
builder.append(" OF ").append(forUpdateTable);
Expand Down Expand Up @@ -685,6 +701,30 @@ 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
21 changes: 19 additions & 2 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,25 @@ public void visit(PlainSelect plainSelect) {
buffer.append(plainSelect.getWindowDefinitions().stream()
.map(WindowDefinition::toString).collect(joining(", ")));
}
if (plainSelect.isForUpdate()) {
buffer.append(" FOR UPDATE");
if (plainSelect.isForUpdate() || plainSelect.isForShare() || plainSelect.isForNoKeyUpdate()
|| plainSelect.isForKeyShare()) {
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);

if (plainSelect.getForUpdateTable() != null) {
buffer.append(" OF ").append(plainSelect.getForUpdateTable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum MySqlVersion implements Version {
Feature.selectForUpdateOfTable,
Feature.selectForUpdateNoWait,
Feature.selectForUpdateSkipLocked,
Feature.selectForShare,
Feature.distinct,

Feature.setOperation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public enum PostgresqlVersion implements Version {
Feature.orderBy,
Feature.orderByNullOrdering,

Feature.selectForNoKeyUpdate,
Feature.selectForKeyShare,
Feature.selectForShare,
Feature.selectForUpdate,
Feature.selectForUpdateOfTable,
Feature.selectForUpdateNoWait,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -85,8 +86,13 @@ public void visit(PlainSelect plainSelect) {
isNotEmpty(plainSelect.getOrderByElements()) && plainSelect.isOracleSiblings(),
Feature.oracleOrderBySiblings);

if (plainSelect.isForUpdate()) {
if (plainSelect.isForUpdate() || plainSelect.isForKeyShare()
|| plainSelect.isForNoKeyUpdate() || plainSelect.isForShare()) {
validateFeature(c, Feature.selectForUpdate);
validateFeature(c, plainSelect.isForKeyShare(), Feature.selectForKeyShare);
validateFeature(c, plainSelect.isForNoKeyUpdate(), Feature.selectForNoKeyUpdate);
validateFeature(c, plainSelect.isForShare(), Feature.selectForShare);

validateOptionalFeature(c, plainSelect.getForUpdateTable(),
Feature.selectForUpdateOfTable);
validateOptionalFeature(c, plainSelect.getWait(), Feature.selectForUpdateWait);
Expand Down
10 changes: 7 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_SETS:"SETS">
| <K_SHOW : "SHOW">
| <K_SHUTDOWN : "SHUTDOWN">
| <K_SHARE : "SHARE">
| <K_SIBLINGS:"SIBLINGS">
| <K_SIMILAR:"SIMILAR">
| <K_SIZE:"SIZE">
Expand Down Expand Up @@ -1801,7 +1802,7 @@ String RelObjectNameWithoutValue() :
{ Token tk = null; }
{
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> | tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME> | tk=<K_ISOLATION> | tk=<K_TIME_KEY_EXPR>
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DDL" | tk="DECLARE" | tk="DEFAULT" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="END" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FALSE" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GUARD" | tk="HASH" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LINK" | tk="LOCAL" | tk="LOCKED" | tk="LOG" | tk="LOOP" | tk="MATCH" | tk="MATCHED" | tk="MATERIALIZED" | tk="MAXVALUE" | tk="MEMBER" | tk="MERGE" | tk="MINVALUE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERLAPS" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PRECEDING" | tk="PRECISION" | tk="PRIMARY" | tk="PRIOR" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TO" | tk="TRIGGER" | tk="TRUE" | tk="TRUNCATE" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DDL" | tk="DECLARE" | tk="DEFAULT" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="END" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FALSE" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GUARD" | tk="HASH" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LINK" | tk="LOCAL" | tk="LOCK" | tk="LOCKED" | tk="LOG" | tk="LOOP" | tk="MATCH" | tk="MATCHED" | tk="MATERIALIZED" | tk="MAXVALUE" | tk="MEMBER" | tk="MERGE" | tk="MINVALUE" | tk="MODE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERLAPS" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PRECEDING" | tk="PRECISION" | tk="PRIMARY" | tk="PRIOR" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHARE" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TO" | tk="TRIGGER" | tk="TRUE" | tk="TRUNCATE" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
{ return tk.image; }
}

Expand Down Expand Up @@ -2268,13 +2269,16 @@ 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); }
[ LOOKAHEAD(2) <K_FOR> (
<K_UPDATE> { plainSelect.setForUpdate(true); } | <K_SHARE> { plainSelect.setForShare(true); } |
manticore-projects marked this conversation as resolved.
Show resolved Hide resolved
(<K_NO> <K_KEY> <K_UPDATE> { plainSelect.setForNoKeyUpdate(true); }) |
(<K_KEY> <K_SHARE> { plainSelect.setForKeyShare(true); })
)
[ LOOKAHEAD(2) <K_OF> updateTable = Table() { plainSelect.setForUpdateTable(updateTable); } ]
[ LOOKAHEAD(<K_WAIT>) wait = Wait() { plainSelect.setWait(wait); } ]
[ LOOKAHEAD(2) (<K_NOWAIT> { plainSelect.setNoWait(true); }
| <K_SKIP> <K_LOCKED> { plainSelect.setSkipLocked(true); }) ]
]

[ LOOKAHEAD(<K_OPTIMIZE>) optimize = OptimizeFor() { plainSelect.setOptimizeFor(optimize); } ]
{
plainSelect.setSelectItems(selectItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4804,6 +4804,14 @@ public void testCurrentIssue940() throws JSQLParserException {
"SELECT date(current) AS test_date FROM systables WHERE tabid = 1");
}

@Test
manticore-projects marked this conversation as resolved.
Show resolved Hide resolved
public void testIssue1878() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM MY_TABLE1 FOR SHARE");
// PostgreSQL ONLY
assertSqlCanBeParsedAndDeparsed("SELECT * FROM MY_TABLE1 FOR NO KEY UPDATE");
assertSqlCanBeParsedAndDeparsed("SELECT * FROM MY_TABLE1 FOR KEY SHARE");
}

@Test
public void testKeyWordView() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
Expand Down
Loading