forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: ForUpdateClause API + multi-table OF clause + ORDER BY after FOR UPDATE #1
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
src/main/java/net/sf/jsqlparser/statement/select/ForUpdateClause.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /*- | ||
| * #%L | ||
| * JSQLParser library | ||
| * %% | ||
| * Copyright (C) 2004 - 2024 JSQLParser | ||
| * %% | ||
| * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 | ||
| * #L% | ||
| */ | ||
| package net.sf.jsqlparser.statement.select; | ||
|
|
||
| import java.util.List; | ||
| import net.sf.jsqlparser.schema.Table; | ||
|
|
||
| /** | ||
| * Represents a FOR UPDATE / FOR SHARE locking clause in a SELECT statement. | ||
| * | ||
| * <p> | ||
| * Supports all common SQL dialects: | ||
| * <ul> | ||
| * <li>{@code FOR UPDATE} – standard row locking</li> | ||
| * <li>{@code FOR UPDATE OF t1, t2} – table-specific locking (Oracle, PostgreSQL)</li> | ||
| * <li>{@code FOR UPDATE NOWAIT} – fail immediately if rows are locked (Oracle, PostgreSQL)</li> | ||
| * <li>{@code FOR UPDATE WAIT n} – wait up to n seconds (Oracle)</li> | ||
| * <li>{@code FOR UPDATE SKIP LOCKED} – skip locked rows (Oracle, PostgreSQL)</li> | ||
| * <li>{@code FOR SHARE} – shared row lock (PostgreSQL)</li> | ||
| * <li>{@code FOR KEY SHARE} – key-level shared lock (PostgreSQL)</li> | ||
| * <li>{@code FOR NO KEY UPDATE} – non-key exclusive lock (PostgreSQL)</li> | ||
| * </ul> | ||
| * </p> | ||
| */ | ||
| public class ForUpdateClause { | ||
|
|
||
| private ForMode mode; | ||
| private List<Table> tables; | ||
| private Wait wait; | ||
| private boolean noWait; | ||
| private boolean skipLocked; | ||
|
|
||
| public ForMode getMode() { | ||
| return mode; | ||
| } | ||
|
|
||
| public ForUpdateClause setMode(ForMode mode) { | ||
| this.mode = mode; | ||
| return this; | ||
| } | ||
|
|
||
| public List<Table> getTables() { | ||
| return tables; | ||
| } | ||
|
|
||
| public ForUpdateClause setTables(List<Table> tables) { | ||
| this.tables = tables; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the first table from the OF clause, or {@code null} if none was specified. | ||
| * | ||
| * @return the first table, or {@code null} | ||
| */ | ||
| public Table getFirstTable() { | ||
| return (tables != null && !tables.isEmpty()) ? tables.get(0) : null; | ||
| } | ||
|
|
||
| public Wait getWait() { | ||
| return wait; | ||
| } | ||
|
|
||
| public ForUpdateClause setWait(Wait wait) { | ||
| this.wait = wait; | ||
| return this; | ||
| } | ||
|
|
||
| public boolean isNoWait() { | ||
| return noWait; | ||
| } | ||
|
|
||
| public ForUpdateClause setNoWait(boolean noWait) { | ||
| this.noWait = noWait; | ||
| return this; | ||
| } | ||
|
|
||
| public boolean isSkipLocked() { | ||
| return skipLocked; | ||
| } | ||
|
|
||
| public ForUpdateClause setSkipLocked(boolean skipLocked) { | ||
| this.skipLocked = skipLocked; | ||
| return this; | ||
| } | ||
|
|
||
| /** Returns {@code true} when the mode is {@link ForMode#UPDATE}. */ | ||
| public boolean isForUpdate() { | ||
| return mode == ForMode.UPDATE; | ||
| } | ||
|
|
||
| /** Returns {@code true} when the mode is {@link ForMode#SHARE}. */ | ||
| public boolean isForShare() { | ||
| return mode == ForMode.SHARE; | ||
| } | ||
|
|
||
| /** Returns {@code true} when at least one table was listed in the OF clause. */ | ||
| public boolean hasTableList() { | ||
| return tables != null && !tables.isEmpty(); | ||
| } | ||
|
|
||
| public StringBuilder appendTo(StringBuilder builder) { | ||
| builder.append(" FOR ").append(mode.getValue()); | ||
| if (tables != null && !tables.isEmpty()) { | ||
| builder.append(" OF "); | ||
| for (int i = 0; i < tables.size(); i++) { | ||
| if (i > 0) { | ||
| builder.append(", "); | ||
| } | ||
| builder.append(tables.get(i)); | ||
| } | ||
| } | ||
| if (wait != null) { | ||
| builder.append(wait); | ||
| } | ||
| if (noWait) { | ||
| builder.append(" NOWAIT"); | ||
| } else if (skipLocked) { | ||
| builder.append(" SKIP LOCKED"); | ||
| } | ||
| return builder; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return appendTo(new StringBuilder()).toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.