mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysql parser support.
This commit is contained in:
+67
-34
@@ -15,47 +15,22 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlLockTableStatement extends MySqlStatementImpl {
|
||||
|
||||
private SQLExprTableSource tableSource;
|
||||
|
||||
private LockType lockType;
|
||||
|
||||
private List<SQLCommentHint> hints;
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLExprTableSource tableSource) {
|
||||
if (tableSource != null) {
|
||||
tableSource.setParent(this);
|
||||
}
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLName name) {
|
||||
setTableSource(new SQLExprTableSource(name));
|
||||
}
|
||||
|
||||
public LockType getLockType() {
|
||||
return lockType;
|
||||
}
|
||||
|
||||
public void setLockType(LockType lockType) {
|
||||
this.lockType = lockType;
|
||||
}
|
||||
private List<Item> items = new ArrayList<Item>();
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, items);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
@@ -70,11 +45,69 @@ public class MySqlLockTableStatement extends MySqlStatementImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
return hints;
|
||||
public List<Item> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setHints(List<SQLCommentHint> hints) {
|
||||
this.hints = hints;
|
||||
public void setItems(List<Item> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public LockType getLockType() {
|
||||
if (items.size() == 1) {
|
||||
return items.get(0).lockType;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
if (items.size() == 1) {
|
||||
return items.get(0).tableSource;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Item extends MySqlObjectImpl {
|
||||
|
||||
private SQLExprTableSource tableSource = new SQLExprTableSource();
|
||||
|
||||
private LockType lockType;
|
||||
|
||||
private List<SQLCommentHint> hints;
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableSource);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLExprTableSource tableSource) {
|
||||
if (tableSource != null) {
|
||||
tableSource.setParent(this);
|
||||
}
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public LockType getLockType() {
|
||||
return lockType;
|
||||
}
|
||||
|
||||
public void setLockType(LockType lockType) {
|
||||
this.lockType = lockType;
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
return hints;
|
||||
}
|
||||
|
||||
public void setHints(List<SQLCommentHint> hints) {
|
||||
this.hints = hints;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+91
-29
@@ -806,31 +806,62 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
MySqlLockTableStatement stmt = new MySqlLockTableStatement();
|
||||
stmt.setTableSource(this.exprParser.name());
|
||||
|
||||
if (lexer.identifierEquals(READ)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals(LOCAL)) {
|
||||
for(;;) {
|
||||
MySqlLockTableStatement.Item item = new MySqlLockTableStatement.Item();
|
||||
|
||||
SQLExprTableSource tableSource = null;
|
||||
SQLName tableName = this.exprParser.name();
|
||||
|
||||
|
||||
if (lexer.token() == Token.AS) {
|
||||
lexer.nextToken();
|
||||
String as = lexer.stringVal();
|
||||
tableSource = new SQLExprTableSource(tableName, as);
|
||||
lexer.nextToken();
|
||||
stmt.setLockType(LockType.READ_LOCAL);
|
||||
} else {
|
||||
stmt.setLockType(LockType.READ);
|
||||
tableSource = new SQLExprTableSource(tableName);
|
||||
}
|
||||
} else if (lexer.identifierEquals(WRITE)) {
|
||||
lexer.nextToken();
|
||||
stmt.setLockType(LockType.WRITE);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.LOW_PRIORITY)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier(WRITE);
|
||||
lexer.nextToken();
|
||||
stmt.setLockType(LockType.LOW_PRIORITY_WRITE);
|
||||
} else {
|
||||
throw new ParserException("syntax error, expect READ or WRITE, actual " + lexer.token() + ", " + lexer.info());
|
||||
item.setTableSource(tableSource);
|
||||
stmt.getItems().add(item);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(READ)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals(LOCAL)) {
|
||||
lexer.nextToken();
|
||||
item.setLockType(LockType.READ_LOCAL);
|
||||
} else {
|
||||
item.setLockType(LockType.READ);
|
||||
}
|
||||
} else if (lexer.identifierEquals(WRITE)) {
|
||||
lexer.nextToken();
|
||||
item.setLockType(LockType.WRITE);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.LOW_PRIORITY)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier(WRITE);
|
||||
lexer.nextToken();
|
||||
item.setLockType(LockType.LOW_PRIORITY_WRITE);
|
||||
} else {
|
||||
throw new ParserException(
|
||||
"syntax error, expect READ or WRITE OR AS, actual " + lexer.token() + ", " + lexer.info());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
item.setHints(this.exprParser.parseHints());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
stmt.setHints(this.exprParser.parseHints());
|
||||
}
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
@@ -859,25 +890,56 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
List<SQLCommentHint> hints = this.exprParser.parseHints();
|
||||
|
||||
boolean tddlHints = false;
|
||||
boolean accept = false;
|
||||
|
||||
final Token token = lexer.token();
|
||||
|
||||
boolean acceptHint = false;
|
||||
switch (lexer.token()) {
|
||||
case SELECT:
|
||||
case WITH:
|
||||
case DELETE:
|
||||
case UPDATE:
|
||||
case INSERT:
|
||||
case SHOW:
|
||||
case REPLACE:
|
||||
case TRUNCATE:
|
||||
case DROP:
|
||||
case ALTER:
|
||||
case CREATE:
|
||||
case CHECK:
|
||||
case SET:
|
||||
acceptHint = true;
|
||||
break;
|
||||
case IDENTIFIER:
|
||||
acceptHint = lexer.hash_lower() == FnvHash.Constants.DUMP
|
||||
|| lexer.hash_lower() == FnvHash.Constants.RENAME;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (hints.size() == 1
|
||||
&& statementList.size() == 0
|
||||
&& (token == Token.SELECT || token == Token.DELETE)) {
|
||||
&& acceptHint) {
|
||||
SQLCommentHint hint = hints.get(0);
|
||||
String hintText = hint.getText();
|
||||
if (hintText.startsWith("+TDDL")) {
|
||||
String hintText = hint.getText().toUpperCase();
|
||||
if (hintText.startsWith("+TDDL")
|
||||
|| hintText.startsWith("+ TDDL")
|
||||
|| hintText.startsWith("TDDL")
|
||||
|| hintText.startsWith("!TDDL"))
|
||||
{
|
||||
tddlHints = true;
|
||||
} else if(hintText.startsWith("+")) {
|
||||
accept = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (tddlHints) {
|
||||
SQLStatementImpl stmt;
|
||||
if (token == Token.SELECT) {
|
||||
stmt = (SQLSelectStatement) this.parseStatement();
|
||||
} else {
|
||||
stmt = (SQLDeleteStatement) this.parseStatement();
|
||||
}
|
||||
SQLStatementImpl stmt = (SQLStatementImpl)this.parseStatement();
|
||||
stmt.setHeadHints(hints);
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
} else if (accept) {
|
||||
SQLStatementImpl stmt = (SQLStatementImpl) this.parseStatement();
|
||||
stmt.setHeadHints(hints);
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
|
||||
@@ -340,6 +340,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(MySqlLockTableStatement x);
|
||||
|
||||
boolean visit(MySqlLockTableStatement.Item x);
|
||||
|
||||
void endVisit(MySqlLockTableStatement.Item x);
|
||||
|
||||
boolean visit(MySqlUnlockTablesStatement x);
|
||||
|
||||
void endVisit(MySqlUnlockTablesStatement x);
|
||||
|
||||
@@ -788,6 +788,16 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlLockTableStatement.Item x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlLockTableStatement.Item x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUnlockTablesStatement x) {
|
||||
return true;
|
||||
|
||||
@@ -2596,7 +2596,22 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlLockTableStatement x) {
|
||||
print0(ucase ? "LOCK TABLES " : "lock tables ");
|
||||
print0(ucase ? "LOCK TABLES" : "lock tables");
|
||||
List<MySqlLockTableStatement.Item> items = x.getItems();
|
||||
if(items.size() > 0) {
|
||||
print(' ');
|
||||
printAndAccept(items, ", ");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlLockTableStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlLockTableStatement.Item x) {
|
||||
x.getTableSource().accept(this);
|
||||
if (x.getLockType() != null) {
|
||||
print(' ');
|
||||
@@ -2611,7 +2626,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlLockTableStatement x) {
|
||||
public void endVisit(MySqlLockTableStatement.Item x) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -886,6 +886,16 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlLockTableStatement.Item x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlLockTableStatement.Item x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUnlockTablesStatement x) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user