1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }