Class DatabaseInterface

java.lang.Object
net.simplace.sim.io.interfaces.FWSimInterface<Connection>
net.simplace.sim.io.interfaces.DatabaseInterface
Direct Known Subclasses:
DBFInterface

public class DatabaseInterface extends FWSimInterface<Connection>
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
  • Field Details

    • iDriver

      protected String iDriver
      JDBC driver class
    • iURL

      protected String iURL
      JDBC URL
    • iPass

      protected String iPass
      password
    • iUser

      protected String iUser
      database user
    • iAlias

      protected String iAlias
      name of an existing connection pool to use instead of an own one, or null
  • 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 interface
      aInterfaceType - type of the interface
      aPoolSize - 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 an alias names an existing pool, that pool is used. Without element a temporary H2 (name starting with h2simpool) 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 pool
      aInterfaceType - type of the interface
      aInterfaceElement - the interface element
    • DatabaseInterface

      protected DatabaseInterface(String aName, org.jdom2.Element aInterfaceElement)
      Creates a database interface from its element.
      Parameters:
      aName - name of the interface and the pool
      aInterfaceElement - the interface element
    • DatabaseInterface

      public DatabaseInterface(String aName, String aDriver, String aURL, String aPass, String aUser)
      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 pool
      aDriver - JDBC driver class, may be null or empty
      aURL - JDBC URL
      aPass - password
      aUser - database user
    • DatabaseInterface

      public DatabaseInterface(String aName)
      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

      public Connection getInterface(FWSimVarMap aVarMap, FWSimVariable.CONTENT_TYPE aContentType)
      Returns a connection from the pool (or the alias pool). The connection has to be returned to the pool after use.
      Specified by:
      getInterface in class FWSimInterface<Connection>
      Parameters:
      aVarMap - variable map used for replacing tokens (e.g. in file names), may be null
      aContentType - res for reading, out for writing
      Returns:
      the connection, or null if none can be obtained (the error is logged)
      See Also:
    • getSQLType

      public static String getSQLType(FWSimVariable.DATA_TYPE aDataType)
      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 plain CHAR is CHAR(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 see getSQLType(DATA_TYPE)) and returns a prepared insert statement for it.
      Parameters:
      aTableName - name of the table
      aHeaderVariables - the header variables (column names and data types)
      aConnection - the database connection
      isArrayMode - 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 MissingSimResourceException
      Converts 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 (resource PROJECT_DATA), the rows are also written to a table. SQL errors are logged.
      Specified by:
      read in class FWSimInterface<Connection>
      Parameters:
      aFwSimSession - the session
      aProjectID - id of the current project
      aCache - the data read, in the format of the interface (e.g. String[][], result set, XML element)
      aKeyMap - key variables of the resource
      aResourceAdapter - the adapter the cache belongs to
      aUniqueID - unique id of the source data
      aInterfaceType - type of the source interface
      Returns:
      the cache, or null if the result set is null
      Throws:
      MissingSimResourceException - if the data cannot be converted
      See Also:
    • getDriver

      public String getDriver()
      Returns:
      the JDBC driver class
    • setDriver

      public void setDriver(String aDriver)
      Parameters:
      aDriver - the JDBC driver class; does not affect the existing pool
    • getURL

      public String getURL()
      Returns:
      the JDBC URL
    • setURL

      public void setURL(String aURL)
      Parameters:
      aURL - the JDBC URL; does not affect the existing pool
    • getPass

      public String getPass()
      Returns:
      the password
    • getAlias

      public String getAlias()
      Returns:
      the name of the pool this interface uses instead of its own (element alias), or null
    • setConnection

      public void setConnection(String aDriver, String aURL, String aUser, String aPass)
      Changes the connection data (e.g. in the GUI) and renews the connection pool of the interface.
      Parameters:
      aDriver - JDBC driver class
      aURL - JDBC URL
      aUser - database user
      aPass - password
    • setPass

      public void setPass(String aPass)
      Parameters:
      aPass - the password; does not affect the existing pool
    • getUser

      public String getUser()
      Returns:
      the database user
    • setUser

      public void setUser(String aUser)
      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:
      toXML in class FWSimInterface<Connection>
      Returns:
      the interface element, or null for temporary pools (neither alias nor driver and URL set)
    • finalize

      public void finalize() throws Exception
      Nothing to release; the pool is managed by the ConnectionPoolManager.
      Specified by:
      finalize in class FWSimInterface<Connection>
      Throws:
      Exception - if releasing fails
      See Also: