net.spy.db
Class GetPK

java.lang.Object
  extended by net.spy.SpyObject
      extended by net.spy.db.GetPK

public class GetPK
extends SpyObject

Primary key generator. This is an extensible singleton that provides access to a database-backed set of primary keys along with a fetch-ahead cache of those keys.

The default implementation assumes you have the following table (primary_key) in the database the configuration defines:

primary_keys
Column Name Column Type Column Description
table_name varchar The name of the table (or other resource) for which we are generating the given primary key. All table names in this column must be in lowercase as the input from the user will be lowercased on key retrieval.
primary_key numeric The next primary key value issued.
incr_value numeric The amount to increment the primary key each time.

Other schemas may exist as long as they fit into the ``update something, select something'' model and they operate as described below.


Constructor Summary
protected GetPK()
          Constructor for an extensible Singleton.
 
Method Summary
static GetPK getInstance()
          Get the instance of GetPK.
 BigDecimal getPrimaryKey(SpyConfig conf, String table)
          Get a primary key from the database described in the given config.
protected  DBSP getSelectDBSP(Connection conn)
          Get the DBSP required for selecting primary key information back out of the primary key table.
protected  DBSP getUpdateDBSP(Connection conn)
          Get the DBSP required for updating the primary key table.
static void setInstance(GetPK to)
          Set the singleton instance.
 
Methods inherited from class net.spy.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GetPK

protected GetPK()
Constructor for an extensible Singleton.

Method Detail

getInstance

public static GetPK getInstance()
Get the instance of GetPK.

Returns:
the instance

setInstance

public static void setInstance(GetPK to)
Set the singleton instance.

Parameters:
to - the new singleton instance

getPrimaryKey

public BigDecimal getPrimaryKey(SpyConfig conf,
                                String table)
                         throws SQLException
Get a primary key from the database described in the given config.

Parameters:
conf - the configuration
table - the table for which the key is needed
Returns:
the key
Throws:
SQLException - if there's a problem getting the key

getUpdateDBSP

protected DBSP getUpdateDBSP(Connection conn)
                      throws SQLException
Get the DBSP required for updating the primary key table.

A subclass may override this to change the behavior of the first part of the ``fetch from db'' stage. The DBSP returned will take exactly one parameter: table_name and will be called via executeUpdate. The update must update exactly one row. Any more or fewer will cause the process to fail and an exception will be thrown.

For an example implementation, please see UpdatePrimaryKey (this is the default).

Parameters:
conn - the connection to use (already in a transaction)
Returns:
the required DBSP
Throws:
SQLException - if there's a problem getting the DBSP

getSelectDBSP

protected DBSP getSelectDBSP(Connection conn)
                      throws SQLException
Get the DBSP required for selecting primary key information back out of the primary key table.

A subclass may override this method to change the behavior of the select statement that finds the range of results for a table. The DBSP returned will take exactly one parameter: table_name and return a result set containing at least the following two columns:

The ResultSet must contain exactly one row. Any more or fewer will cause the process to fail and an exception will be thrown.

For an example implementation, please see SelectPrimaryKey (this is the default).

Parameters:
conn - the connection to use (already in a transaction)
Returns:
the required DBSP
Throws:
SQLException - if there's a problem getting the DBSP


Copyright © 1995-2007 SPY Internetworking. All Rights Reserved.