📄 databasemanager.java
字号:
/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb.util;
import java.applet.Applet;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.MemoryImageSource;
import org.hsqldb.lib.java.JavaSystem;
// sqlbob@users 20020401 - patch 1.7.0 by sqlbob (RMP) - enhancements
// sqlbob@users 20020401 - patch 537501 by ulrivo - command line arguments
// sqlbob@users 20020407 - patch 1.7.0 - reengineering
// nickferguson@users 20021005 - patch 1.7.1 - enhancements
/*
* unsaved@users 20050426 - Switched default switch method from "-switch" to
* "--switch" because "-switch" usage is ambiguous as used here. Single
* switches should
* be reserved for single-letter switches which can be mixed like
* "-u -r -l" = "-url". -blaine
*/
/**
* AWT Tool for manageing a JDBC database.<p>
* <pre>
* Usage: java DatabaseManagerSwing [--options]
* where options include:
* --driver <classname> jdbc driver class
* --url <name> jdbc url
* --user <name> username used for connection
* --password <password> password for this user
* --urlid <urlid> get connection info from RC file
* --rcfile <file> use instead of default (with urlid)
* --dir <path> default directory
* --script <file> reads from script file
*</pre>
*
* Originally in HypersonicSQL. Extended in various versions of HSQLDB.
*
* @author Thomas Mueller (Hypersonic SQL Group)
* @version 1.8.0
* @since Hypersonic SQL
*/
public class DatabaseManager extends Applet
implements ActionListener, WindowListener, KeyListener {
private static final String DEFAULT_RCFILE =
System.getProperty("user.home") + "/dbmanager.rc";
static final String NL = System.getProperty("line.separator");
static final int iMaxRecent = 24;
private static boolean TT_AVAILABLE = false;
//#ifdef JAVA2FULL
static {
try {
Class.forName(DatabaseManager.class.getPackage().getName()
+ ".Transfer");
TT_AVAILABLE = true;
} catch (Throwable t) {}
}
//#endif
private static final String HELP_TEXT =
"See the forums, mailing lists, and HSQLDB User Guide\n"
+ "at http://hsqldb.org.\n\n"
+ "Please paste the following version identifier with any\n"
+ "problem reports or help requests: $Revision: 1.37 $"
+ (TT_AVAILABLE ? ""
: ("\n\nTransferTool classes are not in CLASSPATH.\n"
+ "To enable the Tools menu, add 'transfer.jar' to your class path."));
;
private static final String ABOUT_TEXT =
"$Revision: 1.37 $ of DatabaseManagerSwing\n\n"
+ "Copyright (c) 1995-2000, The Hypersonic SQL Group.\n"
+ "Copyright (c) 2001-2007, The HSQL Development Group.\n"
+ "http://hsqldb.org (User Guide available at this site).\n\n\n"
+ "You may use and redistribute according to the HSQLDB\n"
+ "license documented in the source code and at the web\n"
+ "site above." //
+ (TT_AVAILABLE ? "\n\nTransferTool options are available."
: "");
Connection cConn;
DatabaseMetaData dMeta;
Statement sStatement;
Menu mRecent;
String[] sRecent;
int iRecent;
TextArea txtCommand;
Button butExecute;
Button butClear;
Tree tTree;
Panel pResult;
long lTime;
int iResult; // 0: grid; 1: text
Grid gResult;
TextArea txtResult;
boolean bHelp;
Frame fMain;
Image imgEmpty;
static boolean bMustExit;
String ifHuge = "";
// (ulrivo): variables set by arguments from the commandline
static String defDriver = "org.hsqldb.jdbcDriver";
static String defURL = "jdbc:hsqldb:.";
static String defUser = "sa";
static String defPassword = "";
static String defScript;
static String defDirectory;
/**
* Method declaration
*
*
* @param c
*/
public void connect(Connection c) {
if (c == null) {
return;
}
if (cConn != null) {
try {
cConn.close();
} catch (SQLException e) {}
}
cConn = c;
try {
dMeta = cConn.getMetaData();
sStatement = cConn.createStatement();
refreshTree();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Method declaration
*
*/
public void init() {
DatabaseManager m = new DatabaseManager();
m.main();
try {
m.connect(ConnectionDialog.createConnection(defDriver, defURL,
defUser, defPassword));
m.insertTestData();
m.refreshTree();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Method declaration
*
*
* @param arg
*/
public static void main(String[] arg) {
System.getProperties().put("sun.java2d.noddraw", "true");
// (ulrivo): read all arguments from the command line
String lowerArg;
String urlid = null;
String rcFile = null;
boolean autoConnect = false;
boolean urlidConnect = false;
bMustExit = true;
for (int i = 0; i < arg.length; i++) {
lowerArg = arg[i].toLowerCase();
if (lowerArg.length() > 1 && lowerArg.charAt(1) == '-') {
lowerArg = lowerArg.substring(1);
}
i++;
if (i == arg.length) {
showUsage();
return;
}
if (lowerArg.equals("-driver")) {
defDriver = arg[i];
autoConnect = true;
} else if (lowerArg.equals("-url")) {
defURL = arg[i];
autoConnect = true;
} else if (lowerArg.equals("-user")) {
defUser = arg[i];
autoConnect = true;
} else if (lowerArg.equals("-password")) {
defPassword = arg[i];
autoConnect = true;
} else if (lowerArg.equals("-urlid")) {
urlid = arg[i];
urlidConnect = true;
} else if (lowerArg.equals("-rcfile")) {
rcFile = arg[i];
urlidConnect = true;
} else if (lowerArg.equals("-dir")) {
defDirectory = arg[i];
} else if (lowerArg.equals("-script")) {
defScript = arg[i];
} else if (lowerArg.equals("-noexit")) {
bMustExit = false;
i--;
} else {
showUsage();
return;
}
}
DatabaseManager m = new DatabaseManager();
m.main();
Connection c = null;
try {
if (autoConnect && urlidConnect) {
throw new IllegalArgumentException(
"You may not specify both (urlid) AND (url/user/password).");
}
if (autoConnect) {
c = ConnectionDialog.createConnection(defDriver, defURL,
defUser, defPassword);
} else if (urlidConnect) {
if (urlid == null) {
throw new IllegalArgumentException(
"You must specify an 'urlid' to use an RC file");
}
autoConnect = true;
if (rcFile == null) {
rcFile = DEFAULT_RCFILE;
}
c = new RCData(new File(rcFile), urlid).getConnection(null,
System.getProperty("sqlfile.charset"),
System.getProperty("javax.net.ssl.trustStore"));
} else {
c = ConnectionDialog.createConnection(m.fMain, "Connect");
}
} catch (Exception e) {
e.printStackTrace();
}
if (c == null) {
return;
}
m.connect(c);
}
private static void showUsage() {
System.out.println(
"Usage: java DatabaseManager [--options]\n"
+ "where options include:\n"
+ " --driver <classname> jdbc driver class\n"
+ " --url <name> jdbc url\n"
+ " --user <name> username used for connection\n"
+ " --password <password> password for this user\n"
+ " --urlid <urlid> use url/user/password/driver in rc file\n"
+ " --rcfile <file> (defaults to 'dbmanager.rc' in home dir)\n"
+ " --dir <path> default directory\n"
+ " --script <file> reads from script file\n"
+ " --noexit do not call system.exit()\n"
+ "(Single-hypen switches like '-driver' are also supported)");
}
/**
* Method declaration
*
*/
void insertTestData() {
try {
DatabaseManagerCommon.createTestTables(sStatement);
refreshTree();
txtCommand.setText(
DatabaseManagerCommon.createTestData(sStatement));
refreshTree();
for (int i = 0; i < DatabaseManagerCommon.testDataSql.length;
i++) {
addToRecent(DatabaseManagerCommon.testDataSql[i]);
}
execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Method declaration
*
*/
public void main() {
fMain = new Frame("HSQL Database Manager");
imgEmpty = createImage(new MemoryImageSource(2, 2, new int[4 * 4], 2,
2));
fMain.setIconImage(imgEmpty);
fMain.addWindowListener(this);
MenuBar bar = new MenuBar();
// used shortcuts: CERGTSIUDOLM
String[] fitems = {
"-Connect...", "--", "-Open Script...", "-Save Script...",
"-Save Result...", "-Save Result csv...", "--", "-Exit"
};
addMenu(bar, "File", fitems);
String[] vitems = {
"RRefresh Tree", "--", "GResults in Grid", "TResults in Text",
"--", "1Shrink Tree", "2Enlarge Tree", "3Shrink Command",
"4Enlarge Command"
};
addMenu(bar, "View", vitems);
String[] sitems = {
"SSELECT", "IINSERT", "UUPDATE", "DDELETE", "--", "-CREATE TABLE",
"-DROP TABLE", "-CREATE INDEX", "-DROP INDEX", "--", "-CHECKPOINT",
"-SCRIPT", "-SET", "-SHUTDOWN", "--", "-Test Script"
};
addMenu(bar, "Command", sitems);
Menu recent = new Menu("Recent");
mRecent = new Menu("Recent");
bar.add(mRecent);
String[] soptions = {
"-AutoCommit on", "-AutoCommit off", "OCommit", "LRollback", "--",
"-Disable MaxRows", "-Set MaxRows to 100", "--", "-Logging on",
"-Logging off", "--", "-Insert test data"
};
addMenu(bar, "Options", soptions);
String[] stools = {
"-Dump", "-Restore", "-Transfer"
};
addMenu(bar, "Tools", stools);
Menu hMenu = new Menu("Help");
MenuItem aItem = new MenuItem("About");
aItem.setShortcut(new MenuShortcut('A'));
aItem.addActionListener(this);
hMenu.add(aItem);
MenuItem hItem = new MenuItem("Help");
hItem.setShortcut(new MenuShortcut('H'));
hItem.addActionListener(this);
hMenu.add(hItem);
//bar.add(hMenu);
// Command above disabled only until a help display bug is fixed.
fMain.setMenuBar(bar);
fMain.setSize(640, 480);
fMain.add("Center", this);
initGUI();
sRecent = new String[iMaxRecent];
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = fMain.getSize();
// (ulrivo): full size on screen with less than 640 width
if (d.width >= 640) {
fMain.setLocation((d.width - size.width) / 2,
(d.height - size.height) / 2);
} else {
fMain.setLocation(0, 0);
fMain.setSize(d);
}
fMain.show();
// (ulrivo): load query from command line
if (defScript != null) {
if (defDirectory != null) {
defScript = defDirectory + File.separator + defScript;
}
txtCommand.setText(DatabaseManagerCommon.readFile(defScript));
}
txtCommand.requestFocus();
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -