org.datanucleus.store.rdbms.scostore
Class RDBMSFKListStoreSpecialization

java.lang.Object
  extended by org.datanucleus.store.mapped.scostore.BaseElementContainerStoreSpecialization
      extended by org.datanucleus.store.rdbms.scostore.RDBMSFKListStoreSpecialization
All Implemented Interfaces:
org.datanucleus.store.mapped.scostore.AbstractCollectionStoreSpecialization, org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization, org.datanucleus.store.mapped.scostore.ElementContainerStoreSpecialization, org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

public class RDBMSFKListStoreSpecialization
extends org.datanucleus.store.mapped.scostore.BaseElementContainerStoreSpecialization
implements org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

RDBMS-specific implementation of an FKListStoreSpecialization.


Field Summary
protected  java.lang.String addStmt
          Statement for adding an element to the container.
protected  java.lang.String clearStmt
          Statement for clearing the container.
protected  java.lang.String indexOfStmt
           
protected  java.lang.String lastIndexOfStmt
           
protected  java.lang.String removeAtStmt
           
protected  java.lang.String removeStmt
          Statement for removing an element from the container.
protected  java.lang.String setStmt
           
protected  java.lang.String shiftStmt
           
protected  java.lang.String sizeStmt
          Statement for getting the size of the container.
protected  RDBMSManager storeMgr
           
protected  boolean usingDiscriminatorInSizeStmt
          Whether we are using a discriminator in the "size" statement.
 
Fields inherited from class org.datanucleus.store.mapped.scostore.BaseElementContainerStoreSpecialization
clr, localiser
 
