mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
jconsole
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@245 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -1,12 +1,83 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.visitor.OracleOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
|
||||
public class SQLDetailDialog extends JDialog {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JTextArea textArea;
|
||||
|
||||
public SQLDetailDialog(Object[] row) {
|
||||
public SQLDetailDialog(CompositeData rowData) {
|
||||
textArea = new JTextArea();
|
||||
JScrollPane textAreaScrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
this.getContentPane().add(textAreaScrollPane);
|
||||
|
||||
String sql = (String) rowData.get("SQL");
|
||||
|
||||
sql = format(sql);
|
||||
|
||||
textArea.setText(sql);
|
||||
}
|
||||
|
||||
public static String format(String sql) {
|
||||
if (sql == null || sql.length() == 0) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
try {
|
||||
return mergeMySql(sql);
|
||||
} catch (Exception ex) {
|
||||
// skip
|
||||
}
|
||||
|
||||
try {
|
||||
return mergeOracle(sql);
|
||||
} catch (Exception ex) {
|
||||
// skip
|
||||
}
|
||||
|
||||
return sql; // 返回原来的SQL
|
||||
}
|
||||
|
||||
public static String mergeMySql(String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.isTrue(1 == statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String mergeOracle(String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.isTrue(1 == statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
OracleOutputVisitor visitor = new OracleOutputVisitor(out);
|
||||
statemen.accept(visitor);
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alibaba.druid.jconsole;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.text.DateFormat;
|
||||
@@ -59,7 +61,7 @@ public class SQLPanel extends JPanel {
|
||||
|
||||
Object[] row = new Object[columnNames.length];
|
||||
int columnIndex = 0;
|
||||
row[columnIndex++] = rowData.get("ID");
|
||||
row[columnIndex++] = rowData;
|
||||
row[columnIndex++] = rowData.get("File");
|
||||
row[columnIndex++] = rowData.get("Name");
|
||||
row[columnIndex++] = rowData.get("SQL");
|
||||
@@ -107,18 +109,24 @@ public class SQLPanel extends JPanel {
|
||||
if (e.getClickCount() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int rowIndex = table.getSelectedRow();
|
||||
if (rowIndex < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object[] row = new Object[columnNames.length];
|
||||
for (int i = 0; i < columnNames.length; ++i) {
|
||||
row[i] = table.getValueAt(rowIndex, i);
|
||||
}
|
||||
|
||||
SQLDetailDialog dialog = new SQLDetailDialog(row);
|
||||
|
||||
CompositeData rowData = (CompositeData) table.getValueAt(rowIndex, 0);
|
||||
|
||||
SQLDetailDialog dialog = new SQLDetailDialog(rowData);
|
||||
|
||||
Toolkit kit = Toolkit.getDefaultToolkit();
|
||||
Dimension screenSize = kit.getScreenSize();
|
||||
int width = (int) screenSize.getWidth();
|
||||
int height = (int) screenSize.getHeight();
|
||||
dialog.setSize(800, 600);
|
||||
int w = dialog.getWidth();
|
||||
int h = dialog.getHeight();
|
||||
dialog.setLocation((width - w) / 2, (height - h) / 2);
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
|
||||
@@ -190,6 +198,7 @@ public class SQLPanel extends JPanel {
|
||||
TableColumn column = getColumn(0);
|
||||
column.setMinWidth(50);
|
||||
column.setMaxWidth(50);
|
||||
column.setCellRenderer(new IDRender());
|
||||
}
|
||||
|
||||
{
|
||||
@@ -201,7 +210,7 @@ public class SQLPanel extends JPanel {
|
||||
TableColumn column = getColumn(4);
|
||||
column.setPreferredWidth(60);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
TableColumn column = getColumn(7);
|
||||
column.setCellRenderer(new DateRenderer());
|
||||
@@ -214,7 +223,7 @@ public class SQLPanel extends JPanel {
|
||||
static class DateRenderer extends DefaultTableCellRenderer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
DateFormat formatter;
|
||||
DateFormat formatter;
|
||||
|
||||
public DateRenderer(){
|
||||
super();
|
||||
@@ -227,4 +236,15 @@ public class SQLPanel extends JPanel {
|
||||
setText((value == null) ? "" : formatter.format(value));
|
||||
}
|
||||
}
|
||||
|
||||
static class IDRender extends DefaultTableCellRenderer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public void setValue(Object value) {
|
||||
CompositeData rowData = (CompositeData) value;
|
||||
Object id = rowData.get("ID");
|
||||
setText((id == null) ? "" : id.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user