com.triactive.jdo.store
Class StoreManager

java.lang.Object
  extended bycom.triactive.jdo.store.StoreManager
All Implemented Interfaces:
SchemaManager

public class StoreManager
extends java.lang.Object
implements SchemaManager

Manages the contents of a data store (aka database schema) on behalf of a particular PersistenceManagerFactory and all its persistent instances.

The store manager's responsibilities include:

A store manager's knowledge of its schema's contents is not necessarily complete. It is aware of only those tables whose classes have somehow been accessed, directly or indirectly, by the application during the life of the store manager object.

Author:
Mike Martin

Constructor Summary
(package private) StoreManager(PersistenceManagerFactoryImpl pmf, java.lang.String userName, java.lang.String password)
          Constructs a new StoreManager.
 
Method Summary
 void addClasses(java.lang.Class[] classes)
          Adds the given persistence-capable classes to the store manager's set of active classes ready for persistence.
 void delete(StateManager sm)
          Deletes a persistent object from the database.
 void dropAllTables()
          Drops all tables in the schema.
 void dropTablesFor(java.lang.Class[] classes)
          Removes from the schema any existing database tables used in persisting the given PersistenceCapable class(es).
 void fetch(StateManager sm, int[] fieldNumbers)
          Fetches a persistent object from the database.
 ClassBaseTable getClassBaseTable(java.lang.Class c)
          Returns the primary table serving as backing for the given class.
 java.lang.Class getClassForObjectID(java.lang.Object id)
          Returns the class corresponding to the given object ID.
(package private)  java.util.List getColumnInfo(SQLIdentifier tableName, java.sql.Connection conn)
          Returns the column info for a database table.
 DatabaseAdapter getDatabaseAdapter()
          Returns the database adapter used by this store manager.
(package private)  DeleteRequest getDeleteRequest(ClassBaseTable cbt)
          Returns a request object that will delete a row from the given table.
 javax.jdo.Extent getExtent(PersistenceManager pm, java.lang.Class c, boolean subclasses)
           
(package private)  FetchRequest getFetchRequest(ClassBaseTable cbt, int[] fieldNumbers)
          Returns a request object that will fetch a row from the given table.
(package private)  java.util.List getForeignKeyInfo(SQLIdentifier tableName, java.sql.Connection conn)
          Returns the foreign key info for a database table.
(package private)  InsertRequest getInsertRequest(ClassBaseTable cbt)
          Returns a request object that will insert a row in the given table.
(package private)  int getNextOIDHiValue(int classID)
          Returns the next OID high-order value for IDs of the given class.
 Query getQuery(PersistenceManager pm, java.lang.Object query)
           
 Query getQuery(java.lang.String language, PersistenceManager pm, java.lang.Object query)
           
 java.lang.String getSchemaName()
          Returns the name of the database schema.
(package private)  ClassTable getTable(java.lang.Class c)
          Returns the primary table serving as backing for the given class.
(package private)  JDOTable getTable(MetaData md)
          Returns the JDO table having the given metadata, if any.
 JDOTable getTable(SQLIdentifier name)
          Returns the JDO table having the given SQL name, if any.
 int getTableType(SQLIdentifier tableName, java.sql.Connection conn)
          Returns the type of a database table.
(package private)  UpdateRequest getUpdateRequest(ClassBaseTable cbt, int[] fieldNumbers)
          Returns a request object that will update a row in the given table.
 void insert(StateManager sm)
          Inserts a persistent object into the database.
 void logSQLWarnings(java.sql.Connection conn)
           
 void logSQLWarnings(java.sql.ResultSet rs)
           
 void logSQLWarnings(java.sql.SQLWarning warning)
          Logs SQL warnings to the common log.
 void logSQLWarnings(java.sql.Statement stmt)
           
(package private)  MapTable newMapTable(ClassBaseTable cbt, FieldMetaData fmd)
          Called by Mapping objects in the midst of StoreManager.addClasses() to request the creation of a map table.
 java.lang.Object newObjectID(java.lang.Class c)
          Returns a new, unique ID for an object of the given class.
(package private)  SetTable newSetTable(ClassBaseTable cbt, FieldMetaData fmd)
          Called by Mapping objects in the midst of StoreManager.addClasses() to request the creation of a set table.
 void reset()
          Clears all knowledge of tables, cached requests, metadata, etc and resets the store manager to its initial state.
 void resolveIdentifierMacro(MacroString.IdentifierMacro im)
          Resolves an identifier macro.
 boolean tableExists(SQLIdentifier tableName, java.sql.Connection conn)
          Tests if a database table exists.
 void update(StateManager sm, int[] fieldNumbers)
          Updates a persistent object in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StoreManager

StoreManager(PersistenceManagerFactoryImpl pmf,
             java.lang.String userName,
             java.lang.String password)
Constructs a new StoreManager. On successful return the new StoreManager will have successfully connected to the database with the given credentials and determined the schema name, but will not have inspected the schema contents any further. The contents (tables, views, etc.) will be subsequently created and/or validated on-demand as the application accesses persistent classes.

To avoid creating unnecessary redundant StoreManagers, new StoreManagers should always be obtained from the StoreManagerFactory, rather than constructed directly.

Parameters:
pmf - The corresponding PersistenceManagerFactory. This factory's non-transactional data source will be used to get database connections as needed to perform management functions.
userName - The database user name.
password - The database user's password.
Throws:
javax.jdo.JDODataStoreException - If the database could not be accessed or the name of the schema could not be determined.
See Also:
StoreManagerFactory
Method Detail

reset

public void reset()
Clears all knowledge of tables, cached requests, metadata, etc and resets the store manager to its initial state.

Specified by:
reset in interface SchemaManager

getDatabaseAdapter

public DatabaseAdapter getDatabaseAdapter()
Returns the database adapter used by this store manager.

Returns:
The database adapter used by this store manager.

getSchemaName

public java.lang.String getSchemaName()
Description copied from interface: SchemaManager
Returns the name of the database schema.

Specified by:
getSchemaName in interface SchemaManager
Returns:
The name of the database schema, or null if the DBMS does not support the concept of schemas.

logSQLWarnings

public void logSQLWarnings(java.sql.SQLWarning warning)
Logs SQL warnings to the common log. Should be called after any operation on a JDBC Statement or ResultSet object as:
 storeMgr.logSQLWarnings(obj.getWarnings());
 
If any warnings were generated, the entire list of them are logged to System.err.

Parameters:
warning - the value returned from getWarnings().

logSQLWarnings

public void logSQLWarnings(java.sql.Connection conn)

logSQLWarnings

public void logSQLWarnings(java.sql.Statement stmt)

logSQLWarnings

public void logSQLWarnings(java.sql.ResultSet rs)

addClasses

public void addClasses(java.lang.Class[] classes)
Adds the given persistence-capable classes to the store manager's set of active classes ready for persistence. The act of adding a class to the store manager causes any necessary database objects (tables, views, constraints, indexes, etc.) to be created.

Other StoreManager methods may cause classes to be added as a side effect.

Specified by:
addClasses in interface SchemaManager
Parameters:
classes - The class(es) to be added.

newSetTable

SetTable newSetTable(ClassBaseTable cbt,
                     FieldMetaData fmd)
Called by Mapping objects in the midst of StoreManager.addClasses() to request the creation of a set table.

Parameters:
cbt - The base table for the class containing the set.
fmd - The field metadata describing the set field.

newMapTable

MapTable newMapTable(ClassBaseTable cbt,
                     FieldMetaData fmd)
Called by Mapping objects in the midst of StoreManager.addClasses() to request the creation of a map table.

Parameters:
cbt - The base table for the class containing the map.
fmd - The field metadata describing the map field.

dropTablesFor

public void dropTablesFor(java.lang.Class[] classes)
Description copied from interface: SchemaManager
Removes from the schema any existing database tables used in persisting the given PersistenceCapable class(es).

This method will fail if the relevant tables are depended upon by others used for classes that are not being removed. In case of failure, it is unspecified whether or not any tables were actually removed.

After dropping tables this method performs an implicit SchemaManager.reset().

Specified by:
dropTablesFor in interface SchemaManager
Parameters:
classes - The class(es) whose tables are to be removed.

dropAllTables

public void dropAllTables()
Description copied from interface: SchemaManager
Drops all tables in the schema. This empties the schema of all database objects managed by TJDO. All objects not managed by TJDO are left untouched.

After dropping tables this method performs an implicit SchemaManager.reset().

Specified by:
dropAllTables in interface SchemaManager

getTable

public JDOTable getTable(SQLIdentifier name)
Returns the JDO table having the given SQL name, if any. Returns null if no such table is (yet) known to the store manager.

Parameters:
name - The name of the table.
Returns:
The corresponding JDO table, or null.

getTable

JDOTable getTable(MetaData md)
Returns the JDO table having the given metadata, if any. Returns null if no such table is (yet) known to the store manager.

Parameters:
md - The metadata for the table.
Returns:
The corresponding JDO table, or null.

getTable

ClassTable getTable(java.lang.Class c)
Returns the primary table serving as backing for the given class. If the class is not yet known to the store manager, addClasses(java.lang.Class[]) is called to add it.

Parameters:
c - The class whose table is be returned.
Returns:
The corresponding class table.
Throws:
NoExtentException - If the given class is not persistence-capable.

getClassBaseTable

public ClassBaseTable getClassBaseTable(java.lang.Class c)
Returns the primary table serving as backing for the given class. If the class is not yet known to the store manager, addClasses(java.lang.Class[]) is called to add it.

This method is functionally equivalent to getTable(Class) except that it will only return base tables (not views).

Parameters:
c - The class whose table is be returned.
Returns:
The corresponding class table.
Throws:
NoExtentException - If the given class is not persistence-capable.
ViewNotSupportedException - If the given class is backed by a view.

getClassForObjectID

public java.lang.Class getClassForObjectID(java.lang.Object id)
Returns the class corresponding to the given object ID.

Parameters:
id - The JDO identity of some object.
Returns:
The class of the corresponding object.
Throws:
java.lang.ClassCastException - If the type of ID is not recognized (OID or SCOID).

getExtent

public javax.jdo.Extent getExtent(PersistenceManager pm,
                                  java.lang.Class c,
                                  boolean subclasses)

getQuery

public Query getQuery(PersistenceManager pm,
                      java.lang.Object query)

getQuery

public Query getQuery(java.lang.String language,
                      PersistenceManager pm,
                      java.lang.Object query)

newObjectID

public java.lang.Object newObjectID(java.lang.Class c)
Returns a new, unique ID for an object of the given class.

Parameters:
c - The class of the object.
Returns:
A new object ID.

getNextOIDHiValue

int getNextOIDHiValue(int classID)
Returns the next OID high-order value for IDs of the given class.

Parameters:
classID - The class ID number of the class.
Returns:
The next high-order OID value.
Throws:
javax.jdo.JDODataStoreException - If an error occurs in accessing or updating the schema table.

insert

public void insert(StateManager sm)
Inserts a persistent object into the database.

Parameters:
sm - The state manager of the object to be inserted.

fetch

public void fetch(StateManager sm,
                  int[] fieldNumbers)
Fetches a persistent object from the database.

Parameters:
sm - The state manager of the object to be fetched.
fieldNumbers - The numbers of the fields to be fetched.

update

public void update(StateManager sm,
                   int[] fieldNumbers)
Updates a persistent object in the database.

Parameters:
sm - The state manager of the object to be updated.
fieldNumbers - The numbers of the fields to be updated.

delete

public void delete(StateManager sm)
Deletes a persistent object from the database.

Parameters:
sm - The state manager of the object to be deleted.

getInsertRequest

