javatools.db
Class DbInserter

java.lang.Object
  |
  +--javatools.db.DbInserter

public class DbInserter
extends java.lang.Object

A class used to insert records into SQL tables. The constructor is not public. To obtain a DbInserter call DbTable.inserter(); Example: To insert a record into the people table...

 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbInserter inserter = people.inserter();
 inserter.addColumn(people.getColumn("NAME"), "Fred"));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), "Raiders");
 inserter.addColumn(people.getColumn("AGE"), new Integer(30));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) VALUES('Fred', 'Raiders', 30)
 
The same thing as above can be achieved using a SELECT clause, and this can lead us to creating much more complex expressions...
 DbDatabase db = ...;
 DbSelector selector = db.selector();
 DbTable people = db.getTable("PEOPLE");
 DbInserter inserter = people.inserter(selector);
 inserter.addColumn(people.getColumn("NAME"), selector.addColumn("Fred")));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), selector.addColumn("Raiders"));
 inserter.addColumn(people.getColumn("AGE"), selector.addColumn(new Integer(30)));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) SELECT 'Fred', 'Raiders', 30
 
To get more fancy we can insert data that has been selected from another table. To insert all the people from the PLAYERS table into the PEOPLE table who are older than 20, and we set their favourite team to be the team they play for...
 DbDatabase db = ...;
 DbSelector selector = db.selector();
 DbTable people = db.getTable("PEOPLE");
 DbTable players = db.getTable("PLAYERS");
 DbInserter inserter = people.inserter(selector);
 inserter.addColumn(people.getColumn("NAME"), selector.addColumn(players.getColumn("NAME")));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), selector.addColumn(players.getColumn("TEAM")));
 inserter.addColumn(people.getColumn("AGE"), selector.addColumn(players.getColumn("AGE")));
 selector.setWhere(players.getColumn("AGE").greaterThan(new Integer(20)));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) SELECT NAME, TEAM, AGE FROM PLAYERS WHERE AGE > 20
 


Field Summary
(package private)  java.util.List fromList
           
(package private)  java.util.List intoList
           
(package private)  DbSelector selector
           
(package private)  DbTable table
           
 
Constructor Summary
(package private) DbInserter(DbTable table)
           
(package private) DbInserter(DbTable table, DbSelector selector)
           
 
Method Summary
 void addColumn(DbColumn into, java.lang.Object from)
          Specify the value of a column to add.
 int execute()
          Execute this command on the default connection.
 int execute(DbConnection dbcon)
          Execute this command on a specific connection.
(package private)  java.lang.String getQueryString()
           
(package private)  java.lang.String getValuesQueryString()
           
 int setSqlValues(java.sql.PreparedStatement stmt, int i)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

table

DbTable table

selector

DbSelector selector

intoList

java.util.List intoList

fromList

java.util.List fromList
Constructor Detail

DbInserter

DbInserter(DbTable table,
           DbSelector selector)

DbInserter

DbInserter(DbTable table)
Method Detail

setSqlValues

public int setSqlValues(java.sql.PreparedStatement stmt,
                        int i)
                 throws DbException,
                        java.sql.SQLException
DbException
java.sql.SQLException

addColumn

public void addColumn(DbColumn into,
                      java.lang.Object from)
Specify the value of a column to add.

Parameters:
into - The column we are inserting into.
from - The column from a selector that we are getting a value from.

execute

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

Parameters:
dbcon - Description of Parameter
Returns:
The number of record affected.
Throws:
DbException - Description of Exception

execute

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

Returns:
The number of record affected.
Throws:
DbException - Description of Exception

getValuesQueryString

java.lang.String getValuesQueryString()

getQueryString

java.lang.String getQueryString()
                          throws DbException
DbException