Thursday, November 08, 2012

Types of DAO

  • Table DAO Code

Table DAO code is the most widely used Data Access Object. Each Table DAO represents a single table and contains methods to insert, update, and delete single rows using the following signatures (using an example Customer table):
public void insert(Customer customer) throws CustomerDaoException
public void update(CustomerPk pk, Customer customer) throws CustomerDaoException
public void delete(CustomerPk pk) throws CustomerDaoException
In addition to this, there are also numerous 'finder' methods generated. FireStorm/DAO creates default finders when you first import a schema but these are fully customizable. Some examples of finder methods:
public Customer findByPrimaryKey(CustomerPk pk) throws CustomerDaoException;
public Customer[] findWhereLastNameEquals(String lastName) throws CustomerDaoException;
public Customer[] findByCountry(int countryId) throws CustomerDaoException;
  • View DAO Code
The View DAO is generated for each view in the database. This DAO code offers the same finder methods as the Table DAO code but obviously does not provide the insert, update, and delete operations because views are read-only.

  • Custom DAO Code

Custom DAO code is used when you have a complex SQL query that goes beyond the simple CRUD (create, update, delete) operations on a single table. Examples would be SQL queries that perform a join between several tables, queries that performs aggregration using the GROUP BY operator, and bulk update or delete queries.
Example queries that the Custom DAO code feature in FireStorm/dAO can support:
SELECT a.*, b.* FROM a, b WHERE a.id = b.id AND b.install_date between ? and ?
DELETE customer WHERE status = ? AND create_date = ?
SELECT product, count(*) FROM product WHERE download_date > ? GROUP BY product

No comments: