AnyLogic
Expand
Font size

Database functions

Function Description
java.sql.Connection getDatabaseConnection() Returns connection to AnyLogic database.
DeleteQuery deleteFrom(com.querydsl.sql.RelationalPathBase<?> table) Returns DeleteQuery that allows to build queries by chaining calls.

table — table to delete
InsertQuery insertInto(com.querydsl.sql.RelationalPathBase<?> table) Returns InsertQuery that allows to build queries by chaining calls.

table — table to insert to
SelectQuery selectFrom(com.querydsl.sql.RelationalPathBase<?> table) Returns SelectQuery that allows to build queries by chaining calls.

table — table to select from
UpdateQuery update(com.querydsl.sql.RelationalPathBase<?> table) Returns UpdateQuery that allows to build queries by chaining calls.

table — table to update
java.sql.PreparedStatement prepareStatement(String sql, Object... params) Creates a PreparedStatement object (which may be used to execute INSERT, DELETE, and UPDATE statements in AnyLogic database) with the given SQL query string and fills in the given objects as parameters.
When the object is no longer needed, make sure to call the close() function on this object to terminate the database connection.

sql — a string containing the statement
params — array containing the statement parameters
int selectAndDoForEach(java.util.function.Consumer<ResultSet> action, String sql, Object... params) Performs some action with every result for the given sql and params. Returns the number of selected records.
Example: selectAndDoForEach( rs -> myCollection.add( rs.getString(1) ), ”SELECT name FROM parts_table WHERE priority = ?”, ”high” );

action — what to do with each record (use get... methods of the given result set)
sql — a string containing the select query
params — an array containing select query parameters
double[] selectArrayOfDouble(String sql, Object... params) Returns the array of double numbers from the first column returned by the given sql query and parameters. Given query must return a single column. An empty array is returned in case of no results.

sql — a string containing the select query
params — array containing select query parameters
int[] selectArrayOfInt(String sql, Object... params) Returns the array of int numbers from the first column returned by the given sql query and parameters. Given query must return a single column. An empty array is returned in case of no results.

sql — a string containing the select query
params — array containing select query parameters
boolean selectExists(String sql, Object... params) Returns true if the given sql query and parameters return at least one result. This function caches its results to speed up the default behavior. Use selectExists(false, sql, params) to get non-cached result.

sql — a string containing the select query
params — array containing select query parameters
boolean selectExists(boolean cached, String sql, Object... params) Returns true if the given sql query and parameters return at least one result.

cached — if true, then this function will try to use cached values to avoid database access
sql — a string containing the select query
params — array containing select query parameters
<T> T selectFirstValue(String sql, Object... params) Returns the first result for the given sql query and parameters or null if no result is found. This function caches its results to speed up the default behavior. Use selectFirstValue(false, sql, params) to get non-cached result.

sql — a string containing the select query
params — array containing select query parameters
<T> T selectFirstValue(boolean cached, String sql, Object... params) Returns the first result for the given sql query and parameters or null if no result is found.

cached — if true, then this function will try to use cached values to avoid database access
sql — a string containing the select query
params — array containing select query parameters
<T> T selectFirstValue(Class<T> returnType, String sql, Object... params) Returns the first result for the given sql query and parameters or null if no result is found. This function caches its results to speed up the default behavior. Use selectFirstValue(false, returnType, sql, params) to get non-cached result.



returnType — the type required to be returned by this function
sql — a string containing the select query
params — array containing select query parameters
<T> T selectFirstValue(boolean cached, Class<T> returnType, String sql, Object... params) Returns the first result for the given sql query and parameters or null if no result is found.

cached — if true, then this function will try to use cached values to avoid database access
returnType — the type required to be returned by this function
sql — a string containing the select query
params — array containing select query parameters
ResultSet selectResultSet(String sql, Object... params) Gets the results and returns the result set object for the given sql query and parameters.

sql — a string containing the select query
params — array containing select query parameters
TableFunction selectTableFunction(TableFunction tableFunction, String sql, Object... params) Executes the given SELECT query which should return data in 2 columns: the first column contains argument values; the second column contains corresponding values. Returns the given table function, for convenience.

tableFunction — the table function to fill
sql — a string containing the statement
params — array containing statement parameters
<T> T selectUniqueValue(String sql, Object... params) Returns a unique result for the given sql query and parameters. Returns the resulting value of column; throws RuntimeException if there are no matching results or more than one matching result. This function caches its results to speed up the default behavior. Use selectUniqueValue(false, sql, params) to get non-cached result.

sql — a string containing the select query
params — array containing select query parameters
<T> T selectUniqueValue(boolean cached, String sql, Object... params) Returns a unique result for the given sql query and parameters. Returns the resulting value of column; throws RuntimeException if there are no matching results or more than one matching result.

cached — if true, then this function will try to use cached values to avoid database access
sql — a string containing the select query
params — array containing select query parameters
<T> T selectUniqueValue(Class<T> returnType, String sql, Object... params) Returns an unique result for the given sql query and parameters. Returns the resulting value of column; throws RuntimeException if there are no matching results or more than one matching result. This function caches its results to speed up the default behavior. Use selectUniqueValue(false, returnType, sql, params) to get non-cached result.



returnType — the type required to be returned by this function
sql — a string containing the select query
params — array containing select query parameters
<T> T selectUniqueValue(boolean cached, Class<T> returnType, String sql, Object... params) Returns an unique result for the given sql query and parameters. Returns the resulting value of column; throws RuntimeException if there are no matching results or more than one matching result.

cached — if true, then this function will try to use cached values to avoid database access
returnType — the type required to be returned by this function
sql — a string containing the select query
params — array containing select query parameters
<T> List<T> selectValues(String sql, Object... params) Returns the results for the given sql query and parameters as a list. The query must return a single column. If there are no matching results, returns an empty list.

sql — a string containing the select query
params — array containing select query parameters
<T> List<T> selectValues(Class<T> returnType, String sql, Object... params) Returns the results for the given sql query and parameters as a list. The query must return a single column. If there are no matching results, returns an empty list.

returnType — required type of elements to be returned by this function
sql — a string containing the select query
params — array containing select query parameters
<T> T sqlGetObject(java.sql.ResultSet rs, String columnLabel, Class<T> returnType) Gets values from ResultSet in the given position.

rs — ResultSet with selected values
columnLabel — name of column
returnType — type of value that will be returned
<T> T sqlGetObject(com.anylogic.engine.ResultSet rs, String columnLabel, Class<T> returnType) Gets values from AnyLogic wrapper for ResultSet in the given position.

rs — AnyLogic wrapper for ResultSet with selected values
columnLabel — name of column
returnType — type of value that will be returned
<T> T sqlGetObject(java.sql.ResultSet rs, int index, Class<T> returnType) Gets values from ResultSet in the given position.

rs — ResultSet with selected values
index — index of the result
returnType — type of value that will be returned
<T> T sqlGetObject(com.anylogic.engine.ResultSet rs, int index, Class<T> returnType) Gets values from AnyLogic wrapper for ResultSet in the given position.

rs — AnyLogic wrapper for ResultSet with selected values
index — index of the result
returnType — type of value that will be returned
void sqlSetObject(java.sql.PreparedStatement st, int index, Object object) Sets parameter for the given PreparedStatement in the given index position with the given value.

st — PreparedStatement
index — index of the given parameter in the query
object — parameter value
int executeStatement(String sql, Object... params) Executes the INSERT, DELETE, or UPDATE SQL statement in the AnyLogic database, using the provided query string and parameters.
Returns the number of affected database table rows.

sql — a string containing the SQL statement
params — an array containing the statement parameters
void executeAction(String code, Object... argDescriptors) Execute a code command presented in the String format. Returns nothing.

code — a string containing the Java code expression you want to execute. The expression may include parameters, whose values should be described by argDescriptors
argDescriptors — an array of parameters and their values, presented in the following format: parameter 1, the value of parameter 1, parameter 2, the value of parameter 2, …
<T> T executeExpression(String code, Object... argDescriptors) Execute a code command presented in the String format. Returns the result of the command’s evaluation.

code — a string containing the Java code expression you want to execute. The expression may include parameters, whose values should be described by argDescriptors
argDescriptors — an array of parameters and their values, presented in the following format: parameter 1, the value of parameter 1, parameter 2, the value of parameter 2, …
<T> T executeExpression(Class<T> returnType, String code, Object... argDescriptors) Execute a code command presented in the String format. Returns the result of the command’s evaluation which has the type specified as returnType.

returnType — defines the type of data the expression returns. May be void.class, if the returned data is a Java expression itself
code — a string containing the Java code expression you want to execute. The expression may include parameters, whose values should be described by argDescriptors
argDescriptors — an array of parameters and their values, presented in the following format: parameter 1, the value of parameter 1, parameter 2, the value of parameter 2, …
How can we improve this article?