mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修正运算符优先级别处理不当的问题
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@157 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -52,7 +52,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
public SQLExpr relationalRest(SQLExpr expr) throws ParserException {
|
||||
if (lexer.token() == Token.IDENTIFIER && "REGEXP".equalsIgnoreCase(lexer.stringVal())) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = bitOr();
|
||||
SQLExpr rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
@@ -65,7 +65,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
public SQLExpr multiplicativeRest(SQLExpr expr) throws ParserException {
|
||||
if (lexer.token() == Token.IDENTIFIER && "MOD".equalsIgnoreCase(lexer.stringVal())) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = bitOr();
|
||||
SQLExpr rightExp = primary();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
@@ -78,7 +78,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
public SQLExpr notRationalRest(SQLExpr expr) {
|
||||
if (lexer.token() == Token.IDENTIFIER && "REGEXP".equalsIgnoreCase(lexer.stringVal())) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = bitOr();
|
||||
SQLExpr rightExp = primary();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
|
||||
@@ -93,7 +93,6 @@ public class SQLExprParser extends SQLParser {
|
||||
expr = relationalRest(expr);
|
||||
expr = equalityRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = xorRest(expr);
|
||||
expr = orRest(expr);
|
||||
|
||||
return expr;
|
||||
@@ -642,7 +641,7 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
public final SQLExpr equality() throws ParserException {
|
||||
SQLExpr expr = relational();
|
||||
SQLExpr expr = multiplicative();
|
||||
return equalityRest(expr);
|
||||
}
|
||||
|
||||
@@ -650,14 +649,14 @@ public class SQLExprParser extends SQLParser {
|
||||
SQLExpr rightExp;
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
rightExp = additive();
|
||||
|
||||
rightExp = equalityRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp);
|
||||
} else if (lexer.token() == Token.BANGEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
rightExp = additive();
|
||||
|
||||
rightExp = equalityRest(rightExp);
|
||||
|
||||
@@ -754,7 +753,7 @@ public class SQLExprParser extends SQLParser {
|
||||
public final SQLExpr andRest(SQLExpr expr) throws ParserException {
|
||||
if (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = equality();
|
||||
SQLExpr rightExp = relational();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp);
|
||||
expr = andRest(expr);
|
||||
@@ -763,38 +762,31 @@ public class SQLExprParser extends SQLParser {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public final SQLExpr xor() throws ParserException {
|
||||
SQLExpr expr = relational();
|
||||
return xorRest(expr);
|
||||
}
|
||||
|
||||
public final SQLExpr xorRest(SQLExpr expr) throws ParserException {
|
||||
if (lexer.token() == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = equality();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp);
|
||||
expr = xorRest(expr);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
public final SQLExpr or() throws ParserException {
|
||||
SQLExpr expr = and();
|
||||
return orRest(expr);
|
||||
}
|
||||
|
||||
public final SQLExpr orRest(SQLExpr expr) throws ParserException {
|
||||
while (lexer.token() == Token.OR) {
|
||||
if (lexer.token() == Token.OR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
SQLExpr rightExp = relational();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp);
|
||||
expr = orRest(expr);
|
||||
} else if (lexer.token() == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = relational();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp);
|
||||
expr = orRest(expr);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public final SQLExpr relational() throws ParserException {
|
||||
SQLExpr expr = bitOr();
|
||||
SQLExpr expr = equality();
|
||||
|
||||
return relationalRest(expr);
|
||||
}
|
||||
@@ -804,7 +796,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
if (lexer.token() == Token.LT) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
|
||||
@@ -4,19 +4,24 @@ import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisitor;
|
||||
|
||||
public class OnlineSQLTest extends TestCase {
|
||||
private String url = "jdbc:mysql://10.20.129.146/dragoon_v25monitordb_online";
|
||||
private String user = "dragoon";
|
||||
|
||||
private String url = "jdbc:mysql://10.20.129.146/dragoon_v25monitordb_online";
|
||||
private String user = "dragoon";
|
||||
private String password = "dragoon";
|
||||
|
||||
|
||||
|
||||
|
||||
public void test_list_sql() throws Exception {
|
||||
Connection conn = DriverManager.getConnection(url, user, password);
|
||||
|
||||
|
||||
int count = 0;
|
||||
String sql = "SELECT id, value FROM m_sql_const";
|
||||
Statement stmt = conn.createStatement();
|
||||
@@ -25,13 +30,38 @@ public class OnlineSQLTest extends TestCase {
|
||||
String value = rs.getString(2);
|
||||
System.out.println(value);
|
||||
System.out.println();
|
||||
try {
|
||||
validate(value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
count++;
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
|
||||
System.out.println("COUNT : " + count);
|
||||
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
void validate(String sql) throws Exception {
|
||||
|
||||
try {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlParameterizedOutputVisitor visitor = new MySqlParameterizedOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(sql);
|
||||
System.out.println(out.toString());
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class AssignmentOperatorsTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("UPDATE t1 SET c1 = 2 WHERE c1 = @var1 := 1;", text);
|
||||
Assert.assertEquals("UPDATE t1 SET c1 = 2 WHERE c1 = (@var1 := 1);", text);
|
||||
}
|
||||
|
||||
private String output(List<SQLStatement> stmtList) {
|
||||
|
||||
+2
-2
@@ -12,8 +12,8 @@ import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisit
|
||||
public class MySqlParameterizedOutputVisitorTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
//validate("SELECT * FROM T WHERE ID IN (?, ?, ?)", "SELECT * FROM T WHERE ID IN (##)");
|
||||
//validate("SELECT * FROM T WHERE ID = 5", "SELECT * FROM T WHERE ID = ?");
|
||||
validate("SELECT * FROM T WHERE ID IN (?, ?, ?)", "SELECT * FROM T WHERE ID IN (##)");
|
||||
validate("SELECT * FROM T WHERE ID = 5", "SELECT * FROM T WHERE ID = ?");
|
||||
validate("SELECT * FROM T WHERE 1 = 0 AND ID = 5", "SELECT * FROM T WHERE 1 = 0 AND ID = ?");
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ SELECT channel_desc, calendar_month_desc, co.country_id,
|
||||
---------------------------
|
||||
SELECT channel_desc, calendar_month_desc, co.country_id, TO_CHAR(sum(amount_sold), '9,999,999,999') AS SALES$
|
||||
FROM sales , customers , times , channels , countries co
|
||||
WHERE sales.time_id = times.time_id AND (sales.cust_id = customers.cust_id AND (sales.channel_id = channels.channel_id AND (customers.country_id = co.country_id AND (channels.channel_desc IN ('Direct Sales', 'Internet') AND (times.calendar_month_desc IN ('2000-09', '2000-10') AND co.country_iso_code IN ('UK', 'US'))))))
|
||||
WHERE sales.time_id = times.time_id AND sales.cust_id = customers.cust_id AND sales.channel_id = channels.channel_id AND customers.country_id = co.country_id AND (channels.channel_desc IN ('Direct Sales', 'Internet') AND (times.calendar_month_desc IN ('2000-09', '2000-10') AND co.country_iso_code IN ('UK', 'US')))
|
||||
GROUP BY GROUPING SETS ((channel_desc, calendar_month_desc, co.country_id), (channel_desc, co.country_id), (calendar_month_desc, co.country_id));
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user