AnyLogic AnyLogic
Expand
Font size
  • java.lang.Object
    • com.anylogic.engine.database.ModelDatabase

public class ModelDatabase
extends java.lang.Object
This class represents the built-in AnyLogic Model Database. Almost all the methods of this class are internal. The database object may be accessed from getEngine().getModelDatabase() and may be used for import / export with external data sources.
Author:
AnyLogic North America, LLC https://anylogic.com

Nested Class Summary

Nested Classes 
static class  ModelDatabase.DatabaseCacheKey
This class is internal and shouldn't be called by user.
it may be removed/renamed in future.
Modifier and Type Class Description

Field Summary

Fields 
static java.lang.String AL_DB_OBJECTS  
static java.lang.String AL_OBJECT_NAME  
static java.lang.String AL_OBJECT_TYPE  
static java.lang.String AL_OBJECT_USAGE  
static java.lang.String AL_OBJECT_USAGE_LOG  
static java.lang.String AL_SELECTED_LOG_OBJECT_NAME  
static java.lang.String AL_SELECTED_LOG_OBJECT_TYPE  
static java.lang.String AL_SELECTED_LOG_OBJECTS  
static java.lang.String ANYLOGIC_IDENTIFIER_PREFIX  
static java.lang.String CURRENT_VERSION  
static java.lang.String DATABASE_DB_PATH  
static java.lang.String DATABASE_DIRECTORY  
static java.lang.String DROP_SQL_OBJECT  
static java.lang.String LOG_OBJECT_SUFFIX  
static java.lang.String PUBLIC_SCHEMA_NAME  
static java.lang.String SQL_FUNCTION  
static java.lang.String SQL_TABLE  
static java.lang.String SQL_VIEW  
static java.lang.String UNIQUE_AUTOINC_COLUMN_NAME  
Modifier and Type Field Description

Constructor Summary

Constructors 
ModelDatabase​(Engine engine, ModelProperties modelProperties, DatabaseLogProperties logProperties)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.
Constructor Description

Method Summary

