org.jpox.store.rdbms.scostore
Class ElementContainerStore

java.lang.Object
  extended byorg.jpox.store.rdbms.scostore.BaseContainerStore
      extended byorg.jpox.store.rdbms.scostore.ElementContainerStore
Direct Known Subclasses:
AbstractArrayStore, AbstractCollectionStore

public abstract class ElementContainerStore
extends org.jpox.store.rdbms.scostore.BaseContainerStore

Representation of the store of an element-based container. This is used to represent either a collection or an array. There are 3 types of situation that we try to cater for with respect to elements.

Version:
$Revision: 1.25 $

Nested Class Summary
 class ElementContainerStore.ElementInfo
          Inner class wrapping the information required for a valid element type.
 
Field Summary
protected  java.lang.String clearStmt
          Statement for clearing the container.
protected  ClassLoaderResolver clr
          ClassLoader resolver.
protected  DatastoreContainerObject containerTable
          Table containing the link between owner and element.
protected  DatastoreAdapter dba
          Datastore adapter in use by this store.
protected  ElementContainerStore.ElementInfo[] elementInfo
          Information for the elements of this container.
protected  JavaTypeMapping elementMapping
          Mapping for the element.
protected  boolean elementsAreEmbedded
          Whether the elements are embedded.
protected  boolean elementsAreSerialised
          Whether the elements are serialised.
protected  java.lang.String elementType
          Type of the element.
protected  DatastoreIdentifier elmIdentifier
          Identifier for elements in JDOQL queries.
protected  AbstractClassMetaData emd
          MetaData for the "element-type" class.
protected  boolean iterateUsingDiscriminator
          Flag to set whether the iterator statement will use a discriminator or not.
protected static Localiser LOCALISER
          Localiser for messages.
protected  JavaTypeMapping orderMapping
          Mapping for an ordering column to allow for duplicates in the container.
protected  AbstractPropertyMetaData ownerFieldMetaData
          MetaData for the field in the owner with this container.
protected  JavaTypeMapping ownerMapping
          Mapping to the owner of the container.
protected  JavaTypeMapping relationDiscriminatorMapping
          Optional mapping to distinguish elements of one collection from another when sharing the join table.
protected  java.lang.String relationDiscriminatorValue
          Value to use to discriminate between elements of this collection from others using the same join table.
protected  java.lang.String sizeStmt
          Statement for getting the size of the container.
protected  StoreManager storeMgr
          Manager for the store.
protected  DatastoreIdentifier thisIdentifier
          Identifier for the container in JDOQL queries.
 
Constructor Summary
protected ElementContainerStore(StoreManager storeMgr, ClassLoaderResolver clr)
          Constructor.
 
Method Summary
 void clear(StateManager ownerSM)
          Clear the association from owner to all elements
protected  java.sql.ResultSet executeQuery(java.lang.String stmt, java.sql.PreparedStatement ps)
          Method to execute a PreparedStatement query, and return the ResultSet.
protected  int executeUpdate(java.lang.String stmt, java.sql.PreparedStatement ps)
          Method to execute a PreparedStatement.
protected  java.lang.String getAddStmt()
          Generates the statement for adding items.
protected  java.lang.String getClearStmt()
          Generate statement for clearing the container.
protected  ElementContainerStore.ElementInfo[] getElementInformationForClass()
          Convenience method to find the element information relating to the element type.
 java.lang.String getElementType()
          Accessor for the element type stored in this container.
 JavaTypeMapping getOwnerMapping()
          Accessor for the owner mapping.
protected  java.lang.String getSizeStmt()
          Generate statement for getting the size of the container.
protected  StateManager getStateManagerForEmbeddedPCObject(StateManager sm, java.lang.Object obj, JoinTable table)
          Method to return the StateManager for an embedded PC object (element, key, value).
 StoreManager getStoreManager()
          Accessor for the RDBMSManager.
 boolean hasOrderMapping()
          Accessor for whether the store has an order mapping, to allow for duplicates or ordering.
protected  void initialiseStatements()
          Method to initialise the statements being used.
protected  boolean isEmbeddedMapping(JavaTypeMapping mapping)
          Check if the mapping correspond to a non pc object or embedded field
abstract  java.util.Iterator iterator(StateManager ownerSM)
          Accessor for an iterator through the container elements.
 Query.ResultObjectFactory newResultObjectFactory(StateManager sm, QueryExpression stmt, boolean ignoreCache, boolean useFetchPlan)
          Method to generate a new result object factory from which to retrieve element objects.
protected  int populateElementDiscriminatorInStatement(PersistenceManager pm, java.sql.PreparedStatement ps, int jdbcPosition, boolean includeSubclasses, ElementContainerStore.ElementInfo info)
          Convenience method to populate the passed PreparedStatement with the value from the element discriminator, optionally including all subclasses of the element type.
protected  int populateElementInStatement(PersistenceManager pm, java.sql.PreparedStatement ps, java.lang.Object element, int jdbcPosition)
          Convenience method to populate the passed PreparedStatement with the value for the element.
protected  int populateEmbeddedElementFieldsInStatement(StateManager sm, java.lang.Object element, java.sql.PreparedStatement ps, int jdbcPosition, JoinTable joinTable)
          Convenience method to populate the passed PreparedStatement with the field values from the embedded element starting at the specified jdbc position.
protected  int populateOrderInStatement(PersistenceManager pm, java.sql.PreparedStatement ps, int idx, int jdbcPosition)
          Convenience method to populate the passed PreparedStatement with the value for the order index.
protected  int populateOwnerInStatement(StateManager sm, PersistenceManager pm, java.sql.PreparedStatement ps, int jdbcPosition)
          Convenience method to populate the passed PreparedStatement with the value from the owner.
protected  int populateRelationDiscriminatorInStatement(PersistenceManager pm, java.sql.PreparedStatement ps, int jdbcPosition)
          Convenience method to populate the passed PreparedStatement with the value for the distinguisher value.
 int size(StateManager sm)
          Method to return the size of the container.
protected  boolean validateElementForReading(StateManager sm, java.lang.Object element)
          Method to check if an element is already persistent or is persistent but managed by a different persistence manager.
protected  void validateElementForWriting(StateManager sm, java.lang.Object element)
          Method to check if an element is already persistent, or is managed by a different Persistencemanager.
protected  boolean validateElementType(ClassLoaderResolver clr, java.lang.Object element)
          Method to validate an element against the accepted type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iterateUsingDiscriminator

protected boolean iterateUsingDiscriminator
Flag to set whether the iterator statement will use a discriminator or not.


elementInfo

protected ElementContainerStore.ElementInfo[] elementInfo
Information for the elements of this container. When the "element-type" table is new-table, or superclass-table then there is 1 value here. When the "element-type" table uses subclass-table, or when it is a reference type then there can be multiple.


emd

protected AbstractClassMetaData emd
MetaData for the "element-type" class. Not used for reference types since no metadata is present for the declared type.


containerTable

protected DatastoreContainerObject containerTable
Table containing the link between owner and element. Not set when using FK relations.


elementMapping

protected JavaTypeMapping elementMapping
Mapping for the element.


elementType

protected java.lang.String elementType
Type of the element.


elementsAreEmbedded

protected boolean elementsAreEmbedded
Whether the elements are embedded.


elementsAreSerialised

protected boolean elementsAreSerialised
Whether the elements are serialised.


orderMapping

protected JavaTypeMapping orderMapping
Mapping for an ordering column to allow for duplicates in the container. Can also be used for ordering elements in a List/array. Can also be used where we have an embedded object and so need to form the PK with something.


relationDiscriminatorMapping

protected JavaTypeMapping relationDiscriminatorMapping
Optional mapping to distinguish elements of one collection from another when sharing the join table.


relationDiscriminatorValue

protected java.lang.String relationDiscriminatorValue
Value to use to discriminate between elements of this collection from others using the same join table.


elmIdentifier

protected final DatastoreIdentifier elmIdentifier
Identifier for elements in JDOQL queries.


clr

protected ClassLoaderResolver clr
ClassLoader resolver.


sizeStmt

protected java.lang.String sizeStmt
Statement for getting the size of the container.


clearStmt

protected java.lang.String clearStmt
Statement for clearing the container.


LOCALISER

protected static final Localiser LOCALISER
Localiser for messages.


storeMgr

protected StoreManager storeMgr
Manager for the store.


dba

protected DatastoreAdapter dba
Datastore adapter in use by this store.


ownerMapping

protected JavaTypeMapping ownerMapping
Mapping to the owner of the container.


ownerFieldMetaData

protected AbstractPropertyMetaData ownerFieldMetaData
MetaData for the field in the owner with this container.


thisIdentifier

protected final DatastoreIdentifier thisIdentifier
Identifier for the container in JDOQL queries.

Constructor Detail

ElementContainerStore

protected ElementContainerStore(StoreManager storeMgr,
                                ClassLoaderResolver clr)
Constructor.

Parameters:
storeMgr - Manager for the store
clr - ClassLoader resolver
Method Detail

initialiseStatements

protected void initialiseStatements()
Method to initialise the statements being used.


getElementInformationForClass

protected ElementContainerStore.ElementInfo[] getElementInformationForClass()
Convenience method to find the element information relating to the element type. Used specifically for the "element-type" of a collection/array to find the elements which have table information. Not used for reference types.

Returns:
Element information relating to the element type

hasOrderMapping

public boolean hasOrderMapping()
Accessor for whether the store has an order mapping, to allow for duplicates or ordering.

Returns:
Whether an order mapping is present.

getElementType

public java.lang.String getElementType()
Accessor for the element type stored in this container.

Returns:
The element type.

validateElementType

protected boolean validateElementType(ClassLoaderResolver clr,
                                      java.lang.Object element)
Method to validate an element against the accepted type.

Parameters:
clr - The ClassLoaderResolver
element - The element to validate
Returns:
Whether it is valid.

validateElementForReading

protected boolean validateElementForReading(StateManager sm,
                                            java.lang.Object element)
Method to check if an element is already persistent or is persistent but managed by a different persistence manager.

Parameters:
sm - The state manager of this owner
element - The element
Returns:
Whether it is valid for reading.

validateElementForWriting

protected void validateElementForWriting(StateManager sm,
                                         java.lang.Object element)
Method to check if an element is already persistent, or is managed by a different Persistencemanager. If not persistent, this will persist it.

Parameters:
sm - The state manager of this owner
element - The element

populateElementInStatement

protected int populateElementInStatement(PersistenceManager pm,
                                         java.sql.PreparedStatement ps,
                                         java.lang.Object element,
                                         int jdbcPosition)
Convenience method to populate the passed PreparedStatement with the value for the element. Not used with embedded PC elements.

Parameters:
pm - Persistence Manager
ps - The PreparedStatement
element - The element
jdbcPosition - Position in JDBC statement to populate
Returns:
The next position in the JDBC statement

populateOrderInStatement

protected int populateOrderInStatement(PersistenceManager pm,
                                       java.sql.PreparedStatement ps,
                                       int idx,
                                       int jdbcPosition)
Convenience method to populate the passed PreparedStatement with the value for the order index.

Parameters:
pm - Persistence Manager
ps - The PreparedStatement
idx - The order value
jdbcPosition - Position in JDBC statement to populate
Returns:
The next position in the JDBC statement

populateRelationDiscriminatorInStatement

protected int populateRelationDiscriminatorInStatement(PersistenceManager pm,
                                                       java.sql.PreparedStatement ps,
                                                       int jdbcPosition)
Convenience method to populate the passed PreparedStatement with the value for the distinguisher value.

Parameters:
pm - Persistence Manager
ps - The PreparedStatement
jdbcPosition - Position in JDBC statement to populate
Returns:
The next position in the JDBC statement

populateElementDiscriminatorInStatement

protected int populateElementDiscriminatorInStatement(PersistenceManager pm,
                                                      java.sql.PreparedStatement ps,
                                                      int jdbcPosition,
                                                      boolean includeSubclasses,
                                                      ElementContainerStore.ElementInfo info)
Convenience method to populate the passed PreparedStatement with the value from the element discriminator, optionally including all subclasses of the element type.

Parameters:
pm - Persistence Manager
ps - The PreparedStatement
jdbcPosition - Position in JDBC statement to populate
includeSubclasses - Whether to include subclasses
info - The element information
Returns:
The next position in the JDBC statement

populateEmbeddedElementFieldsInStatement

protected int populateEmbeddedElementFieldsInStatement(StateManager sm,
                                                       java.lang.Object element,
                                                       java.sql.PreparedStatement ps,
                                                       int jdbcPosition,
                                                       JoinTable joinTable)
Convenience method to populate the passed PreparedStatement with the field values from the embedded element starting at the specified jdbc position.

Parameters:
sm - State Manager of the owning container
element - The embedded element
ps - The PreparedStatement
jdbcPosition - JDBC position in the statement to start at
joinTable - The Join table where the elements are embedded
Returns:
The next JDBC position

getClearStmt

protected java.lang.String getClearStmt()
Generate statement for clearing the container.
 DELETE FROM CONTAINERTABLE
 WHERE OWNERCOL = ?
 [AND RELATION_DISCRIM=?]
 
TODO Add a discriminator restriction on this statement so we only clear ones with a valid discriminator value

Returns:
Statement for clearing the container.

