1 package org.controlhaus.hibernate.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.io.BufferedReader;
44 import java.io.FileInputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.sql.Connection;
49 import java.sql.SQLException;
50 import java.sql.Statement;
51
52 /***
53 * @author Chuck Burdick
54 * @author Jim Burke
55 */
56 public class BatchSqlCommandRunner
57 {
58 private StringBuffer _buf = new StringBuffer();
59 private Statement _stmt = null;
60
61 public BatchSqlCommandRunner(Statement stmt)
62 {
63 _stmt = stmt;
64 }
65
66 public BatchSqlCommandRunner(Connection conn) throws SQLException
67 {
68 this(conn.createStatement());
69 }
70
71 public void close()
72 {
73 try
74 {
75 _stmt.close();
76 }
77 catch (Exception e)
78 {
79 }
80 }
81
82 protected String readLine(BufferedReader reader) throws IOException
83 {
84 String result = reader.readLine();
85 if (result != null)
86 {
87 result.trim();
88 }
89 return result;
90 }
91
92 protected String readCommand(BufferedReader reader) throws IOException
93 {
94 _buf.setLength(0);
95 String line = null;
96 boolean done = false;
97 boolean inQuote = false;
98 while (!done && (line = readLine(reader)) != null)
99 {
100 if (line.indexOf("/*") == -1 &&
101 line.indexOf("#") == -1 &&
102 line.indexOf("--") == -1 )
103 {
104 _buf.append(line);
105 _buf.append(' ');
106 inQuote = isInQuotes(line, inQuote);
107 done = (!inQuote && line.trim().endsWith(";"));
108 }
109 }
110 return _buf.toString().trim();
111 }
112
113 /***
114 * loop through all the quotes in the line to see if we are within
115 * a string literal
116 */
117 protected boolean isInQuotes(String line, boolean inQuotes)
118 {
119 boolean result = inQuotes;
120 int quotePos = -1;
121 int startPos = 0;
122 while ((quotePos = line.indexOf("'", startPos)) > -1)
123 {
124 result = !result;
125 startPos = quotePos + 1;
126 }
127 return result;
128 }
129
130 public void runCommands(String resourceLoc)
131 throws IOException, SQLException
132 {
133 ClassLoader loader = getClass().getClassLoader();
134 InputStream in = loader.getResourceAsStream(resourceLoc);
135 if (in == null)
136 {
137 in = new FileInputStream( resourceLoc );
138 }
139
140 if ( in == null )
141 {
142 System.out.println(
143 "Could not find resource " + resourceLoc + " in classpath" );
144 }
145 else
146 {
147 runCommands(in);
148 }
149 }
150
151 public void runCommands(InputStream in) throws IOException, SQLException
152 {
153 BufferedReader reader = null;
154 try
155 {
156 reader = new BufferedReader(new InputStreamReader(in));
157 runCommands(reader);
158 }
159 finally
160 {
161 in.close();
162 }
163 }
164
165 public void runCommands(BufferedReader reader)
166 throws IOException, SQLException
167 {
168 try
169 {
170 String cmd = null;
171 while (!(cmd = readCommand(reader)).equals(""))
172 {
173 System.out.println("executing cmd " + cmd );
174 _stmt.execute(cmd);
175 }
176 }
177 finally
178 {
179 reader.close();
180 }
181 }
182 }