View Javadoc

1   package com.explosion.expfmodules.dbstore;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.sql.Connection;
27  import java.sql.SQLException;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  import org.apache.log4j.LogManager;
32  import org.apache.log4j.Logger;
33  import com.explosion.expfmodules.rdbmsconn.dbom.DataSet;
34  import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
35  import com.explosion.utilities.exception.ExceptionManagerFactory;
36  
37  /***
38   * @author Stephen Cowx
39   * Date created:@14-Feb-2003
40   * Constructs tables if they don't exist
41   */
42  public class ConstructTables 
43  {
44    private Connection conn;
45    private DataSet set;
46    private InputStream stream;
47    private static Logger log = LogManager.getLogger(ConstructTables.class);
48    
49  
50    /***
51     * Constructor for RefreshTopNodesProcess.
52     */
53    public ConstructTables(Connection conn, InputStream stream)
54    {
55      this.conn = conn;
56      this.stream = stream;
57    }
58  
59    public void process()
60    {
61      try
62      {
63        List statements = load(stream);
64  
65        Iterator it = statements.iterator();
66        int numberOfElements = statements.size();
67  
68        while (it.hasNext())
69        {
70          try
71          {
72            String stmt = (String) it.next();
73            SQLEngine engine = new SQLEngine();
74            engine.executeCommand(conn, stmt, -1, null, false);
75            engine.close();
76          }
77          catch (java.sql.SQLException e)
78          {
79            log(e,"Error: " + e.getMessage());
80          }
81        }
82  
83        if (numberOfElements == 0)
84       	 log("Script file complete, no statements found in file.  (Each SQl statements int the file must end with a ';').");
85        else
86        {
87        	 log("Script file complete.");
88        }	 
89        
90        log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
91      }
92      catch (Exception e)
93      {
94        ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while running script.");
95      }
96    }
97  
98    public List load(InputStream stream) throws IOException, SQLException
99    {
100     BufferedReader reader = null;
101     ArrayList statements;
102     try
103     {
104       statements = new ArrayList();
105       reader = new BufferedReader(new InputStreamReader(stream));
106       StringBuffer statement = new StringBuffer();
107       String line = new String();
108       
109       while (true)
110       {
111         line = reader.readLine();
112       
113         if (line == null)
114           break;
115       
116         else
117         {
118       
119           if (line.endsWith(";"))
120           {
121       
122             statement.append(line.trim().substring(0, line.trim().length() - 1));
123             statements.add(statement.toString());
124             statement.delete(0, statement.length());
125           }
126           else
127           {
128             statement.append(line.trim());
129           }
130         }
131       }
132     }
133     finally
134     {
135     	if (reader != null)
136     	{
137     	    try
138 	        {
139 	          reader.close();
140 	        }
141 	        catch (IOException e)
142 	        {
143 	        	 e.printStackTrace();
144 	        }
145     	}
146     }
147 
148     return statements;
149 
150   }
151 
152   public void log(String string)
153   {
154   	log.debug(string);
155   }
156 
157   public void log(Exception exception, String message)
158   {
159   	log.error(message + exception.getMessage(), exception);
160   }
161 
162 }