001: /*
002: * $Author$
003: * $Id$
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql.commands;
013:
014: import isql.Command;
015: import util.PerlWrapper;
016: import isql.*;
017: import util.*;
018: import javax.swing.*;
019: import javax.swing.table.*;
020: import java.awt.*;
021: import java.util.*;
022:
023: /**
024: * The command object associated with our metadata, such as links for
025: * tables, sort options etc.
026: * I am wondering whether they should be separate classes, however,
027: * would like them to be in one place.
028: * @author rahul kumar <rahul_kumar@yahoo.com>
029: * @see XXX
030: */
031:
032: public class SQLCommand implements Command {
033:
034: /** ctor/constructor.
035: */
036: public SQLCommand() {
037: }
038:
039: public String[] getCommandList() {
040: return commands;
041: }
042:
043: /** execute the given sql statement wither in foreground, or background.
044: * use background if sql is prepended with bg, or if user has set mode as bg.
045: */
046: public void execute(SQLForm form, String fword, String SQLString) {
047: _form = form;
048: tp = _form.tp;
049: myjdbc = _form.myjdbc;
050: boolean bg = false;
051: // see if first word is background
052: if (fword.equals("bg")) {
053: bg = true;
054: SQLString = SQLString.substring(3);
055: }
056:
057: if ("true".equalsIgnoreCase((String) _form.getAttribute("bg",
058: "false"))
059: || bg) {
060: _form.setErrorArea('\n' + "Running in background.");
061: new Thread(new SQLBG(this , SQLString)).start();
062: return;
063: }
064: execute_sql(SQLString);
065: }
066:
067: /** method that actually executes in background.
068: * called either from bg thread or execute().
069: */
070: public void execute_sql(String SQLString) {
071: //super.setCursor (java.awt.Cursor.WAIT_CURSOR);
072: _form.waitCursor();
073: if ("JTABLE".equalsIgnoreCase((String) _form.getAttribute(
074: "outputformat", "JTABLE"))) {
075: Object result = null;
076: try {
077: result = myjdbc.runSelect(SQLString);
078: } catch (Exception exc) {
079: //super.setCursor (java.awt.Cursor.DEFAULT_CURSOR);
080: _form.defaultCursor();
081: _form.popup(exc.toString());
082: return;
083: }
084: //tp.clearOutputArea(); // remarked 20011112
085: //tp.clearErrorArea();
086: if (result instanceof Integer) {
087: _form.setErrorArea('\n' + result.toString()
088: + " row(s) affected.");
089: _form.popup(result.toString() + " row(s) affected.");
090: } else {
091: // RK added on 20040102 22:35:22 added check for
092: // null/blank
093: TableModel tm = (TableModel) result;
094: // RK added on 20040202 12:01:58
095: // since the sql is now replaced we would like to
096: // display the modified statement in history.
097: // unfortunately if we do it here, we can only show
098: // modifed for this option.
099: // we have started returning null if user cancels variable replace in SQL
100: if (result != null) {
101: SQLString = ((TableMap) result).getSQL();
102: _form._currentSQL = SQLString;
103: }
104: if (tm == null || tm.getRowCount() == 0) {
105: _form.setErrorArea('\n' + "No result.");
106: _form.popup("No result.");
107: } else {
108: if ("single".equalsIgnoreCase((String) _form
109: .getAttribute("tableview", "single"))) {
110: tp.updateTable((TableModel) result);
111: tp.makeTableAreaVisible();
112: } else { // multiple
113: String name = Actions.getTableFromSQL(
114: _form._currentSQL, "???");
115: tp.makeFramesVisible();
116: tp.getWindowPanel().addFrame(
117: (TableModel) result, name);
118: }
119: }// null tm
120: }
121: } else { // TEXT format output
122: String result = myjdbc.runSQL(SQLString);
123: tp.appendOutputArea(result);
124: tp.makeOutputAreaVisible();
125: }
126:
127: // Put old SQL in scrap area
128: tp.putSQLInScrapArea(SQLString);
129: //super.setCursor (java.awt.Cursor.DEFAULT_CURSOR);
130: _form.defaultCursor();
131:
132: }
133:
134: public String getUsage(String s) {
135: return "sql statement";
136: }
137:
138: // added bg just in case we want to execute one command in bg mode. we can save bg with some commands.
139: String commands[] = { "", "bg", "select", "create", "update",
140: "delete", "alter", "drop" };
141: SQLForm _form;
142: SQLTabbedPane tp;
143: SQLJDBC myjdbc;
144:
145: ////// START INSTANCE VARIABLES //////
146:
147: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
148: static final String P = "SQLCommand"; // used in exception strings
149:
150: } // end of class
151:
152: class SQLBG implements Runnable {
153:
154: String SQLString;
155: SQLCommand command;
156:
157: public SQLBG(SQLCommand command, String SQLString) {
158: this .SQLString = SQLString;
159: this .command = command;
160: }
161:
162: public void run() {
163: command.execute_sql(SQLString);
164: }
165: }
|