Package org.evomaster.client.java.sql
Class SqlScriptRunner
java.lang.Object
org.evomaster.client.java.sql.SqlScriptRunner
Class used to execute SQL commands from a script file
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic QueryResultexecCommand(Connection conn, String command) static LongexecInsert(Connection conn, String command) Executes the given SQL insertion command.static InsertionResultsDtoexecInsert(Connection conn, List<InsertionDto> insertions, InsertionResultsDto... previous) Execute the different SQL insertions.static voidexecScript(Connection conn, String script) this is used for mysql which cannot execute multiple statements at one time.static voidexecScript(Connection conn, String script, List<String> tablesToInsert) extractSql(String script) extractSqlTableMap(List<String> commands) extract a map from table name to a list of SQL INSERT or Update commands for initializing data into the tablereadCommands(Reader reader) readSQLCommandsAsString(Reader reader) static voidrunCommands(Connection connection, List<String> commands) static voidrunScript(Connection connection, Reader reader) Runs an SQL script (read in using the Reader parameter).static voidrunScriptFromResourceFile(Connection connection, String resourcePath) voidsetDelimiter(String delimiter, boolean fullLineDelimiter)
-
Field Details
-
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
-
runScript
Runs an SQL script (read in using the Reader parameter).- Parameters:
connection- the database connection to use for executing the scriptreader- the source of the script- Throws:
NullPointerException- if the reader is null
-
runScriptFromResourceFile
-
runCommands
-
readSQLCommandsAsString
- Parameters:
reader- for SQL script- Returns:
- extract SQL commands
-
readCommands
-
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 databaseinsertions- the SQL insertions to executeprevious- 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
Executes the given SQL insertion command.- Parameters:
conn- a JDBC connection to the databasecommand- the SQL insertion to execute- Returns:
- a single ID for the new row, if any was automatically generated,
nullotherwise. 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
this is used for mysql which cannot execute multiple statements at one time.- Parameters:
conn- a connection to dbscript- 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 dbscript- represents a sql scripttablesToInsert- 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
- Parameters:
script- is a SQL script- Returns:
- a list of SQL commands based on the script
-
extractSqlTableMap
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
- Throws:
SQLException
-