Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J
| **Oracle vector types** | `VECTOR(*, *)` · `VECTOR(*, FLOAT32)` · `VECTOR(3, *)`, including dense and sparse storage formats |
| **PostgreSQL ownership** | `DROP OWNED` · `REASSIGN OWNED` |
| **MySQL table maintenance** | `ANALYZE` · `CHECK` · `CHECKSUM` · `OPTIMIZE` · `REPAIR TABLE` |
| **MySQL locks** | `LOCK TABLES` · `UNLOCK TABLES` · `LOCK INSTANCE FOR BACKUP` · `UNLOCK INSTANCE` |
| **MySQL account management** | `CREATE`/`ALTER`/`RENAME USER` · `CREATE ROLE` · `SET ROLE` · `SET DEFAULT ROLE` · `SET PASSWORD` |
| **Tablespaces** | MySQL and PostgreSQL `CREATE TABLESPACE` · `ALTER TABLESPACE` |
| **PostgreSQL RLS** | `CREATE POLICY` · `ALTER TABLE … ENABLE`/`DISABLE`/`FORCE`/`NO FORCE ROW LEVEL SECURITY` |
Expand Down
22 changes: 22 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 @@ -48,6 +48,9 @@
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.lock.BackupLockStatement;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.lock.UnlockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.reindex.Reindex;
Expand Down Expand Up @@ -676,6 +679,25 @@ public enum Feature {
transactionDeferrable,
/** MySQL WITH CONSISTENT SNAPSHOT transaction characteristic. */
transactionConsistentSnapshot,
/**
* "LOCK [TABLE | TABLES]" statement is allowed: PostgreSQL and Oracle lock modes, and MySQL and
* MariaDB lock types
*
* @see LockStatement
*/
lockTables,
/**
* MySQL/MariaDB "UNLOCK TABLES" statement is allowed
*
* @see UnlockStatement
*/
unlockTables,
/**
* MySQL "LOCK INSTANCE FOR BACKUP" and "UNLOCK INSTANCE" statements are allowed
*
* @see BackupLockStatement
*/
backupLock,
/**
* SQL "COMMENT ON" statement is allowed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.insert.InsertBulk;
import net.sf.jsqlparser.statement.load.LoadDataStatement;
import net.sf.jsqlparser.statement.lock.BackupLockStatement;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.lock.UnlockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.oracle.OracleAssignment;
import net.sf.jsqlparser.statement.oracle.OracleBlock;
Expand Down Expand Up @@ -866,6 +868,16 @@ public <S> Void visit(LockStatement lock, S context) {
return transactionOnly();
}

@Override
public <S> Void visit(UnlockStatement unlock, S context) {
return transactionOnly();
}

@Override
public <S> Void visit(BackupLockStatement backupLock, S context) {
return transactionOnly();
}

private Void transactionOnly() {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_TRANSACTION);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.insert.InsertBulk;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.lock.BackupLockStatement;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.lock.UnlockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.reindex.Reindex;
Expand Down Expand Up @@ -675,6 +677,22 @@ default void visit(LockStatement lock) {
this.visit(lock, null);
}

default <S> T visit(UnlockStatement unlock, S context) {
return null;
}

default void visit(UnlockStatement unlock) {
visit(unlock, null);
}

default <S> T visit(BackupLockStatement backupLock, S context) {
return null;
}

default void visit(BackupLockStatement backupLock) {
visit(backupLock, null);
}

<S> T visit(CreatePolicy createPolicy, S context);

default void visit(CreatePolicy createPolicy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
import net.sf.jsqlparser.statement.insert.InsertBulk;
import net.sf.jsqlparser.statement.insert.InsertConflictAction;
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
import net.sf.jsqlparser.statement.lock.BackupLockStatement;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.lock.UnlockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.merge.MergeOperationVisitor;
import net.sf.jsqlparser.statement.merge.MergeOperationVisitorAdapter;
Expand Down Expand Up @@ -544,6 +546,16 @@ public <S> T visit(LockStatement lock, S context) {
return null;
}

@Override
public <S> T visit(UnlockStatement unlock, S context) {
return null;
}

@Override
public <S> T visit(BackupLockStatement backupLock, S context) {
return null;
}

@Override
public <S> T visit(CreatePolicy createPolicy, S context) {
fromItemVisitor.visitFromItem(createPolicy.getTable(), context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.lock;

import java.util.Objects;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

/**
* MySQL {@code LOCK INSTANCE FOR BACKUP} and {@code UNLOCK INSTANCE}, which take and release the
* instance-level backup lock. It locks no table, unlike {@link LockStatement}.
*
* @see <a href="https://dev.mysql.com/doc/refman/8.4/en/lock-instance-for-backup.html">MySQL LOCK
* INSTANCE FOR BACKUP and UNLOCK INSTANCE</a>
*/
public class BackupLockStatement extends ASTNodeAccessImpl implements Statement {
private Action action = Action.LOCK;

public enum Action {
/** LOCK INSTANCE FOR BACKUP */
LOCK("LOCK INSTANCE FOR BACKUP"),
/** UNLOCK INSTANCE */
UNLOCK("UNLOCK INSTANCE");

private final String sql;

Action(String sql) {
this.sql = sql;
}

public String getSql() {
return sql;
}
}

public BackupLockStatement() {}

public BackupLockStatement(Action action) {
setAction(action);
}

public Action getAction() {
return action;
}

public void setAction(Action action) {
this.action = Objects.requireNonNull(action);
}

public StringBuilder appendTo(StringBuilder builder) {
return builder.append(action.getSql());
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}

@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
}
65 changes: 62 additions & 3 deletions src/main/java/net/sf/jsqlparser/statement/lock/LockStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

/**
* Statement to lock one or more tables.<br>
* Example:<br>
* Examples:<br>
* LOCK TABLE t IN EXCLUSIVE MODE<br>
* LOCK TABLES t READ, u AS a WRITE (MySQL and MariaDB, where each target has a
* {@link TableLockType})<br>
* <br>
*/
public class LockStatement extends ASTNodeAccessImpl implements Statement {

private final List<Target> targets = new ArrayList<>();
private boolean useTableKeyword = true;
private boolean tablesKeyword;
private LockMode lockMode;
private boolean noWait;
private Long waitSeconds;
Expand All @@ -35,11 +39,44 @@ public enum Scope {
DEFAULT, ONLY, INCLUDING_DESCENDANTS
}

/**
* A locked table. A MySQL or MariaDB target also has a lock type and may have an alias, which
* is the {@link Table#getAlias() alias of its table}.
*/
public static class Target extends net.sf.jsqlparser.schema.TableReference {
private TableLockType lockType;

public Target(Table table) {
super(table);
}

/** Returns the MySQL or MariaDB lock type, or null for a PostgreSQL or Oracle target. */
public TableLockType getLockType() {
return lockType;
}

public void setLockType(TableLockType lockType) {
this.lockType = lockType;
}

public Target withLockType(TableLockType lockType) {
setLockType(lockType);
return this;
}

@Override
public StringBuilder appendTo(StringBuilder builder) {
super.appendTo(builder);
Alias alias = getTable().getAlias();
if (alias != null) {
builder.append(alias);
}
if (lockType != null) {
builder.append(' ').append(lockType.getValue());
}
return builder;
}

public Scope getScope() {
return isOnly() ? Scope.ONLY
: isIncludeDescendants() ? Scope.INCLUDING_DESCENDANTS : Scope.DEFAULT;
Expand Down Expand Up @@ -100,8 +137,25 @@ public boolean isUseTableKeyword() {
return useTableKeyword;
}

/** Setting false also clears {@link #isTablesKeyword()}. */
public void setUseTableKeyword(boolean useTableKeyword) {
this.useTableKeyword = useTableKeyword;
if (!useTableKeyword) {
tablesKeyword = false;
}
}

/** Whether the statement is spelled LOCK TABLES, as MySQL and MariaDB allow. */
public boolean isTablesKeyword() {
return tablesKeyword;
}

/** Setting true also sets {@link #isUseTableKeyword()}. */
public void setTablesKeyword(boolean tablesKeyword) {
this.tablesKeyword = tablesKeyword;
if (tablesKeyword) {
useTableKeyword = true;
}
}

/** Returns null when IN ... MODE was omitted. */
Expand Down Expand Up @@ -150,12 +204,17 @@ public Long getWaitSeconds() {
}

public StringBuilder appendTo(StringBuilder builder) {
builder.append(useTableKeyword ? "LOCK TABLE " : "LOCK ");
builder.append("LOCK ");
if (tablesKeyword) {
builder.append("TABLES ");
} else if (useTableKeyword) {
builder.append("TABLE ");
}
for (int i = 0; i < targets.size(); i++) {
if (i > 0) {
builder.append(", ");
}
builder.append(targets.get(i));
targets.get(i).appendTo(builder);
}
if (lockMode != null) {
builder.append(" IN ").append(lockMode.getValue()).append(" MODE");
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/lock/TableLockType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.lock;

/**
* The lock type that MySQL and MariaDB give each table of {@code LOCK TABLES}. MySQL 8.4 has
* {@code READ [LOCAL] | WRITE}.
*
* @see <a href="https://dev.mysql.com/doc/refman/8.4/en/lock-tables.html">MySQL LOCK TABLES</a>
* @see <a href="https://mariadb.com/kb/en/lock-tables/">MariaDB LOCK TABLES</a>
*/
public enum TableLockType {
READ("READ"), READ_LOCAL("READ LOCAL"), WRITE("WRITE"),

/**
* MySQL 8.0, where it is deprecated and has no effect, and MariaDB. MySQL 8.4 rejects it.
*
* @see <a href="https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html">MySQL 8.0 LOCK
* TABLES</a>
*/
LOW_PRIORITY_WRITE("LOW_PRIORITY WRITE"),

/** MariaDB only. */
WRITE_CONCURRENT("WRITE CONCURRENT");

private final String value;

TableLockType(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Loading
Loading