mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
enhance sql parser & add test
This commit is contained in:
@@ -112,6 +112,7 @@ public class MySqlLexer extends Lexer {
|
||||
map.put("KILL", Token.KILL);
|
||||
map.put("IDENTIFIED", Token.IDENTIFIED);
|
||||
map.put("PASSWORD", Token.PASSWORD);
|
||||
map.put("DUAL", Token.DUAL);
|
||||
|
||||
DEFAULT_MYSQL_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -768,7 +768,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
if (identifierEquals("ROWS")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("IDENTIFIED");
|
||||
accept(Token.IDENTIFIED);
|
||||
accept(Token.BY);
|
||||
SQLExpr rowsIdentifiedBy = exprParser.expr();
|
||||
stmt.setRowsIdentifiedBy(rowsIdentifiedBy);
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
@@ -43,6 +44,13 @@ import com.alibaba.druid.stat.TableStat.Mode;
|
||||
|
||||
public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlASTVisitor {
|
||||
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
aliasLocal.get().put("DUAL", null);
|
||||
|
||||
return true;
|
||||
}
|
||||
//DUAL
|
||||
public boolean visit(MySqlDeleteStatement x) {
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
|
||||
|
||||
+9
-1
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
@@ -10,6 +11,13 @@ public class PGSelectStatement extends SQLSelectStatement implements PGSQLStatem
|
||||
private static final long serialVersionUID = 1L;
|
||||
private PGWithClause with;
|
||||
|
||||
public PGSelectStatement(){
|
||||
}
|
||||
|
||||
public PGSelectStatement(SQLSelect select){
|
||||
super(select);
|
||||
}
|
||||
|
||||
public PGWithClause getWith() {
|
||||
return with;
|
||||
}
|
||||
@@ -17,7 +25,7 @@ public class PGSelectStatement extends SQLSelectStatement implements PGSQLStatem
|
||||
public void setWith(PGWithClause with) {
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((PGASTVisitor) visitor);
|
||||
}
|
||||
|
||||
+11
-1
@@ -15,6 +15,7 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithQuery;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGTruncateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGUpdateStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
@@ -311,6 +312,10 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
return withQuery;
|
||||
}
|
||||
|
||||
public PGSelectStatement parseSelect() throws ParserException {
|
||||
return new PGSelectStatement(createSQLSelectParser().select());
|
||||
}
|
||||
|
||||
public SQLStatement parseWith() {
|
||||
PGWithClause with = this.parseWithClause();
|
||||
if (lexer.token() == Token.INSERT) {
|
||||
@@ -318,7 +323,12 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
stmt.setWith(with);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
PGSelectStatement stmt = this.parseSelect();
|
||||
stmt.setWith(with);
|
||||
return stmt;
|
||||
}
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,6 +198,12 @@ public class SQLExprParser extends SQLParser {
|
||||
sqlExpr = new SQLDefaultExpr();
|
||||
lexer.nextToken();
|
||||
break;
|
||||
case USER:
|
||||
case PASSWORD:
|
||||
case DUAL:
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
break;
|
||||
case CASE:
|
||||
SQLCaseExpr caseExpr = new SQLCaseExpr();
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -102,6 +102,7 @@ public enum Token {
|
||||
KILL("KILL"),
|
||||
IDENTIFIED("IDENTIFIED"),
|
||||
PASSWORD("PASSWORD"),
|
||||
DUAL("DUAL"),
|
||||
|
||||
//postgresql
|
||||
WINDOW("WINDOW"),
|
||||
|
||||
Reference in New Issue
Block a user