mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysql & pg parser. for issue #2483
This commit is contained in:
@@ -180,7 +180,7 @@ public class PGExprParser extends SQLExprParser {
|
||||
if (expr.getClass() == SQLIdentifierExpr.class) {
|
||||
String ident = ((SQLIdentifierExpr)expr).getName();
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
if (lexer.token() == Token.COMMA || lexer.token() == Token.RPAREN) {
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ public class SQLExprParser extends SQLParser {
|
||||
} else if (token == Token.EQ) {
|
||||
expr = relationalRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = xorRest(expr);
|
||||
expr = orRest(expr);
|
||||
return expr;
|
||||
} else {
|
||||
@@ -109,6 +110,7 @@ public class SQLExprParser extends SQLParser {
|
||||
expr = relationalRest(expr);
|
||||
// expr = equalityRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = xorRest(expr);
|
||||
expr = orRest(expr);
|
||||
|
||||
return expr;
|
||||
@@ -1914,9 +1916,33 @@ public class SQLExprParser extends SQLParser {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr or() {
|
||||
|
||||
public SQLExpr xor() {
|
||||
SQLExpr expr = and();
|
||||
if (lexer.token == Token.OR || lexer.token == Token.BARBAR || lexer.token == Token.XOR) {
|
||||
if (lexer.token == Token.XOR) {
|
||||
expr = xorRest(expr);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr xorRest(SQLExpr expr) {
|
||||
for (;;) {
|
||||
if (lexer.token == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, dbType);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr or() {
|
||||
SQLExpr expr = xor();
|
||||
if (lexer.token == Token.OR || lexer.token == Token.BARBAR) {
|
||||
expr = orRest(expr);
|
||||
}
|
||||
return expr;
|
||||
@@ -1926,7 +1952,7 @@ public class SQLExprParser extends SQLParser {
|
||||
for (;;) {
|
||||
if (lexer.token == Token.OR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
SQLExpr rightExp = xor();
|
||||
|
||||
if (lexer.token == Token.OR
|
||||
&& lexer.isEnabled(SQLParserFeature.EnableSQLBinaryOpExprGroup)) {
|
||||
@@ -1941,7 +1967,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
for (;;) {
|
||||
lexer.nextToken();
|
||||
SQLExpr more = and();
|
||||
SQLExpr more = xor();
|
||||
group.add(more);
|
||||
if (lexer.token == Token.OR) {
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
@@ -1959,18 +1985,13 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
} else if (lexer.token == Token.BARBAR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
SQLExpr rightExp = xor();
|
||||
|
||||
SQLBinaryOperator op = JdbcConstants.MYSQL.equals(dbType) && !isEnabled(SQLParserFeature.PipesAsConcat)
|
||||
? SQLBinaryOperator.BooleanOr
|
||||
: SQLBinaryOperator.Concat;
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, dbType);
|
||||
} else if (lexer.token == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, dbType);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -450,6 +450,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
where = this.exprParser.andRest(where);
|
||||
where = this.exprParser.xorRest(where);
|
||||
where = this.exprParser.orRest(where);
|
||||
} else {
|
||||
identExpr = this.exprParser.primaryRest(identExpr);
|
||||
|
||||
@@ -458,7 +458,35 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
// ////////////////////
|
||||
|
||||
public boolean visit(SQLBetweenExpr x) {
|
||||
printExpr(x.getTestExpr());
|
||||
final SQLExpr testExpr = x.getTestExpr();
|
||||
final SQLExpr beginExpr = x.getBeginExpr();
|
||||
final SQLExpr endExpr = x.getEndExpr();
|
||||
|
||||
boolean quote = false;
|
||||
if (testExpr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOperator operator = ((SQLBinaryOpExpr) testExpr).getOperator();
|
||||
switch (operator) {
|
||||
case BooleanAnd:
|
||||
case BooleanOr:
|
||||
case BooleanXor:
|
||||
case Assignment:
|
||||
quote = true;
|
||||
break;
|
||||
default:
|
||||
quote = ((SQLBinaryOpExpr) testExpr).isBracket();
|
||||
break;
|
||||
}
|
||||
} else if (testExpr instanceof SQLNotExpr){
|
||||
quote = true;
|
||||
}
|
||||
|
||||
if (quote) {
|
||||
print('(');
|
||||
printExpr(testExpr);
|
||||
print(')');
|
||||
} else {
|
||||
printExpr(testExpr);
|
||||
}
|
||||
|
||||
if (x.isNot()) {
|
||||
print0(ucase ? " NOT BETWEEN " : " not between ");
|
||||
@@ -466,9 +494,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print0(ucase ? " BETWEEN " : " between ");
|
||||
}
|
||||
|
||||
printExpr(x.getBeginExpr());
|
||||
printExpr(beginExpr);
|
||||
print0(ucase ? " AND " : " and ");
|
||||
printExpr(x.getEndExpr());
|
||||
|
||||
printExpr(endExpr);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.h2;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class H2_AlterTableTest_0 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"alter table ACT_GE_BYTEARRAY add constraint ACT_FK_BYTEARR_DEPL foreign key (DEPLOYMENT_ID_) references ACT_RE_DEPLOYMENT (ID_);"; //
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.toStatementList(sql, JdbcConstants.H2);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
System.out.println(stmt.toString());
|
||||
|
||||
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
{
|
||||
String text = SQLUtils.toSQLString(stmt, JdbcConstants.H2);
|
||||
|
||||
assertEquals("ALTER TABLE ACT_GE_BYTEARRAY\n" +
|
||||
"\tADD CONSTRAINT ACT_FK_BYTEARR_DEPL FOREIGN KEY (DEPLOYMENT_ID_)\n" +
|
||||
"\t\tREFERENCES ACT_RE_DEPLOYMENT (ID_);", text);
|
||||
}
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(2, visitor.getTables().size());
|
||||
assertEquals(2, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
assertEquals(0, visitor.getOrderByColumns().size());
|
||||
|
||||
assertTrue(visitor.containsTable("ACT_GE_BYTEARRAY"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.drop;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlDropDatabaseTest2 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "DROP DATABASE IF EXISTS sonar";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
// print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
assertEquals("DROP DATABASE IF EXISTS sonar", //
|
||||
output);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("City")));
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new Column("t2", "id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.select;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSelectTest_166_xor extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select 1 from corona_select_multi_db_multi_tb where (1 XOR 2 ) between 3 and 4";
|
||||
//
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement)statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
assertEquals("SELECT 1\n" +
|
||||
"FROM corona_select_multi_db_multi_tb\n" +
|
||||
"WHERE (1 XOR 2) BETWEEN 3 AND 4", stmt.toString());
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
String sql = "select * from t where id != 4 or id = 4 xor (id = 4 or id < 4)";
|
||||
//
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement)statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t\n" +
|
||||
"WHERE id != 4\n" +
|
||||
"\tOR id = 4 XOR (id = 4\n" +
|
||||
"\t\tOR id < 4)", stmt.toString());
|
||||
}
|
||||
//
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.postgresql;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGInsertTest14 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "insert into wf_task_geom\n" +
|
||||
" (task_id,geom_in_gds,polygon,lseg)\n" +
|
||||
" values \n" +
|
||||
" (?,ST_GeomFromText(?,4326),?,?)";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
// print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
assertTrue(visitor.containsTable("wf_task_geom"));
|
||||
assertEquals(4, visitor.getColumns().size());
|
||||
//
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "did")));
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "dname")));
|
||||
|
||||
assertEquals("INSERT INTO wf_task_geom (task_id, geom_in_gds, polygon, lseg)\n" +
|
||||
"VALUES (?, ST_GeomFromText(?, 4326), ?, ?)", stmt.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user