Class DatabaseInterface
java.lang.Object
net.simplace.sim.io.interfaces.FWSimInterface<Connection>
net.simplace.sim.io.interfaces.DatabaseInterface
- Direct Known Subclasses:
DBFInterface
Interface for databases via JDBC, used for resources and outputs. The access object is a
Connection from a
connection pool named like the interface. Settings (children of the interface element):
driver (default HSQLDB), url (the token ${_WORKDIR_} is replaced),
user (default sa), pass, or alias to use an existing pool.
Connections obtained by getInterface(FWSimVarMap, CONTENT_TYPE) have to be returned to the pool with
ConnectionPoolManager.getInstance().freeConnection(Connection).
- Author:
- Andreas Enders
-
Nested Class Summary
Nested classes/interfaces inherited from class FWSimInterface
FWSimInterface.INTERFACE_TYPE -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringname of an existing connection pool to use instead of an own one, ornullprotected StringJDBC driver classprotected Stringpasswordprotected StringJDBC URLprotected Stringdatabase userFields inherited from class FWSimInterface
iColumnMap, iColumnNamesInt, iIntColumnNames -
Constructor Summary
ConstructorsModifierConstructorDescriptionDatabaseInterface(String aName) Creates the interface with a temporary HSQLDB pool of 10 connections (e.g.Creates the interface with a pool of 5 connections to the given database (e.g.protectedDatabaseInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, int aPoolSize) Creates the interface without element and without pool; to be called by subclasses.protectedDatabaseInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Creates the interface from its element and creates (or renews) its connection pool.protectedDatabaseInterface(String aName, org.jdom2.Element aInterfaceElement) Creates a database interface from its element. -
Method Summary
Modifier and TypeMethodDescriptionvoidfinalize()Nothing to release; the pool is managed by theConnectionPoolManager.getAlias()getInterface(FWSimVarMap aVarMap, FWSimVariable.CONTENT_TYPE aContentType) Returns a connection from the pool (or the alias pool).getPass()static StringgetSQLType(FWSimVariable.DATA_TYPE aDataType) Returns the SQL column type for a SIMPLACE data type.getURL()getUser()static PreparedStatementprepareDatabase(String aTableName, HashMap<String, FWSimVariable<?>> aHeaderVariables, Connection aConnection, boolean isArrayMode) Creates (or replaces) a table with one column per header variable (type seegetSQLType(DATA_TYPE)) and returns a prepared insert statement for it.read(FWSimSession aFwSimSession, String aProjectID, Object aCache, LinkedHashMap<String, String> aKeyMap, FWSimInputAdapter aResourceAdapter, String aUniqueID, FWSimInterface.INTERFACE_TYPE aInterfaceType) Converts a result set into a resource cache: each row is stored under its key; rows without key are skipped with a warning.voidsetConnection(String aDriver, String aURL, String aUser, String aPass) Changes the connection data (e.g.voidvoidvoidvoidorg.jdom2.ElementtoXML()Adds the alias or driver, URL, user and password to the element of the base class.Methods inherited from class FWSimInterface
checkHeaderForColMode, createInterface, createInterface, createStatisticInterface, getInterfaceElement, getInterfaceType, getName, getPoolSize, readColArray, readColArray, toString, transformArrays, writeToDatabase
-
Field Details
-
iDriver
JDBC driver class -
iURL
JDBC URL -
iPass
password -
iUser
database user -
iAlias
name of an existing connection pool to use instead of an own one, ornull
-
-
Constructor Details
-
DatabaseInterface
protected DatabaseInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, int aPoolSize) Creates the interface without element and without pool; to be called by subclasses.- Parameters:
aName- name of the interfaceaInterfaceType- type of the interfaceaPoolSize- number of pooled connections
-
DatabaseInterface
protected DatabaseInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Creates the interface from its element and creates (or renews) its connection pool. If analiasnames an existing pool, that pool is used. Without element a temporary H2 (name starting withh2simpool) or HSQLDB pool would be created; this branch is not reachable, because the super constructor already reads the element.- Parameters:
aName- name of the interface and the poolaInterfaceType- type of the interfaceaInterfaceElement- theinterfaceelement
-
DatabaseInterface
Creates a database interface from its element.- Parameters:
aName- name of the interface and the poolaInterfaceElement- theinterfaceelement
-
DatabaseInterface
Creates the interface with a pool of 5 connections to the given database (e.g. graph panel, statistics outputs). Without a driver, a temporary HSQLDB pool is created instead.- Parameters:
aName- name of the interface and the poolaDriver- JDBC driver class, may benullor emptyaURL- JDBC URLaPass- passwordaUser- database user
-
DatabaseInterface
Creates the interface with a temporary HSQLDB pool of 10 connections (e.g. the default pool of a solution).- Parameters:
aName- name of the interface and the pool
-
-
Method Details
-
getInterface
Returns a connection from the pool (or the alias pool). The connection has to be returned to the pool after use.- Specified by:
getInterfacein classFWSimInterface<Connection>- Parameters:
aVarMap- variable map used for replacing tokens (e.g. in file names), may benullaContentType-resfor reading,outfor writing- Returns:
- the connection, or
nullif none can be obtained (the error is logged) - See Also:
-
getSQLType
Returns the SQL column type for a SIMPLACE data type. Arrays, matrices and date arrays are written as one text column ({v1,v2,...}). Texts get an explicit length, because a plainCHARisCHAR(1)in e.g. PostgreSQL and MySQL (#450).- Parameters:
aDataType- the data type of the variable- Returns:
- the SQL type for
CREATE TABLE
-
prepareDatabase
public static PreparedStatement prepareDatabase(String aTableName, HashMap<String, FWSimVariable<?>> aHeaderVariables, Connection aConnection, boolean isArrayMode) throws SQLException Creates (or replaces) a table with one column per header variable (type seegetSQLType(DATA_TYPE)) and returns a prepared insert statement for it.- Parameters:
aTableName- name of the tableaHeaderVariables- the header variables (column names and data types)aConnection- the database connectionisArrayMode- not used- Returns:
- the prepared insert statement
- Throws:
SQLException- if the table or the statement cannot be created
-
read
public FWSimResourceCache read(FWSimSession aFwSimSession, String aProjectID, Object aCache, LinkedHashMap<String, String> aKeyMap, FWSimInputAdapter aResourceAdapter, String aUniqueID, FWSimInterface.INTERFACE_TYPE aInterfaceType) throws MissingSimResourceExceptionConverts a result set into a resource cache: each row is stored under its key; rows without key are skipped with a warning. Arrays are read in column mode (one column per element) or in row mode (consecutive rows with the same key form one array). If the adapter caches to the database, or for the project data (resourcePROJECT_DATA), the rows are also written to a table. SQL errors are logged.- Specified by:
readin classFWSimInterface<Connection>- Parameters:
aFwSimSession- the sessionaProjectID- id of the current projectaCache- the data read, in the format of the interface (e.g.String[][], result set, XML element)aKeyMap- key variables of the resourceaResourceAdapter- the adapter the cache belongs toaUniqueID- unique id of the source dataaInterfaceType- type of the source interface- Returns:
- the cache, or
nullif the result set isnull - Throws:
MissingSimResourceException- if the data cannot be converted- See Also:
-
getDriver
- Returns:
- the JDBC driver class
-
setDriver
- Parameters:
aDriver- the JDBC driver class; does not affect the existing pool
-
getURL
- Returns:
- the JDBC URL
-
setURL
- Parameters:
aURL- the JDBC URL; does not affect the existing pool
-
getPass
- Returns:
- the password
-
getAlias
- Returns:
- the name of the pool this interface uses instead of its own (element
alias), ornull
-
setConnection
-
setPass
- Parameters:
aPass- the password; does not affect the existing pool
-
getUser
- Returns:
- the database user
-
setUser
- Parameters:
aUser- the database user; does not affect the existing pool
-
toXML
public org.jdom2.Element toXML()Adds the alias or driver, URL, user and password to the element of the base class.- Overrides:
toXMLin classFWSimInterface<Connection>- Returns:
- the
interfaceelement, ornullfor temporary pools (neither alias nor driver and URL set)
-
finalize
Nothing to release; the pool is managed by theConnectionPoolManager.- Specified by:
finalizein classFWSimInterface<Connection>- Throws:
Exception- if releasing fails- See Also:
-