All Methods Static Methods Instance Methods Concrete Methods 
static void createLogSchema​(java.sql.Connection connection)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static void deleteExistingLogObjects​(java.sql.Connection connection)
This method deletes all the model log data (if any were turned on) from the model database
void disconnect()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
java.sql.ResultSet execute​(java.lang.String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
void exportToExternalDB​(java.lang.String sourceTableName, java.sql.Connection targetConnection, java.lang.String targetTableName, boolean clearTargetTable, boolean autoCommit)
Exports a single table data from this database to an external database.
int generateAgentElementId()
This method returns id property of dynamically create model elements
int generateAgentId()
Internal method, used to initialize Agent.getId() property
int getAgentTypeElementId​(LoggingType loggingType, Agent agent, java.lang.String elementName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
int getAgentTypeId​(Agent agent)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
<T extends java.lang.Enum<T> & IStatechartState<?,​T>>
int
getAgentTypeStateId​(Agent agent, Statechart<T> statechart, T state)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
LRUCache<ModelDatabase.DatabaseCacheKey,​java.lang.Object> getCache()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
<R> java.lang.Object getCachedValue​(com.querydsl.sql.SQLBindings sqlBindings, java.lang.String operation, java.util.function.Supplier<R> f)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
java.sql.Connection getConnection()  
static java.lang.String getCreateTableSQL​(java.lang.String tableName, java.lang.String tableContent, boolean cached)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
com.querydsl.sql.Configuration getDSLConfiguration()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
int getEventId​(EventOriginator event)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
int getStatechartId​(Statechart<?> statechart)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
void importFromExternalDB​(java.sql.Connection sourceConnection, java.lang.String sourceTableName, java.lang.String targetTableName, boolean clearTargetTable, boolean autoCommit)
Imports a single table data from an external database.
void log​(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
void resetBeforeStart()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static void restoreUsersViews​(java.sql.Connection connection)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static void restoreUsersViews​(java.sql.Connection connection, java.util.Collection<java.lang.String> viewNames)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
java.sql.ResultSet select​(java.lang.String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
java.sql.ResultSet selectFrom​(java.lang.String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSet wrapResultSet​(java.sql.ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Modifier and Type Method Description

Methods inherited from class java.lang.Object

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

Field Detail

CURRENT_VERSION

@AnyLogicInternalAPI
public static final java.lang.String CURRENT_VERSION
See Also:
Constant Field Values

DATABASE_DIRECTORY

@AnyLogicInternalAPI
public static final java.lang.String DATABASE_DIRECTORY
See Also:
Constant Field Values

DATABASE_DB_PATH

@AnyLogicInternalAPI
public static final java.lang.String DATABASE_DB_PATH

PUBLIC_SCHEMA_NAME

@AnyLogicInternalAPI
public static final java.lang.String PUBLIC_SCHEMA_NAME
See Also:
Constant Field Values

DROP_SQL_OBJECT

@AnyLogicInternalAPI
public static final java.lang.String DROP_SQL_OBJECT
See Also:
Constant Field Values

LOG_OBJECT_SUFFIX

@AnyLogicInternalAPI
public static final java.lang.String LOG_OBJECT_SUFFIX
See Also:
Constant Field Values

ANYLOGIC_IDENTIFIER_PREFIX

@AnyLogicInternalAPI
public static final java.lang.String ANYLOGIC_IDENTIFIER_PREFIX
See Also:
Constant Field Values

UNIQUE_AUTOINC_COLUMN_NAME

@AnyLogicInternalAPI
public static final java.lang.String UNIQUE_AUTOINC_COLUMN_NAME
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECTS

@AnyLogicInternalAPI
public static final java.lang.String AL_SELECTED_LOG_OBJECTS
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECT_NAME

@AnyLogicInternalAPI
public static final java.lang.String AL_SELECTED_LOG_OBJECT_NAME
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECT_TYPE

@AnyLogicInternalAPI
public static final java.lang.String AL_SELECTED_LOG_OBJECT_TYPE
See Also:
Constant Field Values

SQL_TABLE

@AnyLogicInternalAPI
public static final java.lang.String SQL_TABLE
See Also:
Constant Field Values

SQL_VIEW

@AnyLogicInternalAPI
public static final java.lang.String SQL_VIEW
See Also:
Constant Field Values

SQL_FUNCTION

@AnyLogicInternalAPI
public static final java.lang.String SQL_FUNCTION
See Also:
Constant Field Values

AL_DB_OBJECTS

@AnyLogicInternalAPI
public static final java.lang.String AL_DB_OBJECTS
See Also:
Constant Field Values

AL_OBJECT_NAME

@AnyLogicInternalAPI
public static final java.lang.String AL_OBJECT_NAME
See Also:
Constant Field Values

AL_OBJECT_TYPE

@AnyLogicInternalAPI
public static final java.lang.String AL_OBJECT_TYPE
See Also:
Constant Field Values

AL_OBJECT_USAGE

@AnyLogicInternalAPI
public static final java.lang.String AL_OBJECT_USAGE
See Also:
Constant Field Values

AL_OBJECT_USAGE_LOG

@AnyLogicInternalAPI
public static final java.lang.String AL_OBJECT_USAGE_LOG
See Also:
Constant Field Values

Constructor Detail

ModelDatabase

@AnyLogicInternalAPI
public ModelDatabase​(Engine engine,
                     ModelProperties modelProperties,
                     DatabaseLogProperties logProperties)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.

Method Detail

getCache

@AnyLogicInternalAPI
public LRUCache<ModelDatabase.DatabaseCacheKey,​java.lang.Object> getCache()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getCachedValue

@AnyLogicInternalAPI
public <R> java.lang.Object getCachedValue​(com.querydsl.sql.SQLBindings sqlBindings,
                                           java.lang.String operation,
                                           java.util.function.Supplier<R> f)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

createLogSchema

@AnyLogicInternalAPI
public static void createLogSchema​(java.sql.Connection connection)
                            throws java.sql.SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
java.sql.SQLException

restoreUsersViews

@AnyLogicInternalAPI
public static void restoreUsersViews​(java.sql.Connection connection,
                                     java.util.Collection<java.lang.String> viewNames)
                              throws java.sql.SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
java.sql.SQLException

restoreUsersViews

@AnyLogicInternalAPI
public static void restoreUsersViews​(java.sql.Connection connection)
                              throws java.sql.SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
java.sql.SQLException

deleteExistingLogObjects

public static void deleteExistingLogObjects​(java.sql.Connection connection)
                                     throws java.sql.SQLException
This method deletes all the model log data (if any were turned on) from the model database
Parameters:
connection - database connection
Throws:
java.sql.SQLException

getCreateTableSQL

@AnyLogicInternalAPI
public static java.lang.String getCreateTableSQL​(java.lang.String tableName,
                                                 java.lang.String tableContent,
                                                 boolean cached)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

log

@AnyLogicInternalAPI
public void log​(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeId

@AnyLogicInternalAPI
public int getAgentTypeId​(Agent agent)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

generateAgentElementId

@AnyLogicInternalAPI
public int generateAgentElementId()
This method returns id property of dynamically create model elements

generateAgentId

@AnyLogicInternalAPI
public int generateAgentId()
Internal method, used to initialize Agent.getId() property

getEventId

@AnyLogicInternalAPI
public int getEventId​(EventOriginator event)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getStatechartId

@AnyLogicInternalAPI
public int getStatechartId​(Statechart<?> statechart)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeElementId

@AnyLogicInternalAPI
public int getAgentTypeElementId​(LoggingType loggingType,
                                 Agent agent,
                                 java.lang.String elementName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeStateId

@AnyLogicInternalAPI
public <T extends java.lang.Enum<T> & IStatechartState<?,​T>> int getAgentTypeStateId​(Agent agent,
                                                                                           Statechart<T> statechart,
                                                                                           T state)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getConnection

@AnyLogicInternalAPI
public java.sql.Connection getConnection()

disconnect

@AnyLogicInternalAPI
public void disconnect()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

resetBeforeStart

@AnyLogicInternalAPI
public void resetBeforeStart()
                      throws java.lang.Exception
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
java.lang.Exception

getDSLConfiguration

@AnyLogicInternalAPI
public com.querydsl.sql.Configuration getDSLConfiguration()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

select

@AnyLogicInternalAPI
public java.sql.ResultSet select​(java.lang.String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

execute

@AnyLogicInternalAPI
public java.sql.ResultSet execute​(java.lang.String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

selectFrom

@AnyLogicInternalAPI
public java.sql.ResultSet selectFrom​(java.lang.String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

importFromExternalDB

public void importFromExternalDB​(java.sql.Connection sourceConnection,
                                 java.lang.String sourceTableName,
                                 java.lang.String targetTableName,
                                 boolean clearTargetTable,
                                 boolean autoCommit)
Imports a single table data from an external database. Column types should match or be convertible.
Parameters:
sourceConnection - Connection object for external database to copy data from
sourceTableName - name of the table in external database to copy data from
targetTableName - table in this database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = modelDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

exportToExternalDB

public void exportToExternalDB​(java.lang.String sourceTableName,
                               java.sql.Connection targetConnection,
                               java.lang.String targetTableName,
                               boolean clearTargetTable,
                               boolean autoCommit)
Exports a single table data from this database to an external database. Column types should match or be convertible.
When exporting to Database#getConnection(), depending on the external database implementation, the data may be actually saved when the model is stopped (when Engine will automatically call Database#disconnect() at the end). For custom experiment this requires calling engine.stop() at the end.
Parameters:
sourceTableName - table in this database to copy data from
targetConnection - Connection object for external database to copy data to
targetTableName - table in external database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = targetDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

wrapResultSet

@AnyLogicInternalAPI
public ResultSet wrapResultSet​(java.sql.ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
How can we improve this article?