Class SqlScriptRunner

java.lang.Object
org.evomaster.client.java.sql.SqlScriptRunner

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

    • delimP

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

    • SqlScriptRunner

      public SqlScriptRunner()
      Default constructor
  • Method Details

    • 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:
      connection - the database connection to use for executing the script
      reader - the source of the script
      Throws:
      NullPointerException - if the reader is null
    • runScriptFromResourceFile

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

      public static void runCommands(Connection connection, List<String> commands)
    • readSQLCommandsAsString

      public String readSQLCommandsAsString(Reader reader)
      Parameters:
      reader - for SQL script
      Returns:
      extract SQL commands
    • readCommands

      public List<String> readCommands(Reader reader)
    • execInsert

      public static InsertionResultsDto execInsert(Connection conn, List<InsertionDto> insertions, InsertionResultsDto... previous) throws SQLException
      Execute the different SQL insertions. These 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
      previous - the results of previously executed SQL insertions
      Returns:
      a map from InsertionDto ID to ID of auto-generated primary keys in the database (if any were generated). If an InsertionDto has no ID, we will not keep track of any auto-generated value for it.
      Throws:
      SQLException - if a database access error occurs
    • execInsert

      public static Long execInsert(Connection conn, String command) throws SQLException
      Executes the given SQL insertion command.
      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, returns the value of the auto-generated primary key, if any was created.
      Throws:
      SQLException - if a database access error occurs
    • execScript

      public static void execScript(Connection conn, String script) throws SQLException
      this is used for mysql which cannot execute multiple statements at one time.
      Parameters:
      conn - a connection to db
      script - represents a sql script
      Throws:
      SQLException - if the execution of the command fails
    • execScript

      public static void execScript(Connection conn, String script, List<String> tablesToInsert) throws SQLException
      Parameters:
      conn - a connection to db
      script - represents a sql script
      tablesToInsert - represents insertions are executed only on the tables in this list. insertions for other tables will be skipped. if it is null or empty, nothing will be inserted.
      Throws:
      SQLException - if the execution of the command fails
    • extractSql

      public static List<String> extractSql(String script)
      Parameters:
      script - is a SQL script
      Returns:
      a list of SQL commands based on the script
    • extractSqlTableMap

      public static Map<String,List<String>> extractSqlTableMap(List<String> commands)
      extract a map from table name to a list of SQL INSERT or Update commands for initializing data into the table
      Parameters:
      commands - a list of SQL commands to be extracted
      Returns:
      the map from table name (key) to a list of SQL INSERT commands (values) on the table
    • execCommand

      public static QueryResult execCommand(Connection conn, String command) throws SQLException
      Throws:
      SQLException