View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.tools.statementExecutorUtils;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.util.ArrayList;
25  
26  /***
27   * This class loads a .sql file and executes the queries in this file
28   *
29   * @author  Freek Punt
30   * @author Hillebrand Gelderblom
31   */
32  
33  public class SqlScriptReader {
34     /*** Loads the scriptfile
35      * @param is InputSteam of the sql script file.
36      * @throws IOException is thrown when something goes wrong.
37      * @return An arraylist of queries. Each query is a String object.
38      */
39     public static ArrayList read(InputStream is) throws IOException {
40        BufferedReader br = null;
41        ArrayList queries = new ArrayList();
42        try {
43           if (is == null) {
44              throw new RuntimeException("SQLScript not read");
45           }
46           br = new BufferedReader(new InputStreamReader(is));
47           String query = "";
48           String newLine;
49           while ((newLine = br.readLine()) != null) {
50              if (!newLine.startsWith("#") && !newLine.startsWith("//") && !newLine.startsWith("--")) {
51                 query += newLine;
52              }
53              if (newLine.endsWith(";")) {
54                 queries.add(query);
55                 query = "";
56              }
57           }
58        }
59        catch (Exception e) {
60           e.printStackTrace();
61        }
62        finally {
63           if (br != null)
64              br.close();
65        }
66        return queries;
67     }
68  }