Method Summary
 void clearWithoutDelete(org.datanucleus.ObjectManager om, org.datanucleus.StateManager ownerSM, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
 boolean contains(org.datanucleus.StateManager sm, java.lang.Object element, org.datanucleus.store.mapped.scostore.AbstractCollectionStore acs)
           
 void executeClear(org.datanucleus.StateManager ownerSM, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
protected  java.lang.String getAddStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for adding items.
protected  java.lang.String getClearNullifyStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for clearing items by nulling the owner link out.
protected  java.lang.String getClearStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generate statement for clearing the container.
protected  java.lang.String getIndexOfStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generate statement for getting the index of an item.
 int[] getIndicesOf(org.datanucleus.StateManager sm, java.util.Collection elements, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
protected  java.lang.String getIndicesOfStmt(java.util.Collection elements, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for getting the indices of a collection of element.
protected  java.lang.String getLastIndexOfStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for getting the index of the last item.
protected  java.lang.String getRemoveAtNullifyStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for removing an item by nulling it out.
protected  java.lang.String getRemoveAtStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for removing an item.
protected  java.lang.String getRemoveStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generate statement for removing an element from the Collection.
protected  java.lang.String getSetStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for setting an item to be at a position.
protected  java.lang.String getShiftStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for shifting items.
 int getSize(org.datanucleus.StateManager ownerSM, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
protected  java.lang.String getSizeStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generate statement for getting the size of thecontainer.
protected  java.lang.String getUnsetStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Generates the statement for unsetting an item from a list position.
protected  java.lang.String getUpdateEmbeddedElementStmt(org.datanucleus.store.mapped.mapping.JavaTypeMapping fieldMapping, org.datanucleus.store.mapped.scostore.ElementContainerStore acs)
          Generate statement for update the field of an embedded element.
 int indexOf(org.datanucleus.StateManager sm, java.lang.Object element, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
protected  int internalIndexOf(org.datanucleus.StateManager sm, java.lang.Object element, java.lang.String stmt, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Internal method to find the index of an element.
 int[] internalRemove(org.datanucleus.StateManager ownerSM, org.datanucleus.ManagedConnection conn, boolean batched, java.lang.Object element, boolean executeNow, org.datanucleus.store.mapped.scostore.AbstractCollectionStore acs)
           
protected  void internalRemoveAt(org.datanucleus.StateManager sm, int index, java.lang.String stmt, int size, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Internal method to remove an object at a location in the List.
 int[] internalShift(org.datanucleus.StateManager ownerSM, org.datanucleus.ManagedConnection conn, boolean batched, int oldIndex, int amount, boolean executeNow, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Method to process a "shift" statement, updating the index in the list of the specified index.
protected  void invalidateAddStmt()
          Method to remove any stored statement for addition of an element.
 int lastIndexOf(org.datanucleus.StateManager sm, java.lang.Object element, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
protected  void prepareIndicesOfStmt(org.datanucleus.StateManager sm, java.sql.PreparedStatement ps, java.util.Collection elements, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
          Method to prepare the indicesOf statement for use.
 void removeAt(org.datanucleus.StateManager sm, int index, int size, boolean nullify, org.datanucleus.store.mapped.scostore.FKListStore fkListStore)
           
 java.lang.Object set(org.datanucleus.StateManager sm, int index, java.lang.Object element, boolean allowDependentField, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs, java.lang.Object obj)
           
 boolean updateElementFk(org.datanucleus.StateManager sm, java.lang.Object element, java.lang.Object owner, int index, org.datanucleus.ObjectManager om, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
 boolean updateEmbeddedElement(org.datanucleus.StateManager sm, java.lang.Object element, int fieldNumber, java.lang.Object value, org.datanucleus.store.mapped.mapping.JavaTypeMapping fieldMapping, org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization
getIndicesOf, indexOf, internalShift, lastIndexOf
 
Methods inherited from interface org.datanucleus.store.mapped.scostore.AbstractCollectionStoreSpecialization
contains, internalRemove, updateEmbeddedElement
 
Methods inherited from interface org.datanucleus.store.mapped.scostore.ElementContainerStoreSpecialization
executeClear, getSize
 

Field Detail

removeAtStmt

protected java.lang.String removeAtStmt

setStmt

protected java.lang.String setStmt

shiftStmt

protected java.lang.String shiftStmt

indexOfStmt

protected java.lang.String indexOfStmt

lastIndexOfStmt

protected java.lang.String lastIndexOfStmt

storeMgr

protected final RDBMSManager storeMgr

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.


addStmt

protected java.lang.String addStmt
Statement for adding an element to the container.


removeStmt

protected java.lang.String removeStmt
Statement for removing an element from the container.


usingDiscriminatorInSizeStmt

protected boolean usingDiscriminatorInSizeStmt
Whether we are using a discriminator in the "size" statement.

Method Detail

getSetStmt

protected java.lang.String getSetStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for setting an item to be at a position.
 UPDATE LISTTABLE SET OWNERCOL=?, INDEXCOL = ? [,DISTINGUISHER=?]
 WHERE ELEMENTCOL = ?
 

Returns:
The Statement for setting an item

getUnsetStmt

protected java.lang.String getUnsetStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for unsetting an item from a list position.
 UPDATE LISTTABLE SET OWNERCOL=NULL, INDEXCOL=-1 [, DISTINGUISHER = NULL]
 WHERE OWNERCOL = ? AND INDEXCOL = ? [AND DISTINGUISHER = ?]
 

Returns:
The Statement for unsetting an item

set

public java.lang.Object set(org.datanucleus.StateManager sm,
                            int index,
                            java.lang.Object element,
                            boolean allowDependentField,
                            org.datanucleus.store.mapped.scostore.ElementContainerStore ecs,
                            java.lang.Object obj)
Specified by:
set in interface org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

updateElementFk

public boolean updateElementFk(org.datanucleus.StateManager sm,
                               java.lang.Object element,
                               java.lang.Object owner,
                               int index,
                               org.datanucleus.ObjectManager om,
                               org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
updateElementFk in interface org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

getRemoveAtNullifyStmt

protected java.lang.String getRemoveAtNullifyStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for removing an item by nulling it out. When there is only a single element table the statement will be
 UPDATE LISTTABLE SET OWNERCOL=NULL, INDEXCOL=-1
 WHERE OWNERCOL = ?
 AND INDEXCOL = ?
 [AND DISTINGUISHER = ?]
 
and when there are multiple element tables the statement will be
 UPDATE ? SET OWNERCOL=NULL, INDEXCOL=-1
 WHERE OWNERCOL=?
 AND INDEXCOL=?
 [AND DISTINGUISHER = ?]
 

Returns:
The Statement for removing an item from a position

getClearNullifyStmt

protected java.lang.String getClearNullifyStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for clearing items by nulling the owner link out. The statement will be
 UPDATE LISTTABLE SET OWNERCOL=NULL, INDEXCOL=-1 [,DISTINGUISHER=NULL]
 WHERE OWNERCOL=? [AND DISTINGUISHER=?]
 
when there is only one element table, and will be
 UPDATE ? SET OWNERCOL=NULL, INDEXCOL=-1 [,DISTINGUISHER=NULL]
 WHERE OWNERCOL=? [AND DISTINGUISHER=?]
 
when there is more than 1 element table.

Returns:
The Statement for clearing items for the owner.

clearWithoutDelete

public void clearWithoutDelete(org.datanucleus.ObjectManager om,
                               org.datanucleus.StateManager ownerSM,
                               org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
clearWithoutDelete in interface org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

removeAt

public void removeAt(org.datanucleus.StateManager sm,
                     int index,
                     int size,
                     boolean nullify,
                     org.datanucleus.store.mapped.scostore.FKListStore fkListStore)
Specified by:
removeAt in interface org.datanucleus.store.mapped.scostore.FKListStoreSpecialization

getIndexOfStmt

protected java.lang.String getIndexOfStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generate statement for getting the index of an item.
 SELECT INDEXCOL FROM LISTTABLE
 WHERE OWNERCOL=?
 AND ELEMENTCOL=?
 [AND EMBEDDEDFIELD1=? AND EMBEDDEDFIELD2=? AND ...]
 [AND DISTINGUISHER=?]
 ORDER BY INDEXCOL
 

Returns:
The Statement for getting the index of an item

getLastIndexOfStmt

protected java.lang.String getLastIndexOfStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for getting the index of the last item.
 SELECT INDEXCOL FROM LISTTABLE
 WHERE OWNERCOL=?
 AND ELEMENTCOL=?
 [AND EMBEDDEDFIELD1=? AND EMBEDDEDFIELD2=? AND ...]
 [AND DISTINGUISHER=?]
 ORDER BY INDEXCOL DESC
 

Returns:
The Statement for getting the last item

getRemoveAtStmt

protected java.lang.String getRemoveAtStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for removing an item.
 DELETE FROM LISTTABLE
 WHERE OWNERCOL = ?
 AND INDEXCOL = ?
 [AND DISTINGUISHER=?]
 

Returns:
The Statement for removing an item from a position

getShiftStmt

protected java.lang.String getShiftStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for shifting items.
 UPDATE LISTTABLE SET INDEXCOL = ?
 WHERE OWNERCOL = ?
 AND INDEXCOL = ?
 [AND DISTINGUISHER=?]
 

Returns:
The Statement for shifting elements

getIndicesOfStmt

protected java.lang.String getIndicesOfStmt(java.util.Collection elements,
                                            org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for getting the indices of a collection of element. Order into descending index order (highest first) so they will NOT be in the same order as they appear in the input collection "elements".
 SELECT INDEXCOL FROM LISTTABLE
 WHERE (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?]) OR
       (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?]) OR
       (OWNERCOL=? AND ELEMENT_COL=? [AND DISTINGUISHER=?])
 ORDER BY INDEXCOL DESC
 

Parameters:
elements - The elements to retrieve the indices for.
Returns:
The Statement for getting the indices of the collection.

prepareIndicesOfStmt

protected void prepareIndicesOfStmt(org.datanucleus.StateManager sm,
                                    java.sql.PreparedStatement ps,
                                    java.util.Collection elements,
                                    org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Method to prepare the indicesOf statement for use. Populates the various parameters. This is required because the query is built dynamically depending on the number of elements to retrieve the indices for.

Parameters:
sm - State Manager of the container.
ps - The Prepared Statement
elements - Collection of elements

internalIndexOf

protected int internalIndexOf(org.datanucleus.StateManager sm,
                              java.lang.Object element,
                              java.lang.String stmt,
                              org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Internal method to find the index of an element.

Parameters:
sm - The state manager.
element - The element
stmt - The statement to find the element.
Returns:
The index of the element in the List.

indexOf

public int indexOf(org.datanucleus.StateManager sm,
                   java.lang.Object element,
                   org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
indexOf in interface org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization

lastIndexOf

public int lastIndexOf(org.datanucleus.StateManager sm,
                       java.lang.Object element,
                       org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
lastIndexOf in interface org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization

getIndicesOf

public int[] getIndicesOf(org.datanucleus.StateManager sm,
                          java.util.Collection elements,
                          org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
getIndicesOf in interface org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization

internalRemoveAt

protected void internalRemoveAt(org.datanucleus.StateManager sm,
                                int index,
                                java.lang.String stmt,
                                int size,
                                org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Internal method to remove an object at a location in the List.

Parameters:
sm - The state manager.
index - The location
stmt - The statement to remove the element from the List
size - Current list size (if known). -1 if not known

internalShift

public int[] internalShift(org.datanucleus.StateManager ownerSM,
                           org.datanucleus.ManagedConnection conn,
                           boolean batched,
                           int oldIndex,
                           int amount,
                           boolean executeNow,
                           org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
                    throws org.datanucleus.store.mapped.exceptions.MappedDatastoreException
Method to process a "shift" statement, updating the index in the list of the specified index.

Specified by:
internalShift in interface org.datanucleus.store.mapped.scostore.AbstractListStoreSpecialization
Parameters:
ownerSM - StateManager of the owner
conn - The connection
batched - Whether the statement is batched
oldIndex - The old index
amount - Amount to shift by (negative means shift down)
executeNow - Whether to execute the statement now (or wait for batching)
Returns:
Return code(s) from any executed statements
Throws:
org.datanucleus.store.mapped.exceptions.MappedDatastoreException - Thrown if an error occurs

getUpdateEmbeddedElementStmt

protected java.lang.String getUpdateEmbeddedElementStmt(org.datanucleus.store.mapped.mapping.JavaTypeMapping fieldMapping,
                                                        org.datanucleus.store.mapped.scostore.ElementContainerStore acs)
Generate statement for update the field of an embedded element.
 UPDATE SETTABLE
 SET EMBEDDEDFIELD1 = ?
 WHERE OWNERCOL=?
 AND ELEMENTCOL = ?
 

Parameters:
fieldMapping - The mapping for the field within the embedded object to be updated
Returns:
Statement for updating an embedded element in the Set

updateEmbeddedElement

public boolean updateEmbeddedElement(org.datanucleus.StateManager sm,
                                     java.lang.Object element,
                                     int fieldNumber,
                                     java.lang.Object value,
                                     org.datanucleus.store.mapped.mapping.JavaTypeMapping fieldMapping,
                                     org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Specified by:
updateEmbeddedElement in interface org.datanucleus.store.mapped.scostore.AbstractCollectionStoreSpecialization

contains

public boolean contains(org.datanucleus.StateManager sm,
                        java.lang.Object element,
                        org.datanucleus.store.mapped.scostore.AbstractCollectionStore acs)
Specified by:
contains in interface org.datanucleus.store.mapped.scostore.AbstractCollectionStoreSpecialization

internalRemove

public int[] internalRemove(org.datanucleus.StateManager ownerSM,
                            org.datanucleus.ManagedConnection conn,
                            boolean batched,
                            java.lang.Object element,
                            boolean executeNow,
                            org.datanucleus.store.mapped.scostore.AbstractCollectionStore acs)
                     throws org.datanucleus.store.mapped.exceptions.MappedDatastoreException
Specified by:
internalRemove in interface org.datanucleus.store.mapped.scostore.AbstractCollectionStoreSpecialization
Throws:
org.datanucleus.store.mapped.exceptions.MappedDatastoreException

getRemoveStmt

protected java.lang.String getRemoveStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generate statement for removing an element from the Collection.
 DELETE FROM COLLTABLE WHERE OWNERCOL=? AND ELEMENTCOL = ? [AND DISCRIM = ?]
 

Parameters:
ecs - Element container store
Returns:
Statement for deleting an item from the Collection.

invalidateAddStmt

protected void invalidateAddStmt()
Method to remove any stored statement for addition of an element.


getAddStmt

protected java.lang.String getAddStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generates the statement for adding items. The EMBEDDEDFIELDX columns are only added for embedded PC elements.
 INSERT INTO COLLTABLE (OWNERCOL,[ELEMENTCOL],[EMBEDDEDFIELD1, EMBEDDEDFIELD2,...],[ORDERCOL]) 
                       VALUES (?,?,?)
 

Returns:
The Statement for adding an item

executeClear

public void executeClear(org.datanucleus.StateManager ownerSM,
                         org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)

getClearStmt

protected java.lang.String getClearStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
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.

getSize

public int getSize(org.datanucleus.StateManager ownerSM,
                   org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)

getSizeStmt

protected java.lang.String getSizeStmt(org.datanucleus.store.mapped.scostore.ElementContainerStore ecs)
Generate statement for getting the size of thecontainer. 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] - when no null
 [LEFT OUTER JOIN ELEM_TBL ELEM ON TBL.COL = ELEM.ID] - when allows null
 WHERE THIS.OWNERCOL=?
 [AND THIS.ORDERCOL IS NOT NULL]
 [AND (DISCRIMINATOR=? OR DISCRMINATOR=? OR DISCRIMINATOR=? [OR DISCRIMINATOR IS NULL])]
 [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.


Copyright © 2009. All Rights Reserved.