001: package isql;
002:
003: import java.io.*;
004: import java.awt.*;
005: import java.awt.event.ActionEvent;
006: import java.text.*;
007: import javax.swing.*;
008: import javax.swing.KeyStroke;
009: import javax.swing.SwingConstants;
010: import javax.swing.text.*;
011: import util.*;
012: import java.util.ArrayList;
013:
014: /**
015: * Actions pertaining to the list, these all require a table to be
016: * selected (barring refresh list) and they all require a connection.
017: * @author Rahul Kumar $Author: rahul_kumar $
018: * @version $Id: ListActions.java,v 1.3 2004/01/26 15:25:44 rahul_kumar Exp rahul $
019: */
020: public class ListActions {
021:
022: SQLForm _form = null;
023:
024: /** constructor passing SQLForm
025: */
026: public ListActions(SQLForm form) {
027: _form = form;
028: }
029:
030: public Action[] getActions() {
031: return new Action[] { new ViewColumnsAction(_form),
032: new ViewPrimaryKeysAction(_form),
033: new ViewIndexesAction(_form),
034: new RefreshListAction(_form),
035: new CreateCreateScriptAction(_form),
036: new CreateInsertScriptAction(_form),
037: new ViewCountAction(_form),
038: new ViewDistinctAction(_form),
039: new MakeNotNullAction(_form),
040: new GetDuplicatesAction(_form),
041: new MakePrimaryAction(_form),
042: new FormatSQLAction(_form) };
043: }
044:
045: public static final String createCreateScriptAction = "create-createscript";
046: public static final String createInsertScriptAction = "create-insertscript";
047: public static final String viewColumnsAction = "view-columns";
048: public static final String viewPrimaryKeysAction = "view-primarykeys";
049: public static final String viewIndexesAction = "view-indexes";
050: public static final String refreshListAction = "refresh-list";
051: public static final String viewCountAction = "view-count";
052: public static final String viewDistinctAction = "view-distinct";
053: public static final String formatSQLAction = "format-sql";
054: public static final String makeNotNullAction = "makeNotNullAction";
055: public static final String getDuplicatesAction = "getDuplicatesAction";
056: public static final String makePrimaryAction = "makePrimaryAction";
057: // TODO also automcaticall should load into List menu in SQLForm
058: public static final String makeIndexAction = "makeIndexAction";
059:
060: public static class GetDuplicatesAction extends AbstractAction {
061: public GetDuplicatesAction(SQLForm form) {
062: super (getDuplicatesAction);
063: this ._form = form;
064: //putValue("accelerator","Meta C");
065: }
066:
067: final SQLForm _form;
068:
069: public void actionPerformed(ActionEvent e) {
070: String command = "select $clist, count(*) from $tn group by $clist having count(*)>1 ";
071: String[] commands = formatCommand(_form, command);
072: executeCommand(_form, commands, false);
073: }
074: }
075:
076: public static class MakeNotNullAction extends AbstractAction {
077: public MakeNotNullAction(SQLForm form) {
078: super (makeNotNullAction);
079: this ._form = form;
080: //putValue("accelerator","Meta C");
081: }
082:
083: final SQLForm _form;
084:
085: public void actionPerformed(ActionEvent e) {
086: String command = "alter table $tn modify $cn $dt NOT NULL ";
087: String[] commands = formatCommand(_form, command);
088: executeCommand(_form, commands, true);
089: }
090: }
091:
092: public static class MakePrimaryAction extends AbstractAction {
093: public MakePrimaryAction(SQLForm form) {
094: super (makePrimaryAction);
095: this ._form = form;
096: //putValue("accelerator","Meta C");
097: }
098:
099: final SQLForm _form;
100:
101: public void actionPerformed(ActionEvent e) {
102: String command = "alter table $tn add primary key ($clist)";
103: String[] commands = formatCommand(_form, command);
104: executeCommand(_form, commands, true);
105: }
106: }
107:
108: public static class CreateCreateScriptAction extends AbstractAction {
109: public CreateCreateScriptAction(SQLForm form) {
110: super (createCreateScriptAction);
111: this ._form = form;
112: putValue("accelerator", "Meta C");
113: }
114:
115: final SQLForm _form;
116:
117: public void actionPerformed(ActionEvent e) {
118: String tname = getSelectedTableName(_form);
119: String fname = tname + ".sql";
120: if (tname != null) {
121: _form.myjdbc.createCreateScript(tname, fname);
122: _form.setErrorArea('\n' + "Written create script to "
123: + fname);
124: } else
125: _form.popup("Do select a table.");
126: }
127: }
128:
129: public static class CreateInsertScriptAction extends AbstractAction {
130: public CreateInsertScriptAction(SQLForm form) {
131: super (createInsertScriptAction);
132: this ._form = form;
133: putValue("accelerator", "Meta I");
134: }
135:
136: final SQLForm _form;
137:
138: public void actionPerformed(ActionEvent e) {
139: String tname = getSelectedTableName(_form);
140: String fname = tname + ".sql";
141: if (tname != null) {
142: _form.myjdbc.createInsertScript(tname, fname, 0, 1000);
143: _form.setErrorArea('\n' + "Written insert script to "
144: + fname);
145:
146: } else
147: _form.popup("Do select a table.");
148: }
149: }
150:
151: public static class ViewColumnsAction extends AbstractAction {
152:
153: public ViewColumnsAction(SQLForm form) {
154: super (viewColumnsAction);
155: this ._form = form;
156: putValue("accelerator", "control C");
157: }
158:
159: final SQLForm _form;
160:
161: public void actionPerformed(ActionEvent e) {
162: // The following somehow isnt working on Oracle
163: // _form.Run( "invoke getColumns(null,null,"+tname+"'%')" );
164: String tname = getSelectedTableName(_form);
165: if (tname != null) {
166: _form.show(_form.myjdbc.getColumnInfo(tname), tname);
167: } else
168: _form.popup("Do select a table.");
169: }
170: }
171:
172: public static class ViewPrimaryKeysAction extends AbstractAction {
173:
174: public ViewPrimaryKeysAction(SQLForm form) {
175: super (viewPrimaryKeysAction);
176: this ._form = form;
177: }
178:
179: final SQLForm _form;
180:
181: public void actionPerformed(ActionEvent e) {
182: String tname = getSelectedTableName(_form);
183: if (tname != null) {
184: _form
185: .show(_form.myjdbc.getPrimaryKeyInfo(tname),
186: tname);
187: } else
188: _form.popup("Do select a table.");
189: }
190: }
191:
192: public static class ViewIndexesAction extends AbstractAction {
193:
194: public ViewIndexesAction(SQLForm form) {
195: super (viewIndexesAction);
196: this ._form = form;
197: putValue("accelerator", "alt I");
198: }
199:
200: final SQLForm _form;
201:
202: public void actionPerformed(ActionEvent e) {
203: String tname = getSelectedTableName(_form);
204: if (tname != null) {
205: _form.show(_form.myjdbc.getIndexInfo(tname), tname);
206: } else
207: _form.popup("Do select a table.");
208: }
209: }
210:
211: public static String getSelectedTableName(SQLForm _form) {
212: Object[] o = _form.tablePanel.getSelectedValues();
213: if (o != null && o.length > 0)
214: return o[0].toString();
215: else
216: return null;
217: }
218:
219: /** refreshes the table list. equivalent to calling "refresh tables"
220: * in the input area.
221: */
222: public static class RefreshListAction extends AbstractAction {
223:
224: public RefreshListAction(SQLForm form) {
225: super (refreshListAction);
226: this ._form = form;
227: putValue("accelerator", "Meta R");
228: }
229:
230: final SQLForm _form;
231:
232: public void actionPerformed(ActionEvent e) {
233: _form.Run("refresh tables");
234: }
235: }
236:
237: /** view count of records in selected table */
238: public static class ViewCountAction extends AbstractAction {
239:
240: public ViewCountAction(SQLForm form) {
241: super (viewCountAction);
242: this ._form = form;
243: putValue("accelerator", "alt C");
244: }
245:
246: final SQLForm _form;
247:
248: public void actionPerformed(ActionEvent e) {
249: String tname = getSelectedTableName(_form);
250: if (tname != null) {
251: _form.Run("select '" + tname + "', count(*) from "
252: + tname);
253: } else
254: _form.popup("Do select a table.");
255: }
256: }
257:
258: /** view distinct records in selected table for selected columns.
259: */
260: public static class ViewDistinctAction extends AbstractAction {
261:
262: public ViewDistinctAction(SQLForm form) {
263: super (viewDistinctAction);
264: this ._form = form;
265: putValue("accelerator", "control D");
266: }
267:
268: final SQLForm _form;
269:
270: public void actionPerformed(ActionEvent e) {
271: String tname = getSelectedTableName(_form);
272: Object[] o = _form.columnPanel.getSelectedValues();
273: String cnames = null;
274: if (o == null || o.length == 0)
275: cnames = " * ";
276: else
277: cnames = SQLForm.getDelimString(o, ",");
278: if (tname != null) {
279: _form.Run("select distinct " + cnames + " from "
280: + tname);
281: } else
282: _form.popup("Do select a table.");
283: }
284: }
285:
286: /** this action will substitute table and column names in the
287: * sqlpattern variable, and then execute the same.
288: * sample patterns are:
289: * set sqlpattern select $clist, count(*) from $tn group by $clist
290: * having count(*) > 1
291: * set sqlpattern alter table $tn add primary key ($clist)
292: *
293: */
294: public static class FormatSQLAction extends AbstractAction {
295: public FormatSQLAction(SQLForm form) {
296: super (formatSQLAction);
297: this ._form = form;
298: //putValue("accelerator","");
299: }
300:
301: final SQLForm _form;
302:
303: public void actionPerformed(ActionEvent e) {
304: String sqlpattern = null;
305: if ((sqlpattern = (String) _form.getAttribute("sqlpattern")) == null) {
306: sqlpattern = (String) _form
307: .getInput("Need a variable named sqlpattern to play with.");
308: if (sqlpattern == null)
309: return;
310: _form.setAttribute("sqlpattern", sqlpattern);
311: }
312: String[] commands = formatCommand(_form, sqlpattern);
313: executeCommand(_form, commands, true);
314: }
315: }
316:
317: public static void executeCommand(SQLForm _form, String[] commands,
318: boolean promptuser) {
319: if (commands == null)
320: return;
321: String tmpsql;
322: for (int i = 0; i < commands.length; i++) {
323: tmpsql = commands[i];
324: if (tmpsql == null)
325: continue;
326: _form.tp.appendInputArea(tmpsql);
327: if (promptuser) {
328: tmpsql = (String) JOptionPane.showInputDialog(null,
329: "Edit and execute the following command:",
330: "Format Pattern", JOptionPane.QUESTION_MESSAGE,
331: null, null, tmpsql);
332: // if user cancels it will be null
333: }
334: if (tmpsql != null)
335: _form.Run(tmpsql);
336: } // for
337: }
338:
339: /** should return a String[] so you can do what you want.
340: * selects should not be prmpted. Others should be.
341: */
342: public static String[] formatCommand(SQLForm _form,
343: String sqlpattern) {
344: if (sqlpattern == null) {
345: _form
346: .popup("Need a string with $tn and $clist or $cn to do magic with.");
347: return null;
348: }
349: String tname = getSelectedTableName(_form);
350: String cname[] = getSelectedColumnNames(_form);
351: if (tname == null || cname == null) {
352: _form
353: .popup("Please select a table and one or more columns.");
354: return null;
355: }
356: String clist = ArrayUtil.join(cname, ',');
357: ArrayList /*<String>*/commands = new ArrayList(12);
358:
359: /* if specific column then we want to iterate through each
360: * column */
361: if (sqlpattern.indexOf("$cn") != -1) {
362: for (int i = 0; i < cname.length; i++) {
363: String tmpsql = PerlWrapper.perlSubstitute("s/\\$tn/"
364: + tname + "/g", sqlpattern);
365: tmpsql = PerlWrapper.perlSubstitute("s/\\$cn/"
366: + cname[i] + "/g", tmpsql);
367: tmpsql = PerlWrapper.perlSubstitute("s/\\$clist/"
368: + clist + "/g", tmpsql);
369: if (tmpsql.indexOf("$dt") > -1) {
370: String dt = _form.myjdbc.getFormattedDatatypeFor(
371: tname, cname[i]);
372: if (dt != null)
373: tmpsql = PerlWrapper.perlSubstitute("s/\\$dt/"
374: + dt + "/g", tmpsql);
375:
376: }
377:
378: commands.add(tmpsql);
379: // append to input are instead
380: // _form.tp.appendInputArea("\n");
381: // _form.tp.appendInputArea(tmpsql);
382: }
383: // _form.setErrorArea('\n'+ "See Input Area for generated statements.");
384: //_form.tp.makeInputAreaVisible();
385: if (commands == null)
386: return null;
387: return ArrayUtil.toStringArray(commands);
388: }
389: if (sqlpattern.indexOf("$tn") != -1) {
390: //_form.tp.makeOutputAreaVisible();
391: String tmpsql = PerlWrapper.perlSubstitute("s/\\$tn/"
392: + tname + "/g", sqlpattern);
393: tmpsql = PerlWrapper.perlSubstitute("s/\\$clist/" + clist
394: + "/g", tmpsql);
395:
396: _form.setErrorArea('\n' + tmpsql);
397: //_form.tp.appendInputArea(tmpsql);
398: commands.add(tmpsql);
399: return ArrayUtil.toStringArray(commands);
400: } else {
401: System.out
402: .println("SQLPATTERN doesnt contain $tn or $clist :"
403: + sqlpattern);
404: _form.setErrorArea('\n' + sqlpattern);
405: }
406: return null;
407: }
408:
409: public static String[] getSelectedColumnNames(SQLForm _form) {
410: Object[] o = _form.columnPanel.getSelectedValues();
411: String ret[] = new String[o.length];
412: for (int i = 0; i < ret.length; i++) {
413: ret[i] = o[i].toString();
414: }
415: return ret;
416: }
417:
418: } //class
|