iterator

public abstract java.util.Iterator iterator(StateManager ownerSM)
Accessor for an iterator through the container elements.

Parameters:
ownerSM - State Manager for the container.
Returns:
The Iterator

clear

public void clear(StateManager ownerSM)
Clear the association from owner to all elements

Parameters:
ownerSM - State Manager for the container.

getSizeStmt

protected java.lang.String getSizeStmt()
Generate statement for getting the size of the container. The order part is only present when an order mapping is used. The discriminator part is only present when the element has a discriminator.
 SELECT COUNT(*) FROM TBL THIS
 [INNER JOIN ELEM_TBL ELEM ON TBL.COL = ELEM.ID]
 WHERE THIS.OWNERCOL=?
 [AND THIS.ORDERCOL IS NOT NULL]
 [AND (DISCRIMINATOR=? OR DISCRMINATOR=? OR DISCRIMINATOR=?)]
 [AND RELATION_DISCRIM=?]
 
The discriminator part includes all subclasses of the element type. If the element is in a different table to the container then an INNER JOIN will be present to link the two tables, and table aliases will be present also. TODO Update this to allow for getting the size when more than 1 element table.

Returns:
The Statement returning the size of the container.

size

public int size(StateManager sm)
Method to return the size of the container.

Parameters:
sm - The state manager.
Returns:
The size.

getAddStmt

protected java.lang.String getAddStmt()
Generates the statement for adding items. The EMBEDDEDFIELDX columns are only added for embedded PC elements.
 
 INSERT INTO LISTTABLE (OWNERCOL,[ELEMENTCOL],[EMBEDDEDFIELD1, EMBEDDEDFIELD2,...],[ORDERCOL]) 
 VALUES (?,?,?)
 

Returns:
The Statement for adding an item

newResultObjectFactory

public Query.ResultObjectFactory newResultObjectFactory(StateManager sm,
                                                        QueryExpression stmt,
                                                        boolean ignoreCache,
                                                        boolean useFetchPlan)
Method to generate a new result object factory from which to retrieve element objects.

Parameters:
sm - State Manager for owner
stmt - The query
ignoreCache - Whether to ignore the cache
useFetchPlan - Whether to use the current FetchPlan
Returns:
The Result object factory

getStoreManager

public StoreManager getStoreManager()
Accessor for the RDBMSManager.

Returns:
The RDBMSManager.

getOwnerMapping

public JavaTypeMapping getOwnerMapping()
Accessor for the owner mapping.

Returns:
Owner mapping.

executeUpdate

protected int executeUpdate(java.lang.String stmt,
                            java.sql.PreparedStatement ps)
                     throws java.sql.SQLException
Method to execute a PreparedStatement. Prints logging information about timings.

Parameters:
stmt - The statement text
ps - The Prepared Statement
Returns:
The numer of rows affected (as per PreparedStatement.executeUpdate)
Throws:
java.sql.SQLException - Thrown if an error occurs

executeQuery

protected java.sql.ResultSet executeQuery(java.lang.String stmt,
                                          java.sql.PreparedStatement ps)
                                   throws java.sql.SQLException
Method to execute a PreparedStatement query, and return the ResultSet. Prints logging information about timings.

Parameters:
stmt - The statement text
ps - The Prepared Statement
Returns:
The ResultSet from the query
Throws:
java.sql.SQLException - Thrown if an error occurs

isEmbeddedMapping

protected boolean isEmbeddedMapping(JavaTypeMapping mapping)
Check if the mapping correspond to a non pc object or embedded field

Parameters:
mapping - the mapping
Returns:
true if the field is embedded into one column

populateOwnerInStatement

protected int populateOwnerInStatement(StateManager sm,
                                       PersistenceManager pm,
                                       java.sql.PreparedStatement ps,
                                       int jdbcPosition)
Convenience method to populate the passed PreparedStatement with the value from the owner.

Parameters:
sm - State Manager
pm - Persistence Manager
ps - The PreparedStatement
jdbcPosition - Position in JDBC statement to populate
Returns:
The next position in the JDBC statement

getStateManagerForEmbeddedPCObject

protected StateManager getStateManagerForEmbeddedPCObject(StateManager sm,
                                                          java.lang.Object obj,
                                                          JoinTable table)
Method to return the StateManager for an embedded PC object (element, key, value). It creates one if the element is not currently managed.

Parameters:
sm - State Manager of the owner
obj - The embedded PC object
table - Join table where the objects are stored
Returns:
The state manager


Copyright © -2007 . All Rights Reserved.