Klasse BasePeerImpl<T>

java.lang.Object
org.apache.torque.util.BasePeerImpl<T>
Typparameter:
T - The data object class for this Peer.
Alle implementierten Schnittstellen:
Serializable
Bekannte direkte Unterklassen:
AbstractPeerImpl

public class BasePeerImpl<T> extends Object implements Serializable
This is the base class for all Peer classes in the system. Peer classes are responsible for isolating all of the database access for a specific business object. They execute all of the SQL against the database. Over time this class has grown to include utility methods which ease execution of cross-database queries and the implementation of concrete Peers.
Version:
$Id: BasePeerImpl.java 1917245 2024-04-21 14:06:23Z tv $
Autor:
Frank Y. Kim, John D. McNally, Brett McLaughlin, Stephen Haberman, Martin Poeschl, Augustin Vidovic, Henning P. Schmiedehausen
Siehe auch:
  • Konstruktordetails

    • BasePeerImpl

      public BasePeerImpl()
      Default constructor
    • BasePeerImpl

      public BasePeerImpl(RecordMapper<T> recordMapper, TableMap tableMap, String databaseName)
      Constructor providing the objects to be injected as parameters.
      Parameter:
      recordMapper - a record mapper to map JDBC result sets to objects
      tableMap - the default table map
      databaseName - the name of the database
  • Methodendetails

    • setRecordMapper

      public void setRecordMapper(RecordMapper<T> recordMapper)
      Set the record mapper for this instance.
      Parameter:
      recordMapper - the recordMapper to set
    • getRecordMapper

      public RecordMapper<T> getRecordMapper()
      Get the record mapper for this instance.
      Gibt zurück:
      the recordMapper
    • setTableMap

      public void setTableMap(TableMap tableMap)
      Set the default table map for this instance.
      Parameter:
      tableMap - the tableMap to set
    • getTableMap

      public TableMap getTableMap()
      Get the default table map for this instance.
      Gibt zurück:
      the tableMap
    • setDatabaseName

      public void setDatabaseName(String databaseName)
      Set the database name for this instance.
      Parameter:
      databaseName - the databaseName to set
    • getDatabaseName

      public String getDatabaseName()
      Get the database name for this instance.
      Gibt zurück:
      the databaseName
    • doDelete

      public int doDelete(Criteria criteria) throws TorqueException
      Deletes rows from a database table.
      Parameter:
      criteria - defines the rows to be deleted, not null.
      Gibt zurück:
      the number of deleted rows.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doDelete

      public int doDelete(Criteria criteria, Connection connection) throws TorqueException
      Deletes rows from a table. This method is to be used during a transaction, otherwise use the doDelete(Criteria) method.
      Parameter:
      criteria - defines the rows to be deleted, not null.
      connection - the connection to use, not null.
      Gibt zurück:
      the number of deleted rows.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doInsert

      public ObjectKey<?> doInsert(ColumnValues insertValues) throws TorqueException
      Inserts a record into a database table.

      If the primary key is included in Criteria, then that value will be used to insert the row.

      Otherwise, if the primary key can be generated automatically, the generated key will be used for the insert and will be returned.

      If no value is given for the primary key is defined and it cannot be generated automatically or the table has no primary key, the values will be inserted as specified and null will be returned.

      Parameter:
      insertValues - Contains the values to insert, not null.
      Gibt zurück:
      the primary key of the inserted row (if the table has a primary key) or null (if the table does not have a primary key).
      Löst aus:
      TorqueException - if a database error occurs.
    • doInsert

      public ObjectKey<?> doInsert(ColumnValues insertValues, Connection connection) throws TorqueException
      Inserts a record into a database table.

      If the primary key is included in Criteria, then that value will be used to insert the row.

      Otherwise, if the primary key can be generated automatically, the generated key will be used for the insert and will be returned.

      If no value is given for the primary key is defined and it cannot be generated automatically or the table has no primary key, the values will be inserted as specified and null will be returned.

      Parameter:
      insertValues - Contains the values to insert, not null.
      connection - the connection to use for the insert, not null.
      Gibt zurück:
      the primary key of the inserted row (if the table has a primary key) or null (if the table does not have a primary key).
      Löst aus:
      TorqueException - if a database error occurs.
    • doInsert

      public int doInsert(Column[] toInsertInto, Criteria criteria) throws TorqueException
      Executes a insert into...select statement.
      Parameter:
      toInsertInto - the columns in which to insert, not null.
      criteria - the criteria which selects the values to insert, not null.
      Gibt zurück:
      the number of inserted rows, not null.
      Löst aus:
      TorqueException - if a database error occurs.
    • doInsert

      public int doInsert(Column[] toInsertInto, Criteria criteria, String dbName) throws TorqueException
      Executes a insert into...select statement.
      Parameter:
      toInsertInto - the columns in which to insert, not null.
      criteria - the criteria which selects the values to insert, not null.
      dbName - the database name, or null to take the database name from getDatabaseName().
      Gibt zurück:
      the number of inserted rows, not null.
      Löst aus:
      TorqueException - if a database error occurs.
    • doInsert

      public int doInsert(Column[] toInsertInto, Criteria criteria, Connection connection) throws TorqueException
      Executes a insert into...select statement.
      Parameter:
      toInsertInto - the columns in which to insert, not null.
      criteria - the criteria which selects the values to insert, not null.
      connection - the database connection to use, not null.
      Gibt zurück:
      the number of inserted rows, not null.
      Löst aus:
      TorqueException - if a database error occurs.
    • doInsert

      public int doInsert(Column[] toInsertInto, Criteria criteria, String dbName, Connection connection) throws TorqueException
      Executes a insert into...select statement.
      Parameter:
      toInsertInto - the columns in which to insert, not null, must not contain null.
      criteria - the criteria which selects the values to insert, not null.
      dbName - the database name, or null to take the database name from getDatabaseName().
      connection - the database connection to use, not null.
      Gibt zurück:
      the number of inserted rows, not null.
      Löst aus:
      TorqueException - if a database error occurs.
    • addSelectColumns

      public void addSelectColumns(Criteria criteria)
      Add all the columns needed to create a new object.
      Parameter:
      criteria - the Criteria to which the select columns should be added.
    • doSelect

      public List<T> doSelect(Criteria criteria) throws TorqueException
      Selects objects from a database.
      Parameter:
      criteria - object used to create the SELECT statement.
      Gibt zurück:
      the list of selected objects, not null.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doSelect

      public List<T> doSelect(Criteria criteria, Connection connection) throws TorqueException
      Selects objects from a database within a transaction.
      Parameter:
      criteria - object used to create the SELECT statement.
      connection - the connection to use, not null.
      Gibt zurück:
      the list of selected objects, not null.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doSelectAsStream

      public Stream<T> doSelectAsStream(Criteria criteria, Connection connection) throws TorqueException
      Selects objects from a database within a transaction. This method returns a stream that must be closed after use. All resources used by this method will be closed when the stream is closed.
      Parameter:
      criteria - object used to create the SELECT statement.
      connection - the connection to use, not null.
      Gibt zurück:
      The results of the query as a Stream, not null.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doSelectSingleRecord

      public T doSelectSingleRecord(Criteria criteria) throws TorqueException
      Selects at most one object from a database.
      Parameter:
      criteria - object used to create the SELECT statement.
      Gibt zurück:
      the selected Object, or null if no object was selected.
      Löst aus:
      TorqueException - If more than one record is selected or if an error occurs when processing the query.
    • doSelectSingleRecord

      public T doSelectSingleRecord(Criteria criteria, Connection connection) throws TorqueException
      Selects at most one object from a database within a transaction.
      Parameter:
      criteria - object used to create the SELECT statement.
      connection - the connection holding the transaction, not null.
      Gibt zurück:
      the selected Object, or null if no object was selected.
      Löst aus:
      TorqueException - If more than one record is selected or if an error occurs when processing the query.
    • doSelect

      public <TT> List<TT> doSelect(Criteria criteria, RecordMapper<TT> mapper) throws TorqueException
      Selects rows from a database an maps them to objects.
      Typparameter:
      TT - Object type class
      Parameter:
      criteria - A Criteria specifying the records to select, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelect

      public List<T> doSelect(String query) throws TorqueException
      Selects rows from a database an maps them to objects.
      Parameter:
      query - the sql query to execute, not null.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelect

      public List<T> doSelect(String query, Connection connection) throws TorqueException
      Selects rows from a database an maps them to objects.
      Parameter:
      query - the SQL Query to execute, not null.
      connection - the database connection, not null.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelectAsStream

      public Stream<T> doSelectAsStream(String query, Connection connection) throws TorqueException
      Selects rows from a database an maps them to objects. This method returns a stream that must be closed after use. All resources used by this method will be closed when the stream is closed.
      Parameter:
      query - the SQL Query to execute, not null.
      connection - the database connection, not null.
      Gibt zurück:
      The results of the query as a Stream, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelect

      public <TT> List<TT> doSelect(String query, RecordMapper<TT> mapper, String dbName) throws TorqueException
      Selects rows from a database an maps them to objects.
      Typparameter:
      TT - Object type class
      Parameter:
      query - the sql query to execute, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      dbName - The name of the database to create the connection for, or null for the default DB.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelect

      public <TT> List<TT> doSelect(String query, RecordMapper<TT> mapper, Connection connection) throws TorqueException
      Selects rows from a database an maps them to objects.
      Typparameter:
      TT - Object type class
      Parameter:
      query - the SQL Query to execute, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      connection - the database connection, not null.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelectAsStream

      public <TT> Stream<TT> doSelectAsStream(String query, RecordMapper<TT> mapper, Connection connection) throws TorqueException
      Selects rows from a database an maps them to objects. This method returns a stream that must be closed after use. All resources used by this method will be closed when the stream is closed.
      Typparameter:
      TT - Object type class
      Parameter:
      query - the SQL Query to execute, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      connection - the database connection, not null.
      Gibt zurück:
      The results of the query as a Stream, not null.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelect

      public <TT> List<TT> doSelect(Criteria criteria, RecordMapper<TT> mapper, Connection connection) throws TorqueException
      Performs a SQL select using a PreparedStatement.
      Typparameter:
      TT - Object type class
      Parameter:
      criteria - A Criteria specifying the records to select, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      connection - the database connection for selecting records, not null.
      Gibt zurück:
      The results of the query, not null.
      Löst aus:
      TorqueException - Error performing database query.
    • doSelectAsStream

      public <TT> Stream<TT> doSelectAsStream(Criteria criteria, RecordMapper<TT> mapper, Connection connection) throws TorqueException
      Performs a SQL select using a PreparedStatement. This method returns a stream that must be closed after use. All resources used by this method will be closed when the stream is closed.
      Typparameter:
      TT - Object type class
      Parameter:
      criteria - A Criteria specifying the records to select, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      connection - the database connection for selecting records, not null.
      Gibt zurück:
      The results of the query as a Stream, not null.
      Löst aus:
      TorqueException - Error performing database query.
    • doSelectSingleRecord

      public <TT> TT doSelectSingleRecord(Criteria criteria, RecordMapper<TT> mapper) throws TorqueException
      Selects at most a single row from a database an maps them to objects.
      Typparameter:
      TT - Object type class
      Parameter:
      criteria - A Criteria specifying the records to select, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      Gibt zurück:
      The selected row, or null if no records was selected.
      Löst aus:
      TorqueException - if querying the database fails.
    • doSelectSingleRecord

      public <TT> TT doSelectSingleRecord(Criteria criteria, RecordMapper<TT> mapper, Connection connection) throws TorqueException
      Selects at most a single row from a database an maps them to objects.
      Typparameter:
      TT - Object type class
      Parameter:
      criteria - A Criteria specifying the records to select, not null.
      mapper - The mapper creating the objects from the resultSet, not null.
      connection - the database connection, not null.
      Gibt zurück:
      The selected row, or null if no records was selected.
      Löst aus:
      TorqueException - if querying the database fails.
    • doUpdate

      public int doUpdate(ColumnValues updateValues) throws TorqueException
      Convenience method used to update rows in the DB. Checks if a single primary key is specified in the Criteria object and uses it to perform the update. If no primary key is specified or the table has multiple primary keys, an Exception will be thrown.

      Use this method for performing an update of the kind:

      "WHERE primary_key_id = someValue"

      To perform an update on a table with multiple primary keys or an update with non-primary key fields in the WHERE clause, use doUpdate(ColumnValues, Criteria).

      Parameter:
      updateValues - Which columns to update with which values for which primary key value, not null.
      Gibt zurück:
      the number of affected rows.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doUpdate

      public int doUpdate(ColumnValues updateValues, Connection connection) throws TorqueException
      Convenience method used to update rows in the DB. Checks if a single primary key is specified in the Criteria object and uses it to perform the update. If no primary key is specified or the table has multiple primary keys, an Exception will be thrown.

      Use this method for performing an update of the kind:

      "WHERE primary_key_id = someValue"

      To perform an update on a table with multiple primary keys or an update with non-primary key fields in the WHERE clause, use doUpdate(ColumnValues, Criteria, Connection).

      Parameter:
      updateValues - Which columns to update with which values for which primary key value, not null.
      connection - the database connection to use.
      Gibt zurück:
      the number of affected rows.
      Löst aus:
      TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
    • doUpdate

      public int doUpdate(Criteria selectCriteria, ColumnValues updateValues) throws TorqueException
      Executes an update against the database. The rows to be updated are selected using criteria and updated using the values in updateValues.
      Parameter:
      selectCriteria - selects which rows of which table should be updated, not null.
      updateValues - Which columns to update with which values, not null.
      Gibt zurück:
      the number of affected rows.
      Löst aus:
      TorqueException - if updating fails.
    • doUpdate

      public int doUpdate(Criteria criteria, ColumnValues updateValues, Connection connection) throws TorqueException
      Executes an update against the database. The rows to be updated are selected using criteria and updated using the values in updateValues.
      Parameter:
      criteria - selects which rows of which table should be updated.
      updateValues - Which columns to update with which values, not null.
      connection - the database connection to use, not null.
      Gibt zurück:
      the number of affected rows.
      Löst aus:
      TorqueException - if updating fails.
    • executeStatement

      public int executeStatement(String statementString) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails or no database connection can be established.
    • executeStatement

      public int executeStatement(String statementString, List<JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute.
      replacementValues - values to use as placeholders in the query. or null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails or no database connection can be established.
    • executeStatement

      public int executeStatement(String statementString, String dbName, List<JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute.
      dbName - The name of the database to execute the statement against, or null for the default DB.
      replacementValues - values to use as placeholders in the query. or null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails or no database connection can be established.
    • executeStatement

      public int executeStatement(String statementString, Connection con, List<JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute.
      con - The database connection to use.
      replacementValues - values to use as placeholders in the query. or null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails.
    • executeStatement

      public int executeStatement(String statementString, Map<String,JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute, containing placeholders of the form ":${placeholderName}". ${placeholderName} must contain only letters, digits and the underscore Each placeholder must be followed by a space, except when it is at the end of the statement.
      replacementValues - a map mapping the placeholder names to values to use as placeholders in the query. Can be null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails.
    • executeStatement

      public int executeStatement(String statementString, String dbName, Map<String,JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute, containing placeholders of the form ":${placeholderName}". ${placeholderName} must contain only letters, digits and the underscore Each placeholder must be followed by a space, except when it is at the end of the statement.
      dbName - The name of the database to execute the statement against, or null for the default DB.
      replacementValues - a map mapping the placeholder names to values to use as placeholders in the query. Can be null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails.
    • executeStatement

      public int executeStatement(String statementString, Connection con, Map<String,JdbcTypedValue> replacementValues) throws TorqueException
      Utility method which executes a given sql statement as prepared statement. This method should be used for update, insert, and delete statements. Use executeQuery() for selects.
      Parameter:
      statementString - A String with the sql statement to execute, containing placeholders of the form ":${placeholderName}". ${placeholderName} must contain only letters, digits and the underscore Each placeholder must be followed by a space, except when it is at the end of the statement.
      con - The database connection to use.
      replacementValues - a map mapping the placeholder names (without leading colons) to values to use as placeholders in the query. Can be null or empty if no placeholders need to be filled.
      Gibt zurück:
      The number of rows affected.
      Löst aus:
      TorqueException - if executing the statement fails.
    • correctBooleans

      public void correctBooleans(Criteria criteria) throws TorqueException
      Checks all columns in the criteria to see whether booleanchar and booleanint columns are queried with a boolean. If yes, the query values are mapped onto values the database does understand, i.e. 0 and 1 for booleanints and N and Y for booleanchar columns.
      Parameter:
      criteria - The criteria to be checked for booleanint and booleanchar columns.
      Löst aus:
      TorqueException - if the database map for the criteria cannot be retrieved.
    • correctBooleans

      public void correctBooleans(ColumnValues columnValues) throws TorqueException
      Checks all columns in the criteria to see whether booleanchar and booleanint columns are queried with a boolean. If yes, the query values are mapped onto values the database does understand, i.e. 0 and 1 for booleanints and N and Y for booleanchar columns.
      Parameter:
      columnValues - The value to be checked for booleanint and booleanchar columns.
      Löst aus:
      TorqueException - if the database map for the criteria cannot be retrieved.
    • setDbName

      protected void setDbName(Criteria crit) throws TorqueException
      Sets the database name in the passed criteria to the table's default, if it is not already set.
      Parameter:
      crit - the criteria to set the database name in, not null.
      Löst aus:
      TorqueException - if unable to set the database name