PageRenderTime 110ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/hsqldb19b3/org/hsqldb_voltpatches/util/ZaurusDatabaseManager.java

https://github.com/vkhoroshko/voltdb
Java | 670 lines | 469 code | 114 blank | 87 comment | 156 complexity | 0db1c033eb9c37c9f3e3a126f439ef31 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. /* Copyright (c) 2001-2009, The HSQL Development Group
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the HSQL Development Group nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package org.hsqldb_voltpatches.util;
  31. import java.io.IOException;
  32. import java.sql.Connection;
  33. import java.sql.DriverManager;
  34. import java.sql.SQLException;
  35. import java.awt.BorderLayout;
  36. import java.awt.Button;
  37. import java.awt.CardLayout;
  38. import java.awt.Dimension;
  39. import java.awt.FileDialog;
  40. import java.awt.Font;
  41. import java.awt.Frame;
  42. import java.awt.GridLayout;
  43. import java.awt.Insets;
  44. import java.awt.Menu;
  45. import java.awt.MenuBar;
  46. import java.awt.MenuItem;
  47. import java.awt.Panel;
  48. import java.awt.TextArea;
  49. import java.awt.Toolkit;
  50. import java.awt.event.ActionEvent;
  51. import java.awt.event.ActionListener;
  52. import java.awt.event.KeyEvent;
  53. import java.awt.event.KeyListener;
  54. import java.awt.event.WindowListener;
  55. import java.awt.image.MemoryImageSource;
  56. import org.hsqldb_voltpatches.lib.java.JavaSystem;
  57. /**
  58. * Class declaration
  59. *
  60. *
  61. * @version 1.0.0.1
  62. * @author ulrivo@users
  63. *
  64. */
  65. public class ZaurusDatabaseManager extends DatabaseManager
  66. implements ActionListener, WindowListener, KeyListener {
  67. // (ulrivo): new buttons to switch the cards
  68. Button butTree;
  69. Button butCommand;
  70. Button butResult;
  71. Button butEditor;
  72. // (ulrivo): Panel pCard with CardLayout inside Frame fMain
  73. Panel pCard;
  74. CardLayout layoutCard;
  75. // the editor/input form
  76. ZaurusEditor eEditor;
  77. // (ulrivo): variables set by arguments from the commandline
  78. static String defDriver;
  79. static String defURL;
  80. static String defUser;
  81. static String defPassword;
  82. static String defQuery;
  83. static String defDirectory;
  84. static String defDatabase;
  85. static int defWidth = 237;
  86. static int defHeight = 259;
  87. static int defLocX = 0;
  88. static int defLocY = 0;
  89. /**
  90. * Method declaration
  91. *
  92. *
  93. * @param c
  94. */
  95. public void connect(Connection c) {
  96. if (c == null) {
  97. return;
  98. }
  99. if (cConn != null) {
  100. try {
  101. cConn.close();
  102. } catch (SQLException e) {}
  103. }
  104. cConn = c;
  105. try {
  106. dMeta = cConn.getMetaData();
  107. sStatement = cConn.createStatement();
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. refreshTree();
  112. }
  113. /**
  114. * Method declaration
  115. *
  116. *
  117. * @param arg
  118. */
  119. public static void main(String[] arg) {
  120. bMustExit = true;
  121. // (ulrivo): read all arguments from the command line
  122. int i = 0;
  123. while (i < arg.length) {
  124. if (arg[i].equalsIgnoreCase("-driver") && (i + 1 < arg.length)) {
  125. i++;
  126. defDriver = arg[i];
  127. } else if (arg[i].equalsIgnoreCase("-url")
  128. && (i + 1 < arg.length)) {
  129. i++;
  130. defURL = arg[i];
  131. } else if (arg[i].equalsIgnoreCase("-width")
  132. && (i + 1 < arg.length)) {
  133. i++;
  134. try {
  135. defWidth = Integer.parseInt(arg[i]);
  136. } catch (Exception e) {}
  137. } else if (arg[i].equalsIgnoreCase("-height")
  138. && (i + 1 < arg.length)) {
  139. i++;
  140. try {
  141. defHeight = Integer.parseInt(arg[i]);
  142. } catch (Exception e) {}
  143. } else if (arg[i].equalsIgnoreCase("-locx")
  144. && (i + 1 < arg.length)) {
  145. i++;
  146. try {
  147. defLocX = Integer.parseInt(arg[i]);
  148. } catch (Exception e) {}
  149. } else if (arg[i].equalsIgnoreCase("-locy")
  150. && (i + 1 < arg.length)) {
  151. i++;
  152. try {
  153. defLocY = Integer.parseInt(arg[i]);
  154. } catch (Exception e) {}
  155. } else if (arg[i].equalsIgnoreCase("-user")
  156. && (i + 1 < arg.length)) {
  157. i++;
  158. defUser = arg[i];
  159. } else if (arg[i].equalsIgnoreCase("-password")
  160. && (i + 1 < arg.length)) {
  161. i++;
  162. defPassword = arg[i];
  163. } else if (arg[i].equalsIgnoreCase("-query")
  164. && (i + 1 < arg.length)) {
  165. i++;
  166. defQuery = arg[i];
  167. } else if (arg[i].equalsIgnoreCase("-defDirectory")
  168. && (i + 1 < arg.length)) {
  169. i++;
  170. defDirectory = arg[i];
  171. } else if (arg[i].equalsIgnoreCase("-database")
  172. && (i + 1 < arg.length)) {
  173. i++;
  174. defDatabase = arg[i];
  175. } else {
  176. showUsage();
  177. return;
  178. }
  179. i++;
  180. }
  181. ZaurusDatabaseManager m = new ZaurusDatabaseManager();
  182. m.main();
  183. // (ulrivo): make default connection if arguments set via the command line
  184. Connection c = null;
  185. if ((defDriver != null && defURL != null) || (defDatabase != null)) {
  186. if (defDatabase != null) {
  187. defDriver = "org.hsqldb_voltpatches.jdbcDriver";
  188. defURL = "jdbc:hsqldb:" + defDatabase;
  189. defUser = "SA";
  190. defPassword = "";
  191. }
  192. try {
  193. Class.forName(defDriver).newInstance();
  194. c = DriverManager.getConnection(defURL, defUser, defPassword);
  195. } catch (Exception e) {
  196. System.out.println("No connection for " + defDriver + " at "
  197. + defURL);
  198. e.printStackTrace();
  199. }
  200. } else {
  201. c = ZaurusConnectionDialog.createConnection(m.fMain, "Connect",
  202. new Insets(defWidth, defHeight, defLocX, defLocY));
  203. }
  204. if (c == null) {
  205. return;
  206. }
  207. m.connect(c);
  208. }
  209. private static void showUsage() {
  210. System.out.println(
  211. "Usage: java org.hsqldb_voltpatches.util.ZaurusDatabaseManager [options]");
  212. System.out.println("where options could be:");
  213. System.out.println(
  214. "If the next two options are set, the specified connection will be used:");
  215. System.out.println(" -driver dr");
  216. System.out.println(" -url address");
  217. System.out.println("-user name");
  218. System.out.println("-password passw");
  219. System.out.println("Alternative the database argument is used,");
  220. System.out.println(
  221. "and the hsqldb Driver Standalone is choosen for user 'SA'.");
  222. System.out.println("-database db");
  223. System.out.println(
  224. "-query qu the query qu will be read during initialization");
  225. System.out.println(
  226. "-defaultDirectory defdir default dir for the file open dialog");
  227. System.out.println(
  228. "If the next two options are set, the frame will be set to the specified values:");
  229. System.out.println(" -width width");
  230. System.out.println(" -height height");
  231. System.out.println("-locX positon left ");
  232. System.out.println("-locY positon top ");
  233. System.out.println("");
  234. System.out.println(
  235. "1. Example: java org.hsqldb_voltpatches.util.ZaurusDatabaseManager +");
  236. System.out.println(" -driver 'org.hsqldb_voltpatches.jdbcDriver' +");
  237. System.out.println(" -url 'jdbc:hsqldb:test'");
  238. System.out.println(
  239. "2. Example: java org.hsqldb_voltpatches.util.ZaurusDatabaseManager +");
  240. System.out.println(" -database 'test'");
  241. }
  242. /**
  243. * Method declaration
  244. *
  245. */
  246. public void main() {
  247. fMain = new Frame("HSQLDB Database Manager for Zaurus");
  248. imgEmpty = createImage(new MemoryImageSource(2, 2, new int[4 * 4], 2,
  249. 2));
  250. fMain.setIconImage(imgEmpty);
  251. fMain.addWindowListener(this);
  252. MenuBar bar = new MenuBar();
  253. // no shortcuts used
  254. String[] fitems = {
  255. "-Connect...", "--", "-Open Script...", "-Save Script...",
  256. "-Save Result...", "--", "-Exit"
  257. };
  258. addMenu(bar, "File", fitems);
  259. String[] vitems = {
  260. "-Refresh Tree", "--", "-View Tree", "-View Command",
  261. "-View Result", "-View Editor", "--", "-Results in Grid",
  262. "-Results in Text"
  263. };
  264. addMenu(bar, "View", vitems);
  265. String[] sitems = {
  266. "-SELECT", "-INSERT", "-UPDATE", "-DELETE", "--", "-CREATE TABLE",
  267. "-DROP TABLE", "-CREATE INDEX", "-DROP INDEX", "--", "-SCRIPT",
  268. "-SHUTDOWN", "--", "-Test Script"
  269. };
  270. addMenu(bar, "SQL", sitems);
  271. Menu recent = new Menu("Recent");
  272. mRecent = new Menu("Recent");
  273. bar.add(mRecent);
  274. String[] soptions = {
  275. "-AutoCommit on", "-AutoCommit off", "-Commit", "-Rollback", "--",
  276. "-Disable MaxRows", "-Set MaxRows to 100", "--", "-Logging on",
  277. "-Logging off", "--",
  278. "-Insert test data" // , "-Transfer"
  279. };
  280. addMenu(bar, "Options", soptions);
  281. String[] shelp = { "-Show HTML-Help in browser" };
  282. addMenu(bar, "?", shelp);
  283. fMain.setMenuBar(bar);
  284. fMain.setSize(defWidth, defHeight);
  285. fMain.add("Center", this);
  286. initGUI();
  287. sRecent = new String[iMaxRecent];
  288. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  289. Dimension size = fMain.getSize();
  290. // (ulrivo): arguments from command line or
  291. // full size on screen with less than 640 width
  292. if (d.width > 640) {
  293. fMain.setLocation((d.width - size.width) / 2,
  294. (d.height - size.height) / 2);
  295. } else if (defWidth > 0 && defHeight > 0) {
  296. fMain.setLocation(defLocX, defLocY);
  297. fMain.setSize(defWidth, defHeight);
  298. } else {
  299. fMain.setLocation(0, 0);
  300. fMain.setSize(d);
  301. }
  302. fMain.show();
  303. // (ulrivo): load query from command line
  304. if (defQuery != null) {
  305. txtCommand.setText(DatabaseManagerCommon.readFile(defQuery));
  306. }
  307. txtCommand.requestFocus();
  308. }
  309. /**
  310. * Method declaration
  311. *
  312. *
  313. * @param k
  314. */
  315. public void keyTyped(KeyEvent k) {
  316. // Strg+Enter or Shift+Enter executes the actual SQL statement in command panel
  317. if (k.getKeyChar() == '\n'
  318. && (k.isControlDown() || k.isShiftDown())) {
  319. k.consume();
  320. execute();
  321. layoutCard.show(pCard, "result");
  322. }
  323. }
  324. public void keyPressed(KeyEvent k) {
  325. // System.out.println("Key pressed: " + k.getKeyCode());
  326. }
  327. /**
  328. * Method declaration
  329. *
  330. *
  331. * @param ev
  332. */
  333. public void actionPerformed(ActionEvent ev) {
  334. String s = ev.getActionCommand();
  335. if (s == null) {
  336. if (ev.getSource() instanceof MenuItem) {
  337. MenuItem i;
  338. s = ((MenuItem) ev.getSource()).getLabel();
  339. }
  340. }
  341. if (s.equals("Execute")) {
  342. execute();
  343. layoutCard.show(pCard, "result");
  344. } else if (s.equals("Tree")) {
  345. layoutCard.show(pCard, "tree");
  346. } else if (s.equals("Command")) {
  347. layoutCard.show(pCard, "command");
  348. } else if (s.equals("Result")) {
  349. layoutCard.show(pCard, "result");
  350. } else if (s.equals("Editor")) {
  351. layoutCard.show(pCard, "editor");
  352. } else if (s.equals("Exit")) {
  353. windowClosing(null);
  354. } else if (s.equals("Logging on")) {
  355. JavaSystem.setLogToSystem(true);
  356. } else if (s.equals("Logging off")) {
  357. JavaSystem.setLogToSystem(false);
  358. } else if (s.equals("Refresh Tree")) {
  359. refreshTree();
  360. layoutCard.show(pCard, "tree");
  361. } else if (s.startsWith("#")) {
  362. int i = Integer.parseInt(s.substring(1));
  363. txtCommand.setText(sRecent[i]);
  364. } else if (s.equals("Connect...")) {
  365. connect(ZaurusConnectionDialog.createConnection(fMain, "Connect",
  366. new Insets(defWidth, defHeight, defLocX, defLocY)));
  367. refreshTree();
  368. layoutCard.show(pCard, "tree");
  369. } else if (s.equals("View Tree")) {
  370. layoutCard.show(pCard, "tree");
  371. } else if (s.equals("View Command")) {
  372. layoutCard.show(pCard, "command");
  373. } else if (s.equals("View Result")) {
  374. layoutCard.show(pCard, "result");
  375. } else if (s.equals("View Editor")) {
  376. layoutCard.show(pCard, "editor");
  377. } else if (s.equals("Results in Grid")) {
  378. iResult = 0;
  379. pResult.removeAll();
  380. pResult.add("Center", gResult);
  381. pResult.doLayout();
  382. layoutCard.show(pCard, "result");
  383. } else if (s.equals("Open Script...")) {
  384. FileDialog f = new FileDialog(fMain, "Open Script",
  385. FileDialog.LOAD);
  386. // (ulrivo): set default directory if set from command line
  387. if (defDirectory != null) {
  388. f.setDirectory(defDirectory);
  389. }
  390. f.show();
  391. String file = f.getFile();
  392. if (file != null) {
  393. txtCommand.setText(
  394. DatabaseManagerCommon.readFile(f.getDirectory() + file));
  395. }
  396. layoutCard.show(pCard, "command");
  397. } else if (s.equals("Save Script...")) {
  398. FileDialog f = new FileDialog(fMain, "Save Script",
  399. FileDialog.SAVE);
  400. // (ulrivo): set default directory if set from command line
  401. if (defDirectory != null) {
  402. f.setDirectory(defDirectory);
  403. }
  404. f.show();
  405. String file = f.getFile();
  406. if (file != null) {
  407. DatabaseManagerCommon.writeFile(f.getDirectory() + file,
  408. txtCommand.getText());
  409. }
  410. } else if (s.equals("Save Result...")) {
  411. FileDialog f = new FileDialog(fMain, "Save Result",
  412. FileDialog.SAVE);
  413. // (ulrivo): set default directory if set from command line
  414. if (defDirectory != null) {
  415. f.setDirectory(defDirectory);
  416. }
  417. f.show();
  418. String file = f.getFile();
  419. if (file != null) {
  420. showResultInText();
  421. DatabaseManagerCommon.writeFile(f.getDirectory() + file,
  422. txtResult.getText());
  423. }
  424. } else if (s.equals("Results in Text")) {
  425. iResult = 1;
  426. pResult.removeAll();
  427. pResult.add("Center", txtResult);
  428. pResult.doLayout();
  429. showResultInText();
  430. layoutCard.show(pCard, "result");
  431. } else if (s.equals("AutoCommit on")) {
  432. try {
  433. cConn.setAutoCommit(true);
  434. } catch (SQLException e) {}
  435. } else if (s.equals("AutoCommit off")) {
  436. try {
  437. cConn.setAutoCommit(false);
  438. } catch (SQLException e) {}
  439. } else if (s.equals("Commit")) {
  440. try {
  441. cConn.commit();
  442. } catch (SQLException e) {}
  443. } else if (s.equals("Insert test data")) {
  444. insertTestData();
  445. layoutCard.show(pCard, "result");
  446. } else if (s.equals("Rollback")) {
  447. try {
  448. cConn.rollback();
  449. } catch (SQLException e) {}
  450. } else if (s.equals("Disable MaxRows")) {
  451. try {
  452. sStatement.setMaxRows(0);
  453. } catch (SQLException e) {}
  454. } else if (s.equals("Set MaxRows to 100")) {
  455. try {
  456. sStatement.setMaxRows(100);
  457. } catch (SQLException e) {}
  458. } else if (s.equals("SELECT")) {
  459. showHelp(DatabaseManagerCommon.selectHelp);
  460. } else if (s.equals("INSERT")) {
  461. showHelp(DatabaseManagerCommon.insertHelp);
  462. } else if (s.equals("UPDATE")) {
  463. showHelp(DatabaseManagerCommon.updateHelp);
  464. } else if (s.equals("DELETE")) {
  465. showHelp(DatabaseManagerCommon.deleteHelp);
  466. } else if (s.equals("CREATE TABLE")) {
  467. showHelp(DatabaseManagerCommon.createTableHelp);
  468. } else if (s.equals("DROP TABLE")) {
  469. showHelp(DatabaseManagerCommon.dropTableHelp);
  470. } else if (s.equals("CREATE INDEX")) {
  471. showHelp(DatabaseManagerCommon.createIndexHelp);
  472. } else if (s.equals("DROP INDEX")) {
  473. showHelp(DatabaseManagerCommon.dropIndexHelp);
  474. } else if (s.equals("CHECKPOINT")) {
  475. showHelp(DatabaseManagerCommon.checkpointHelp);
  476. } else if (s.equals("SCRIPT")) {
  477. showHelp(DatabaseManagerCommon.scriptHelp);
  478. } else if (s.equals("SHUTDOWN")) {
  479. showHelp(DatabaseManagerCommon.shutdownHelp);
  480. } else if (s.equals("SET")) {
  481. showHelp(DatabaseManagerCommon.setHelp);
  482. } else if (s.equals("Test Script")) {
  483. showHelp(DatabaseManagerCommon.testHelp);
  484. } else if (s.equals("Show HTML-Help in browser")) {
  485. try {
  486. System.out.println("Starting Opera on index.html");
  487. Runtime.getRuntime().exec(new String[] {
  488. "opera", "/home/QtPalmtop/help/html/hsqldb/index.html"
  489. });
  490. } catch (IOException e) {
  491. System.out.println("A problem with Opera occured.");
  492. }
  493. }
  494. }
  495. /**
  496. * Method declaration
  497. *
  498. */
  499. private void initGUI() {
  500. Panel pQuery = new Panel();
  501. Panel pCommand = new Panel();
  502. // define a Panel pCard which takes four different cards/views:
  503. // tree of tables, command SQL text area, result window and an editor/input form
  504. pCard = new Panel();
  505. layoutCard = new CardLayout(2, 2);
  506. pCard.setLayout(layoutCard);
  507. // four buttons at the top to quickly switch between the four views
  508. butTree = new Button("Tree");
  509. butCommand = new Button("Command");
  510. butResult = new Button("Result");
  511. butEditor = new Button("Editor");
  512. butTree.addActionListener(this);
  513. butCommand.addActionListener(this);
  514. butResult.addActionListener(this);
  515. butEditor.addActionListener(this);
  516. Panel pButtons = new Panel();
  517. pButtons.setLayout(new GridLayout(1, 4, 8, 8));
  518. pButtons.add(butTree);
  519. pButtons.add(butCommand);
  520. pButtons.add(butResult);
  521. pButtons.add(butEditor);
  522. pResult = new Panel();
  523. pQuery.setLayout(new BorderLayout());
  524. pCommand.setLayout(new BorderLayout());
  525. pResult.setLayout(new BorderLayout());
  526. Font fFont = new Font("Dialog", Font.PLAIN, 12);
  527. txtCommand = new TextArea(5, 40);
  528. txtCommand.addKeyListener(this);
  529. txtResult = new TextArea(20, 40);
  530. txtCommand.setFont(fFont);
  531. txtResult.setFont(new Font("Courier", Font.PLAIN, 12));
  532. butExecute = new Button("Execute");
  533. butExecute.addActionListener(this);
  534. pCommand.add("South", butExecute);
  535. pCommand.add("Center", txtCommand);
  536. gResult = new Grid();
  537. setLayout(new BorderLayout());
  538. pResult.add("Center", gResult);
  539. tTree = new Tree();
  540. tTree.setMinimumSize(new Dimension(200, 100));
  541. gResult.setMinimumSize(new Dimension(200, 300));
  542. eEditor = new ZaurusEditor();
  543. pCard.add("tree", tTree);
  544. pCard.add("command", pCommand);
  545. pCard.add("result", pResult);
  546. pCard.add("editor", eEditor);
  547. fMain.add("Center", pCard);
  548. fMain.add("North", pButtons);
  549. doLayout();
  550. fMain.pack();
  551. }
  552. protected void refreshTree() {
  553. super.refreshTree();
  554. eEditor.refresh(cConn);
  555. }
  556. }