001: /*
002: * Created on Sep 19, 2004
003: * Chris Taylor
004: */
005: package com.pk.script;
006:
007: import java.io.BufferedReader;
008: import java.io.BufferedWriter;
009: import java.io.File;
010: import java.io.FileNotFoundException;
011: import java.io.FileReader;
012: import java.io.FileWriter;
013: import java.io.IOException;
014: import java.sql.Connection;
015: import java.sql.SQLException;
016: import java.sql.Statement;
017: import java.util.ArrayList;
018: import java.util.Date;
019: import java.util.List;
020: import java.util.StringTokenizer;
021:
022: import com.pk.ConnectionInformation;
023: import com.pk.DatabaseDialect;
024:
025: /**
026: * @author Chris Taylor
027: *
028: */
029: public class Script {
030: public static final int NEW_COMMAND_INSERT_END_MODE = 0;
031: public static final int NEW_COMMAND_INSERT_BEGINNING_MODE = 1;
032: public static final int NEW_COMMAND_INSERT_AT_CURSER_MODE = 2;
033: public static final int NEW_COMMAND_REPLACE_CURRENT_SCRIPT_MODE = 3;
034:
035: private List commands = null;
036: private Date startDate = null;
037: private Date endDate = null;
038: private ConnectionInformation connectionInformation = null;
039: private Connection connection = null;
040:
041: /**
042: * @return Returns the commands.
043: */
044: public List getCommands() {
045: return commands;
046: }
047:
048: /**
049: * @param commands The commands to set.
050: */
051: public void setCommands(List commands) {
052: this .commands = commands;
053: }
054:
055: /**
056: * @return Returns the endDate.
057: */
058: public Date getEndDate() {
059: return endDate;
060: }
061:
062: /**
063: * @param endDate The endDate to set.
064: */
065: public void setEndDate(Date endDate) {
066: this .endDate = endDate;
067: }
068:
069: /**
070: * @return Returns the startDate.
071: */
072: public Date getStartDate() {
073: return startDate;
074: }
075:
076: /**
077: * @param startDate The startDate to set.
078: */
079: public void setStartDate(Date startDate) {
080: this .startDate = startDate;
081: }
082:
083: public int addCommand(String argCommand) {
084: if (commands == null) {
085: commands = new ArrayList();
086: }
087: commands.add(argCommand);
088: return commands.size() - 1;
089: }
090:
091: public void removeCmmand(int argIndex) {
092: if (commands == null)
093: throw new ArrayIndexOutOfBoundsException();
094: commands.remove(argIndex);
095: }
096:
097: public void clearCommands() {
098: if (commands != null) {
099: commands.clear();
100: }
101: startDate = null;
102: endDate = null;
103: }
104:
105: public void parseScriptFile(String argPath, String argDelimiter) {
106: if (argPath == null || argDelimiter == null) {
107: throw new IllegalArgumentException();
108: }
109:
110: FileReader tmpFile = null;
111: BufferedReader in = null;
112: StringBuffer fileString = new StringBuffer();
113: String tmp = null;
114: StringTokenizer tokenizer = null;
115: ScriptCommand scriptCommand = null;
116: String token = null;
117: try {
118: tmpFile = new FileReader(argPath);
119: in = new BufferedReader(tmpFile);
120: tmp = in.readLine();
121: while (tmp != null) {
122: fileString.append(tmp + "\n");
123: tmp = in.readLine();
124: }
125: tokenizer = new StringTokenizer(fileString.toString(),
126: argDelimiter, false);
127: commands = new ArrayList();
128: while (tokenizer.hasMoreTokens()) {
129: token = tokenizer.nextToken();
130: if (token.length() == 1
131: && Character
132: .isWhitespace(token.toCharArray()[0])) {
133: //if the only character is white space skip it.
134: continue;
135: }
136: scriptCommand = new ScriptCommand();
137: scriptCommand.setCommand(token);
138: commands.add(scriptCommand);
139: }
140:
141: } catch (FileNotFoundException e) {
142: // TODO Display error msg here.
143: e.printStackTrace();
144: } catch (IOException e) {
145: // TODO Auto-generated catch block
146: e.printStackTrace();
147: } finally {
148: if (in != null) {
149: try {
150: in.close();
151: } catch (IOException e1) {
152: }
153: }
154: }
155:
156: }
157:
158: public void parseScriptFromClipBoard(String argScriptText,
159: String argDelimiter, int insertMode, int argCurserPosition) {
160: String token = null;
161: StringTokenizer tokenizer = null;
162: ScriptCommand scriptCommand = null;
163: tokenizer = new StringTokenizer(argScriptText, argDelimiter,
164: false);
165: List newCommands = new ArrayList();
166: while (tokenizer.hasMoreTokens()) {
167: token = tokenizer.nextToken();
168: if (token.length() == 1
169: && Character.isWhitespace(token.toCharArray()[0])) {
170: //if the only character is white space skip it.
171: continue;
172: }
173: scriptCommand = new ScriptCommand();
174: scriptCommand.setCommand(token);
175: newCommands.add(scriptCommand);
176: }
177: if (commands == null
178: || insertMode == NEW_COMMAND_REPLACE_CURRENT_SCRIPT_MODE) {
179: commands = new ArrayList();
180: }
181:
182: if (insertMode == NEW_COMMAND_INSERT_BEGINNING_MODE) {
183: commands.addAll(0, newCommands);
184: } else if (insertMode == NEW_COMMAND_INSERT_AT_CURSER_MODE) {
185: if (argCurserPosition < 0) {
186: commands.addAll(0, newCommands);
187: } else {
188: commands.addAll(argCurserPosition, newCommands);
189: }
190: } else {
191: commands.addAll(newCommands);
192: }
193:
194: }
195:
196: public int getNumberOfCommands() {
197: if (commands == null) {
198: return 0;
199: }
200: return commands.size();
201: }
202:
203: public ScriptCommand getCommand(int argIndex) {
204: if (commands == null) {
205: throw new ArrayIndexOutOfBoundsException();
206: }
207: return (ScriptCommand) commands.get(argIndex);
208: }
209:
210: public void run() {
211: if (commands == null || connection == null) {
212: throw new IllegalStateException();
213: }
214: startDate = new Date();
215: ScriptCommand scriptCommand = null;
216: int size = commands.size();
217: for (int x = 0; x < size; x++) {
218: scriptCommand = (ScriptCommand) commands.get(x);
219: scriptCommand.setStatus(ScriptCommand.STATUS_NOT_RUN);
220: if (scriptCommand.isRun()) {
221: try {
222: prexecuteSQL(scriptCommand.getCommand());
223: scriptCommand
224: .setStatus(ScriptCommand.STATUS_SUCCESS);
225: scriptCommand.setErrorMessage(null);
226: } catch (SQLException ex) {
227: scriptCommand
228: .setStatus(ScriptCommand.STATUS_FAILED);
229: scriptCommand.setErrorMessage(ex.getMessage());
230: }
231: }
232: }
233: endDate = new Date();
234: }
235:
236: /**
237: * @return Returns the connection.
238: */
239: public Connection getConnection() {
240: return connection;
241: }
242:
243: /**
244: * @param connection The connection to set.
245: */
246: public void setConnection(Connection connection) {
247: this .connection = connection;
248: }
249:
250: /**
251: * @return Returns the connectionInformation.
252: */
253: public ConnectionInformation getConnectionInformation() {
254: return connectionInformation;
255: }
256:
257: /**
258: * @param connectionInformation The connectionInformation to set.
259: */
260: public void setConnectionInformation(
261: ConnectionInformation connectionInformation) {
262: this .connectionInformation = connectionInformation;
263: }
264:
265: public void prexecuteSQL(String argQuery) throws SQLException {
266: DatabaseDialect tmpDatabaseDialect = getConnectionInformation()
267: .getDatabaseDialect();
268:
269: argQuery = commentHandling(argQuery);
270:
271: argQuery = argQuery.trim();
272:
273: if (tmpDatabaseDialect.isSelectStatement(argQuery)) {
274: executeSQL(argQuery);
275: } else if (tmpDatabaseDialect.isDescribeStatement(argQuery)) {
276: //query = query.substring(5);
277: executeDescSQL(argQuery);
278: } else if (argQuery.toUpperCase().equals("COMMIT")) {
279: connection.commit();
280: } else if (argQuery.toUpperCase().equals("ROLLBACK")) {
281: connection.rollback();
282: } else {
283: updateSQL(argQuery);
284: }
285: }
286:
287: public String commentHandling(String query) {
288: query = query.trim();
289: int cmtAstStart = query.indexOf("/*");
290: int cmtDasStart = query.indexOf("--");
291: while (cmtAstStart != -1 || cmtDasStart != -1) {
292: if (cmtAstStart != -1) {
293: int cmtEnd = query.indexOf("*/");
294: StringBuffer queryStrBuffer = new StringBuffer(query);
295: queryStrBuffer = queryStrBuffer.delete(cmtAstStart,
296: cmtEnd + 2);
297: queryStrBuffer = queryStrBuffer.append(" ");
298: query = queryStrBuffer.toString();
299: }
300:
301: if (cmtDasStart != -1) {
302: int cmtEnd = query.indexOf("\n", cmtDasStart);
303:
304: StringBuffer queryStrBuffer = new StringBuffer(query);
305: queryStrBuffer = queryStrBuffer.delete(cmtDasStart,
306: cmtEnd);
307: queryStrBuffer = queryStrBuffer.append(" ");
308: query = queryStrBuffer.toString();
309: }
310: cmtAstStart = query.indexOf("/*");
311: cmtDasStart = query.indexOf("--");
312: }
313: return query;
314: }
315:
316: public void executeSQL(String argQuery) throws SQLException {
317:
318: if (argQuery == null) // If there's nothing we are done
319: return;
320: Statement statement = connection.createStatement();
321: statement.executeQuery(argQuery);
322:
323: }
324:
325: public void executeDescSQL(String argQuery) throws SQLException {
326:
327: ConnectionInformation tmpConnectionInformation = getConnectionInformation();
328: //String queryDesc = "SELECT COLUMN_NAME, DECODE(NULLABLE,'N', 'NOT NULL', 'Y', NULL) AS NULLABLE, " + "DATA_TYPE, DECODE(DATA_TYPE, 'VARCHAR2', TO_CHAR(DATA_LENGTH), 'NUMBER', " + "DECODE(DATA_SCALE,0,TO_CHAR(DATA_PRECISION),NULL,NULL,DATA_PRECISION||','||DATA_SCALE)) AS \"SIZE\" " + "FROM ALL_TAB_COLUMNS " + "WHERE TABLE_NAME = '" + tName + "'" + " ORDER BY COLUMN_ID";
329: String queryDesc = tmpConnectionInformation
330: .getDatabaseDialect().getDescribeSQLString(argQuery,
331: tmpConnectionInformation);
332:
333: if (queryDesc == null) // If there's nothing we are done
334: return;
335:
336: Statement statement = connection.createStatement();
337: statement.executeQuery(queryDesc);
338: }
339:
340: public void updateSQL(String argQuery) throws SQLException {
341:
342: if (argQuery == null) // If there's nothing we are done
343: return;
344: Statement statement = connection.createStatement();
345: statement.executeUpdate(argQuery);
346: }
347:
348: public void exportResultsDelimited(String argPath,
349: String argDelimiter, String argEncloseByChar) {
350: File tmpFile = new File(argPath);
351:
352: BufferedWriter out = null;
353: try {
354: out = new BufferedWriter(new FileWriter(tmpFile));
355: } catch (Exception ex) {
356: //TODO report error to user
357: ex.printStackTrace();
358: } finally {
359: if (out != null) {
360: try {
361: out.close();
362: } catch (IOException e) {
363: }
364: }
365: }
366: }
367:
368: }
|