//# 0 errors, 154 messages
//#
/*
    //#SQLScriptRunner.java:1:1: class: org.apache.roller.weblogger.business.startup.SQLScriptRunner
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.business.startup;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * SQL script runner, parses script and allows you to run it. 
 * You can run the script multiple times if necessary.
 * Assumes that anything on an input line after "--" or ";" can be ignored.
 */
public class SQLScriptRunner {
    
    private static Log   log = LogFactory.getLog(SQLScriptRunner.class);
    //#SQLScriptRunner.java:44: method: org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init
    //#SQLScriptRunner.java:44: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.startup.SQLScriptRunner
    //#    method: org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Descendant_Table[org/apache/roller/weblogger/business/startup/SQLScriptRunner]
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.errorMessage(Ljava/lang/String;)V
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getCommandCount()I
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getErrors()Z
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getFailed()Z
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getMessages()Ljava/util/List;
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.runScript(Ljava/sql/Connection;Z)V
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.successMessage(Ljava/lang/String;)V
    //#output(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): log
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Descendant_Table[org/apache/roller/weblogger/business/startup/SQLScriptRunner] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.errorMessage(Ljava/lang/String;)V == &errorMessage
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getCommandCount()I == &getCommandCount
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getErrors()Z == &getErrors
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getFailed()Z == &getFailed
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.getMessages()Ljava/util/List; == &getMessages
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.runScript(Ljava/sql/Connection;Z)V == &runScript
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): __Dispatch_Table.successMessage(Ljava/lang/String;)V == &successMessage
    //#post(org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init): init'ed(log)
    //#SQLScriptRunner.java:44: end of method: org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner__static_init
    
    private List<String> commands = new ArrayList<String>();
    private List<String> messages = new ArrayList<String>();
    private boolean      failed = false;
    private boolean      errors = false;
        
    
    /** Creates a new instance of SQLScriptRunner */
    public SQLScriptRunner(InputStream is) throws IOException {
    //#SQLScriptRunner.java:53: method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): " "._tainted
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): ""._tainted
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): is
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): log
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#1) num objects
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#2) num objects
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.commands
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.errors
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.failed
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.messages
    //#new obj(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#1)
    //#new obj(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#2)
    //#pre[2] (void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): log != null
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.commands == &new ArrayList(SQLScriptRunner#1)
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.errors == 0
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.failed == 0
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): this.messages == &new ArrayList(SQLScriptRunner#2)
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#1) num objects == 1
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): new ArrayList(SQLScriptRunner#2) num objects == 1
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): Effects-of-calling:java.util.List:add
    //#test_vector(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): java.io.BufferedReader:readLine(...)@60: Addr_Set{null}, Inverse{null}
    //#test_vector(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): java.lang.String:endsWith(...)@73: {0}, {1}
    //#test_vector(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): java.lang.String:indexOf(...)@66: {-2_147_483_648..0}, {1..4_294_967_295}
    //#test_vector(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)): java.lang.String:startsWith(...)@64: {1}, {0}
        
        log.debug("instantiated");
    //#SQLScriptRunner.java:55: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.startup.SQLScriptRunner
    //#    method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String command = ""; 
        String line;
        while ((line = in.readLine()) != null) {
            line = line.trim();
            log.debug(line);
    //#SQLScriptRunner.java:62: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.startup.SQLScriptRunner
    //#    method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
            
            if (!line.startsWith("--")) { // ignore lines starting with "--"    
                
                if (line.indexOf("--") > 0) {
                    // trim comment off end of line
                    line = line.substring(0, line.indexOf("--")).trim();
                }
                
                // add line to current command
                command += line.trim();
                if (command.endsWith(";")) { 
                    // ";" is end of command, so add completed command to list
                    commands.add(command.substring(0, command.length() - 1));   
                    command = "";
                } else {
                    command += " "; // still more command coming so add space
                }
            } else {
                successMessage(line);
            }
        }
        in.close();    
    }
    //#SQLScriptRunner.java:85: end of method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner(InputStream)
    
    
    /** Creates a new instance of SQLScriptRunner */
    public SQLScriptRunner(String scriptPath) throws IOException {
        this(new FileInputStream(scriptPath));
    //#SQLScriptRunner.java:90: method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): " "._tainted
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): ""._tainted
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): log
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): scriptPath
    //#input(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#1) num objects
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#2) num objects
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.commands
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.errors
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.failed
    //#output(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.messages
    //#new obj(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#1)
    //#new obj(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#2)
    //#pre[1] (void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): log != null
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.commands == &new ArrayList(SQLScriptRunner#1)
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.errors == 0
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.failed == 0
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): this.messages == &new ArrayList(SQLScriptRunner#2)
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#1) num objects == 1
    //#post(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): new ArrayList(SQLScriptRunner#2) num objects == 1
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.util.List:add
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.util.ArrayList
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:org.apache.commons.logging.Log:debug
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.io.InputStreamReader
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.io.BufferedReader
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.io.BufferedReader:readLine
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:trim
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:startsWith
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:indexOf
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:substring
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:endsWith
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.lang.String:length
    //#unanalyzed(void org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)): Effects-of-calling:java.io.BufferedReader:close
    }
    //#SQLScriptRunner.java:91: end of method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.org.apache.roller.weblogger.business.startup.SQLScriptRunner(String)
    
    
    /** Number of SQL commands in script */
    public int getCommandCount() {
        return commands.size();
    //#SQLScriptRunner.java:96: method: int org.apache.roller.weblogger.business.startup.SQLScriptRunner.getCommandCount()
    //#input(int getCommandCount()): this
    //#input(int getCommandCount()): this.commands
    //#output(int getCommandCount()): return_value
    //#pre[2] (int getCommandCount()): this.commands != null
    //#post(int getCommandCount()): init'ed(return_value)
    //#SQLScriptRunner.java:96: end of method: int org.apache.roller.weblogger.business.startup.SQLScriptRunner.getCommandCount()
    }
    
    
    /** Return messages from last run of script, empty if no previous run */
    public List<String> getMessages() {
        return messages;
    //#SQLScriptRunner.java:102: method: List org.apache.roller.weblogger.business.startup.SQLScriptRunner.getMessages()
    //#input(List getMessages()): this
    //#input(List getMessages()): this.messages
    //#output(List getMessages()): return_value
    //#pre[2] (List getMessages()): init'ed(this.messages)
    //#post(List getMessages()): return_value == this.messages
    //#post(List getMessages()): init'ed(return_value)
    //#SQLScriptRunner.java:102: end of method: List org.apache.roller.weblogger.business.startup.SQLScriptRunner.getMessages()
    }
    
    
    /** Returns true if last call to runScript() threw an exception */
    public boolean getFailed() {
        return failed;
    //#SQLScriptRunner.java:108: method: bool org.apache.roller.weblogger.business.startup.SQLScriptRunner.getFailed()
    //#input(bool getFailed()): this
    //#input(bool getFailed()): this.failed
    //#output(bool getFailed()): return_value
    //#pre[2] (bool getFailed()): init'ed(this.failed)
    //#post(bool getFailed()): return_value == this.failed
    //#post(bool getFailed()): init'ed(return_value)
    //#SQLScriptRunner.java:108: end of method: bool org.apache.roller.weblogger.business.startup.SQLScriptRunner.getFailed()
    }
    
    
    /** Returns true if last run had any errors */
    public boolean getErrors() {
        return errors;
    //#SQLScriptRunner.java:114: method: bool org.apache.roller.weblogger.business.startup.SQLScriptRunner.getErrors()
    //#input(bool getErrors()): this
    //#input(bool getErrors()): this.errors
    //#output(bool getErrors()): return_value
    //#pre[2] (bool getErrors()): init'ed(this.errors)
    //#post(bool getErrors()): return_value == this.errors
    //#post(bool getErrors()): init'ed(return_value)
    //#SQLScriptRunner.java:114: end of method: bool org.apache.roller.weblogger.business.startup.SQLScriptRunner.getErrors()
    }
    
    
    /** Run script, logs messages, and optionally throws exception on error */
    public void runScript(
            Connection con, boolean stopOnError) throws SQLException {
        failed = false;
    //#SQLScriptRunner.java:121: method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.runScript(Connection, bool)
    //#input(void runScript(Connection, bool)): "ERROR: SQLException executing SQL ["._tainted
    //#input(void runScript(Connection, bool)): "] : "._tainted
    //#input(void runScript(Connection, bool)): con
    //#input(void runScript(Connection, bool)): stopOnError
    //#input(void runScript(Connection, bool)): this
    //#input(void runScript(Connection, bool)): this.commands
    //#input(void runScript(Connection, bool)): this.messages
    //#output(void runScript(Connection, bool)): this.errors
    //#output(void runScript(Connection, bool)): this.failed
    //#pre[4] (void runScript(Connection, bool)): this.commands != null
    //#pre[1] (void runScript(Connection, bool)): (soft) con != null
    //#pre[2] (void runScript(Connection, bool)): (soft) stopOnError == 0
    //#pre[5] (void runScript(Connection, bool)): (soft) this.messages != null
    //#presumption(void runScript(Connection, bool)): java.sql.Connection:createStatement(...)@127 != null
    //#post(void runScript(Connection, bool)): this.errors == 0
    //#post(void runScript(Connection, bool)): this.failed == 0
    //#unanalyzed(void runScript(Connection, bool)): Effects-of-calling:java.util.List:add
    //#test_vector(void runScript(Connection, bool)): java.sql.Connection:getAutoCommit(...)@129: {1}, {0}
    //#test_vector(void runScript(Connection, bool)): java.util.Iterator:hasNext(...)@123: {0}, {1}
        errors = false;
        for (String command : commands) {
            
            // run each command
            try {
                Statement stmt = con.createStatement();
                stmt.executeUpdate(command);
                if (!con.getAutoCommit()) con.commit();
                
                // on success, echo command to messages
                successMessage(command);
                
            } catch (SQLException ex) {
                // add error message with text of SQL command to messages
                errorMessage("ERROR: SQLException executing SQL [" + command 
                        + "] : " + ex.getLocalizedMessage());
                // add stack trace to messages
                StringWriter sw = new StringWriter();
                ex.printStackTrace(new PrintWriter(sw));
                errorMessage(sw.toString());
                if (stopOnError) {
                    failed = true;
                    throw ex;
                }
            }
        }
    }
    //#SQLScriptRunner.java:148: end of method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.runScript(Connection, bool)
    
    
    private void errorMessage(String msg) {
        messages.add(msg);
    //#SQLScriptRunner.java:152: method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.errorMessage(String)
    //#input(void errorMessage(String)): msg
    //#input(void errorMessage(String)): this
    //#input(void errorMessage(String)): this.messages
    //#pre[3] (void errorMessage(String)): this.messages != null
    }    
    //#SQLScriptRunner.java:153: end of method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.errorMessage(String)
    
    
    private void successMessage(String msg) {
        messages.add(msg);
    //#SQLScriptRunner.java:157: method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.successMessage(String)
    //#input(void successMessage(String)): msg
    //#input(void successMessage(String)): this
    //#input(void successMessage(String)): this.messages
    //#pre[3] (void successMessage(String)): this.messages != null
    }
    //#SQLScriptRunner.java:158: end of method: void org.apache.roller.weblogger.business.startup.SQLScriptRunner.successMessage(String)
    
}
    //#SQLScriptRunner.java:: end of class: org.apache.roller.weblogger.business.startup.SQLScriptRunner
