AnyLogic
Expand
Font size
All Implemented Interfaces:
Serializable

public class Query
extends DatabaseAccessor
Query object for database connectivity
Works in 2 modes:
  • Returns ResultSet object (see execute()) which can be iterated to retrieve result values
  • Maps rows of database resulting table to the fields of newly created elements in collection or new Agents in agent population (see executeAndMap())

This object is only available in the AnyLogic Professional
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Constructor Summary

ConstructorDescription
Query(String name, Database database, boolean selectFromTable, String tableNameOrQueryText)
Creates new Query for work only with execute() method
Query(String name, Database database, boolean selectFromTable, String tableNameOrQueryText, String[][] mapping)
Creates new Query for work in the Mapping mode (executeAndMap() method)
Note the execute() method is still available for this object

Method Summary

Modifier and TypeMethodDescription
ObjectcreateNewRowElementInstance_xjal()
Extension-point for element creation
In the Mapping mode, this method should be overridden to create specific element and return it
final ResultSetexecute()
Executes query in the database and returns single ResultSet
If any error occurs, throws RuntimeException
Connects Database if it is not connected.
final intexecuteAndMap()
This method fills underlying collection or agent population (created agents are started automatically) with elements created from rows of database Query result
This method works only in the Mapping mode
final intexecuteAndMap(boolean startAgents)
This method fills underlying collection or agent population with elements created from rows of database Query result
This method works only in the Mapping mode
voidregisterNewRowElement_xjal(Object element, boolean startAgents)
Extension-point for element creation
In the Mapping mode, this method should be overridden to start given element (previously created by createNewRowElementInstance_xjal() and parametrized) add it to the underlying collection and return it
StringtoString() 

Methods inherited from class com.anylogic.engine.connectivity.ConnectivityBase

destroy, getName

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Details

Query

public Query(String name,
 Database database,
 boolean selectFromTable,
 String tableNameOrQueryText)
Creates new Query for work only with execute() method
Parameters:
name - object name (decoration purpose)
database -
selectFromTable -
tableNameOrQueryText -

Query

public Query(String name,
 Database database,
 boolean selectFromTable,
 String tableNameOrQueryText,
 String[][] mapping)
Creates new Query for work in the Mapping mode (executeAndMap() method)
Note the execute() method is still available for this object
Parameters:
name - object name (decoration purpose)
database - database to connect to
selectFromTable - flag indicating what value is contained in parameter tableNameOrQueryText
tableNameOrQueryText -
  • the table name to select data from, - if selectFromTable is true
  • SQL query text, otherwise
mapping - mappings array with dimensions N x 2 like:
 new String[][] { { "fieldName0", "columnLabel0" },
                { "fieldName1", "columnLabel1" },
 // ...
 }
 
where
  • fieldName is name of field in the created collection element or Agent
  • columnLabel is column's title which is used in printouts and displays. This title is usually specified by the SQL AS clause. If a SQL AS is not specified, the table column name is assumed

Method Details

createNewRowElementInstance_xjal

@AnyLogicInternalCodegenAPI
public Object createNewRowElementInstance_xjal()
Extension-point for element creation
In the Mapping mode, this method should be overridden to create specific element and return it
Returns:
new element instance created for each row

registerNewRowElement_xjal

@AnyLogicInternalCodegenAPI
public void registerNewRowElement_xjal(Object element,
 boolean startAgents)
Extension-point for element creation
In the Mapping mode, this method should be overridden to start given element (previously created by createNewRowElementInstance_xjal() and parametrized) add it to the underlying collection and return it
Parameters:
startAgents - defines either created agents are started or not
overridden method should call start() method on agent only if this parameter is true

execute

public final ResultSet execute()
Executes query in the database and returns single ResultSet
If any error occurs, throws RuntimeException
Connects Database if it is not connected. After this method call Database remains connected.
Returns:
new ResultSet object; never null
Please call ResultSet.close() method after all needed operations are completed

executeAndMap

public final int executeAndMap()
This method fills underlying collection or agent population (created agents are started automatically) with elements created from rows of database Query result
This method works only in the Mapping mode
Returns:
number of created objects

executeAndMap

public final int executeAndMap(boolean startAgents)
This method fills underlying collection or agent population with elements created from rows of database Query result
This method works only in the Mapping mode
Parameters:
startAgents - defines either created agents are started or not
Returns:
number of created objects

toString

public String toString()
Overrides:
toString in class ConnectivityBase