修正参数不当的问题

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@213 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-05-30 03:31:50 +00:00
parent 642f3b90b1
commit 846b9bb816
9 changed files with 222 additions and 80 deletions
@@ -2,7 +2,10 @@ package com.alibaba.druid.jconsole;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -10,21 +13,27 @@ import java.util.Set;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.JdbcStatManager;
import com.sun.tools.jconsole.JConsoleContext;
public class DruidPanel extends JPanel {
protected JSplitPane mainSplit;
protected JTree tree;
protected JPanel sheet;
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
protected JSplitPane mainSplit;
protected JTree tree;
protected JPanel sheet;
protected DefaultMutableTreeNode rootNode;
protected DefaultMutableTreeNode dataSourcesNode;
public DruidPanel(){
setLayout(new BorderLayout());
@@ -33,7 +42,12 @@ public class DruidPanel extends JPanel {
mainSplit.setDividerLocation(160);
mainSplit.setBorder(BorderFactory.createEmptyBorder());
tree = new JTree();
rootNode = new DefaultMutableTreeNode("root", true);
dataSourcesNode = new DefaultMutableTreeNode("DataSources", true);
rootNode.add(dataSourcesNode);
tree = new JTree(rootNode);
tree.setRootVisible(false);
JScrollPane theScrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel treePanel = new JPanel(new BorderLayout());
@@ -46,22 +60,51 @@ public class DruidPanel extends JPanel {
add(mainSplit);
this.setBackground(Color.BLUE);
init();
}
public void init() {
try {
ManagementFactory.getPlatformMBeanServer().registerMBean(JdbcStatManager.getInstance(), new ObjectName("com.alibaba.druid:type=JdbcStatManager"));
DruidDataSource dataSource = new DruidDataSource();
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource, new ObjectName("com.alibaba.druid:type=DataSource"));
dataSource.setUrl("jdbc:mock:");
dataSource.setFilters("stat,trace");
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
while (rs.next()) {
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
}
}
protected Object doInBackground(JConsoleContext context) throws Exception {
MBeanServerConnection conn = context.getMBeanServerConnection();
List<ObjectInstance> stats = new ArrayList<ObjectInstance>();
List<ObjectInstance> dataSourceInstances = new ArrayList<ObjectInstance>();
Set<ObjectInstance> instances = conn.queryMBeans(null, null);
for (ObjectInstance instance : instances) {
MBeanInfo info = conn.getMBeanInfo(instance.getObjectName());
if ("com.alibaba.druid.stat.JdbcStatManager".equals(info.getClassName())) {
stats.add(instance);
break;
continue;
}
if ("com.alibaba.druid.pool.DruidDataSource".equals(info.getClassName())) {
dataSourceInstances.add(instance);
continue;
}
}
@@ -69,7 +112,24 @@ public class DruidPanel extends JPanel {
}
this.setPreferredSize(new Dimension(400, 500));
for (ObjectInstance dataSourceInstance : dataSourceInstances) {
String name = (String) conn.getAttribute(dataSourceInstance.getObjectName(), "Name");
DefaultMutableTreeNode dataSource = new DefaultMutableTreeNode(name, true);
DefaultMutableTreeNode connections = new DefaultMutableTreeNode("Connections", true);
{
}
dataSource.add(connections);
DefaultMutableTreeNode sqlListNode = new DefaultMutableTreeNode("SQL", true);
{
}
dataSource.add(sqlListNode);
dataSourcesNode.add(dataSource);
}
return null;
}
@@ -385,6 +385,9 @@ public abstract class DruidAbstractDataSource implements DataSource, DataSourceP
}
public String getName() {
if (name == null) {
return "DataSource-" + System.identityHashCode(this);
}
return name;
}
@@ -21,6 +21,8 @@ import java.util.List;
* @author wenshao<szujobs@hotmail.com>
*/
public interface DruidDataSourceMBean {
String getName();
int getInitialSize();
@@ -9,11 +9,14 @@ import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
private static final String ATTR_PARAMS_SKIP = "_params.skip_";
public MySqlParameterizedOutputVisitor(Appendable appender){
super(appender);
}
@@ -26,30 +29,27 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
x.getExpr().accept(this);
if (x.isNot()) {
print(" NOT IN (##)");
print(" NOT IN (?)");
} else {
print(" IN (##)");
print(" IN (?)");
}
return false;
}
public boolean visit(SQLBinaryOpExpr x) {
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
print(x.getLeft().toString());
print(' ');
print(x.getOperator().name);
print(' ');
print(x.getRight().toString());
return false;
}
x = merge(x);
return super.visit(x);
}
public SQLBinaryOpExpr merge(SQLBinaryOpExpr x) {
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
x.getLeft().getAttributes().put(ATTR_PARAMS_SKIP, true);
x.getRight().getAttributes().put(ATTR_PARAMS_SKIP, true);
return x;
}
if (x.getRight() instanceof SQLLiteralExpr) {
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), new SQLVariantRefExpr("?"));
}
@@ -63,7 +63,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
}
if (x.getLeft() instanceof SQLBinaryOpExpr) {
x = new SQLBinaryOpExpr((SQLBinaryOpExpr) x.getLeft(), x.getOperator(), x.getRight());
x = new SQLBinaryOpExpr(merge((SQLBinaryOpExpr) x.getLeft()), x.getOperator(), x.getRight());
}
// ID = ? OR ID = ? => ID = ?
@@ -83,6 +83,17 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
}
}
}
if ((left.getLeft() instanceof SQLPropertyExpr) && (right.getLeft() instanceof SQLPropertyExpr)) {
String leftColumn = left.getLeft().toString();
String rightColumn = right.getLeft().toString();
if (leftColumn.equals(rightColumn)) {
if ((left.getRight() instanceof SQLVariantRefExpr) && (right.getRight() instanceof SQLVariantRefExpr)) {
return left; // merge
}
}
}
}
}
}
@@ -91,21 +102,37 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
}
public boolean visit(SQLIntegerExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLNumberExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLCharExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLNCharExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
@@ -9,10 +9,12 @@ import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
public class OracleParameterizedOutputVisitor extends OracleOutputVisitor {
private static final String ATTR_PARAMS_SKIP = "_params.skip_";
public OracleParameterizedOutputVisitor(Appendable appender){
super(appender);
}
@@ -25,30 +27,27 @@ public class OracleParameterizedOutputVisitor extends OracleOutputVisitor {
x.getExpr().accept(this);
if (x.isNot()) {
print(" NOT IN (##)");
print(" NOT IN (?)");
} else {
print(" IN (##)");
print(" IN (?)");
}
return false;
}
public boolean visit(SQLBinaryOpExpr x) {
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
print(x.getLeft().toString());
print(' ');
print(x.getOperator().name);
print(' ');
print(x.getRight().toString());
return false;
}
x = merge(x);
return super.visit(x);
}
public SQLBinaryOpExpr merge(SQLBinaryOpExpr x) {
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
x.getLeft().getAttributes().put(ATTR_PARAMS_SKIP, true);
x.getRight().getAttributes().put(ATTR_PARAMS_SKIP, true);
return x;
}
if (x.getRight() instanceof SQLLiteralExpr) {
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), new SQLVariantRefExpr("?"));
}
@@ -82,6 +81,17 @@ public class OracleParameterizedOutputVisitor extends OracleOutputVisitor {
}
}
}
if ((left.getLeft() instanceof SQLPropertyExpr) && (right.getLeft() instanceof SQLPropertyExpr)) {
String leftColumn = left.getLeft().toString();
String rightColumn = right.getLeft().toString();
if (leftColumn.equals(rightColumn)) {
if ((left.getRight() instanceof SQLVariantRefExpr) && (right.getRight() instanceof SQLVariantRefExpr)) {
return left; // merge
}
}
}
}
}
}
@@ -90,21 +100,37 @@ public class OracleParameterizedOutputVisitor extends OracleOutputVisitor {
}
public boolean visit(SQLIntegerExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLNumberExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLCharExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}
public boolean visit(SQLNCharExpr x) {
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
return super.visit(x);
}
print('?');
return false;
}