Class BasePeerImpl<T>
- Type Parameters:
T- The data object class for this Peer.
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AbstractPeerImpl
- Version:
- $Id: BasePeerImpl.java 1917245 2024-04-21 14:06:23Z tv $
- Author:
- Frank Y. Kim, John D. McNally, Brett McLaughlin, Stephen Haberman, Martin Poeschl, Augustin Vidovic, Henning P. Schmiedehausen
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructorBasePeerImpl(RecordMapper<T> recordMapper, TableMap tableMap, String databaseName) Constructor providing the objects to be injected as parameters. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSelectColumns(Criteria criteria) Add all the columns needed to create a new object.voidcorrectBooleans(Criteria criteria) Checks all columns in the criteria to see whether booleanchar and booleanint columns are queried with a boolean.voidcorrectBooleans(ColumnValues columnValues) Checks all columns in the criteria to see whether booleanchar and booleanint columns are queried with a boolean.intDeletes rows from a database table.intdoDelete(Criteria criteria, Connection connection) Deletes rows from a table.intExecutes a insert into...select statement.intExecutes a insert into...select statement.intdoInsert(Column[] toInsertInto, Criteria criteria, String dbName, Connection connection) Executes a insert into...select statement.intdoInsert(Column[] toInsertInto, Criteria criteria, Connection connection) Executes a insert into...select statement.ObjectKey<?> doInsert(ColumnValues insertValues) Inserts a record into a database table.ObjectKey<?> doInsert(ColumnValues insertValues, Connection connection) Inserts a record into a database table.Selects rows from a database an maps them to objects.doSelect(String query, Connection connection) Selects rows from a database an maps them to objects.<TT> List<TT> doSelect(String query, RecordMapper<TT> mapper, String dbName) Selects rows from a database an maps them to objects.<TT> List<TT> doSelect(String query, RecordMapper<TT> mapper, Connection connection) Selects rows from a database an maps them to objects.Selects objects from a database.doSelect(Criteria criteria, Connection connection) Selects objects from a database within a transaction.<TT> List<TT> doSelect(Criteria criteria, RecordMapper<TT> mapper) Selects rows from a database an maps them to objects.<TT> List<TT> doSelect(Criteria criteria, RecordMapper<TT> mapper, Connection connection) Performs a SQLselectusing a PreparedStatement.doSelectAsStream(String query, Connection connection) Selects rows from a database an maps them to objects.<TT> Stream<TT> doSelectAsStream(String query, RecordMapper<TT> mapper, Connection connection) Selects rows from a database an maps them to objects.doSelectAsStream(Criteria criteria, Connection connection) Selects objects from a database within a transaction.<TT> Stream<TT> doSelectAsStream(Criteria criteria, RecordMapper<TT> mapper, Connection connection) Performs a SQLselectusing a PreparedStatement.doSelectSingleRecord(Criteria criteria) Selects at most one object from a database.doSelectSingleRecord(Criteria criteria, Connection connection) Selects at most one object from a database within a transaction.<TT> TTdoSelectSingleRecord(Criteria criteria, RecordMapper<TT> mapper) Selects at most a single row from a database an maps them to objects.<TT> TTdoSelectSingleRecord(Criteria criteria, RecordMapper<TT> mapper, Connection connection) Selects at most a single row from a database an maps them to objects.intdoUpdate(Criteria selectCriteria, ColumnValues updateValues) Executes an update against the database.intdoUpdate(Criteria criteria, ColumnValues updateValues, Connection connection) Executes an update against the database.intdoUpdate(ColumnValues updateValues) Convenience method used to update rows in the DB.intdoUpdate(ColumnValues updateValues, Connection connection) Convenience method used to update rows in the DB.intexecuteStatement(String statementString) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, String dbName, List<JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, String dbName, Map<String, JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, Connection con, List<JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, Connection con, Map<String, JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, List<JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.intexecuteStatement(String statementString, Map<String, JdbcTypedValue> replacementValues) Utility method which executes a given sql statement as prepared statement.Get the database name for this instance.Get the record mapper for this instance.Get the default table map for this instance.voidsetDatabaseName(String databaseName) Set the database name for this instance.protected voidSets the database name in the passed criteria to the table's default, if it is not already set.voidsetRecordMapper(RecordMapper<T> recordMapper) Set the record mapper for this instance.voidsetTableMap(TableMap tableMap) Set the default table map for this instance.
-
Constructor Details
-
BasePeerImpl
public BasePeerImpl()Default constructor -
BasePeerImpl
Constructor providing the objects to be injected as parameters.- Parameters:
recordMapper- a record mapper to map JDBC result sets to objectstableMap- the default table mapdatabaseName- the name of the database
-
-
Method Details
-
setRecordMapper
Set the record mapper for this instance.- Parameters:
recordMapper- the recordMapper to set
-
getRecordMapper
Get the record mapper for this instance.- Returns:
- the recordMapper
-
setTableMap
Set the default table map for this instance.- Parameters:
tableMap- the tableMap to set
-
getTableMap
Get the default table map for this instance.- Returns:
- the tableMap
-
setDatabaseName
Set the database name for this instance.- Parameters:
databaseName- the databaseName to set
-
getDatabaseName
Get the database name for this instance.- Returns:
- the databaseName
-
doDelete
Deletes rows from a database table.- Parameters:
criteria- defines the rows to be deleted, not null.- Returns:
- the number of deleted rows.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doDelete
Deletes rows from a table. This method is to be used during a transaction, otherwise use the doDelete(Criteria) method.- Parameters:
criteria- defines the rows to be deleted, not null.connection- the connection to use, not null.- Returns:
- the number of deleted rows.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doInsert
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.
- Parameters:
insertValues- Contains the values to insert, not null.- Returns:
- 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).
- Throws:
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.
- Parameters:
insertValues- Contains the values to insert, not null.connection- the connection to use for the insert, not null.- Returns:
- 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).
- Throws:
TorqueException- if a database error occurs.
-
doInsert
Executes a insert into...select statement.- Parameters:
toInsertInto- the columns in which to insert, not null.criteria- the criteria which selects the values to insert, not null.- Returns:
- the number of inserted rows, not null.
- Throws:
TorqueException- if a database error occurs.
-
doInsert
Executes a insert into...select statement.- Parameters:
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().- Returns:
- the number of inserted rows, not null.
- Throws:
TorqueException- if a database error occurs.
-
doInsert
public int doInsert(Column[] toInsertInto, Criteria criteria, Connection connection) throws TorqueException Executes a insert into...select statement.- Parameters:
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.- Returns:
- the number of inserted rows, not null.
- Throws:
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.- Parameters:
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.- Returns:
- the number of inserted rows, not null.
- Throws:
TorqueException- if a database error occurs.
-
addSelectColumns
Add all the columns needed to create a new object.- Parameters:
criteria- the Criteria to which the select columns should be added.
-
doSelect
Selects objects from a database.- Parameters:
criteria- object used to create the SELECT statement.- Returns:
- the list of selected objects, not null.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doSelect
Selects objects from a database within a transaction.- Parameters:
criteria- object used to create the SELECT statement.connection- the connection to use, not null.- Returns:
- the list of selected objects, not null.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doSelectAsStream
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.- Parameters:
criteria- object used to create the SELECT statement.connection- the connection to use, not null.- Returns:
- The results of the query as a Stream, not null.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doSelectSingleRecord
Selects at most one object from a database.- Parameters:
criteria- object used to create the SELECT statement.- Returns:
- the selected Object, or null if no object was selected.
- Throws:
TorqueException- If more than one record is selected or if an error occurs when processing the query.
-
doSelectSingleRecord
Selects at most one object from a database within a transaction.- Parameters:
criteria- object used to create the SELECT statement.connection- the connection holding the transaction, not null.- Returns:
- the selected Object, or null if no object was selected.
- Throws:
TorqueException- If more than one record is selected or if an error occurs when processing the query.
-
doSelect
Selects rows from a database an maps them to objects.- Type Parameters:
TT- Object type class- Parameters:
criteria- A Criteria specifying the records to select, not null.mapper- The mapper creating the objects from the resultSet, not null.- Returns:
- The results of the query, not null.
- Throws:
TorqueException- if querying the database fails.
-
doSelect
Selects rows from a database an maps them to objects.- Parameters:
query- the sql query to execute, not null.- Returns:
- The results of the query, not null.
- Throws:
TorqueException- if querying the database fails.
-
doSelect
Selects rows from a database an maps them to objects.- Parameters:
query- the SQL Query to execute, not null.connection- the database connection, not null.- Returns:
- The results of the query, not null.
- Throws:
TorqueException- if querying the database fails.
-
doSelectAsStream
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.- Parameters:
query- the SQL Query to execute, not null.connection- the database connection, not null.- Returns:
- The results of the query as a Stream, not null.
- Throws:
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.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The results of the query, not null.
- Throws:
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.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The results of the query, not null.
- Throws:
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.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The results of the query as a Stream, not null.
- Throws:
TorqueException- if querying the database fails.
-
doSelect
public <TT> List<TT> doSelect(Criteria criteria, RecordMapper<TT> mapper, Connection connection) throws TorqueException Performs a SQLselectusing a PreparedStatement.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The results of the query, not null.
- Throws:
TorqueException- Error performing database query.
-
doSelectAsStream
public <TT> Stream<TT> doSelectAsStream(Criteria criteria, RecordMapper<TT> mapper, Connection connection) throws TorqueException Performs a SQLselectusing 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.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The results of the query as a Stream, not null.
- Throws:
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.- Type Parameters:
TT- Object type class- Parameters:
criteria- A Criteria specifying the records to select, not null.mapper- The mapper creating the objects from the resultSet, not null.- Returns:
- The selected row, or null if no records was selected.
- Throws:
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.- Type Parameters:
TT- Object type class- Parameters:
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.- Returns:
- The selected row, or null if no records was selected.
- Throws:
TorqueException- if querying the database fails.
-
doUpdate
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).
- Parameters:
updateValues- Which columns to update with which values for which primary key value, not null.- Returns:
- the number of affected rows.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doUpdate
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).
- Parameters:
updateValues- Which columns to update with which values for which primary key value, not null.connection- the database connection to use.- Returns:
- the number of affected rows.
- Throws:
TorqueException- Any exceptions caught during processing will be rethrown wrapped into a TorqueException.
-
doUpdate
Executes an update against the database. The rows to be updated are selected usingcriteriaand updated using the values inupdateValues.- Parameters:
selectCriteria- selects which rows of which table should be updated, not null.updateValues- Which columns to update with which values, not null.- Returns:
- the number of affected rows.
- Throws:
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 usingcriteriaand updated using the values inupdateValues.- Parameters:
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.- Returns:
- the number of affected rows.
- Throws:
TorqueException- if updating fails.
-
executeStatement
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.- Parameters:
statementString- A String with the sql statement to execute.- Returns:
- The number of rows affected.
- Throws:
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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
TorqueException- if executing the statement fails.
-
executeStatement
public int executeStatement(String statementString, Map<String, JdbcTypedValue> replacementValues) throws TorqueExceptionUtility 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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
TorqueException- if executing the statement fails.
-
executeStatement
public int executeStatement(String statementString, String dbName, Map<String, JdbcTypedValue> replacementValues) throws TorqueExceptionUtility 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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
TorqueException- if executing the statement fails.
-
executeStatement
public int executeStatement(String statementString, Connection con, Map<String, JdbcTypedValue> replacementValues) throws TorqueExceptionUtility 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.- Parameters:
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.- Returns:
- The number of rows affected.
- Throws:
TorqueException- if executing the statement fails.
-
correctBooleans
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.- Parameters:
criteria- The criteria to be checked for booleanint and booleanchar columns.- Throws:
TorqueException- if the database map for the criteria cannot be retrieved.
-
correctBooleans
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.- Parameters:
columnValues- The value to be checked for booleanint and booleanchar columns.- Throws:
TorqueException- if the database map for the criteria cannot be retrieved.
-
setDbName
Sets the database name in the passed criteria to the table's default, if it is not already set.- Parameters:
crit- the criteria to set the database name in, not null.- Throws:
TorqueException- if unable to set the database name
-