FileDocCategorySizeDatePackage
CustomerPeer.javaAPI DocExample3253Sun Mar 02 21:42:02 GMT 1997bank.server

CustomerPeer.java

/**
 * The CustomerPeer handles database access for the Customer business
 * object.  The DatabasePeer that it extends specifically prescribes
 * for it methods that allow it to create SQL statements.
 */
package bank.server;

import imaginary.persist.Persistent;

public class CustomerPeer extends imaginary.persist.DatabasePeer {
    /**
     * The column header for the customer ID field in the t_customer table
     */
    static public final String CUST_ID    = "t_customer.cust_id";

    /**
     * The column header for the last_name field in the t_customer table
     */
    static public final String FIRST_NAME = "t_customer.first_name";

    /**
     * The column header for the last name field in the t_customer table
     */
    static public final String LAST_NAME  = "t_customer.last_name";

    /**
     * Provides the SQL used to insert a customer object into the database.
     * @param p the Customer being inserted
     * @return the SQL to insert the customer
     */
    protected String getInsertSql(Persistent p) {
        Customer cust = (Customer)p;        // Cast it to a Customer
        int cust_id = cust.getId();         // The customer id
        String last = cust.getLastName();   // The last name
        String first = cust.getFirstName(); // The first name

        // INSERT INTO t_customer (cust_id, last_name, first_name)
        // VALUES (1, 'the Clown', 'Bozo')
        return "INSERT INTO t_customer (cust_id, last_name, first_name) " +
            "VALUES (" + cust_id + ", '" + last + "', '" + first +
            "')";
    }

    /**
     * Provides the SQL used to delete a customer from the database
     * @param p the Customer being deleted
     * @return the SQL used to delete the customer
     */
    public String getRemoveSql(Persistent p) {
        // DELETE FROM t_customer WHERE cust_id = 1
        return "DELETE FROM t_customer WHERE cust_id = " + p.getId();
    }

    /**
     * Provides the SQL used to restore a customer from the database
     * @param p the Customer being restored (only the ID field is filled in)
     * @return the SQL used to restore that customer
     */
    public String getRestoreSql(Persistent p) {
        // SELECT * from t_customer WHERE cust_id = 1
        return "SELECT * from t_customer WHERE cust_id = " +
            p.getId();
    }

    /**
     * Provides the SQL used to update a customer in the database
     * @param p the Customer being updated
     * @return the SQL used to UPDATE that customer
     */
    public String getUpdateSql(Persistent p) {
        Customer cust = (Customer)p;             // Cast p to Customer
        int cust_id = cust.getId();              // Get the cust_id
        String last_name = cust.getLastName();   // Get the last name
        String first_name = cust.getFirstName(); // Get the first name

        // UPDATE t_customer
        // SET last_name  = 'the Clown',
        //     first_name = 'Bozo'
        // WHERE cust_id  = 1
        return "UPDATE t_customer " +
            "SET last_name = '" + last_name + "', " +
            "first_name = '" + first_name + "' " +
            "WHERE cust_id = " + cust_id;
    }
}