Class StandardDBStatement

java.lang.Object
overit.geocall.sql.StandardDBStatement
All Implemented Interfaces:
AutoCloseable, DataAttributesHolder, DBFieldMapper, DBObjectMapper, DBStatement, CancelListener

@Component @Scope("prototype") public class StandardDBStatement extends Object implements DBStatement, DBFieldMapper, DBObjectMapper, CancelListener
The class is the standard implementation of the DBStatement interface
  • Field Details

    • _delimiter

      protected static String _delimiter
    • _variableDelimiter

      protected static String _variableDelimiter
      Delimiter used for variables, ie the portions of SQL that come after a question mark. For these portions of code, the dot character is not considered a delimiter; in this way we can support variables composed by TABLE.FIELD
    • _pc

      protected PooledConnection _pc
      The connection of the statement
    • _alSqlParis

      protected List<StandardDBStatement.SqlPair> _alSqlParis
    • _pairs

      protected Map<String,Object> _pairs
      Map of pairs <variable,value> used to compile the statement. This map must be appropriately filled with the set() method
    • _definitions

      protected HashMap _definitions
      Map of pairs <variable,value> used for the definition of any DBObjectMap associated with some parameter of the statement. This map must be appropriately filled with the define() method
    • _alreadySetted

      protected HashSet<String> _alreadySetted
      Set of explicitly setted parameters. It's useful for don't re-set unnecessarily the same arguments during the compilation of the statement
    • _sql

      protected Fragment _sql
      Temporary variable that define the SQL statement, as setted into the setSql method
    • _parsing

      protected StandardDBStatement.Parsing _parsing
    • _maxRows

      protected int _maxRows
      Stores the maximum number of rows requested to a statement. The default value is 0, that means that there isn't a limitation
    • concurResultSet

      protected int concurResultSet
      Define the permission when the result set is managed. The default value is 1007, that means that the result set is only in read mode
    • _skipRows

      protected int _skipRows
      Stores the number of initial rows to be skipped
    • _batched

      protected boolean _batched
    • _queryTimeout

      protected int _queryTimeout
    • _parameterWithReferences

      protected boolean _parameterWithReferences
    • _batchResults

      protected ArrayList<int[]> _batchResults
    • _fieldmap

      protected DBFieldMap _fieldmap
    • _objectmap

      protected DBObjectMap _objectmap
  • Constructor Details

    • StandardDBStatement

      public StandardDBStatement(PooledConnection pc)
      Creates a statement starting from the connection
      Parameters:
      pc - The connection that create the instance
  • Method Details

    • out

      protected void out(StringBuilder sb, String s)
    • setResultSetToDBViewConverter

      @Autowired public void setResultSetToDBViewConverter(overit.geocall.sql.ResultSetToDBViewConverter resultSetToDBViewConverter)
    • setExtendedAttributesAdapter

      @Autowired public void setExtendedAttributesAdapter(overit.geocall.edm.service.ExtendedAttributeAdapter extendedAttributeAdapter)
    • setExtendedAttributeDatabaseAdapter

      public void setExtendedAttributeDatabaseAdapter(overit.geocall.edm.service.ExtendedAttributeDatabaseAdapter extendedAttributeDatabaseAdapter)
    • setEdmRegistry

      @Autowired public void setEdmRegistry(EDMRegistry edmRegistry)
    • setSql

      public void setSql(String sqlpart)
      Sets the SQL string of the statement.
      The string contains variables like @field that can be properly converted in a DB field name or like ?value. Those must be different and must be expressly set by one of the method set(map) or set(key,value)
      Specified by:
      setSql in interface DBStatement
      Parameters:
      sqlpart - The SQL string
    • setSqlOn

      public void setSqlOn(String sqlpart, String sqlkey)
      Sets conditionally the SQL string of the statement.
      The SQL string is inserted into the statement only if it will be setted to the statement (with one of the methods set(map) or set(key,value)) a key that corresponds to the key passed as second parameter
      Specified by:
      setSqlOn in interface DBStatement
      Parameters:
      sqlpart - The SQL string
      sqlkey - The key that must be setted to the statement
    • setSqlIn

      public void setSqlIn(String sqlpart, String sqlkey, Object... values)
      Sets conditionally the SQL string of the statement.
      The SQL string is inserted into the statement only if it will be setted to the statement with one of the methods set(map) or set(key,value)) a key that corresponds to the key passed as second parameter and the object associated to the key is equal to one of the objects passed as third parameter
      Specified by:
      setSqlIn in interface DBStatement
      Parameters:
      sqlpart - The SQL string
      sqlkey - The key that must be setted to the statement
      values - The array of Objects used for comparison
    • setSqlIn

      public void setSqlIn(String sqlpart, String sqlkey, Collection values)
      Sets conditionally the SQL string of the statement.
      The SQL string is inserted into the statement only if it will be setted to the statement with one of the methods set(map) or set(key,value)) a key that corresponds to the key passed as second parameter and the object associated to the key is different to all of the objects passed as third parameter
      Specified by:
      setSqlIn in interface DBStatement
      Parameters:
      sqlpart - The SQL string
      sqlkey - The key that must be setted to the statement
      values - The array of Objects used for comparison
    • setSqlOut

      public void setSqlOut(String sqlpart, String sqlkey, Object... values)
      Description copied from interface: DBStatement
      Sets conditionally the SQL string of the statement.
      The SQL string is inserted into the statement only if it will be set to the statement with one of the methods set(map) or set(key,value)) a key that corresponds to the key passed as second parameter and the object associated to the key is different to all of the objects passed as third parameter
      Specified by:
      setSqlOut in interface DBStatement
      Parameters:
      sqlpart - The SQL string
      sqlkey - The key that must be set to the statement
      values - The array of Objects used for comparison
    • setSqlOut

      public void setSqlOut(String sqlpart, String sqlkey, Collection values)
      Description copied from interface: DBStatement
      Sets conditionally the SQL string of the statement.
      The SQL string is inserted into the statement only if it will be set to the statement with one of the methods set(map) or set(key,value)) a key that corresponds to the key passed as second parameter and the object associated to the key is different to all of the objects passed as third parameter
      Specified by:
      setSqlOut in interface DBStatement
      Parameters:
      sqlpart - The SQL string
      sqlkey - The key that must be set to the statement
      values - The collection of Objects used for comparison
    • setSql

      public void setSql(Fragment sql)
      Description copied from interface: DBStatement
      Sets the SQL Fragment of the statement.
      The string contains variables like @field that can be properly converted in a DB field name or like ?value. Those must be different and must be expressly set by one of the method set(map) or set(key,value)
      Specified by:
      setSql in interface DBStatement
      Parameters:
      sql - The SQL Fragment
    • set

      public void set(String k, Object o)
      Sets the value of a variable ?value. With this method one value of an SQL statement is compiled. These value can be appropriately converted by a converter (like the DBObjectMap) before being passed to the DB
      Specified by:
      set in interface DBStatement
      Parameters:
      k - The name of the variable ?value in the SQL string
      o - The value of the variable ?value in the SQL string
    • set

      public void set(Map<String,Object> map)
      Sets the values of all the variables ?value. With this method all the values of an SQL statement are compiled. These values can be appropriately converted by a converter (like the DBObjectMap) before being passed to the DB
      Specified by:
      set in interface DBStatement
      Parameters:
      map - The map in which each entry defines the value of a variable ?value
    • define

      public void define(String field, DBObjectMap dbom)
      Specifies a DBObjectMap that must be used to load a column resulting from a SELECT
      Specified by:
      define in interface DBStatement
      Parameters:
      field - The name of the column of the ResultSet obtained from a query
      dbom - The object that can map every object in the column
    • defineParam

      public void defineParam(String field, DBObjectMap dbom)
      Explicitly specifies a DBObjectMap that must be used to prepare the statement for a specific variable in the SQL string.
      Specified by:
      defineParam in interface DBStatement
      Parameters:
      field - The name of the variable in the SQL string
      dbom - The object that can map every object specified for the variable
    • toInStatementString

      protected static String toInStatementString(String s)
      Parses the string passed as parameter and and makes it suitable to be inserted in a like statement
      Parameters:
      s - The string that must be inserted in a like statement
      Returns:
      The string correctly adapted to be inserted in a like statement
    • close

      public void close() throws SQLException
      Closes the SQL statement
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface DBStatement
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • registerKeys

      protected void registerKeys() throws SQLException
      Throws:
      SQLException
    • removeKeys

      protected void removeKeys() throws SQLException
      Throws:
      SQLException
    • executeUpdate

      public int executeUpdate() throws SQLException
      Translates the statement, compiles and execute it
      Specified by:
      executeUpdate in interface DBStatement
      Returns:
      The number of the rows updated or inserted into the database
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • addBatch

      public void addBatch() throws SQLException
      Description copied from interface: DBStatement
      Adds a set of parameters to the statement object's batch of commands. See Statement.addBatch(String)
      Specified by:
      addBatch in interface DBStatement
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • addBatch

      public void addBatch(int batchSize) throws SQLException
      Description copied from interface: DBStatement
      Adds a set of parameters to the statement object's batch of commands. See Statement.addBatch(String).
      Specified by:
      addBatch in interface DBStatement
      Parameters:
      batchSize - size of the batch (useful for executing the batch whenever the batch size reaches this value).
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller.
    • executeBatch

      public int[] executeBatch() throws SQLException
      Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. See Statement.executeBatch()
      Specified by:
      executeBatch in interface DBStatement
      Returns:
      An array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • getResultSet

      public ResultSet getResultSet() throws SQLException
      Executes the SQL query in this statement object and returns the ResultSet object generated by the query
      Specified by:
      getResultSet in interface DBStatement
      Returns:
      The ResultSet object that contains the data produced by the query
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • executeQuery

      public DBView executeQuery() throws SQLException
      Translates the statement, compiles and execute it
      Specified by:
      executeQuery in interface DBStatement
      Returns:
      The view resulting for the SQL query, totally kept in RAM
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • executeQuery

      public DBView executeQuery(boolean roData) throws SQLException
      Deprecated.
      use dbStatement.executeQuery() or dbStatement.executeQuery().setRoData() instead
      Translates the statement, compiles and execute it.
      Specified by:
      executeQuery in interface DBStatement
      Parameters:
      roData - true if the result must be not modifiable, false otherwise. A value of true is the same as calling dbStatement.executeQuery().setRoData();
      Returns:
      The DBView resulting for the SQL query, totally kept in RAM
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • executeQueryLoad

      protected DBView executeQueryLoad(StandardDBStatement.Parsing p) throws SQLException
      Throws:
      SQLException
    • getRowMapFromResultSet

      public HashGetter getRowMapFromResultSet(ResultSet rs) throws SQLException
      Description copied from interface: DBStatement
      reads the result set raw at the current position and construct a key-value map af all the fields according to the statement specification
      Specified by:
      getRowMapFromResultSet in interface DBStatement
      Parameters:
      rs - the result set
      Returns:
      the key-value map
      Throws:
      SQLException - if some sql error happens in the record loading
    • scan

      public void scan(DBScanner dbs) throws SQLException
      Description copied from interface: DBStatement
      extracts a DBCursor from the statement and submits it to an explicit scanning prcedure
      Specified by:
      scan in interface DBStatement
      Parameters:
      dbs - the scanning procedure
      Throws:
      SQLException - if some sql error happens in the scanning procedure
    • setMaxRows

      public void setMaxRows(int n)
      Sets the maximum number of rows to search
      Specified by:
      setMaxRows in interface DBStatement
      Parameters:
      n - Maximum number of rows
    • setUpdatable

      public void setUpdatable(boolean updatable)
      Description copied from interface: DBStatement
      Sets if the ResultSet is updatable
      Specified by:
      setUpdatable in interface DBStatement
      Parameters:
      updatable - if true the ResultSet is updatable, otherwise false
    • setSkipRows

      public void setSkipRows(int n)
      Sets the number of initial rows to be skipped. Even these lines, although skipped, are counted in the maxrows parameter.
      Specified by:
      setSkipRows in interface DBStatement
      Parameters:
      n - Number of rows to be skipped
    • setQueryTimeout

      public void setQueryTimeout(int secs)
      Sets a time (in seconds) after which the query times out
      Parameters:
      secs - Number of seconds before the timeout
    • getDBFieldMap

      public DBFieldMap getDBFieldMap()
      Retrieves the DBFieldMap object
      Specified by:
      getDBFieldMap in interface DBFieldMapper
      Returns:
      The DBFieldMap object of the statement
    • setDBFieldMap

      public void setDBFieldMap(DBFieldMap fieldmap)
      Sets the DBFieldMap object
      Specified by:
      setDBFieldMap in interface DBFieldMapper
      Parameters:
      fieldmap - The DBFieldMap to set
    • haveDBFieldMap

      public boolean haveDBFieldMap()
      Checks the presence of a DBFieldMap object
      Specified by:
      haveDBFieldMap in interface DBFieldMapper
      Returns:
      true if there is a correct DBFieldMap object (not null), false otherwise
    • getDBObjectMap

      public DBObjectMap getDBObjectMap()
      Implements the DBObjectMapper interface. It returns the DBObjectMap of the instance
      Specified by:
      getDBObjectMap in interface DBObjectMapper
      Returns:
      The DBObjectMap of the instance
    • setDBObjectMap

      public void setDBObjectMap(DBObjectMap objectmap)
      Implements the DBObjectMapper interface. Sets the DBObjectMap of the instance
      Specified by:
      setDBObjectMap in interface DBObjectMapper
      Parameters:
      objectmap - The DBObjectMap of the instance
    • haveDBObjectMap

      public boolean haveDBObjectMap()
      Implements the DBObjectMapper interface. Checks the presence of a valid DBObjectMap in the instance
      Specified by:
      haveDBObjectMap in interface DBObjectMapper
      Returns:
      true if there is a correct DBObjectMap object (not null), false otherwise
    • getStatement

      public Statement getStatement() throws SQLException
      Returns the Statement on which the DBStatement acts
      Specified by:
      getStatement in interface DBStatement
      Returns:
      SQL statement used to access the database
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • createStatement

      public Statement createStatement() throws SQLException
      Explicitly creates a PreparedStatement. It is useful when you want to use a PreparedStatement and also have the same release mechanisms of the DBStatement
      Specified by:
      createStatement in interface DBStatement
      Returns:
      The PreparedStatement created and used to access the database
      Throws:
      SQLException - If the communication with the database throws an exception, that it is raised to the caller
    • createCall

      public CallableStatement createCall(String call) throws SQLException
      Specified by:
      createCall in interface DBStatement
      Throws:
      SQLException
    • cancel

      public void cancel()
      Cancels this statement object. See Statement.cancel()
      Specified by:
      cancel in interface CancelListener
    • getDataAttributes

      public List<DataAttribute> getDataAttributes()
      Description copied from interface: DataAttributesHolder
      Returns a List<DataAttribute> about the EDM data attributes.
      Specified by:
      getDataAttributes in interface DataAttributesHolder
      Returns:
      a List<DataAttribute> about the EDM data attributes.
    • setDataAttributes

      public void setDataAttributes(List<DataAttribute> dataAttributes)
      Description copied from interface: DataAttributesHolder
      Sets the provided dataAttributes.
      Specified by:
      setDataAttributes in interface DataAttributesHolder
      Parameters:
      dataAttributes - List<DataAttribute> about the EDM data attributes to be set.
    • isUpdateInProgress

      public boolean isUpdateInProgress()
      Description copied from interface: DBStatement
      Returns true in case a SQL UPDATE query is in progress; false otherwise.
      Specified by:
      isUpdateInProgress in interface DBStatement
      Returns:
      true in case a SQL UPDATE query is in progress; false otherwise.
    • isInsertInProgress

      public boolean isInsertInProgress()
      Description copied from interface: DBStatement
      Returns true in case a SQL INSERT query is in progress; false otherwise.
      Specified by:
      isInsertInProgress in interface DBStatement
      Returns:
      true in case a SQL INSERT query is in progress; false otherwise.
    • getDbType

      public DBType getDbType()
      Description copied from interface: DBStatement
      Returns the DBType associated to the PooledConnection currently in use.
      Specified by:
      getDbType in interface DBStatement
      Returns:
      the DBType associated to the PooledConnection currently in use.