/* * 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. */ import java.sql.*; import java.io.*; /** * 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. */ class SQLScriptRunner { private List commands = new ArrayList(); private List messages = new ArrayList(); private boolean failed = false; private boolean errors = false; /** Creates a new instance of SQLScriptRunner */ public SQLScriptRunner(InputStream is) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(is)); String command = ""; String line; while ((line = br.readLine()) != null) { line = line.trim(); if (!line.startsWith("--") && !line.startsWith("/*")) { 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(";")) { if (!command.trim().startsWith("DROP") && !command.trim().startsWith("LOCK") && !command.trim().startsWith("UNLOCK")) { // ";" 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 } } } br.close(); } /** Creates a new instance of SQLScriptRunner */ public SQLScriptRunner(String scriptPath) throws IOException { this(new FileInputStream(scriptPath)); } /** Number of SQL commands in script */ public int getCommandCount() { return commands.size(); } /** Return messages from last run of script, empty if no previous run */ public List getMessages() { return messages; } /** Returns true if last call to runScript() threw an exception */ public boolean getFailed() { return failed; } /** Returns true if last run had any errors */ public boolean getErrors() { return errors; } /** Run script, logs messages, and optionally throws exception on error */ public void runScript( Connection con, boolean stopOnError) throws SQLException { failed = false; 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; } } } } private void errorMessage(String msg) { messages.add(msg); } private void successMessage(String msg) { messages.add(msg); } } ////////////////////////////////////////////////////////////////////////////////////////////////// if (args.length < 2) { println "USAGE: groovy runsql.gy "; return; } Properties props = new Properties(); props.load(new FileInputStream("roller-custom.properties")); println "Loaded Roller properties"; Class.forName(props.getProperty("database.jdbc.driverClass")); con = DriverManager.getConnection( props.getProperty("database.jdbc.connectionURL"), props.getProperty("database.jdbc.username"), props.getProperty("database.jdbc.password")); println "Created database connection"; runner = null; try { runner = new SQLScriptRunner(new FileInputStream(args[0])); runner.runScript(con, true); } catch (Exception e) { e.printStackTrace(); } runner.messages.each() { print it; }