Class SQLProcessor

java.lang.Object
org.ofbiz.core.entity.jdbc.SQLProcessor
Direct Known Subclasses:
AutoCommitSQLProcessor, ExplcitCommitSQLProcessor, ExplicitCommitSQLProcessor, PassThruSQLProcessor, ReadOnlySQLProcessor

@NotThreadSafe public class SQLProcessor extends Object
SQLProcessor - provides utility functions to ease database access
Since:
2.0
Version:
$Revision: 1.7 $
Author:
David E. Jones
  • Field Details

    • module

      public static final String module
      Module Name Used for debugging
    • MSSQL

      public static final DatabaseType MSSQL
  • Constructor Details

    • SQLProcessor

      public SQLProcessor(String helperName)
      Construct a SQLProcessor based on the helper/datasource.

      It will be in SQLProcessor.CommitMode.EXPLICIT_COMMIT by default

      This is the legacy mode of SQLProcessor as it was before the CommitMode support was added. This constructor has been left in place because other plugins may be using it and hence to preserve the API.

      Parameters:
      helperName - The datasource helper (see entityengine.xml <datasource name="..">)
    • SQLProcessor

      public SQLProcessor(String helperName, Connection connection)
      Construct a SQLProcessor with an connection given. The connection will not be closed by this SQLProcessor, but may be by some other.

      It does not participate in any commiting not does it set the autoCommit mode of the connection

      Parameters:
      helperName - The datasource helper (see entityengine.xml <datasource name="..">)
      connection - The connection to be used
  • Method Details

    • getCommitMode

      public SQLProcessor.CommitMode getCommitMode()
      Returns:
      the SQLProcessor.CommitMode the SQLProcessor is in
    • commit

      public void commit() throws GenericDataSourceException
      Commit all modifications
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • rollback

      public void rollback() throws GenericDataSourceException
      Rollback all modifications
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • close

      public void close() throws GenericDataSourceException
      Commit if required and remove all allocated resources
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • getConnection

      public Connection getConnection() throws GenericEntityException
      Get a connection from the ConnectionFactory
      Returns:
      The connection created
      Throws:
      GenericEntityException - if an SQLException occurs
    • prepareStatement

      public SQLProcessor prepareStatement(String sql) throws GenericEntityException
      Prepare a statement. In case no connection has been given, allocate a new one.
      Parameters:
      sql - The SQL statement to be executed
      Returns:
      this for method chaining
      Throws:
      GenericEntityException - if an SQLException occurs
    • prepareStatement

      public SQLProcessor prepareStatement(String sql, boolean specifyTypeAndConcur, int resultSetType, int resultSetConcurrency) throws GenericEntityException
      Prepare a statement. In case no connection has been given, allocate a new one.
      Parameters:
      sql - The SQL statement to be executed
      specifyTypeAndConcur - true if the result set Type and Concurrency has been specified
      resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
      resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
      Returns:
      this for method chaining
      Throws:
      GenericEntityException - if an SQLException occurs
    • executeQuery

      public ResultSet executeQuery() throws GenericDataSourceException
      Execute a query based on the prepared statement
      Returns:
      The result set of the query
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • executeQuery

      public ResultSet executeQuery(String sql) throws GenericEntityException
      Execute a query based on the SQL string given
      Parameters:
      sql - The SQL string to be executed
      Returns:
      The result set of the query
      Throws:
      GenericDataSourceException - if an SQLException occurs
      GenericEntityException
    • executeUpdate

      public int executeUpdate() throws GenericDataSourceException
      Execute updates
      Returns:
      The number of rows updated
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • executeUpdate

      public int executeUpdate(String sql) throws GenericDataSourceException
      Execute update based on the SQL statement given
      Parameters:
      sql - SQL statement to be executed
      Returns:
      either (1) the row count for SQL Data Manipulation Language (DML) statements
      Throws:
      GenericDataSourceException - if an SQLException occurs or (2) 0 for SQL statements that return nothing
    • executeBatch

      public int[] executeBatch() throws GenericDataSourceException
      Execute a batch query based on the prepared statement
      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:
      GenericDataSourceException - if an SQLException occurs
      See Also:
    • next

      public boolean next() throws GenericDataSourceException
      Test if there more records available
      Returns:
      true, if there more records available
      Throws:
      GenericDataSourceException - if an SQLException occurs
    • getResultSet

      public ResultSet getResultSet()
      Getter: get the currently activ ResultSet
      Returns:
      ResultSet
    • getPreparedStatement

      public PreparedStatement getPreparedStatement()
      Getter: get the prepared statement
      Returns:
      PreparedStatement
    • execQuery

      public void execQuery(String sql, ExecQueryCallbackFunctionIF aListener) throws GenericEntityException
      Execute a query based on the SQL string given. For each record of the ResultSet return, execute a callback function
      Parameters:
      sql - The SQL string to be executed
      aListener - The callback function object
      Throws:
      GenericEntityException - if an SQLException occurs
    • setValue

      public SQLProcessor setValue(String field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Timestamp field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Time field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Date field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Integer field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Long field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Float field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Double field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Boolean field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Object field) throws SQLException
      Set the next binding variable of the currently active prepared statement.
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Blob field) throws SQLException
      Set the next binding variable of the currently active prepared statement
      Parameters:
      field - the field value in play
      Returns:
      Throws:
      SQLException - if somethings goes wrong
    • setValue

      public SQLProcessor setValue(Clob field) throws SQLException
      Set the next binding variable of the currently active prepared statement
      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if somethings goes wrong
    • setBlob

      public SQLProcessor setBlob(byte[] field) throws SQLException
      Set the next binding variable of the currently active prepared statement. Note that this *must* actually be a BLOB field, not a PostgreSQL BYTEA, a SQLServer IMAGE, or an HSQL OTHER field. Use setByteArray(byte[]) to set those!
      Parameters:
      field - the field value
      Returns:
      Throws:
      SQLException - if something goes wrong
    • setByteArray

      public SQLProcessor setByteArray(byte[] field) throws SQLException
      Set the next binding variable of the currently active prepared statement. Note that this *must* actually be a PostgreSQL BYTEA, a SQLServer IMAGE, or an HSQL OTHER field. Use setBlob(byte[]) to set BLOBs!
      Parameters:
      field - the field value
      Returns:
      Throws:
      SQLException - if something goes wrong
    • setBinaryStream

      public SQLProcessor setBinaryStream(Object field) throws SQLException
      Set the next binding variable of the currently active prepared statement to write the serialized data of 'field' to a BLOB that is stored as an OID SQL type.
      Parameters:
      field - the field value in play
      Returns:
      Throws:
      SQLException - if somethings goes wrong
    • addBatch

      public SQLProcessor addBatch() throws SQLException
      Adds a set of parameters to underlying PreparedStatement object's batch of commands.
      Returns:
      this for method chaining
      Throws:
      SQLException - is something goes wrong
      See Also:
    • setByteArrayData

      public SQLProcessor setByteArrayData(Object field) throws SQLException
      Set the next binding variable of the currently active prepared statement to write the serialized data of 'field' to a BLOB that is stored as a Byte Array SQL type.

      This method is specifically added to support PostgreSQL BYTEA and SQLServer IMAGE datatypes.

      Parameters:
      field - the field value in play
      Returns:
      this for method chaining
      Throws:
      SQLException - if something goes wrong
    • toString

      public String toString()
      Overrides:
      toString in class Object