mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser.
This commit is contained in:
+29
@@ -21,6 +21,7 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
|
||||
public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements SQLServerExpr, SQLName {
|
||||
|
||||
@@ -28,6 +29,8 @@ public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements
|
||||
private String database;
|
||||
private String schema;
|
||||
|
||||
protected long schema_hash;
|
||||
|
||||
public SQLServerObjectReferenceExpr(){
|
||||
|
||||
}
|
||||
@@ -118,4 +121,30 @@ public class SQLServerObjectReferenceExpr extends SQLServerObjectImpl implements
|
||||
x.schema = schema;
|
||||
return x;
|
||||
}
|
||||
|
||||
public long name_hash_lower() {
|
||||
if (schema_hash == 0
|
||||
&& schema != null) {
|
||||
final int len = schema.length();
|
||||
|
||||
boolean quote = false;
|
||||
|
||||
String name = this.schema;
|
||||
if (len > 2) {
|
||||
char c0 = name.charAt(0);
|
||||
char c1 = name.charAt(len - 1);
|
||||
if ((c0 == '`' && c1 == '`')
|
||||
|| (c0 == '"' && c1 == '"')
|
||||
|| (c0 == '[' && c1 == ']')) {
|
||||
quote = true;
|
||||
}
|
||||
}
|
||||
if (quote) {
|
||||
schema_hash = FNVUtils.fnv_64_lower(name, 1, len -1);
|
||||
} else {
|
||||
schema_hash = FNVUtils.fnv_64_lower(name);
|
||||
}
|
||||
}
|
||||
return schema_hash;
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -38,6 +38,9 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
}
|
||||
|
||||
public void setTop(SQLServerTop top) {
|
||||
if (top != null) {
|
||||
top.setParent(this);
|
||||
}
|
||||
this.top = top;
|
||||
}
|
||||
|
||||
@@ -46,6 +49,9 @@ public class SQLServerUpdateStatement extends SQLUpdateStatement implements SQLS
|
||||
}
|
||||
|
||||
public void setOutput(SQLServerOutput output) {
|
||||
if (output != null) {
|
||||
output.setParent(this);
|
||||
}
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.expr.SQLServerObjectReferenceExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
@@ -65,6 +66,12 @@ public class SQLServerExprParser extends SQLExprParser {
|
||||
this.dbType = JdbcConstants.SQL_SERVER;
|
||||
}
|
||||
|
||||
public SQLServerExprParser(String sql, SQLParserFeature... features){
|
||||
this(new SQLServerLexer(sql, features));
|
||||
this.lexer.nextToken();
|
||||
this.dbType = JdbcConstants.SQL_SERVER;
|
||||
}
|
||||
|
||||
public SQLExpr primary() {
|
||||
|
||||
if (lexer.token() == Token.LBRACKET) {
|
||||
|
||||
@@ -21,10 +21,7 @@ import static com.alibaba.druid.sql.parser.Token.IDENTIFIER;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Keywords;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.NotAllowCommentException;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
|
||||
public class SQLServerLexer extends Lexer {
|
||||
|
||||
@@ -59,6 +56,14 @@ public class SQLServerLexer extends Lexer {
|
||||
super(input);
|
||||
super.keywods = DEFAULT_SQL_SERVER_KEYWORDS;
|
||||
}
|
||||
|
||||
public SQLServerLexer(String input, SQLParserFeature... features){
|
||||
super(input);
|
||||
super.keywods = DEFAULT_SQL_SERVER_KEYWORDS;
|
||||
for (SQLParserFeature feature : features) {
|
||||
config(feature, true);
|
||||
}
|
||||
}
|
||||
|
||||
public void scanComment() {
|
||||
if (ch != '/' && ch != '-') {
|
||||
|
||||
+5
-6
@@ -17,11 +17,7 @@ package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprHint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
@@ -42,7 +38,10 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
public SQLSelect select() {
|
||||
SQLSelect select = new SQLSelect();
|
||||
|
||||
withSubquery(select);
|
||||
if (lexer.token() == Token.WITH) {
|
||||
SQLWithSubqueryClause with = this.parseWith();
|
||||
select.setWithSubQuery(with);
|
||||
}
|
||||
|
||||
select.setQuery(query());
|
||||
select.setOrderBy(parseOrderBy());
|
||||
|
||||
+5
-5
@@ -37,11 +37,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerSetTransactionIsolationLevelStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerWaitForStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
|
||||
public class SQLServerStatementParser extends SQLStatementParser {
|
||||
|
||||
@@ -49,6 +45,10 @@ public class SQLServerStatementParser extends SQLStatementParser {
|
||||
super(new SQLServerExprParser(sql));
|
||||
}
|
||||
|
||||
public SQLServerStatementParser(String sql, SQLParserFeature... features){
|
||||
super(new SQLServerExprParser(sql, features));
|
||||
}
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new SQLServerSelectParser(this.exprParser);
|
||||
}
|
||||
|
||||
+44
-42
@@ -15,10 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
@@ -57,42 +54,45 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
print0(ucase ? "UNIQUE " : "unique ");
|
||||
}
|
||||
|
||||
if (x.getTop() != null) {
|
||||
x.getTop().accept(this);
|
||||
SQLServerTop top = x.getTop();
|
||||
if (top != null) {
|
||||
visit(top);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
printSelectList(x.getSelectList());
|
||||
|
||||
if (x.getInto() != null) {
|
||||
SQLExprTableSource into = x.getInto();
|
||||
if (into != null) {
|
||||
println();
|
||||
print0(ucase ? "INTO " : "into ");
|
||||
x.getInto().accept(this);
|
||||
printTableSource(into);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from != null) {
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
x.getFrom().accept(this);
|
||||
printTableSource(from);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
x.getWhere().setParent(x);
|
||||
x.getWhere().accept(this);
|
||||
printExpr(where);
|
||||
}
|
||||
|
||||
SQLSelectGroupByClause groupBy = x.getGroupBy();
|
||||
if (groupBy != null) {
|
||||
println();
|
||||
groupBy.accept(this);
|
||||
visit(groupBy);
|
||||
}
|
||||
|
||||
SQLOrderBy orderBy = x.getOrderBy();
|
||||
if (orderBy != null) {
|
||||
println();
|
||||
orderBy.accept(this);
|
||||
visit(orderBy);
|
||||
}
|
||||
|
||||
printFetchFirst(x);
|
||||
@@ -203,13 +203,13 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
public boolean visit(SQLServerUpdateStatement x) {
|
||||
print0(ucase ? "UPDATE " : "update ");
|
||||
|
||||
if (x.getTop() != null) {
|
||||
x.getTop().setParent(x);
|
||||
x.getTop().accept(this);
|
||||
SQLServerTop top = x.getTop();
|
||||
if (top != null) {
|
||||
top.accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
x.getTableSource().accept(this);
|
||||
printTableSource(x.getTableSource());
|
||||
|
||||
println();
|
||||
print0(ucase ? "SET " : "set ");
|
||||
@@ -217,29 +217,30 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
if (i != 0) {
|
||||
print0(", ");
|
||||
}
|
||||
x.getItems().get(i).accept(this);
|
||||
}
|
||||
|
||||
if (x.getOutput() != null) {
|
||||
println();
|
||||
x.getOutput().setParent(x);
|
||||
x.getOutput().accept(this);
|
||||
SQLUpdateSetItem item = x.getItems().get(i);
|
||||
visit(item);
|
||||
}
|
||||
|
||||
if (x.getFrom() != null) {
|
||||
SQLServerOutput output = x.getOutput();
|
||||
if (output != null) {
|
||||
println();
|
||||
visit(output);
|
||||
}
|
||||
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from != null) {
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
x.getFrom().setParent(x);
|
||||
x.getFrom().accept(this);
|
||||
printTableSource(from);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
indentCount++;
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
incrementIndent();
|
||||
x.getWhere().setParent(x);
|
||||
x.getWhere().accept(this);
|
||||
decrementIndent();
|
||||
printExpr(where);
|
||||
indentCount--;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -253,9 +254,10 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
printTableSourceExpr(x.getExpr());
|
||||
|
||||
if (x.getAlias() != null) {
|
||||
String alias = x.getAlias();
|
||||
if (alias != null) {
|
||||
print(' ');
|
||||
print0(x.getAlias());
|
||||
print0(alias);
|
||||
}
|
||||
|
||||
if (x.getHints() != null && x.getHints().size() > 0) {
|
||||
@@ -366,13 +368,13 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
printSelectList(x.getSelectList());
|
||||
|
||||
if (x.getInto() != null) {
|
||||
incrementIndent();
|
||||
this.indentCount++;
|
||||
println();
|
||||
print0(ucase ? "INTO " : "into ");
|
||||
x.getInto().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
incrementIndent();
|
||||
this.indentCount++;
|
||||
println();
|
||||
print('(');
|
||||
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
|
||||
@@ -386,10 +388,10 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
x.getColumns().get(i).accept(this);
|
||||
}
|
||||
print(')');
|
||||
decrementIndent();
|
||||
this.indentCount--;
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
this.indentCount--;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -401,7 +403,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
@Override
|
||||
public boolean visit(SQLBlockStatement x) {
|
||||
print0(ucase ? "BEGIN" : "begin");
|
||||
incrementIndent();
|
||||
this.indentCount++;
|
||||
println();
|
||||
for (int i = 0, size = x.getStatementList().size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
@@ -412,7 +414,7 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
|
||||
stmt.accept(this);
|
||||
print(';');
|
||||
}
|
||||
decrementIndent();
|
||||
this.indentCount--;
|
||||
println();
|
||||
print0(ucase ? "END" : "end");
|
||||
return false;
|
||||
|
||||
+3
-4
@@ -33,13 +33,12 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerUpdateStatement
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerWaitForStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class SQLServerSchemaStatVisitor extends SchemaStatVisitor implements SQLServerASTVisitor {
|
||||
|
||||
@Override
|
||||
public String getDbType() {
|
||||
return JdbcUtils.SQL_SERVER;
|
||||
public SQLServerSchemaStatVisitor() {
|
||||
super(JdbcConstants.SQL_SERVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user