FileDocCategorySizeDatePackage
DatabaseSetPeer.javaAPI DocExample4395Sat Feb 01 07:16:52 GMT 1997imaginary.persist

DatabaseSetPeer

public abstract class DatabaseSetPeer extends Object implements PersistentSetPeer
The DatabaseSetPeer performs database queries on behalf of PersistentSet sets. Subclasses must provide methods specifying the class name of persistent objects it should create from database rows as well as the proper SELECT SQL for restoring objects from the database.

Fields Summary
Constructors Summary
Methods Summary
public abstract java.lang.StringgetPersistentClass(java.util.Hashtable h)
Subclasses must implement this method to provide the DatabaseSetPeer with the name of a subclasses of Persistent to instantiate for each row returned from the database.

param
h the Hashtable of values returned from the database query
return
the name of the class to instantiate for this row

public abstract java.lang.StringgetSql(java.util.Hashtable h)
Subclasses must implement this method to provide the DatabaseSetPeer with the proper SQL for performing a query. The Hashtable may be null.

param
h a list of values with which to limit the query
return
the proper SELECT SQL for this set

public voidrestore(PersistentSet set, Transaction trans)
Implementation of the PersistentSetPeer method for restoring a set without query criteria.

param
set the set being restored
param
trans the transaction to use for the restore
exception
imaginary.persist.PersistenceException An error occurred restoring from the database.
see
imaginary.persist.PersistentSetPeer#restore

        restore(set, trans, null); 
    
public voidrestore(PersistentSet set, Transaction trans, java.util.Hashtable data)
Implementation of the PersistentSetPeer method for restoring based on specified query criteria

param
set the set being restored
param
trans the transaction to use for the restore
param
data the query criteria
exception
imaginary.persist.PersistenceException An error occurred restoring from the database.
see
imaginary.persist.PersistentSetPeer#restore

        ResultSetMetaData meta = null;
        Connection connection = null;
        Statement statement = null;
        ResultSet results = null;
        String sql = getSql(data); // Get the SQL from the subclass
        
        try {
            // Get the Connection from the DatabaseTransaction
            connection = ((DatabaseTransaction)trans).getConnection();
            // Create a Statement from the Connection
            statement = connection.createStatement();
            // Get the results
            results = statement.executeQuery(sql);
            // Get the meta data
            meta = results.getMetaData();
            // While there are rows in the result set
            while( results.next() ) {
                Hashtable h = new Hashtable(); // Store the results here
                String class_name;
                Persistent p;

                // For each column in the row
                for(int i=1; i<=meta.getColumnCount(); i++) {
                    Object ob = results.getObject(i); // Get the value
                    
                    h.put(meta.getColumnLabel(i), ob); // Stick it in Hashtable
                }
                // Get the name of the class to create from this row
                class_name = getPersistentClass(h);
                // Get a Persistent from the Persistent
                p = Persistent.getPersistent(trans, h, class_name);
                // Add the Persistent to the set
                set.addPersistent(p);
            }
            statement.close(); // closing statement closes results
            connection.commit(); // Release database locks
        }
        catch( SQLException e ) {
            if( statement != null ) {
                try { statement.close(); } // closes results too
                catch( SQLException e2 ) { }
            }
            throw new PersistenceException(e);
        }