Class SqlScriptRunner


  • public class SqlScriptRunner
    extends Object
    Class used to execute SQL commands from a script file
    • Field Detail

      • delimP

        public static final Pattern delimP
        regex to detect delimiter. ignores spaces, allows delimiter in comment, allows an equals-sign
    • Constructor Detail

      • SqlScriptRunner

        public SqlScriptRunner()
        Default constructor
    • Method Detail

      • setDelimiter

        public void setDelimiter​(String delimiter,
                                 boolean fullLineDelimiter)
      • runScript

        public static void runScript​(Connection connection,
                                     Reader reader)
        Runs an SQL script (read in using the Reader parameter)
        Parameters:
        reader - - the source of the script
      • runScriptFromResourceFile

        public static void runScriptFromResourceFile​(Connection connection,
                                                     String resourcePath)
      • execInsert

        public static Map<Long,​Long> execInsert​(Connection conn,
                                                      List<InsertionDto> insertions)
                                               throws SQLException
        Execute the different SQL insertions. Those can refer to each other via foreign keys, even in the case of auto-generated ids
        Parameters:
        conn - a JDBC connection to the database
        insertions - the SQL insertions to execute
        Returns:
        a map from InsertionDto id to id of auto-generated primary keys in the database (if any was generated). If an InsertionDto has no id, we will not keep track of any auto-generated value for it.
        Throws:
        SQLException
      • execInsert

        public static Long execInsert​(Connection conn,
                                      String command)
                               throws SQLException
        Parameters:
        conn - a JDBC connection to the database
        command - the SQL insertion to execute
        Returns:
        a single id for the new row, if any was automatically generated, null otherwise. In other words, return the value of auto-generated primary key, if any was created.
        Throws:
        SQLException