Merge pull request #217 from wenshao/master

Request for approval for bulk integration
This commit is contained in:
温高铁
2012-12-05 22:28:40 -08:00
233 changed files with 1948 additions and 721 deletions
@@ -279,7 +279,8 @@ public class ConfigFilter extends FilterAdapter {
}
}
private InputStream getFileAsStream(String filePath) throws FileNotFoundException {
@SuppressWarnings("resource")
private InputStream getFileAsStream(String filePath) throws FileNotFoundException {
InputStream inStream = null;
File file = new File(filePath);
if (file.exists()) {
@@ -56,6 +56,7 @@ import com.alibaba.druid.pool.vendor.NullExceptionSorter;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
import com.alibaba.druid.stat.JdbcDataSourceStat;
import com.alibaba.druid.stat.JdbcSqlStat;
import com.alibaba.druid.stat.JdbcStatManager;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
@@ -1161,6 +1162,11 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
protected boolean testConnectionInternal(Connection conn) {
String sqlFile = JdbcSqlStat.getContextSqlFile();
String sqlName = JdbcSqlStat.getContextSqlName();
JdbcSqlStat.setContextSqlFile(null);
JdbcSqlStat.setContextSqlName(null);
try {
if (validConnectionChecker != null) {
return validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout);
@@ -1194,6 +1200,9 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
} catch (Exception ex) {
// skip
return false;
} finally {
JdbcSqlStat.setContextSqlFile(sqlFile);
JdbcSqlStat.setContextSqlName(sqlName);
}
}
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2011 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.sql.ast.statement;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2011 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.sql.ast.statement;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -21,15 +21,24 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelect extends SQLObjectImpl {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
protected SQLSelectQuery query;
protected SQLOrderBy orderBy;
protected SQLWithSubqueryClause withSubQuery;
protected SQLSelectQuery query;
protected SQLOrderBy orderBy;
public SQLSelect(){
}
public SQLWithSubqueryClause getWithSubQuery() {
return withSubQuery;
}
public void setWithSubQuery(SQLWithSubqueryClause withSubQuery) {
this.withSubQuery = withSubQuery;
}
public SQLSelectQuery getQuery() {
return this.query;
}
@@ -1,102 +1,91 @@
/*
* Copyright 1999-2011 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.sql.dialect.oracle.ast.clause;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObjectImpl;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
public class SubqueryFactoringClause extends OracleSQLObjectImpl {
private static final long serialVersionUID = 1L;
private final List<Entry> entries = new ArrayList<Entry>();
public List<Entry> getEntries() {
return entries;
}
public static class Entry extends OracleSQLObjectImpl {
private static final long serialVersionUID = 1L;
private SQLIdentifierExpr name;
private final List<SQLName> columns = new ArrayList<SQLName>();
private SQLSelectQuery subQuery;
private SearchClause searchClause;
private CycleClause cycleClause;
public CycleClause getCycleClause() {
return cycleClause;
}
public void setCycleClause(CycleClause cycleClause) {
this.cycleClause = cycleClause;
}
public SearchClause getSearchClause() {
return searchClause;
}
public void setSearchClause(SearchClause searchClause) {
this.searchClause = searchClause;
}
public SQLIdentifierExpr getName() {
return name;
}
public void setName(SQLIdentifierExpr name) {
this.name = name;
}
public SQLSelectQuery getSubQuery() {
return subQuery;
}
public void setSubQuery(SQLSelectQuery subQuery) {
this.subQuery = subQuery;
}
public List<SQLName> getColumns() {
return columns;
}
@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, columns);
acceptChild(visitor, subQuery);
}
visitor.endVisit(this);
}
}
@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, entries);
}
visitor.endVisit(this);
}
}
/*
* Copyright 1999-2011 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.sql.ast.statement;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLWithSubqueryClause extends SQLObjectImpl {
private static final long serialVersionUID = 1L;
private Boolean recursive;
private final List<Entry> entries = new ArrayList<Entry>();
public List<Entry> getEntries() {
return entries;
}
public Boolean getRecursive() {
return recursive;
}
public void setRecursive(Boolean recursive) {
this.recursive = recursive;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, entries);
}
visitor.endVisit(this);
}
public static class Entry extends SQLObjectImpl {
private static final long serialVersionUID = 1L;
protected SQLIdentifierExpr name;
protected final List<SQLName> columns = new ArrayList<SQLName>();
protected SQLSelectQuery subQuery;
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, columns);
acceptChild(visitor, subQuery);
}
visitor.endVisit(this);
}
public SQLIdentifierExpr getName() {
return name;
}
public void setName(SQLIdentifierExpr name) {
this.name = name;
}
public SQLSelectQuery getSubQuery() {
return subQuery;
}
public void setSubQuery(SQLSelectQuery subQuery) {
this.subQuery = subQuery;
}
public List<SQLName> getColumns() {
return columns;
}
}
}
@@ -0,0 +1,49 @@
package com.alibaba.druid.sql.dialect.oracle.ast.clause;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause.Entry;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class OracleWithSubqueryEntry extends Entry implements OracleSQLObject {
private static final long serialVersionUID = 1L;
private SearchClause searchClause;
private CycleClause cycleClause;
public CycleClause getCycleClause() {
return cycleClause;
}
public void setCycleClause(CycleClause cycleClause) {
this.cycleClause = cycleClause;
}
public SearchClause getSearchClause() {
return searchClause;
}
public void setSearchClause(SearchClause searchClause) {
this.searchClause = searchClause;
}
@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, columns);
acceptChild(visitor, subQuery);
acceptChild(visitor, searchClause);
acceptChild(visitor, cycleClause);
}
visitor.endVisit(this);
}
@Override
protected void accept0(SQLASTVisitor visitor) {
this.accept0((OracleASTVisitor) visitor);
}
}
@@ -16,7 +16,6 @@
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -24,7 +23,6 @@ public class OracleSelect extends SQLSelect {
private static final long serialVersionUID = 1L;
private SubqueryFactoringClause factoring;
private OracleSelectForUpdate forUpdate;
private OracleSelectRestriction restriction;
@@ -32,14 +30,6 @@ public class OracleSelect extends SQLSelect {
}
public SubqueryFactoringClause getFactoring() {
return factoring;
}
public void setFactoring(SubqueryFactoringClause factoring) {
this.factoring = factoring;
}
public OracleSelectRestriction getRestriction() {
return this.restriction;
}
@@ -71,7 +61,7 @@ public class OracleSelect extends SQLSelect {
protected void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.factoring);
acceptChild(visitor, this.withSubQuery);
acceptChild(visitor, this.query);
acceptChild(visitor, this.restriction);
acceptChild(visitor, this.orderBy);
@@ -23,11 +23,13 @@ import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUnionOperator;
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause.AsOfFlashbackQueryClause;
@@ -46,10 +48,10 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ModelRulesCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.QueryPartitionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ReferenceModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ReturnRowsClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleOrderByItem;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect;
@@ -82,78 +84,7 @@ public class OracleSelectParser extends SQLSelectParser {
public OracleSelect select() {
OracleSelect select = new OracleSelect();
if (lexer.token() == Token.WITH) {
lexer.nextToken();
SubqueryFactoringClause subqueryFactoringClause = new SubqueryFactoringClause();
for (;;) {
SubqueryFactoringClause.Entry entry = new SubqueryFactoringClause.Entry();
entry.setName((SQLIdentifierExpr) this.exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
exprParser.names(entry.getColumns());
accept(Token.RPAREN);
}
accept(Token.AS);
accept(Token.LPAREN);
entry.setSubQuery(query());
accept(Token.RPAREN);
if (identifierEquals("SEARCH")) {
lexer.nextToken();
SearchClause searchClause = new SearchClause();
if (lexer.token() != Token.IDENTIFIER) {
throw new SQLParseException("syntax erorr : " + lexer.token());
}
searchClause.setType(SearchClause.Type.valueOf(lexer.stringVal()));
lexer.nextToken();
acceptIdentifier("FIRST");
accept(Token.BY);
searchClause.getItems().add((OracleOrderByItem) exprParser.parseSelectOrderByItem());
while (lexer.token() == (Token.COMMA)) {
lexer.nextToken();
searchClause.getItems().add((OracleOrderByItem) exprParser.parseSelectOrderByItem());
}
accept(Token.SET);
searchClause.setOrderingColumn((SQLIdentifierExpr) exprParser.name());
entry.setSearchClause(searchClause);
}
if (identifierEquals("CYCLE")) {
lexer.nextToken();
CycleClause cycleClause = new CycleClause();
exprParser.exprList(cycleClause.getAliases());
accept(Token.SET);
cycleClause.setMark(exprParser.expr());
acceptIdentifier("TO");
cycleClause.setValue(exprParser.expr());
accept(Token.DEFAULT);
cycleClause.setDefaultValue(exprParser.expr());
entry.setCycleClause(cycleClause);
}
subqueryFactoringClause.getEntries().add(entry);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
select.setFactoring(subqueryFactoringClause);
}
withSubquery(select);
select.setQuery(query());
select.setOrderBy(this.parseOrderBy());
@@ -226,6 +157,81 @@ public class OracleSelectParser extends SQLSelectParser {
return select;
}
protected void withSubquery(SQLSelect select) {
if (lexer.token() == Token.WITH) {
lexer.nextToken();
SQLWithSubqueryClause subqueryFactoringClause = new SQLWithSubqueryClause();
for (;;) {
OracleWithSubqueryEntry entry = new OracleWithSubqueryEntry();
entry.setName((SQLIdentifierExpr) this.exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
exprParser.names(entry.getColumns());
accept(Token.RPAREN);
}
accept(Token.AS);
accept(Token.LPAREN);
entry.setSubQuery(query());
accept(Token.RPAREN);
if (identifierEquals("SEARCH")) {
lexer.nextToken();
SearchClause searchClause = new SearchClause();
if (lexer.token() != Token.IDENTIFIER) {
throw new SQLParseException("syntax erorr : " + lexer.token());
}
searchClause.setType(SearchClause.Type.valueOf(lexer.stringVal()));
lexer.nextToken();
acceptIdentifier("FIRST");
accept(Token.BY);
searchClause.getItems().add((OracleOrderByItem) exprParser.parseSelectOrderByItem());
while (lexer.token() == (Token.COMMA)) {
lexer.nextToken();
searchClause.getItems().add((OracleOrderByItem) exprParser.parseSelectOrderByItem());
}
accept(Token.SET);
searchClause.setOrderingColumn((SQLIdentifierExpr) exprParser.name());
entry.setSearchClause(searchClause);
}
if (identifierEquals("CYCLE")) {
lexer.nextToken();
CycleClause cycleClause = new CycleClause();
exprParser.exprList(cycleClause.getAliases());
accept(Token.SET);
cycleClause.setMark(exprParser.expr());
acceptIdentifier("TO");
cycleClause.setValue(exprParser.expr());
accept(Token.DEFAULT);
cycleClause.setDefaultValue(exprParser.expr());
entry.setCycleClause(cycleClause);
}
subqueryFactoringClause.getEntries().add(entry);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
select.setWithSubQuery(subqueryFactoringClause);
}
}
public SQLSelectQuery query() {
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
@@ -644,7 +650,7 @@ public class OracleSelectParser extends SQLSelectParser {
if (identifierEquals("NOCYCLE")) {
hierachical.setNoCycle(true);
lexer.nextToken();
if (lexer.token() == Token.PRIOR) {
lexer.nextToken();
hierachical.setPrior(true);
@@ -679,7 +685,7 @@ public class OracleSelectParser extends SQLSelectParser {
if (identifierEquals("NOCYCLE")) {
hierachical.setNoCycle(true);
lexer.nextToken();
if (lexer.token() == Token.PRIOR) {
lexer.nextToken();
hierachical.setPrior(true);
@@ -29,10 +29,10 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.OraclePartitionByRangeCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleRangeValuesClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing;
@@ -266,13 +266,9 @@ public interface OracleASTVisitor extends SQLASTVisitor {
void endVisit(GroupingSetExpr x);
boolean visit(SubqueryFactoringClause x);
boolean visit(OracleWithSubqueryEntry x);
void endVisit(SubqueryFactoringClause x);
boolean visit(SubqueryFactoringClause.Entry x);
void endVisit(SubqueryFactoringClause.Entry x);
void endVisit(OracleWithSubqueryEntry x);
boolean visit(SearchClause x);
@@ -37,11 +37,10 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.OraclePartitionByRangeCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleRangeValuesClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause.Entry;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing;
@@ -539,24 +538,13 @@ public class OracleASTVisitorAdapter extends SQLASTVisitorAdapter implements Ora
}
@Override
public boolean visit(SubqueryFactoringClause x) {
public boolean visit(OracleWithSubqueryEntry x) {
return true;
}
@Override
public void endVisit(SubqueryFactoringClause x) {
}
@Override
public boolean visit(Entry x) {
return true;
}
@Override
public void endVisit(Entry x) {
public void endVisit(OracleWithSubqueryEntry x) {
}
@@ -62,10 +62,10 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.OraclePartitionByRangeCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleRangeValuesClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing;
@@ -192,7 +192,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
return;
}
if(isPrettyFormat()) {
if (isPrettyFormat()) {
if (x.getParent() != null) {
print(";");
} else {
@@ -213,12 +213,12 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
public boolean visit(OracleAggregateExpr expr) {
print(expr.getMethodName());
print("(");
if (expr.getOption() != null) {
print(expr.getOption().toString());
print(' ');
}
printAndAccept(expr.getArguments(), ", ");
print(")");
@@ -394,8 +394,8 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
public boolean visit(OracleSelect x) {
if (x.getFactoring() != null) {
x.getFactoring().accept(this);
if (x.getWithSubQuery() != null) {
x.getWithSubQuery().accept(this);
println();
}
@@ -1122,7 +1122,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
@Override
public boolean visit(SubqueryFactoringClause.Entry x) {
public boolean visit(OracleWithSubqueryEntry x) {
x.getName().accept(this);
if (x.getColumns().size() > 0) {
@@ -1154,22 +1154,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
@Override
public void endVisit(SubqueryFactoringClause.Entry x) {
}
@Override
public boolean visit(SubqueryFactoringClause x) {
print("WITH");
incrementIndent();
println();
printlnAndAccept(x.getEntries(), ", ");
decrementIndent();
return false;
}
@Override
public void endVisit(SubqueryFactoringClause x) {
public void endVisit(OracleWithSubqueryEntry x) {
}
@@ -58,11 +58,10 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.OraclePartitionByRangeCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleRangeValuesClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.SubqueryFactoringClause.Entry;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing;
@@ -281,7 +280,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
x.getOrderBy().setParent(x);
}
accept(x.getFactoring());
accept(x.getWithSubQuery());
accept(x.getQuery());
setCurrentTable(x, (String) x.getQuery().getAttribute("table"));
@@ -810,18 +809,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
}
@Override
public boolean visit(SubqueryFactoringClause x) {
return true;
}
@Override
public void endVisit(SubqueryFactoringClause x) {
}
@Override
public boolean visit(Entry x) {
public boolean visit(OracleWithSubqueryEntry x) {
Map<String, String> aliasMap = getAliasMap();
if (aliasMap != null) {
String alias = null;
@@ -839,7 +827,7 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
}
@Override
public void endVisit(Entry x) {
public void endVisit(OracleWithSubqueryEntry x) {
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
@@ -29,8 +30,10 @@ import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUnionOperator;
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
public class SQLSelectParser extends SQLParser {
protected SQLExprParser exprParser;
public SQLSelectParser(String sql){
@@ -40,7 +43,7 @@ public class SQLSelectParser extends SQLParser {
public SQLSelectParser(Lexer lexer){
super(lexer);
}
public SQLSelectParser(SQLExprParser exprParser){
super(exprParser.getLexer());
this.exprParser = exprParser;
@@ -49,6 +52,8 @@ public class SQLSelectParser extends SQLParser {
public SQLSelect select() {
SQLSelect select = new SQLSelect();
withSubquery(select);
select.setQuery(query());
select.setOrderBy(parseOrderBy());
@@ -159,6 +164,46 @@ public class SQLSelectParser extends SQLParser {
return queryRest(queryBlock);
}
protected void withSubquery(SQLSelect select) {
if (lexer.token() == Token.WITH) {
lexer.nextToken();
SQLWithSubqueryClause withQueryClause = new SQLWithSubqueryClause();
if (lexer.token == Token.RECURSIVE || identifierEquals("RECURSIVE")) {
lexer.nextToken();
withQueryClause.setRecursive(true);
}
for (;;) {
SQLWithSubqueryClause.Entry entry = new SQLWithSubqueryClause.Entry();
entry.setName((SQLIdentifierExpr) this.exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
exprParser.names(entry.getColumns());
accept(Token.RPAREN);
}
accept(Token.AS);
accept(Token.LPAREN);
entry.setSubQuery(query());
accept(Token.RPAREN);
withQueryClause.getEntries().add(entry);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
select.setWithSubQuery(withQueryClause);
}
}
protected void parseWhere(SQLSelectQueryBlock queryBlock) {
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
@@ -221,7 +266,7 @@ public class SQLSelectParser extends SQLParser {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLTableSource tableSource;
if (lexer.token() == Token.SELECT) {
if (lexer.token() == Token.SELECT || lexer.token() == Token.WITH) {
SQLSelect select = select();
accept(Token.RPAREN);
queryRest(select.getQuery());
@@ -98,6 +98,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
@@ -550,6 +551,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
public boolean visit(SQLSelect x) {
x.getQuery().setParent(x);
if (x.getWithSubQuery() != null) {
x.getWithSubQuery().accept(this);
println();
}
x.getQuery().accept(this);
if (x.getOrderBy() != null) {
@@ -1193,4 +1200,40 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
print(" UNIQUE INDEX");
return false;
}
@Override
public boolean visit(SQLWithSubqueryClause x) {
print("WITH");
if (x.getRecursive() == Boolean.TRUE) {
print(" RECURSIVE");
}
incrementIndent();
println();
printlnAndAccept(x.getEntries(), ", ");
decrementIndent();
return false;
}
@Override
public boolean visit(SQLWithSubqueryClause.Entry x) {
x.getName().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
printAndAccept(x.getColumns(), ", ");
print(")");
}
println();
print("AS");
println();
print("(");
incrementIndent();
println();
x.getSubQuery().accept(this);
decrementIndent();
println();
print(")");
return false;
}
}
@@ -91,6 +91,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
public interface SQLASTVisitor {
@@ -405,4 +406,12 @@ public interface SQLASTVisitor {
void endVisit(SQLColumnUniqueIndex x);
boolean visit(SQLColumnUniqueIndex x);
void endVisit(SQLWithSubqueryClause x);
boolean visit(SQLWithSubqueryClause x);
void endVisit(SQLWithSubqueryClause.Entry x);
boolean visit(SQLWithSubqueryClause.Entry x);
}
@@ -92,6 +92,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
public class SQLASTVisitorAdapter implements SQLASTVisitor {
@@ -735,4 +736,22 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
public boolean visit(SQLColumnUniqueIndex x) {
return true;
}
@Override
public void endVisit(SQLWithSubqueryClause x) {
}
@Override
public boolean visit(SQLWithSubqueryClause x) {
return true;
}
@Override
public void endVisit(SQLWithSubqueryClause.Entry x) {
}
@Override
public boolean visit(SQLWithSubqueryClause.Entry x) {
return true;
}
}
@@ -251,7 +251,6 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
this.dataSource = dataSource;
}
@Deprecated
public final static String getContextSqlName() {
JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
if (context == null) {
@@ -260,7 +259,6 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
return context.getName();
}
@Deprecated
public final static void setContextSqlName(String val) {
JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
if (context == null) {
@@ -271,7 +269,6 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
context.setName(val);
}
@Deprecated
public final static String getContextSqlFile() {
JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
if (context == null) {
@@ -280,7 +277,6 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
return context.getFile();
}
@Deprecated
public final static void setContextSqlFile(String val) {
JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
if (context == null) {
@@ -36,7 +36,6 @@ import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
@SuppressWarnings({ "restriction" })
public class DruidStat {
private static final String LOCAL_CONNECTOR_ADDRESS_PROP = "com.sun.management.jmxremote.localConnectorAddress";
@@ -0,0 +1,144 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl;
import com.ibatis.sqlmap.engine.scope.SessionScope;
public class IbatisUtils {
private static Log LOG = LogFactory.getLog(IbatisUtils.class);
private static boolean VERSION_2_3_4 = false;
private static Method methodGetId = null;
private static Method methodGetResource = null;
static {
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("com.ibatis.sqlmap.engine.mapping.result.AutoResultMap");
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.equals("setResultObjectValues")) { // ibatis 2.3.4 add method 'setResultObjectValues'
VERSION_2_3_4 = true;
break;
}
}
} catch (Throwable e) {
// skip
}
}
public static boolean isVersion2_3_4() {
return VERSION_2_3_4;
}
public static SqlMapExecutor setClientImpl(SqlMapExecutor session, SqlMapClientImplWrapper clientImplWrapper) {
if (session == null || clientImplWrapper == null) {
return session;
}
if (session.getClass() == SqlMapSessionImpl.class) {
SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) session;
set(sessionImpl, clientImplWrapper);
}
return session;
}
/**
* 通过反射的方式得到id,能够兼容2.3.0和2.3.4
*
* @return
*/
protected static String getId(Object statement) {
try {
if (methodGetId == null) {
Class<?> clazz = statement.getClass();
methodGetId = clazz.getMethod("getId");
}
Object returnValue = methodGetId.invoke(statement);
if (returnValue == null) {
return null;
}
return returnValue.toString();
} catch (Exception ex) {
LOG.error("createIdError", ex);
return null;
}
}
/**
* 通过反射的方式得到resource,能够兼容2.3.0和2.3.4
*
* @return
*/
protected static String getResource(Object statement) {
try {
if (methodGetResource == null) {
methodGetResource = statement.getClass().getMethod("getResource");
}
return (String) methodGetResource.invoke(statement);
} catch (Exception ex) {
return null;
}
}
private static Field sessionField;
public static void set(SqlMapSessionImpl session, SqlMapClientImpl client) {
if (sessionField == null) {
for (Field field : SqlMapSessionImpl.class.getDeclaredFields()) {
if (field.getName().equals("session") || field.getName().equals("sessionScope")) {
sessionField = field;
sessionField.setAccessible(true);
break;
}
}
}
if (sessionField != null) {
SessionScope sessionScope;
try {
sessionScope = (SessionScope) sessionField.get(session);
if (sessionScope != null) {
if (sessionScope.getSqlMapClient() != null && sessionScope.getSqlMapClient().getClass() == SqlMapClientImpl.class) {
sessionScope.setSqlMapClient(client);
}
if (sessionScope.getSqlMapExecutor() != null && sessionScope.getSqlMapExecutor().getClass() == SqlMapClientImpl.class) {
sessionScope.setSqlMapExecutor(client);
}
if (sessionScope.getSqlMapTxMgr() != null && sessionScope.getSqlMapTxMgr().getClass() == SqlMapClientImpl.class) {
sessionScope.setSqlMapTxMgr(client);
}
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
}
}
@@ -0,0 +1,143 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
/**
* 类BeanTypeAutoProxyCreator.java的实现描述:使用配置类型代替Springframework中配置名称的实现
*
* @author hualiang.lihl 2011-12-31 上午10:48:20
*/
@SuppressWarnings("deprecation")
public class SpringIbatisBeanTypeAutoProxyCreator extends AbstractAutoProxyCreator implements InitializingBean, ApplicationContextAware, SpringIbatisBeanTypeAutoProxyCreatorMBean {
private final static Log LOG = LogFactory.getLog(SpringIbatisBeanTypeAutoProxyCreator.class);
private static final long serialVersionUID = -9094985530794052264L;
private Class<?> targetBeanType = SqlMapClient.class;
private ApplicationContext context;
private List<String> beanNames = new ArrayList<String>();
private final List<String> proxyBeanNames = new ArrayList<String>();
/**
* @param targetClass the targetClass to set
*/
public void setTargetBeanType(Class<?> targetClass) {
this.targetBeanType = targetClass;
}
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
/**
* Identify as bean to proxy if the bean name is in the configured list of names.
*/
@SuppressWarnings("rawtypes")
protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) {
for (Iterator<String> it = this.beanNames.iterator(); it.hasNext();) {
String mappedName = (String) it.next();
if (FactoryBean.class.isAssignableFrom(beanClass)) {
if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
continue;
}
mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
}
if (isMatch(beanName, mappedName)) {
return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
}
}
return DO_NOT_PROXY;
}
@SuppressWarnings({ "rawtypes" })
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors,
TargetSource targetSource) {
try {
Object target = targetSource.getTarget();
if (target instanceof SqlMapClientWrapper) {
proxyBeanNames.add(beanName);
return target;
}
if (target instanceof SqlMapClient) {
proxyBeanNames.add(beanName);
return new SqlMapClientWrapper((ExtendedSqlMapClient) target);
}
return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
} catch (Throwable ex) {
LOG.error(ex.getMessage(), ex);
return super.createProxy(beanClass, beanName, specificInterceptors, targetSource);
}
}
/**
* Return if the given bean name matches the mapped name.
* <p>
* The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality. Can be
* overridden in subclasses.
*
* @param beanName the bean name to check
* @param mappedName the name in the configured list of names
* @return if the names match
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
*/
protected boolean isMatch(String beanName, String mappedName) {
return PatternMatchUtils.simpleMatch(mappedName, beanName);
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(targetBeanType, "targetType cannot be null");
String[] beanNames = context.getBeanNamesForType(targetBeanType);
for (String name : beanNames) {
this.beanNames.add(name);
}
}
public List<String> getBeanNames() {
return beanNames;
}
public List<String> getProxyBeanNames() {
return proxyBeanNames;
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.util.List;
public interface SpringIbatisBeanTypeAutoProxyCreatorMBean {
List<String> getProxyBeanNames();
}
@@ -0,0 +1,274 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.alibaba.druid.stat.JdbcSqlStat;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ibatis.sqlmap.client.SqlMapSession;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.execution.BatchException;
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl;
@SuppressWarnings("deprecation")
public class SqlMapClientImplWrapper extends SqlMapClientImpl {
private SqlMapClientImpl raw;
private static Method getLocalSqlMapSessionMethod = null;
private ConcurrentMap<String, IbatisStatementInfo> statementInfoMap = new ConcurrentHashMap<String, IbatisStatementInfo>();
public SqlMapClientImplWrapper(SqlMapClientImpl raw) {
super(raw.getDelegate());
this.raw = raw;
}
public void setLocal(String id, SqlMapExecutor executor) {
IbatisStatementInfo stmtInfo = statementInfoMap.get(id);
if (stmtInfo != null) {
JdbcSqlStat.setContextSqlName(stmtInfo.getId());
JdbcSqlStat.setContextSqlFile(stmtInfo.getResource());
return;
}
Object statement = null;
if (executor instanceof SqlMapSessionImpl) {
statement = ((SqlMapSessionImpl) executor).getMappedStatement(id);
}
if (executor instanceof SqlMapClientImpl) {
statement = ((SqlMapClientImpl) executor).getMappedStatement(id);
}
if (statement == null) {
return;
}
String stmtId = IbatisUtils.getId(statement);
String stmtResource = IbatisUtils.getResource(statement);
stmtInfo = new IbatisStatementInfo(stmtId, stmtResource);
statementInfoMap.putIfAbsent(id, stmtInfo);
JdbcSqlStat.setContextSqlName(stmtId);
JdbcSqlStat.setContextSqlFile(stmtResource);
}
protected SqlMapSessionWrapper getLocalSqlMapSessionWrapper() {
try {
if (getLocalSqlMapSessionMethod == null) {
getLocalSqlMapSessionMethod = raw.getClass().getDeclaredMethod(
"getLocalSqlMapSession");
getLocalSqlMapSessionMethod.setAccessible(true);
}
SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) getLocalSqlMapSessionMethod
.invoke(raw);
IbatisUtils.set(sessionImpl, this);
return new SqlMapSessionWrapper(raw, sessionImpl);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public SqlMapSession openSession(Connection conn) {
SqlMapSession session = raw.openSession(conn);
IbatisUtils.setClientImpl(session, this);
return new SqlMapSessionWrapper(raw, session);
}
public SqlMapSession getSession() {
SqlMapSession session = raw.getSession();
IbatisUtils.setClientImpl(session, this);
return new SqlMapSessionWrapper(raw, session);
}
// /////
public Object insert(String id, Object param) throws SQLException {
return getLocalSqlMapSessionWrapper().insert(id, param);
}
public Object insert(String id) throws SQLException {
return getLocalSqlMapSessionWrapper().insert(id);
}
public int update(String id, Object param) throws SQLException {
return getLocalSqlMapSessionWrapper().update(id, param);
}
public int update(String id) throws SQLException {
return getLocalSqlMapSessionWrapper().update(id);
}
public int delete(String id, Object param) throws SQLException {
return getLocalSqlMapSessionWrapper().delete(id, param);
}
public int delete(String id) throws SQLException {
return getLocalSqlMapSessionWrapper().delete(id);
}
public Object queryForObject(String id, Object paramObject)
throws SQLException {
return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject);
}
public Object queryForObject(String id) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForObject(id);
}
public Object queryForObject(String id, Object paramObject,
Object resultObject) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject,
resultObject);
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, Object paramObject) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForList(id, paramObject);
}
@SuppressWarnings("rawtypes")
public List queryForList(String id) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForList(id);
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, Object paramObject, int skip, int max)
throws SQLException {
return getLocalSqlMapSessionWrapper().queryForList(id, paramObject,
skip, max);
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, int skip, int max) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForList(id, skip, max);
}
/**
* @deprecated All paginated list features have been deprecated
*/
public PaginatedList queryForPaginatedList(String id, Object paramObject,
int pageSize) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForPaginatedList(id,
paramObject, pageSize);
}
/**
* @deprecated All paginated list features have been deprecated
*/
public PaginatedList queryForPaginatedList(String id, int pageSize)
throws SQLException {
return getLocalSqlMapSessionWrapper().queryForPaginatedList(id,
pageSize);
}
@SuppressWarnings("rawtypes")
public Map queryForMap(String id, Object paramObject, String keyProp)
throws SQLException {
return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject,
keyProp);
}
@SuppressWarnings("rawtypes")
public Map queryForMap(String id, Object paramObject, String keyProp,
String valueProp) throws SQLException {
return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject,
keyProp, valueProp);
}
public void queryWithRowHandler(String id, Object paramObject,
RowHandler rowHandler) throws SQLException {
getLocalSqlMapSessionWrapper().queryWithRowHandler(id, paramObject,
rowHandler);
}
public void queryWithRowHandler(String id, RowHandler rowHandler)
throws SQLException {
getLocalSqlMapSessionWrapper().queryWithRowHandler(id, rowHandler);
}
public void startTransaction() throws SQLException {
getLocalSqlMapSessionWrapper().startTransaction();
}
public void startTransaction(int transactionIsolation) throws SQLException {
getLocalSqlMapSessionWrapper().startTransaction(transactionIsolation);
}
public void commitTransaction() throws SQLException {
getLocalSqlMapSessionWrapper().commitTransaction();
}
public void endTransaction() throws SQLException {
try {
getLocalSqlMapSessionWrapper().endTransaction();
} finally {
getLocalSqlMapSessionWrapper().close();
}
}
public void startBatch() throws SQLException {
getLocalSqlMapSessionWrapper().startBatch();
}
public int executeBatch() throws SQLException {
return getLocalSqlMapSessionWrapper().executeBatch();
}
@SuppressWarnings("rawtypes")
public List executeBatchDetailed() throws SQLException, BatchException {
return getLocalSqlMapSessionWrapper().executeBatchDetailed();
}
public void setUserConnection(Connection connection) throws SQLException {
try {
getLocalSqlMapSessionWrapper().setUserConnection(connection);
} finally {
if (connection == null) {
getLocalSqlMapSessionWrapper().close();
}
}
}
public static class IbatisStatementInfo {
private final String id;
private final String resource;
public IbatisStatementInfo(String id, String resource) {
this.id = id;
this.resource = resource;
}
public String getId() {
return id;
}
public String getResource() {
return resource;
}
}
}
@@ -0,0 +1,127 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapSession;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
@SuppressWarnings("deprecation")
public class SqlMapClientWrapper extends SqlMapExecutorWrapper implements SqlMapClient, ExtendedSqlMapClient {
protected final ExtendedSqlMapClient client;
public SqlMapClientWrapper(ExtendedSqlMapClient client){
super(client, client);
this.client = client;
}
public ExtendedSqlMapClient getClient() {
return this.client;
}
public void startTransaction() throws SQLException {
client.startTransaction();
}
public void startTransaction(int transactionIsolation) throws SQLException {
client.startTransaction(transactionIsolation);
}
public void commitTransaction() throws SQLException {
client.commitTransaction();
}
public void endTransaction() throws SQLException {
client.endTransaction();
}
public void setUserConnection(Connection connnection) throws SQLException {
client.setUserConnection(connnection);
}
public Connection getUserConnection() throws SQLException {
return client.getUserConnection();
}
public Connection getCurrentConnection() throws SQLException {
return client.getCurrentConnection();
}
public DataSource getDataSource() {
return client.getDataSource();
}
public SqlMapSession openSession() {
SqlMapSession session = client.openSession();
IbatisUtils.setClientImpl(session, clientImplWrapper);
return new SqlMapSessionWrapper(client, session);
}
public SqlMapSession openSession(Connection conn) {
SqlMapSession session = client.openSession(conn);
IbatisUtils.setClientImpl(session, clientImplWrapper);
return new SqlMapSessionWrapper(client, session);
}
public SqlMapSession getSession() {
SqlMapSession session = client.getSession();
IbatisUtils.setClientImpl(session, clientImplWrapper);
return new SqlMapSessionWrapper(client, session);
}
public void flushDataCache() {
client.flushDataCache();
}
public void flushDataCache(String cacheId) {
client.flushDataCache(cacheId);
}
public MappedStatement getMappedStatement(String id) {
return client.getMappedStatement(id);
}
public boolean isLazyLoadingEnabled() {
return client.isLazyLoadingEnabled();
}
public boolean isEnhancementEnabled() {
return client.isEnhancementEnabled();
}
public SqlExecutor getSqlExecutor() {
return client.getSqlExecutor();
}
public SqlMapExecutorDelegate getDelegate() {
return client.getDelegate();
}
public ResultObjectFactory getResultObjectFactory() {
return client.getResultObjectFactory();
}
}
@@ -0,0 +1,210 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.execution.BatchException;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
@SuppressWarnings("deprecation")
public class SqlMapExecutorWrapper implements SqlMapExecutor {
private SqlMapExecutor executor;
protected final SqlMapClientImpl clientImpl;
protected final SqlMapClientImplWrapper clientImplWrapper;
public SqlMapExecutorWrapper(ExtendedSqlMapClient client, SqlMapExecutor executor){
this.executor = executor;
this.clientImpl = client.getClass() == SqlMapClientImpl.class ? (SqlMapClientImpl) client : null;
this.clientImplWrapper = clientImpl != null ? new SqlMapClientImplWrapper(clientImpl) : null;
}
public Object insert(String id, Object parameterObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
return executor.insert(id, parameterObject);
}
public Object insert(String id) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
return executor.insert(id);
}
public int update(String id, Object parameterObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
int effectedRowCount = executor.update(id, parameterObject);
return effectedRowCount;
}
public int update(String id) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
int effectedRowCount = executor.update(id);
return effectedRowCount;
}
public int delete(String id, Object parameterObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
int effectedRowCount = executor.delete(id, parameterObject);
return effectedRowCount;
}
public int delete(String id) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
int effectedRowCount = executor.delete(id);
return effectedRowCount;
}
public Object queryForObject(String id, Object parameterObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
Object object = executor.queryForObject(id, parameterObject);
return object;
}
public Object queryForObject(String id) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
Object object = executor.queryForObject(id);
return object;
}
public Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
Object object = executor.queryForObject(id, parameterObject, resultObject);
return object;
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, Object parameterObject) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
List list = executor.queryForList(id, parameterObject);
return list;
}
@SuppressWarnings("rawtypes")
public List queryForList(String id) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
List list = executor.queryForList(id);
return list;
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, Object parameterObject, int skip, int max) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
List list = executor.queryForList(id, parameterObject, skip, max);
return list;
}
@SuppressWarnings("rawtypes")
public List queryForList(String id, int skip, int max) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
List list = executor.queryForList(id, skip, max);
return list;
}
public void queryWithRowHandler(String id, Object parameterObject, RowHandler rowHandler) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
executor.queryWithRowHandler(id, parameterObject, rowHandler);
}
public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
executor.queryWithRowHandler(id, rowHandler);
}
public PaginatedList queryForPaginatedList(String id, Object parameterObject, int pageSize) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
PaginatedList list = executor.queryForPaginatedList(id, parameterObject, pageSize);
return list;
}
public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
PaginatedList list = executor.queryForPaginatedList(id, pageSize);
return list;
}
@SuppressWarnings("rawtypes")
public Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
Map map = executor.queryForMap(id, parameterObject, keyProp);
return map;
}
@SuppressWarnings("rawtypes")
public Map queryForMap(String id, Object parameterObject, String keyProp, String valueProp) throws SQLException {
clientImplWrapper.setLocal(id, executor);
IbatisUtils.setClientImpl(executor, clientImplWrapper);
Map map = executor.queryForMap(id, parameterObject, keyProp, valueProp);
return map;
}
public void startBatch() throws SQLException {
IbatisUtils.setClientImpl(executor, clientImplWrapper);
executor.startBatch();
}
public int executeBatch() throws SQLException {
IbatisUtils.setClientImpl(executor, clientImplWrapper);
return executor.executeBatch();
}
@SuppressWarnings("rawtypes")
public List executeBatchDetailed() throws SQLException, BatchException {
IbatisUtils.setClientImpl(executor, clientImplWrapper);
return executor.executeBatchDetailed();
}
}
@@ -0,0 +1,71 @@
/*
* Copyright 1999-2011 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.support.ibatis;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.ibatis.sqlmap.client.SqlMapSession;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
@SuppressWarnings("deprecation")
public class SqlMapSessionWrapper extends SqlMapExecutorWrapper implements SqlMapSession {
private SqlMapSession session;
public SqlMapSessionWrapper(ExtendedSqlMapClient client, SqlMapSession session){
super(client, session);
this.session = session;
}
public void startTransaction() throws SQLException {
session.startTransaction();
}
public void startTransaction(int transactionIsolation) throws SQLException {
session.startTransaction(transactionIsolation);
}
public void commitTransaction() throws SQLException {
session.commitTransaction();
}
public void endTransaction() throws SQLException {
session.endTransaction();
}
public void setUserConnection(Connection connnection) throws SQLException {
session.setUserConnection(connnection);
}
@Deprecated
public Connection getUserConnection() throws SQLException {
return session.getUserConnection();
}
public Connection getCurrentConnection() throws SQLException {
return session.getCurrentConnection();
}
public DataSource getDataSource() {
return session.getDataSource();
}
public void close() {
session.close();
}
}
@@ -21,7 +21,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import com.alibaba.druid.sql.ast.SQLExpr;
@@ -245,16 +247,45 @@ public class WallVisitorUtils {
}
}
}
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
List<SQLExpr> groupList = new ArrayList<SQLExpr>();
SQLExpr left = x.getLeft();
for (;;) {
if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) {
SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left;
groupList.add(binaryLeft.getRight());
left = binaryLeft.getLeft();
} else {
groupList.add(left);
break;
}
}
groupList.add(x.getRight());
boolean allFalse = true;
for (int i = groupList.size() - 1; i >= 0; --i) {
Object result = getValue(groupList.get(i));
if (Boolean.TRUE == result) {
return true;
}
if (Boolean.FALSE != result) {
allFalse = false;
}
}
if (allFalse) {
return false;
}
return null;
}
Object leftResult = getValue(x.getLeft());
Object rightResult = getValue(x.getRight());
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
if (Boolean.TRUE == leftResult || Boolean.TRUE == rightResult) {
return true;
}
}
if (x.getOperator() == SQLBinaryOperator.BooleanAnd) {
if (Boolean.FALSE == leftResult || Boolean.FALSE == rightResult) {
return false;