📄 sqlclientui.java
字号:
package com.cownew.PIS.base.sqlClient.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.JToolBar.Separator;
import javax.swing.table.DefaultTableModel;
import com.cownew.PIS.base.sqlClient.common.ISQLClientService;
import com.cownew.PIS.framework.client.MainFrame;
import com.cownew.PIS.framework.client.RemoteServiceLocator;
import com.cownew.PIS.framework.common.multisql.TargetDBEnum;
import com.cownew.PIS.framework.common.services.ISQLExecutor;
import com.cownew.PIS.ui.base.ExceptionHandler;
import com.cownew.PIS.ui.base.UIPanel;
import com.cownew.PIS.ui.utils.PISAbstractAction;
import com.cownew.ctk.common.EnvironmentUtils;
import com.cownew.ctk.common.StringUtils;
import com.cownew.ctk.ui.swing.MsgBox;
import com.cownew.ctk.ui.swing.SwingUtils;
public class SQLClientUI extends UIPanel
{
private JScrollPane scrollPaneSQL;
private JTextArea txtSQL;
private JScrollPane scrollPaneResult;
private JTable tableResult;
private JComboBox comboTargetDB;
private ActionExecuteSQL actionExeSQL;
private ActionExecuteDialectSQL actionExeDialectSQL;
private ActionBuildEntityIndex actionBuildEntityIndex;
private ActionSearchEntity actionSearchEntity;
private ActionTranslateSQL actionTransSQL;
private ActionGenCreateSQL actionGenCreateSQL;
private JSplitPane jSplitPane;
public SQLClientUI() throws Exception
{
super();
}
/**
* This method initializes this
*
*/
protected void initialize()
{
super.initialize();
this.setLayout(new BorderLayout());
this.setSize(new Dimension(751, 363));
this.add(getJSplitPane(), BorderLayout.CENTER);
}
protected void initAction()
{
super.initAction();
actionExeSQL = new ActionExecuteSQL("执行");
actionExeDialectSQL = new ActionExecuteDialectSQL("执行方言");
actionTransSQL = new ActionTranslateSQL("翻译");
actionBuildEntityIndex = new ActionBuildEntityIndex("重建表索引");
actionSearchEntity = new ActionSearchEntity("实体检索");
actionGenCreateSQL = new ActionGenCreateSQL("生成建库SQL");
}
public JToolBar getToolBar()
{
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
JButton btnExeSQL = new JButton();
btnExeSQL.setAction(actionExeSQL);
toolBar.add(btnExeSQL);
JButton btnExeDialectSQL = new JButton();
btnExeDialectSQL.setAction(actionExeDialectSQL);
toolBar.add(btnExeDialectSQL);
JButton btnBuildEntityIndex = new JButton();
btnBuildEntityIndex.setAction(actionBuildEntityIndex);
toolBar.add(btnBuildEntityIndex);
JButton btnSearchEntity = new JButton();
btnSearchEntity.setAction(actionSearchEntity);
toolBar.add(btnSearchEntity);
JButton btnGenCreateSQL = new JButton();
btnGenCreateSQL.setAction(actionGenCreateSQL);
toolBar.add(btnGenCreateSQL);
Separator sep = new Separator();
sep.setOrientation(SwingConstants.VERTICAL);
toolBar.add(sep);
toolBar.add(getComboTargetDB());
JButton btnTransSQL = new JButton();
btnTransSQL.setAction(actionTransSQL);
toolBar.add(btnTransSQL);
return toolBar;
}
public String getUITitle()
{
return "查询分析器";
}
protected void actionExecuteSQL_actionPerformed(ActionEvent e)
throws Exception
{
String sql = getSQL();
if (StringUtils.isEmpty(sql))
{
return;
}
execute(sql, new Object[] {});
}
protected void actionExecuteDialectSQL_actionPerformed(ActionEvent e)
throws Exception
{
String sql = getSQL();
if (StringUtils.isEmpty(sql))
{
return;
}
executeDialect(sql);
}
private String getSQL()
{
String sql = null;
String selectedText = txtSQL.getSelectedText();
if (!StringUtils.isEmpty(selectedText))
{
sql = selectedText;
} else
{
sql = txtSQL.getText();
}
return sql.trim();
}
protected void execute(String sql, Object[] params)
{
try
{
ISQLExecutor sqle = (ISQLExecutor) RemoteServiceLocator
.getRemoteService(ISQLExecutor.class);
ResultSet rs = sqle.execute(sql, params);
if (rs != null)
{
fillData(tableResult, rs);
} else
{
MsgBox.showInfo(this, "执行完毕!");
}
} catch (Exception e)
{
ExceptionHandler.handle(e);
}
}
protected void executeDialect(String sql)
{
try
{
ISQLExecutor sqle = (ISQLExecutor) RemoteServiceLocator
.getRemoteService(ISQLExecutor.class);
ResultSet rs = sqle.executeDialect(sql);
if (rs != null)
{
fillData(tableResult, rs);
} else
{
MsgBox.showInfo(this, "执行完毕!");
}
} catch (Exception e)
{
ExceptionHandler.handle(e);
}
}
public static void fillData(JTable tblMain, ResultSet rs)
throws SQLException
{
ResultSetMetaData meta = rs.getMetaData();
DefaultTableModel model = new DefaultTableModel();
tblMain.setModel(model);
fillHeader(tblMain, meta);
fillBody(tblMain, rs);
}
public static void fillBody(JTable tblMain, ResultSet rs)
throws SQLException
{
int c = rs.getMetaData().getColumnCount();
DefaultTableModel model = (DefaultTableModel) tblMain.getModel();
while (rs.next())
{
Vector vectorRow = new Vector();
for (int i = 0; i < c; i++)
{
Object object = rs.getObject(i + 1);
vectorRow.add(object);
}
model.addRow(vectorRow);
}
tblMain.setModel(model);
}
public static void fillHeader(JTable tblMain, ResultSetMetaData meta)
throws SQLException
{
DefaultTableModel model = (DefaultTableModel) tblMain.getModel();
int c = meta.getColumnCount();
for (int i = 0; i < c; i++)
{
model.addColumn(meta.getColumnName(i + 1));
}
}
protected void actionBuildEntityIndex_actionPerformed(ActionEvent e)
throws Exception
{
ISQLClientService ss = (ISQLClientService) RemoteServiceLocator
.getRemoteService(ISQLClientService.class);
ss.buildEntityInfoIndex();
MsgBox.showInfo(this, "重建完毕!");
}
protected void actionSearchEntity_actionPerformed(ActionEvent e)
throws Exception
{
String eName = MsgBox.showInput(this, "请输入要查找的实体别名");
if (StringUtils.isEmpty(eName))
{
return;
}
ISQLClientService ss = (ISQLClientService) RemoteServiceLocator
.getRemoteService(ISQLClientService.class);
if (!ss.isEntityInfoIndex())
{
ss.buildEntityInfoIndex();
}
String value = "%" + eName + "%";
StringBuffer sb = new StringBuffer();
sb.append("select FName,FPackageName, FAlias,FTableName\n");
sb.append("from T_BS_EntityInfoIndex\n");
sb.append("where FAlias like ?");
execute(sb.toString(), new Object[] { value });
}
public void actionTranslateSQL_actionPerformed(ActionEvent e)
throws Exception
{
String sql = getSQL();
if (StringUtils.isEmpty(sql))
{
return;
}
Object objTargetDB = getComboTargetDB().getSelectedItem();
String dialectSQL = null;
ISQLExecutor sqle = (ISQLExecutor) RemoteServiceLocator
.getRemoteService(ISQLExecutor.class);
if (objTargetDB instanceof TargetDBEnum)
{
dialectSQL = sqle.translateSQL(sql, (TargetDBEnum) objTargetDB);
} else
{
dialectSQL = sqle.translateSQL(sql);
}
getTxtSQL().append(EnvironmentUtils.getLineSeparator() + dialectSQL);
}
protected void actionGenCreateSQL_actionPerformed(ActionEvent e)
throws Exception
{
ISQLClientService intf = (ISQLClientService) RemoteServiceLocator
.getRemoteService(ISQLClientService.class);
String[] sqls = intf.generateCreateSQL();
for (int i = 0, n = sqls.length; i < n; i++)
{
getTxtSQL().append(sqls[i]);
getTxtSQL().append("\n");
}
}
private JComboBox getComboTargetDB()
{
if (comboTargetDB == null)
{
comboTargetDB = new JComboBox();
comboTargetDB.addItem("当前");
SwingUtils.fillWithEnum(comboTargetDB, TargetDBEnum.class, false);
}
return comboTargetDB;
}
/**
* This method initializes scrollPaneSQL
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getScrollPaneSQL()
{
if (scrollPaneSQL == null)
{
scrollPaneSQL = new JScrollPane();
scrollPaneSQL.setViewportView(getTxtSQL());
}
return scrollPaneSQL;
}
/**
* This method initializes txtSQL
*
* @return javax.swing.JTextArea
*/
private JTextArea getTxtSQL()
{
if (txtSQL == null)
{
txtSQL = new JTextArea();
txtSQL.setLineWrap(true);
txtSQL.setWrapStyleWord(true);
}
return txtSQL;
}
/**
* This method initializes jSplitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getJSplitPane()
{
if (jSplitPane == null)
{
jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
jSplitPane.setTopComponent(getScrollPaneSQL());
jSplitPane.setBottomComponent(getScrollPaneResult());
jSplitPane
.setDividerLocation(MainFrame.getMainFrame().getHeight() / 2);
}
return jSplitPane;
}
/**
* This method initializes scrollPaneResult
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getScrollPaneResult()
{
if (scrollPaneResult == null)
{
scrollPaneResult = new JScrollPane();
scrollPaneResult.setViewportView(getTableResult());
}
return scrollPaneResult;
}
/**
* This method initializes tableResult
*
* @return javax.swing.JTable
*/
private JTable getTableResult()
{
if (tableResult == null)
{
tableResult = new JTable();
}
return tableResult;
}
protected class ActionExecuteSQL extends PISAbstractAction
{
public ActionExecuteSQL(String name, Icon icon)
{
super(name, icon);
}
public ActionExecuteSQL(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionExecuteSQL_actionPerformed(e);
}
}
protected class ActionExecuteDialectSQL extends PISAbstractAction
{
public ActionExecuteDialectSQL(String name, Icon icon)
{
super(name, icon);
}
public ActionExecuteDialectSQL(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionExecuteDialectSQL_actionPerformed(e);
}
}
protected class ActionBuildEntityIndex extends PISAbstractAction
{
public ActionBuildEntityIndex(String name, Icon icon)
{
super(name, icon);
}
public ActionBuildEntityIndex(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionBuildEntityIndex_actionPerformed(e);
}
}
protected class ActionSearchEntity extends PISAbstractAction
{
public ActionSearchEntity(String name, Icon icon)
{
super(name, icon);
}
public ActionSearchEntity(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionSearchEntity_actionPerformed(e);
}
}
protected class ActionTranslateSQL extends PISAbstractAction
{
public ActionTranslateSQL(String name, Icon icon)
{
super(name, icon);
}
public ActionTranslateSQL(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionTranslateSQL_actionPerformed(e);
}
}
protected class ActionGenCreateSQL extends PISAbstractAction
{
public ActionGenCreateSQL(String name, Icon icon)
{
super(name, icon);
}
public ActionGenCreateSQL(String name)
{
super(name);
}
public void onActionPerformed(ActionEvent e) throws Exception
{
actionGenCreateSQL_actionPerformed(e);
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -