View Javadoc

1   package org.controlhaus.hibernate.util;
2   
3   /*
4    * $Id: BatchSqlCommandRunner.java,v 1.2 2004/10/10 17:31:55 trygvis Exp $
5    * =======================================================================
6    * Copyright (c) 2002 Axion Development Team.  All rights reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   * 1. Redistributions of source code must retain the above
13   *    copyright notice, this list of conditions and the following
14   *    disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The names "Tigris", "Axion", nor the names of its contributors may
22   *    not be used to endorse or promote products derived from this
23   *    software without specific prior written permission.
24   *
25   * 4. Products derived from this software may not be called "Axion", nor
26   *    may "Tigris" or "Axion" appear in their names without specific prior
27   *    written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }