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.JTextArea;
019: import javax.swing.table.TableModel;
020: import java.awt.Font;
021: import java.util.*;
022: import java.sql.SQLException;
023: import java.io.FileNotFoundException;
024:
025: /**
026: * The command object associated with the batch command, used to
027: * execute a batch file.
028: * @author rahul kumar <rahul_kumar@yahoo.com>
029: * @see XXX
030: */
031:
032: public class BatchCommand implements Command {
033:
034: /** ctor/constructor.
035: */
036: public BatchCommand() {
037: }
038:
039: public String[] getCommandList() {
040: return commands;
041: }
042:
043: public void execute(SQLForm form, String command, String SQLString) {
044: _form = form;
045: SQLTabbedPane tp = _form.tp;
046: myjdbc = _form.myjdbc;
047:
048: int[] iaResult = runBatch(SQLString.substring(1));
049: final String rowsp = " rows processed.\n";
050: StringBuffer sb1 = new StringBuffer(iaResult.length
051: * rowsp.length());
052: int ialen = iaResult.length;
053: for (int i = 0; i < ialen; i++) {
054: //if (i < ialen - 1 || (i == ialen-1 && iaResult[i]>0))
055: sb1.append(iaResult[i] + rowsp);
056: }
057: tp.appendOutputArea('\n' + sb1.toString());
058: tp.makeOutputAreaVisible();
059:
060: }
061:
062: /** execute a batch file */
063: public int[] runBatch(String filename) {
064: //String batchsep = (String) getAttribute("batchsep",";\n")
065: String batchsep = null;
066: if ((batchsep = (String) _form.getAttribute("batchsep")) == null) {
067: System.err
068: .println("Using ';\\n' as batch separator. Pls change using set batchsep xxx. You may also use '\\n/'.");
069: _form.setAttribute("batchsep", ";\n");
070: batchsep = ";\n";
071: }
072: try {
073: String content = IsqlUtil.getFileContents(filename);
074: if (content.indexOf(batchsep) == -1) {
075: System.err
076: .println("Couldn't find batchsep in contents!");
077: return null;
078: }
079: //String[] batch = ArrayUtil.split (content, ';');
080: //String[] batch = ArrayUtil.split (content, batchsep);
081: ArrayList al = new ArrayList(16);
082: PerlWrapper.perlSplit(al, '/' + batchsep + '/', content);
083: if (((String) al.get(al.size() - 1)).trim().length() == 0)
084: al.remove(al.size() - 1);
085: String[] batch = new String[al.size()];
086: al.toArray(batch);
087:
088: if ("on".equalsIgnoreCase((String) _form.getAttribute(
089: "debugging", "off")))
090: System.err.println("Batch consists of:" + batch.length
091: + " statements.");
092:
093: if (batch.length > 0)
094: return myjdbc.runBatch(batch);
095: else
096: System.err.println("Empty batch:" + content);
097: } catch (SQLException exc) {
098: System.err.println("SQL869:" + exc.toString());
099: } catch (FileNotFoundException exc) {
100: System.err.println("FNF870:" + exc.toString());
101: } catch (java.io.IOException exc) {
102: System.err.println("io870:" + exc.toString());
103: } catch (Exception exc) {
104: System.err.println("880:" + exc.toString());
105: }
106: return null;
107:
108: }
109:
110: public String getUsage(String s) {
111: return usage;
112: }
113:
114: String commands[] = { "@", "batch" };
115: SQLForm _form;
116: SQLJDBC myjdbc;
117:
118: final String usage = "@<batchfile>";
119:
120: ////// START INSTANCE VARIABLES //////
121:
122: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
123: static final String P = "BatchCommand"; // used in exception strings
124:
125: } // end of class
|