Repository: marmotta Updated Branches: refs/heads/develop 1d2048394 -> 9e23c0c91
MARMOTTA-589: moved ScriptRunner.java into src/ext/java Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/df332e42 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/df332e42 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/df332e42 Branch: refs/heads/develop Commit: df332e42155ae5b4dd0278c01ac98b3f0e40d19e Parents: 1d20483 Author: Sergio Fernández <[email protected]> Authored: Mon May 2 12:56:24 2016 +0200 Committer: Sergio Fernández <[email protected]> Committed: Mon May 2 12:56:24 2016 +0200 ---------------------------------------------------------------------- libraries/kiwi/kiwi-triplestore/pom.xml | 16 + .../kiwi/persistence/util/ScriptRunner.java | 302 +++++++++++++++++++ .../kiwi/persistence/util/ScriptRunner.java | 302 ------------------- 3 files changed, 318 insertions(+), 302 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/df332e42/libraries/kiwi/kiwi-triplestore/pom.xml ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/pom.xml b/libraries/kiwi/kiwi-triplestore/pom.xml index b590209..a4ae217 100644 --- a/libraries/kiwi/kiwi-triplestore/pom.xml +++ b/libraries/kiwi/kiwi-triplestore/pom.xml @@ -54,6 +54,22 @@ </excludes> </configuration> </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>add-ext</id> + <phase>generate-sources</phase> + <goals><goal>add-source</goal></goals> + <configuration> + <sources> + <source>src/ext/java</source> + </sources> + </configuration> + </execution> + </executions> + </plugin> </plugins> </build> http://git-wip-us.apache.org/repos/asf/marmotta/blob/df332e42/libraries/kiwi/kiwi-triplestore/src/ext/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/ext/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java b/libraries/kiwi/kiwi-triplestore/src/ext/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java new file mode 100644 index 0000000..350d362 --- /dev/null +++ b/libraries/kiwi/kiwi-triplestore/src/ext/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. 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. + */ +package org.apache.marmotta.kiwi.persistence.util; + +/* + * Added additional null checks when closing the ResultSet and Statements. + * + * Thanks to pihug12 and Grzegorz Oledzki at stackoverflow.com + * http://stackoverflow.com/questions/5332149/jdbc-scriptrunner-java-lang-nullpointerexception?tab=active#tab-top + */ +/* + * Modified: Use logWriter in print(Object), JavaDoc comments, correct Typo. + */ +/* + * Modified by Pantelis Sopasakis <[email protected]> to take care of DELIMITER statements. This way you + * can execute scripts that contain some TRIGGER creation code. New version using REGEXPs! Latest + * modification: Cater for a NullPointerException while parsing. Date: Feb 16, 2011, 11:48 EET + */ +/* + * Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class from the iBATIS Apache + * project. Only removed dependency on Resource class and a constructor + */ +/* + * Adapted to Apache Marmotta: + * - redirect logging output to SLF4J logger + * + */ +/* + * Copyright 2004 Clinton Begin Licensed 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. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.Reader; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Tool to run database scripts. This version of the script can be found at + * https://gist.github.com/gists/831762/ + */ +public class ScriptRunner { + + private static Logger log = LoggerFactory.getLogger(ScriptRunner.class); + + private static final String DEFAULT_DELIMITER = ";"; + private static final String DELIMITER_LINE_REGEX = "(?i)DELIMITER.+"; + private static final String DELIMITER_LINE_SPLIT_REGEX = "(?i)DELIMITER"; + + private final Connection connection; + private final boolean stopOnError; + private final boolean autoCommit; + private String delimiter = DEFAULT_DELIMITER; + private boolean fullLineDelimiter = false; + + private StringBuilder logBuffer; + + /** + * Default constructor. + * + * @param connection + * @param autoCommit + * @param stopOnError + */ + public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnError) { + this.connection = connection; + this.autoCommit = autoCommit; + this.stopOnError = stopOnError; + } + + /** + * @param delimiter + * @param fullLineDelimiter + */ + public void setDelimiter(String delimiter, boolean fullLineDelimiter) { + this.delimiter = delimiter; + this.fullLineDelimiter = fullLineDelimiter; + } + + /** + + /** + * Runs an SQL script (read in using the Reader parameter). + * + * @param reader + * - the source of the script + * @throws SQLException + * if any SQL errors occur + * @throws IOException + * if there is an error reading from the Reader + */ + public void runScript(Reader reader) throws IOException, SQLException { + try { + boolean originalAutoCommit = connection.getAutoCommit(); + try { + if (originalAutoCommit != autoCommit) { + connection.setAutoCommit(autoCommit); + } + runScript(connection, reader); + } finally { + connection.setAutoCommit(originalAutoCommit); + } + } catch (IOException e) { + throw e; + } catch (SQLException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException("Error running script. Cause: " + e, e); + } + } + + /** + * Runs an SQL script (read in using the Reader parameter) using the connection passed in. + * + * @param conn + * - the connection to use for the script + * @param reader + * - the source of the script + * @throws SQLException + * if any SQL errors occur + * @throws IOException + * if there is an error reading from the Reader + */ + private void runScript(Connection conn, Reader reader) throws IOException, SQLException { + StringBuffer command = null; + try { + LineNumberReader lineReader = new LineNumberReader(reader); + String line = null; + while ((line = lineReader.readLine()) != null) { + if (command == null) { + command = new StringBuffer(); + } + String trimmedLine = line.trim(); + if (trimmedLine.startsWith("--")) { + println(trimmedLine); + } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { + // Do nothing + } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { + // Do nothing + } else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) + || fullLineDelimiter && trimmedLine.equals(getDelimiter())) { + + Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX); + Matcher matcher = pattern.matcher(trimmedLine); + if (matcher.matches()) { + setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), + fullLineDelimiter); + line = lineReader.readLine(); + if (line == null) { + break; + } + trimmedLine = line.trim(); + } + + command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); + command.append(" "); + Statement statement = conn.createStatement(); + + println(command); + + boolean hasResults = false; + if (stopOnError) { + hasResults = statement.execute(command.toString()); + } else { + try { + statement.execute(command.toString()); + } catch (SQLException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + } + } + + if (autoCommit && !conn.getAutoCommit()) { + conn.commit(); + } + + ResultSet rs = statement.getResultSet(); + if (hasResults && rs != null) { + ResultSetMetaData md = rs.getMetaData(); + int cols = md.getColumnCount(); + for (int i = 0; i < cols; i++) { + String name = md.getColumnLabel(i); + print(name + "\t"); + } + println(""); + while (rs.next()) { + for (int i = 1; i <= cols; i++) { + String value = rs.getString(i); + print(value + "\t"); + } + println(""); + } + } + + command = null; + try { + if (rs != null) { + rs.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + try { + if (statement != null) { + statement.close(); + } + } catch (Exception e) { + e.printStackTrace(); + // Ignore to workaround a bug in Jakarta DBCP + } + } else { + Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX); + Matcher matcher = pattern.matcher(trimmedLine); + if (matcher.matches()) { + setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), + fullLineDelimiter); + line = lineReader.readLine(); + if (line == null) { + break; + } + trimmedLine = line.trim(); + } + command.append(line); + command.append(" "); + } + } + if (!autoCommit) { + conn.commit(); + } + } catch (SQLException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + throw e; + } catch (IOException e) { + e.fillInStackTrace(); + printlnError("Error executing: " + command); + printlnError(e); + throw e; + } finally { + conn.rollback(); + flush(); + } + } + + private String getDelimiter() { + return delimiter; + } + + private void print(Object o) { + if(logBuffer == null) { + logBuffer = new StringBuilder(); + } + logBuffer.append(o.toString()); + } + + private void println(Object o) { + if(logBuffer != null) { + logBuffer.append(o.toString()); + log.debug("SQL: {}",logBuffer.toString()); + logBuffer = null; + } else { + log.debug("SQL: {}",o.toString()); + } + } + + private void printlnError(Object o) { + log.error(o.toString()); + } + + private void flush() { + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/df332e42/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java deleted file mode 100644 index 350d362..0000000 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/util/ScriptRunner.java +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. 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. - */ -package org.apache.marmotta.kiwi.persistence.util; - -/* - * Added additional null checks when closing the ResultSet and Statements. - * - * Thanks to pihug12 and Grzegorz Oledzki at stackoverflow.com - * http://stackoverflow.com/questions/5332149/jdbc-scriptrunner-java-lang-nullpointerexception?tab=active#tab-top - */ -/* - * Modified: Use logWriter in print(Object), JavaDoc comments, correct Typo. - */ -/* - * Modified by Pantelis Sopasakis <[email protected]> to take care of DELIMITER statements. This way you - * can execute scripts that contain some TRIGGER creation code. New version using REGEXPs! Latest - * modification: Cater for a NullPointerException while parsing. Date: Feb 16, 2011, 11:48 EET - */ -/* - * Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class from the iBATIS Apache - * project. Only removed dependency on Resource class and a constructor - */ -/* - * Adapted to Apache Marmotta: - * - redirect logging output to SLF4J logger - * - */ -/* - * Copyright 2004 Clinton Begin Licensed 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. - */ - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.LineNumberReader; -import java.io.Reader; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Tool to run database scripts. This version of the script can be found at - * https://gist.github.com/gists/831762/ - */ -public class ScriptRunner { - - private static Logger log = LoggerFactory.getLogger(ScriptRunner.class); - - private static final String DEFAULT_DELIMITER = ";"; - private static final String DELIMITER_LINE_REGEX = "(?i)DELIMITER.+"; - private static final String DELIMITER_LINE_SPLIT_REGEX = "(?i)DELIMITER"; - - private final Connection connection; - private final boolean stopOnError; - private final boolean autoCommit; - private String delimiter = DEFAULT_DELIMITER; - private boolean fullLineDelimiter = false; - - private StringBuilder logBuffer; - - /** - * Default constructor. - * - * @param connection - * @param autoCommit - * @param stopOnError - */ - public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnError) { - this.connection = connection; - this.autoCommit = autoCommit; - this.stopOnError = stopOnError; - } - - /** - * @param delimiter - * @param fullLineDelimiter - */ - public void setDelimiter(String delimiter, boolean fullLineDelimiter) { - this.delimiter = delimiter; - this.fullLineDelimiter = fullLineDelimiter; - } - - /** - - /** - * Runs an SQL script (read in using the Reader parameter). - * - * @param reader - * - the source of the script - * @throws SQLException - * if any SQL errors occur - * @throws IOException - * if there is an error reading from the Reader - */ - public void runScript(Reader reader) throws IOException, SQLException { - try { - boolean originalAutoCommit = connection.getAutoCommit(); - try { - if (originalAutoCommit != autoCommit) { - connection.setAutoCommit(autoCommit); - } - runScript(connection, reader); - } finally { - connection.setAutoCommit(originalAutoCommit); - } - } catch (IOException e) { - throw e; - } catch (SQLException e) { - throw e; - } catch (Exception e) { - throw new RuntimeException("Error running script. Cause: " + e, e); - } - } - - /** - * Runs an SQL script (read in using the Reader parameter) using the connection passed in. - * - * @param conn - * - the connection to use for the script - * @param reader - * - the source of the script - * @throws SQLException - * if any SQL errors occur - * @throws IOException - * if there is an error reading from the Reader - */ - private void runScript(Connection conn, Reader reader) throws IOException, SQLException { - StringBuffer command = null; - try { - LineNumberReader lineReader = new LineNumberReader(reader); - String line = null; - while ((line = lineReader.readLine()) != null) { - if (command == null) { - command = new StringBuffer(); - } - String trimmedLine = line.trim(); - if (trimmedLine.startsWith("--")) { - println(trimmedLine); - } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { - // Do nothing - } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { - // Do nothing - } else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) - || fullLineDelimiter && trimmedLine.equals(getDelimiter())) { - - Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX); - Matcher matcher = pattern.matcher(trimmedLine); - if (matcher.matches()) { - setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), - fullLineDelimiter); - line = lineReader.readLine(); - if (line == null) { - break; - } - trimmedLine = line.trim(); - } - - command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); - command.append(" "); - Statement statement = conn.createStatement(); - - println(command); - - boolean hasResults = false; - if (stopOnError) { - hasResults = statement.execute(command.toString()); - } else { - try { - statement.execute(command.toString()); - } catch (SQLException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - } - } - - if (autoCommit && !conn.getAutoCommit()) { - conn.commit(); - } - - ResultSet rs = statement.getResultSet(); - if (hasResults && rs != null) { - ResultSetMetaData md = rs.getMetaData(); - int cols = md.getColumnCount(); - for (int i = 0; i < cols; i++) { - String name = md.getColumnLabel(i); - print(name + "\t"); - } - println(""); - while (rs.next()) { - for (int i = 1; i <= cols; i++) { - String value = rs.getString(i); - print(value + "\t"); - } - println(""); - } - } - - command = null; - try { - if (rs != null) { - rs.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - try { - if (statement != null) { - statement.close(); - } - } catch (Exception e) { - e.printStackTrace(); - // Ignore to workaround a bug in Jakarta DBCP - } - } else { - Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX); - Matcher matcher = pattern.matcher(trimmedLine); - if (matcher.matches()) { - setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), - fullLineDelimiter); - line = lineReader.readLine(); - if (line == null) { - break; - } - trimmedLine = line.trim(); - } - command.append(line); - command.append(" "); - } - } - if (!autoCommit) { - conn.commit(); - } - } catch (SQLException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - throw e; - } catch (IOException e) { - e.fillInStackTrace(); - printlnError("Error executing: " + command); - printlnError(e); - throw e; - } finally { - conn.rollback(); - flush(); - } - } - - private String getDelimiter() { - return delimiter; - } - - private void print(Object o) { - if(logBuffer == null) { - logBuffer = new StringBuilder(); - } - logBuffer.append(o.toString()); - } - - private void println(Object o) { - if(logBuffer != null) { - logBuffer.append(o.toString()); - log.debug("SQL: {}",logBuffer.toString()); - logBuffer = null; - } else { - log.debug("SQL: {}",o.toString()); - } - } - - private void printlnError(Object o) { - log.error(o.toString()); - } - - private void flush() { - } -}
