mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #947 from wenshao/master
bug fixed for mysql connection validation checker, and improved odps …
This commit is contained in:
+10
-4
@@ -35,12 +35,14 @@ import java.util.Properties;
|
||||
|
||||
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
public static final int DEFAULT_VALIDATION_QUERY_TIMEOUT = 1000;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
|
||||
private Class<?> clazz;
|
||||
private Method ping;
|
||||
private boolean usePingMethod = false;
|
||||
private Class<?> clazz;
|
||||
private Method ping;
|
||||
private boolean usePingMethod = false;
|
||||
|
||||
public MySqlValidConnectionChecker(){
|
||||
try {
|
||||
@@ -52,7 +54,7 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
|
||||
}
|
||||
|
||||
|
||||
configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
@@ -94,6 +96,10 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
}
|
||||
|
||||
if (clazz.isAssignableFrom(conn.getClass())) {
|
||||
if (validationQueryTimeout < 0) {
|
||||
validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
|
||||
}
|
||||
|
||||
try {
|
||||
ping.invoke(conn, true, validationQueryTimeout);
|
||||
return true;
|
||||
|
||||
@@ -38,6 +38,7 @@ import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleToMySqlOutputVisitor;
|
||||
@@ -370,6 +371,10 @@ public class SQLUtils {
|
||||
if (JdbcUtils.DB2.equals(dbType)) {
|
||||
return new DB2SchemaStatVisitor();
|
||||
}
|
||||
|
||||
if (JdbcUtils.ODPS.equals(dbType)) {
|
||||
return new OdpsSchemaStatVisitor();
|
||||
}
|
||||
|
||||
return new SchemaStatVisitor();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.expr;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLCharExpr extends SQLTextLiteralExpr implements SQLValuableExpr{
|
||||
@@ -47,4 +48,8 @@ public class SQLCharExpr extends SQLTextLiteralExpr implements SQLValuableExpr{
|
||||
public Object getValue() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,4 +97,7 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.expr;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
@@ -76,4 +77,10 @@ public class MySqlCharExpr extends SQLCharExpr implements MySqlExpr {
|
||||
visitor.visit(this);
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
output(buf);
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.odps.ast;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLHint;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
@@ -34,8 +35,8 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
|
||||
protected List<SQLHint> hints;
|
||||
|
||||
protected SQLExpr distributeBy;
|
||||
protected SQLSelectOrderByItem sortBy;
|
||||
protected SQLExpr distributeBy;
|
||||
protected List<SQLSelectOrderByItem> sortBy = new ArrayList<SQLSelectOrderByItem>(2);
|
||||
|
||||
public OdpsSelectQueryBlock(){
|
||||
|
||||
@@ -57,14 +58,10 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
this.distributeBy = distributeBy;
|
||||
}
|
||||
|
||||
public SQLSelectOrderByItem getSortBy() {
|
||||
public List<SQLSelectOrderByItem> getSortBy() {
|
||||
return sortBy;
|
||||
}
|
||||
|
||||
public void setSortBy(SQLSelectOrderByItem sortBy) {
|
||||
this.sortBy = sortBy;
|
||||
}
|
||||
|
||||
public SQLExpr getLimit() {
|
||||
return limit;
|
||||
}
|
||||
@@ -138,4 +135,7 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toOdpsString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,15 @@ package com.alibaba.druid.sql.dialect.odps.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem;
|
||||
import com.alibaba.druid.sql.parser.EOFParserException;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
@@ -131,4 +135,23 @@ public class OdpsExprParser extends SQLExprParser {
|
||||
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
public SQLExpr equalityRest(SQLExpr expr) {
|
||||
if (lexer.token() == Token.EQEQ) {
|
||||
SQLExpr rightExp;
|
||||
lexer.nextToken();
|
||||
try {
|
||||
rightExp = bitOr();
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
rightExp = equalityRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, getDbType());
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
return super.equalityRest(expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,19 +90,28 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
if (identifierEquals("SORT")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
SQLExpr expr = this.expr();
|
||||
|
||||
SQLSelectOrderByItem sortBy = new SQLSelectOrderByItem(expr);
|
||||
|
||||
if (lexer.token() == Token.ASC) {
|
||||
sortBy.setType(SQLOrderingSpecification.ASC);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DESC) {
|
||||
sortBy.setType(SQLOrderingSpecification.DESC);
|
||||
lexer.nextToken();
|
||||
for (;;) {
|
||||
SQLExpr expr = this.expr();
|
||||
|
||||
SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
|
||||
|
||||
if (lexer.token() == Token.ASC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.ASC);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DESC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.DESC);
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
queryBlock.getSortBy().add(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
queryBlock.setSortBy(sortBy);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -526,9 +526,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
print("DISTRIBUTE BY ");
|
||||
x.getDistributeBy().accept(this);
|
||||
|
||||
if (x.getSortBy() != null) {
|
||||
if (!x.getSortBy().isEmpty()) {
|
||||
print(" SORT BY ");
|
||||
x.getSortBy().accept(this);
|
||||
printAndAccept(x.getSortBy(), ", ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -438,8 +438,13 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
case IDENTIFIER: // 当负号后面为字段的情况
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.LPAREN || lexer.token() == Token.LBRACKET) {
|
||||
sqlExpr = primaryRest(sqlExpr);
|
||||
}
|
||||
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
|
||||
|
||||
break;
|
||||
case QUES:
|
||||
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, new SQLVariantRefExpr("?"));
|
||||
@@ -705,6 +710,14 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (lexer.token() == Token.OVER) {
|
||||
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr(methodName);
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getParameters());
|
||||
over(aggregateExpr);
|
||||
|
||||
return primaryRest(aggregateExpr);
|
||||
}
|
||||
|
||||
return primaryRest(methodInvokeExpr);
|
||||
}
|
||||
@@ -947,33 +960,37 @@ public class SQLExprParser extends SQLParser {
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (lexer.token() == Token.OVER) {
|
||||
lexer.nextToken();
|
||||
SQLOver over = new SQLOver();
|
||||
accept(Token.LPAREN);
|
||||
|
||||
if (lexer.token() == Token.PARTITION || identifierEquals("PARTITION")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
if (lexer.token() == (Token.LPAREN)) {
|
||||
lexer.nextToken();
|
||||
exprList(over.getPartitionBy(), over);
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
exprList(over.getPartitionBy(), over);
|
||||
}
|
||||
}
|
||||
|
||||
over.setOrderBy(parseOrderBy());
|
||||
|
||||
accept(Token.RPAREN);
|
||||
aggregateExpr.setOver(over);
|
||||
over(aggregateExpr);
|
||||
|
||||
}
|
||||
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
protected void over(SQLAggregateExpr aggregateExpr) {
|
||||
lexer.nextToken();
|
||||
SQLOver over = new SQLOver();
|
||||
accept(Token.LPAREN);
|
||||
|
||||
if (lexer.token() == Token.PARTITION || identifierEquals("PARTITION")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
if (lexer.token() == (Token.LPAREN)) {
|
||||
lexer.nextToken();
|
||||
exprList(over.getPartitionBy(), over);
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
exprList(over.getPartitionBy(), over);
|
||||
}
|
||||
}
|
||||
|
||||
over.setOrderBy(parseOrderBy());
|
||||
|
||||
accept(Token.RPAREN);
|
||||
aggregateExpr.setOver(over);
|
||||
}
|
||||
|
||||
protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr) {
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
@@ -736,6 +736,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
if (selectList != null) {
|
||||
for (SQLSelectItem item : selectList) {
|
||||
if (!item.getClass().equals(SQLSelectItem.class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String itemAlias = item.getAlias();
|
||||
SQLExpr itemExpr = item.getExpr();
|
||||
if (itemAlias == null) {
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ExportColumns extends UDF {
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
} catch (Exception ex) {
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("error sql : " + sql);
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
||||
@@ -51,7 +51,7 @@ public class ExportTables extends UDF {
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
} catch (Exception ex) {
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("error sql : " + sql);
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user