Class DatabaseResourceAdapter

All Implemented Interfaces:
FWSimFieldContainer
Direct Known Subclasses:
DBFResourceAdapter

public class DatabaseResourceAdapter extends FWSimResourceAdapter<ResultSet>
Resource adapter for databases (DatabaseInterface). The data is read with the SQL statement of the statement child (default SELECT * FROM <resource id>); tokens ${...} in the statement are replaced with the values of the current simulation. The query is only executed again if the resulting statement changed.
Author:
Andreas Enders
  • Field Details

    • iStatement

      protected String iStatement
      SQL statement, may contain ${...} tokens
    • iLastStatement

      protected String iLastStatement
      statement executed last (tokens replaced); the same statement is not executed twice
  • Constructor Details

    • DatabaseResourceAdapter

      public DatabaseResourceAdapter(org.jdom2.Element aResourceElement, FWSimSession aSession, int aOrderNumber)
      Creates the adapter from a resource element of the solution.
      Parameters:
      aResourceElement - the resource element
      aSession - the session
      aOrderNumber - order number of the adapter
    • DatabaseResourceAdapter

      public DatabaseResourceAdapter(String aPoolName, String aComponentName, int aOrderNumber, FWSimSession aFwSimSession, FWSimIOAdapter.FREQUENCE aFrequence, Map<String,String> aKeyMap, String aDriver, String aUrl, String aUser, String aPass, String aStatement) throws Exception
      Creates the adapter from the graph panel: creates the database interface and derives the header variables from the result of the statement (see readHeaderList(DatabaseInterface, String, Map, FWSimSession)).
      Parameters:
      aPoolName - name of the connection pool
      aComponentName - name of the adapter
      aOrderNumber - order number of the adapter
      aFwSimSession - the session
      aFrequence - frequency the data is read with
      aKeyMap - key variables; if empty, the user is asked to select them
      aDriver - JDBC driver class
      aUrl - JDBC URL
      aUser - database user
      aPass - password
      aStatement - SQL statement, default SELECT * FROM <component name>
      Throws:
      Exception - if the connection or the query fails
      SimFieldMissingException - if no key variables are given or selected
    • DatabaseResourceAdapter

      public DatabaseResourceAdapter(FWSimResourceAdapter<?> aAdapter, DatabaseInterface aInterface)
      Copy constructor used when the interface is replaced; the key variables are taken over.
      Parameters:
      aAdapter - the adapter to copy
      aInterface - the new interface
  • Method Details

    • getData

      Executes the statement and reads the result into a new cache.
      Specified by:
      getData in class FWSimInputAdapter
      Parameters:
      aVarMap - variable map of the simulation (e.g. for the key values of the rule)
      Returns:
      the new cache, or null if the statement is unchanged (the previous cache stays valid)
      Throws:
      MissingSimResourceException - if the data cannot be read
      See Also:
    • getResultSet

      public ResultSet getResultSet(FWSimVarMap aVarMap) throws MissingSimResourceException
      Replaces the tokens in the statement and executes it, unless the resulting statement was executed last time.
      Parameters:
      aVarMap - variable map of the simulation
      Returns:
      the result set, or null if the statement is unchanged
      Throws:
      MissingSimResourceException - if the query fails
    • getStatement

      public String getStatement()
      Returns:
      the SQL statement (with tokens)
    • setStatement

      public void setStatement(String aStatement)
      Changes the SQL statement (e.g. in the GUI); empty means SELECT * FROM <resource id>. The statement is executed again on the next read.
      Parameters:
      aStatement - SQL statement, may contain tokens
    • toXML

      public org.jdom2.Element toXML()
      Adds the statement element to the element of the base class, if it differs from the default SELECT * FROM <resource id>.
      Specified by:
      toXML in interface FWSimFieldContainer
      Overrides:
      toXML in class FWSimResourceAdapter<ResultSet>
      Returns:
      an XML element with all settings of this container
      See Also:
    • readHeaderList

      public Map<String,String> readHeaderList(DatabaseInterface aInterface, String aStatement, Map<String,String> aKeyMap, FWSimSession aSession) throws Exception
      Determines the header variables from the column names and types of the statement result, plus a variable projectid. If no key variables are given, the user is asked to select them.
      Parameters:
      aInterface - the database interface
      aStatement - SQL statement
      aKeyMap - key variables, may be null or empty
      aSession - the session (provides the model variables for the key selection)
      Returns:
      the key variables, or null if none were given or selected
      Throws:
      Exception - if the query fails