javatools.db
Class DbUpdater

java.lang.Object
  |
  +--javatools.db.DbUpdater
Direct Known Subclasses:
DbReferencedUpdater

public class DbUpdater
extends java.lang.Object

A class used to update records from SQL tables. The constructor is not public. To obtain a DbUpdater call DbTable.updater(); Example: To update all the people who are (younger than 18 or older than 80) and whose name is "Fred"... to have a favourite_team of "Raiders";

 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbUpdater updater = people.deleter();
 updater.addColumn(people.getColumn("FAVOURITE_TEAM"), "Raiders");
 updater.setWhere(people.getColumn("AGE").lessThan(new Integer(18)).or(
  people.getColumn("AGE").greaterThan(new Integer(80))).and(
  people.getColumn("NAME").equal("FRED"));
 int numberOfPeopleUpdated = updater.execute();
 
This is equivilent to...
 UPDATE PEOPLE SET FAVOURITE_TEAM='Raiders' WHERE (AGE < 18 OR AGE > 80 ) AND NAME='Fred'
 
Note the use of equal(), NOT equals(). To get more fancy, to update the same group of people to have a favourite team the same as Bill's team, we use a sub-select...
 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbSelector bills_team = db.selector();
 bills_team.addColumn(people.getColumn("FAVOURITE_TEAM"));
 bills_team.setWhere(people.getColumn("NAME").equal("BILL"));
 DbUpdater updater = people.deleter();
 updater.addColumn(people.getColumn("FAVOURITE_TEAM"), bills_team);
 updater.setWhere(people.getColumn("AGE").lessThan(new Integer(18)).or(
  people.getColumn("AGE").greaterThan(new Integer(80))).and(
  people.getColumn("NAME").equal("FRED"));
 int numberOfPeopleUpdated = updater.execute();
 
This is equivilent to...
 UPDATE PEOPLE SET FAVOURITE_TEAM=(SELECT FAVOURITE_TEAM FROM PEOPLE WHERE NAME='Bill')
  WHERE (AGE < 18 OR AGE > 80 ) AND NAME='Fred'
 


Method Summary
 void addColumn(DbColumn into, java.lang.Object from)
          Add a column specification to update.
 int execute()
          Execute this command on the default connection.
 int execute(DbConnection dbcon)
          Execute this delete command on a specific connection.
 void setLists(java.util.List pIntoList, java.util.List pFromList)
          Sets in one time the column list and the value list.
 void setWhere(DbExpr where)
          Set the where condition on which records to update.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setWhere

public void setWhere(DbExpr where)
Set the where condition on which records to update.

Parameters:
where - The new where value

addColumn

public void addColumn(DbColumn into,
                      java.lang.Object from)
Add a column specification to update. The new value can either be a raw value - Integer, String, java.sql.Date etc. Or it can be a DbSelector in the case of a sub-select.

Parameters:
into - The column to update.
from - The new value.

setLists

public void setLists(java.util.List pIntoList,
                     java.util.List pFromList)
Sets in one time the column list and the value list.

Parameters:
pIntoList - The column list.
pFromList - The value list.

execute

public int execute(DbConnection dbcon)
            throws DbException
Execute this delete command on a specific connection.

Parameters:
dbcon - The connection to use.
Returns:
The number of record affected.
Throws:
DbException - If something goes wrong.

execute

public int execute()
            throws DbException
Execute this command on the default connection.

Returns:
The number of record affected.
Throws:
DbException - If something goes wrong.