mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysq 8 support, support for update skip locked / for share
This commit is contained in:
@@ -53,6 +53,8 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
protected boolean parenthesized = false;
|
||||
protected boolean forUpdate = false;
|
||||
protected boolean noWait = false;
|
||||
protected boolean skipLocked = false;
|
||||
protected boolean forShare = false;
|
||||
protected SQLExpr waitTime;
|
||||
protected SQLLimit limit;
|
||||
|
||||
@@ -534,7 +536,23 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
public void setNoWait(boolean noWait) {
|
||||
this.noWait = noWait;
|
||||
}
|
||||
|
||||
|
||||
public boolean isSkipLocked() {
|
||||
return skipLocked;
|
||||
}
|
||||
|
||||
public void setSkipLocked(boolean skipLocked) {
|
||||
this.skipLocked = skipLocked;
|
||||
}
|
||||
|
||||
public boolean isForShare() {
|
||||
return forShare;
|
||||
}
|
||||
|
||||
public void setForShare(boolean forShare) {
|
||||
this.forShare = forShare;
|
||||
}
|
||||
|
||||
public SQLExpr getWaitTime() {
|
||||
return waitTime;
|
||||
}
|
||||
@@ -986,6 +1004,8 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
x.parenthesized = parenthesized;
|
||||
x.forUpdate = forUpdate;
|
||||
x.noWait = noWait;
|
||||
x.skipLocked = skipLocked;
|
||||
x.forShare = forShare;
|
||||
if (waitTime != null) {
|
||||
x.setWaitTime(waitTime.clone());
|
||||
}
|
||||
|
||||
@@ -292,17 +292,28 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
accept(Token.UPDATE);
|
||||
|
||||
queryBlock.setForUpdate(true);
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.NO_WAIT) || lexer.identifierEquals(FnvHash.Constants.NOWAIT)) {
|
||||
lexer.nextToken();
|
||||
queryBlock.setNoWait(true);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.WAIT)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr waitTime = this.exprParser.primary();
|
||||
queryBlock.setWaitTime(waitTime);
|
||||
if (lexer.token() == Token.UPDATE) {
|
||||
queryBlock.setForUpdate(true);
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.NO_WAIT)
|
||||
|| lexer.identifierEquals(FnvHash.Constants.NOWAIT)) {
|
||||
lexer.nextToken();
|
||||
queryBlock.setNoWait(true);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.WAIT)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr waitTime = this.exprParser.primary();
|
||||
queryBlock.setWaitTime(waitTime);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SKIP)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("LOCKED");
|
||||
queryBlock.setSkipLocked(true);
|
||||
}
|
||||
} else {
|
||||
acceptIdentifier("SHARE");
|
||||
queryBlock.setForShare(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -238,6 +238,15 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? " WAIT " : " wait ");
|
||||
x.getWaitTime().accept(this);
|
||||
}
|
||||
|
||||
if (x.isSkipLocked()) {
|
||||
print0(ucase ? " SKIP LOCKED" : " skip locked");
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isForShare()) {
|
||||
println();
|
||||
print0(ucase ? "FOR SHARE" : "for share");
|
||||
}
|
||||
|
||||
if (x.isLockInShareMode()) {
|
||||
|
||||
@@ -58,10 +58,19 @@ public class MySqlResourceTest extends TestCase {
|
||||
public void test_16() throws Exception {
|
||||
exec_test("bvt/parser/mysql-16.txt");
|
||||
}
|
||||
|
||||
public void test_17() throws Exception {
|
||||
exec_test("bvt/parser/mysql-17.txt");
|
||||
}
|
||||
|
||||
public void test_18() throws Exception {
|
||||
exec_test("bvt/parser/mysql-18.txt");
|
||||
}
|
||||
|
||||
public void test_19() throws Exception {
|
||||
exec_test("bvt/parser/mysql-19.txt");
|
||||
}
|
||||
|
||||
public void exec_test(String resource) throws Exception {
|
||||
// System.out.println(resource);
|
||||
InputStream is = null;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT * FROM table_name WHERE ref = ? AND flag = 0 LIMIT ? FOR UPDATE SKIP LOCKED
|
||||
---------------------------
|
||||
SELECT *
|
||||
FROM table_name
|
||||
WHERE ref = ?
|
||||
AND flag = 0
|
||||
LIMIT ?
|
||||
FOR UPDATE SKIP LOCKED
|
||||
@@ -0,0 +1,6 @@
|
||||
SELECT * FROM parent WHERE NAME = 'Jones' FOR SHARE;
|
||||
---------------------------
|
||||
SELECT *
|
||||
FROM parent
|
||||
WHERE NAME = 'Jones'
|
||||
FOR SHARE;
|
||||
Reference in New Issue
Block a user