1 //
2 // Copyright (c) 2004, International Decision Systems
3 // all rights reserved
4 /*
5 * Copyright (c) 2004 International Decision Systems, Inc. All Rights Reserved.
6 *
7 * By using this Software, You acknowledge that the Software is a valuable asset
8 * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9 * third party supplier of IDSI and constitutes confidential and proprietary
10 * information.
11 *
12 * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13 * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14 * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15 * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16 * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17 * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18 * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20 * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21 */
22 package net.sourceforge.addam.ddlrun.runners;
23
24 import net.sourceforge.addam.ddlrun.grammars.ScriptGrammar;
25 import net.sourceforge.addam.ddlrun.utils.ResourceReader;
26 import net.sourceforge.addam.ddlrun.utils.ResourceReaderFactory;
27
28 /**
29 * Used to run a single SQL script.
30 *
31 * @author TIM3
32 * @since Mar 5, 2005
33 */
34 public class JDBCScriptRunner implements Runner {
35
36 /**
37 * Constructs a JDBCScriptRunner.
38 *
39 * @param readerFactory used to locate and read scripts
40 * @param grammar used to parse the SQL file
41 * @param statementRunner used to run individual statements
42 */
43 public JDBCScriptRunner(ResourceReaderFactory readerFactory, ScriptGrammar grammar,
44 Runner statementRunner) {
45 this.statementRunner = statementRunner;
46 this.readerFactory = readerFactory;
47 this.grammar = grammar;
48 }
49
50 // not very object-oriented... need to tease out some objects
51 public void run(String resource) throws Exception {
52 ResourceReader reader = readerFactory.getReader(resource);
53 StringBuffer buf = new StringBuffer();
54 boolean simpleStatement = false;
55 boolean complexStatement = false;
56 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
57 if (simpleStatement) {
58 // append the new line (make sure to include \n because the terminator might want it)
59 if (buf.length() > 0) {
60 buf.append("\n");
61 }
62 buf.append(line);
63 // check to see if this line ends with the terminator
64 if (grammar.hasSimpleStatementTerminator(buf.toString())) {
65 String bufferString = grammar.removeSimpleStatementTerminator(buf.toString().trim());
66 runStatement(bufferString);
67 simpleStatement = false;
68 buf.setLength(0);
69 continue;
70 }
71 } else if (complexStatement) {
72 // append the new line (make sure to include \n because the terminator might want it)
73 if (buf.length() > 0) {
74 buf.append("\n");
75 }
76 buf.append(line);
77 if (grammar.hasComplexStatementTerminator(buf.toString())) {
78 String bufferString = grammar.removeComplexStatementTerminator(buf.toString().trim());
79 runStatement(bufferString);
80 simpleStatement = false;
81 complexStatement = false;
82 buf.setLength(0);
83 }
84 } else {
85 // ignore empty lines outside statements
86 // look for comment
87 if (grammar.shouldIgnore(line)) {
88 // skip this line
89 continue;
90 }
91
92 // look for start of new multi-line statement (keys are originators)
93 if (grammar.beginsComplexStatement(line)) {
94 complexStatement = true;
95 buf.append(line);
96 continue;
97 }
98
99 // see if this line is a statement all in itself
100 if (grammar.hasSimpleStatementTerminator(line)) {
101 line = grammar.removeSimpleStatementTerminator(line);
102 runStatement(line);
103 continue;
104 }
105
106 // this is a multiline simple statement
107 simpleStatement = true;
108 buf.append(line);
109 }
110 }
111 }
112
113 private void runStatement(String statement) throws Exception {
114 try {
115 if (statement.length() > 0) {
116 statementRunner.run(statement);
117 }
118 } catch (Exception e) {
119 System.err.println("failed to execute:\n" + statement);
120 throw e;
121 }
122 }
123
124 private final ResourceReaderFactory readerFactory;
125 private final ScriptGrammar grammar;
126 private final Runner statementRunner;
127 }