InsertRequest getInsertRequest(ClassBaseTable cbt)
Returns a request object that will insert a row in the given table. The store manager will cache the request object for re-use by subsequent requests to the same table.

Parameters:
cbt - The table into which to insert.
Returns:
An insertion request object.

getFetchRequest

FetchRequest getFetchRequest(ClassBaseTable cbt,
                             int[] fieldNumbers)
Returns a request object that will fetch a row from the given table. The store manager will cache the request object for re-use by subsequent requests to the same table.

Parameters:
cbt - The table from which to fetch.
fieldNumbers - The field numbers corresponding to the columns to be fetched. Field numbers whose columns exist in supertables will be ignored.
Returns:
A fetch request object.

getUpdateRequest

UpdateRequest getUpdateRequest(ClassBaseTable cbt,
                               int[] fieldNumbers)
Returns a request object that will update a row in the given table. The store manager will cache the request object for re-use by subsequent requests to the same table.

Parameters:
cbt - The table in which to update.
fieldNumbers - The field numbers corresponding to the columns to be updated. Field numbers whose columns exist in supertables will be ignored.
Returns:
An update request object.

getDeleteRequest

DeleteRequest getDeleteRequest(ClassBaseTable cbt)
Returns a request object that will delete a row from the given table. The store manager will cache the request object for re-use by subsequent requests to the same table.

Parameters:
cbt - The table from which to delete.
Returns:
A deletion request object.

getTableType

public int getTableType(SQLIdentifier tableName,
                        java.sql.Connection conn)
                 throws java.sql.SQLException
Returns the type of a database table.

Parameters:
tableName - The name of the table (or view).
conn - A JDBC connection to the database.
Returns:
one of the TABLE_TYPE_* values from Table.
Throws:
java.sql.SQLException
See Also:
Table

getColumnInfo

java.util.List getColumnInfo(SQLIdentifier tableName,
                             java.sql.Connection conn)
                       throws java.sql.SQLException
Returns the column info for a database table. This should be used instead of making direct calls to DatabaseMetaData.getColumns().

Where possible, this method loads and caches column info for more than just the table being requested, improving performance by reducing the overall number of calls made to DatabaseMetaData.getColumns() (each of which usually results in one or more database queries).

Parameters:
tableName - The name of the table (or view).
conn - A JDBC connection to the database.
Returns:
A list of ColumnInfo objects describing the columns of the table. The list is in the same order as was supplied by getColumns(). If no column info is found for the given table, an empty list is returned.
Throws:
java.sql.SQLException
See Also:
ColumnInfo

getForeignKeyInfo

java.util.List getForeignKeyInfo(SQLIdentifier tableName,
                                 java.sql.Connection conn)
                           throws java.sql.SQLException
Returns the foreign key info for a database table. This should be used instead of making direct calls to DatabaseMetaData.getImportedKeys() or DatabaseMetaData.getExportedKeys().

Parameters:
tableName - The name of the table (or view).
conn - A JDBC connection to the database.
Returns:
A list of ForeignKeyInfo objects describing the columns of the table's foreign keys. The list is in the same order as was supplied by get??portedKeys(). If no column info is found for the given table, an empty list is returned.
Throws:
java.sql.SQLException
See Also:
ForeignKeyInfo

tableExists

public boolean tableExists(SQLIdentifier tableName,
                           java.sql.Connection conn)
                    throws java.sql.SQLException
Tests if a database table exists.

Parameters:
tableName - The name of the table (or view).
conn - A JDBC connection to the database.
Returns:
true if the table exists in the database, false otherwise.
Throws:
java.sql.SQLException

resolveIdentifierMacro

public void resolveIdentifierMacro(MacroString.IdentifierMacro im)
Resolves an identifier macro. The public fields clazz, fieldName, and subfieldName of the given macro are taken as inputs, and the public value field is set to the SQL identifier of the corresponding database table or column.

Parameters:
im - The macro to resolve.


Copyright © 2001 TriActive, Inc. All Rights Reserved.