mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #955 from wenshao/master
bug fixed for pg sql parser
This commit is contained in:
@@ -59,24 +59,24 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
accept(Token.RPAREN);
|
||||
return queryRest(valuesQuery);
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectQuery select = query();
|
||||
if (select instanceof SQLSelectQueryBlock) {
|
||||
((SQLSelectQueryBlock) select).setParenthesized(true);
|
||||
}
|
||||
if (select instanceof SQLSelectQueryBlock) {
|
||||
((SQLSelectQueryBlock) select).setParenthesized(true);
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
}
|
||||
|
||||
|
||||
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
if (lexer.token() == Token.COMMENT) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
@@ -160,7 +160,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.LIMIT) {
|
||||
PGLimit limit = new PGLimit();
|
||||
|
||||
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.ALL) {
|
||||
limit.setRowCount(new SQLIdentifierExpr("ALL"));
|
||||
@@ -168,7 +168,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
} else {
|
||||
limit.setRowCount(expr());
|
||||
}
|
||||
|
||||
|
||||
queryBlock.setLimit(limit);
|
||||
} else if (lexer.token() == Token.OFFSET) {
|
||||
PGLimit limit = queryBlock.getLimit();
|
||||
@@ -259,13 +259,22 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
|
||||
if (lexer.token() == Token.AS && tableSource instanceof SQLExprTableSource) {
|
||||
lexer.nextToken();
|
||||
|
||||
String alias = null;
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
alias = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
|
||||
|
||||
String alias = this.as();
|
||||
PGFunctionTableSource functionTableSource = new PGFunctionTableSource(exprTableSource.getExpr());
|
||||
functionTableSource.setAlias(alias);
|
||||
|
||||
if (alias != null) {
|
||||
functionTableSource.setAlias(alias);
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
parserParameters(functionTableSource.getParameters());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
+3
-1
@@ -97,6 +97,8 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
|
||||
@Override
|
||||
public boolean visit(PGWithQuery x) {
|
||||
x.getQuery().accept(this);
|
||||
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
String alias = null;
|
||||
@@ -109,7 +111,7 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
|
||||
subQueryMap.put(alias, x.getQuery());
|
||||
}
|
||||
}
|
||||
x.getQuery().accept(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
@@ -103,20 +104,20 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
protected final List<Column> orderByColumns = new ArrayList<Column>();
|
||||
protected final Set<Column> groupByColumns = new LinkedHashSet<Column>();
|
||||
|
||||
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
|
||||
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
|
||||
|
||||
protected final Map<String, SQLObject> variants = new LinkedHashMap<String, SQLObject>();
|
||||
protected final Map<String, SQLObject> variants = new LinkedHashMap<String, SQLObject>();
|
||||
|
||||
protected Map<String, String> aliasMap = new HashMap<String, String>();
|
||||
protected Map<String, String> aliasMap = new HashMap<String, String>();
|
||||
|
||||
protected String currentTable;
|
||||
protected String currentTable;
|
||||
|
||||
public final static String ATTR_TABLE = "_table_";
|
||||
public final static String ATTR_COLUMN = "_column_";
|
||||
public final static String ATTR_TABLE = "_table_";
|
||||
public final static String ATTR_COLUMN = "_column_";
|
||||
|
||||
private List<Object> parameters;
|
||||
private List<Object> parameters;
|
||||
|
||||
private Mode mode;
|
||||
private Mode mode;
|
||||
|
||||
public SchemaStatVisitor(){
|
||||
this(new ArrayList<Object>());
|
||||
@@ -739,7 +740,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
if (!item.getClass().equals(SQLSelectItem.class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
String itemAlias = item.getAlias();
|
||||
SQLExpr itemExpr = item.getExpr();
|
||||
if (itemAlias == null) {
|
||||
@@ -880,7 +881,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
public void endVisit(SQLSelectStatement x) {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLWithSubqueryClause.Entry x) {
|
||||
String alias = x.getName().toString();
|
||||
@@ -888,22 +889,22 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
SQLWithSubqueryClause with = (SQLWithSubqueryClause) x.getParent();
|
||||
|
||||
if (Boolean.TRUE == with.getRecursive()) {
|
||||
|
||||
|
||||
if (aliasMap != null && alias != null) {
|
||||
aliasMap.put(alias, null);
|
||||
subQueryMap.put(alias, x.getSubQuery().getQuery());
|
||||
}
|
||||
|
||||
|
||||
x.getSubQuery().accept(this);
|
||||
} else {
|
||||
x.getSubQuery().accept(this);
|
||||
|
||||
|
||||
if (aliasMap != null && alias != null) {
|
||||
aliasMap.put(alias, null);
|
||||
subQueryMap.put(alias, x.getSubQuery().getQuery());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1069,7 +1070,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLInListExpr x) {
|
||||
if (x.isNot()) {
|
||||
handleCondition(x.getExpr(), "NOT IN", x.getTargetList());
|
||||
@@ -1111,7 +1112,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
accept(x.getTableElementList());
|
||||
|
||||
restoreCurrentTable(x);
|
||||
|
||||
|
||||
if (x.getInherits() != null) {
|
||||
x.getInherits().accept(this);
|
||||
}
|
||||
@@ -1317,7 +1318,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLRevokeStatement x) {
|
||||
if (x.getOn() != null) {
|
||||
@@ -1325,12 +1326,12 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropDatabaseStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableAddIndex x) {
|
||||
for (SQLSelectOrderByItem item : x.getItems()) {
|
||||
@@ -1338,30 +1339,44 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLCheck x) {
|
||||
x.getExpr().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLCreateTriggerStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLDropFunctionStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLDropTableSpaceStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLDropProcedureStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableRename x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLArrayExpr x) {
|
||||
accept(x.getValues());
|
||||
|
||||
SQLExpr exp = x.getExpr();
|
||||
if (exp instanceof SQLIdentifierExpr) {
|
||||
if (((SQLIdentifierExpr) exp).getName().equals("ARRAY")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
exp.accept(this);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
@@ -35,9 +37,9 @@ public class TestMySqlPing extends TestCase {
|
||||
// password = "ccbuauto";
|
||||
// SQL = "SELECT * FROM WP_ORDERS WHERE ID = ?";
|
||||
|
||||
jdbcUrl = "jdbc:mysql://a.b.c.d:3308/dragoon_v25_masterdb";
|
||||
user = "dragoon_admin";
|
||||
password = "dragoon_root";
|
||||
jdbcUrl = "jdbc:mysql://a.b.c.d/tddl5_00";
|
||||
user = "aa";
|
||||
password = "bb";
|
||||
|
||||
driverClass = "com.mysql.jdbc.Driver";
|
||||
}
|
||||
@@ -48,6 +50,10 @@ public class TestMySqlPing extends TestCase {
|
||||
Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
|
||||
|
||||
com.mysql.jdbc.Connection mysqlConn = (com.mysql.jdbc.Connection) conn;
|
||||
|
||||
MySqlValidConnectionChecker checker = new MySqlValidConnectionChecker();
|
||||
|
||||
checker.isValidConnection(mysqlConn, "select 'x'", 30);
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
ping_1000(mysqlConn);
|
||||
|
||||
@@ -17,8 +17,8 @@ public class Bug_for_qianbi extends TestCase {
|
||||
+ "where match(auctionTitle,\"男鞋\\n中石化&加油卡\\n中石化&充值卡\\n中石化&冲值卡\\n中石化&代冲\\n中石化&代充\\n中国石化&加油卡\\n中国石化&充值卡\\n中国石化&冲值卡\\n中国石化&代冲\\n中国石化&代充\",\"\\n\")";
|
||||
|
||||
String expected = "INSERT INTO TABLE lol"
|
||||
+ "\nSELECT detail(sellerid, id) AS count1, "
|
||||
+ "\n\tSUM(sellerid) AS sum1"
|
||||
+ "\nSELECT detail(sellerid, id) AS count1"
|
||||
+ "\n\t, SUM(sellerid) AS sum1"
|
||||
+ "\nFROM ctu_trade_paid_done:time('natural', '1d', '1h')"
|
||||
+ "\nWHERE match(auctionTitle, '男鞋\\n中石化&加油卡\\n中石化&充值卡\\n中石化&冲值卡\\n中石化&代冲\\n中石化&代充\\n中国石化&加油卡\\n中国石化&充值卡\\n中国石化&冲值卡\\n中国石化&代冲\\n中国石化&代充', '\\n');\n";
|
||||
|
||||
|
||||
@@ -55,8 +55,10 @@ public class PGDeleteTest8 extends PGTest {
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("parts")));
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("included_parts")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().size() == 4);
|
||||
Assert.assertTrue(visitor.getColumns().size() == 3);
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("parts", "sub_part")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("included_parts", "sub_part")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("parts", "part")));
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public class PGSelectTest15 extends PGTest {
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
Assert.assertEquals(6, visitor.getColumns().size());
|
||||
Assert.assertEquals(7, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ 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 com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class PGSelectTest16 extends PGTest {
|
||||
|
||||
@@ -57,6 +58,16 @@ public class PGSelectTest16 extends PGTest {
|
||||
|
||||
Assert.assertEquals(9, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "id")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "link")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "data")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("search_graph", "depth")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "path")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("search_graph", "link")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "cycle")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("sg", "depth")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("sg", "link")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,12 @@ public class PGSelectTest6 extends PGTest {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select fromUserId, toMemberId, rank, tag " + //
|
||||
"from cnres.function_get_re_cni_myali_acion_fct0(?) " + //
|
||||
"as (fromUserId character varying(128), toMemberId character varying(128), rank numeric, tag character varying(128))";
|
||||
"as ("//
|
||||
+ "fromUserId character varying(128), " //
|
||||
+ "toMemberId character varying(128), " //
|
||||
+ "rank numeric, " //
|
||||
+ "tag character varying(128)" //
|
||||
+ ")";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestMySql extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource = new DruidDataSource();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
String jdbcUrl = "jdbc:mysql://10.249.193.126/tddl5_00";
|
||||
String user = "tddl5";
|
||||
String password = "tddl5";
|
||||
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user