Class BasePeerImpl<T>

    • Constructor Detail

      • BasePeerImpl

        public BasePeerImpl()
        Default constructor
      • BasePeerImpl

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

      • setRecordMapper

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

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

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

        public TableMap getTableMap()
        Get the default table map for this instance.
        Returns:
        the tableMap
      • setDatabaseName

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

        public String getDatabaseName()
        Get the database name for this instance.
        Returns:
        the databaseName
      • doDelete

        public int doDelete​(Criteria criteria)
                     throws TorqueException
        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

        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.
        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

        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.

        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

        public int doInsert​(Column[] toInsertInto,
                            Criteria criteria)
                     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.
        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)
                     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.
        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

        public void addSelectColumns​(Criteria criteria)
        Add all the columns needed to create a new object.
        Parameters:
        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.
        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

        public List<T> doSelect​(Criteria criteria,
                                Connection connection)
                         throws TorqueException
        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

        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.
        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

        public T doSelectSingleRecord​(Criteria criteria)
                               throws TorqueException
        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

        public T doSelectSingleRecord​(Criteria criteria,
                                      Connection connection)
                               throws TorqueException
        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

        public <TT> List<TT> doSelect​(Criteria criteria,
                                      RecordMapper<TT> mapper)
                               throws TorqueException
        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

        public List<T> doSelect​(String query)
                         throws TorqueException
        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

        public List<T> doSelect​(String query,
                                Connection connection)
                         throws TorqueException
        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

        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.
        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 SQL select using 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 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.
        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

        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).

        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

        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).

        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

        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.
        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 using criteria and updated using the values in updateValues.
        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

        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.
        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 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, 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 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, 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 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, 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

        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.
        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

        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.
        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

        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.
        Parameters:
        crit - the criteria to set the database name in, not null.
        Throws:
        TorqueException - if unable to set the database name