improved postgresql parser, fix #4154

This commit is contained in:
高铁
2021-01-31 02:57:50 +08:00
parent ff4aabaecd
commit af51ed1317
4 changed files with 30 additions and 2 deletions
@@ -145,6 +145,7 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock implements PGSQLObje
private List<SQLExpr> of = new ArrayList<SQLExpr>(2);
private boolean noWait;
private boolean skipLocked;
private Option option;
public Option getOption() {
@@ -171,6 +172,14 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock implements PGSQLObje
this.noWait = noWait;
}
public boolean isSkipLocked() {
return skipLocked;
}
public void setSkipLocked(boolean skipLocked) {
this.skipLocked = skipLocked;
}
@Override
public void accept0(PGASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -26,6 +26,7 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.IntoOption;
import com.alibaba.druid.sql.ast.statement.SQLValuesQuery;
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.util.FnvHash;
import java.util.List;
@@ -231,6 +232,10 @@ public class PGSelectParser extends SQLSelectParser {
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forClause.setNoWait(true);
} else if (lexer.identifierEquals(FnvHash.Constants.SKIP)) {
lexer.nextToken();
acceptIdentifier("LOCKED");
forClause.setSkipLocked(true);
}
queryBlock.setForClause(forClause);
@@ -67,12 +67,13 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
public boolean visit(ForClause x) {
print0(ucase ? "FOR " : "for ");
if (ForClause.Option.UPDATE.equals(x.getOption())) {
print0(ucase ? "UPDATE " : "update ");
print0(ucase ? "UPDATE" : "update");
} else if (ForClause.Option.SHARE.equals(x.getOption())) {
print0(ucase ? "SHARE " : "share ");
print0(ucase ? "SHARE" : "share");
}
if (x.getOf().size() > 0) {
print(' ');
for (int i = 0; i < x.getOf().size(); ++i) {
if (i != 0) {
println(", ");
@@ -83,6 +84,8 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.isNoWait()) {
print0(ucase ? " NOWAIT" : " nowait");
} else if (x.isSkipLocked()) {
print0(ucase ? " SKIP LOCKED" : " skip locked");
}
return false;
@@ -0,0 +1,11 @@
select * from msg.msg_pool
where status=?
order by create_time
limit 100 for update skip locked
---------------------------
SELECT *
FROM msg.msg_pool
WHERE status = ?
ORDER BY create_time
LIMIT 100
FOR UPDATE SKIP LOCKED