use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.
the class BackingStoreHelper method populateEmbeddedElementFieldsInStatement.
/**
* Convenience method to populate the passed PreparedStatement with the field values from
* the embedded element starting at the specified jdbc position.
* @param op ObjectProvider of the owning container
* @param element The embedded element
* @param ps The PreparedStatement
* @param jdbcPosition JDBC position in the statement to start at
* @param ownerFieldMetaData The meta data for the owner field
* @param elementMapping mapping for the element
* @param emd Metadata for the element class
* @param bcs Container store
* @return The next JDBC position
*/
public static int populateEmbeddedElementFieldsInStatement(ObjectProvider op, Object element, PreparedStatement ps, int jdbcPosition, AbstractMemberMetaData ownerFieldMetaData, JavaTypeMapping elementMapping, AbstractClassMetaData emd, BaseContainerStore bcs) {
EmbeddedElementPCMapping embeddedMapping = (EmbeddedElementPCMapping) elementMapping;
StatementClassMapping mappingDefinition = new StatementClassMapping();
int[] elementFieldNumbers = new int[embeddedMapping.getNumberOfJavaTypeMappings()];
for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping fieldMapping = embeddedMapping.getJavaTypeMapping(i);
int absFieldNum = emd.getAbsolutePositionOfMember(fieldMapping.getMemberMetaData().getName());
elementFieldNumbers[i] = absFieldNum;
StatementMappingIndex stmtMapping = new StatementMappingIndex(fieldMapping);
int[] jdbcParamPositions = new int[fieldMapping.getNumberOfDatastoreMappings()];
for (int j = 0; j < fieldMapping.getNumberOfDatastoreMappings(); j++) {
jdbcParamPositions[j] = jdbcPosition++;
}
stmtMapping.addParameterOccurrence(jdbcParamPositions);
mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
}
ObjectProvider elementOP = bcs.getObjectProviderForEmbeddedPCObject(op, element, ownerFieldMetaData, ObjectProvider.EMBEDDED_COLLECTION_ELEMENT_PC);
elementOP.provideFields(elementFieldNumbers, new ParameterSetter(elementOP, ps, mappingDefinition));
return jdbcPosition;
}
use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.
the class AbstractRDBMSQueryResult method registerMemberBulkResultSet.
public void registerMemberBulkResultSet(IteratorStatement iterStmt, ResultSet rs) {
if (bulkLoadedValueByMemberNumber == null) {
bulkLoadedValueByMemberNumber = new HashMap<>();
}
try {
ExecutionContext ec = query.getExecutionContext();
AbstractMemberMetaData mmd = iterStmt.getBackingStore().getOwnerMemberMetaData();
if (mmd.hasCollection() || mmd.hasArray()) {
ElementIteratorStatement elemIterStmt = (ElementIteratorStatement) iterStmt;
ElementContainerStore backingStore = (ElementContainerStore) iterStmt.getBackingStore();
if (backingStore.isElementsAreEmbedded() || backingStore.isElementsAreSerialised()) {
int[] param = new int[backingStore.getElementMapping().getNumberOfColumnMappings()];
for (int i = 0; i < param.length; ++i) {
param[i] = i + 1;
}
if (backingStore.getElementMapping() instanceof SerialisedPCMapping || backingStore.getElementMapping() instanceof SerialisedReferenceMapping || backingStore.getElementMapping() instanceof EmbeddedElementPCMapping) {
// Element = Serialised
while (rs.next()) {
Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
Object element = backingStore.getElementMapping().getObject(ec, rs, param, ec.findStateManager(owner), backingStore.getOwnerMemberMetaData().getAbsoluteFieldNumber());
addOwnerMemberCollectionElement(mmd, owner, element);
}
} else {
// Element = Non-PC
while (rs.next()) {
Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
Object element = backingStore.getElementMapping().getObject(ec, rs, param);
addOwnerMemberCollectionElement(mmd, owner, element);
}
}
} else if (backingStore.getElementMapping() instanceof ReferenceMapping) {
// Element is Reference (interface/Object) so just use elementMapping
int[] param = new int[backingStore.getElementMapping().getNumberOfColumnMappings()];
for (int i = 0; i < param.length; ++i) {
param[i] = i + 1;
}
while (rs.next()) {
Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
Object element = backingStore.getElementMapping().getObject(ec, rs, param);
addOwnerMemberCollectionElement(mmd, owner, element);
}
} else {
String elementType = mmd.hasCollection() ? backingStore.getOwnerMemberMetaData().getCollection().getElementType() : backingStore.getOwnerMemberMetaData().getArray().getElementType();
ResultObjectFactory<E> scoROF = new PersistentClassROF(ec, rs, query.getIgnoreCache(), fp, elemIterStmt.getElementClassMapping(), backingStore.getElementClassMetaData(), ec.getClassLoaderResolver().classForName(elementType));
while (rs.next()) {
Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
Object element = scoROF.getObject();
addOwnerMemberCollectionElement(mmd, owner, element);
}
}
} else if (mmd.hasMap()) {
// TODO Cater for maps
}
} catch (SQLException sqle) {
NucleusLogger.DATASTORE.error("Exception thrown processing bulk loaded field " + iterStmt.getBackingStore().getOwnerMemberMetaData().getFullFieldName(), sqle);
} finally {
// Close the ResultSet (and its Statement)
try {
Statement stmt = null;
try {
stmt = rs.getStatement();
// Close the result set
rs.close();
} catch (SQLException e) {
NucleusLogger.DATASTORE.error(Localiser.msg("052605", e));
} finally {
try {
if (stmt != null) {
// Close the original statement
stmt.close();
}
} catch (SQLException e) {
// Do nothing
}
}
} finally {
rs = null;
}
}
}
use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.
the class BackingStoreHelper method populateEmbeddedElementFieldsInStatement.
/**
* Convenience method to populate the passed PreparedStatement with the field values from
* the embedded element starting at the specified jdbc position.
* @param sm StateManager of the owning container
* @param element The embedded element
* @param ps The PreparedStatement
* @param jdbcPosition JDBC position in the statement to start at
* @param ownerFieldMetaData The meta data for the owner field
* @param elementMapping mapping for the element
* @param emd Metadata for the element class
* @param bcs Container store
* @return The next JDBC position
*/
public static int populateEmbeddedElementFieldsInStatement(DNStateManager sm, Object element, PreparedStatement ps, int jdbcPosition, AbstractMemberMetaData ownerFieldMetaData, JavaTypeMapping elementMapping, AbstractClassMetaData emd, BaseContainerStore bcs) {
EmbeddedElementPCMapping embeddedMapping = (EmbeddedElementPCMapping) elementMapping;
StatementClassMapping mappingDefinition = new StatementClassMapping();
int[] elementFieldNumbers = new int[embeddedMapping.getNumberOfJavaTypeMappings()];
for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping fieldMapping = embeddedMapping.getJavaTypeMapping(i);
int absFieldNum = emd.getAbsolutePositionOfMember(fieldMapping.getMemberMetaData().getName());
elementFieldNumbers[i] = absFieldNum;
StatementMappingIndex stmtMapping = new StatementMappingIndex(fieldMapping);
int[] jdbcParamPositions = new int[fieldMapping.getNumberOfColumnMappings()];
for (int j = 0; j < fieldMapping.getNumberOfColumnMappings(); j++) {
jdbcParamPositions[j] = jdbcPosition++;
}
stmtMapping.addParameterOccurrence(jdbcParamPositions);
mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
}
DNStateManager elementSM = bcs.getStateManagerForEmbeddedPCObject(sm, element, ownerFieldMetaData, PersistableObjectType.EMBEDDED_COLLECTION_ELEMENT_PC);
elementSM.provideFields(elementFieldNumbers, new ParameterSetter(elementSM, ps, mappingDefinition));
return jdbcPosition;
}
use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.
the class ElementContainerTable method getExpectedForeignKeys.
/**
* Accessor for the expected foreign keys for this table.
* @param clr The ClassLoaderResolver
* @return The expected foreign keys.
*/
@Override
public List<ForeignKey> getExpectedForeignKeys(ClassLoaderResolver clr) {
assertIsInitialized();
// Find the mode that we're operating in for FK addition
boolean autoMode = false;
if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
autoMode = true;
}
List<ForeignKey> foreignKeys = new ArrayList();
try {
// FK from join table to owner table
DatastoreClass referencedTable = storeMgr.getDatastoreClass(ownerType, clr);
if (referencedTable != null) {
// Single owner table, so add a single FK to the owner as appropriate
ForeignKey fk = getForeignKeyToOwner(referencedTable, autoMode);
if (fk != null) {
foreignKeys.add(fk);
}
} else {
// No single owner so we don't bother with the FK since referential integrity by FK cannot work
// if we don't have a single owner at the other end of the FK(s).
}
// FK from join table to element table(s)
if (elementMapping instanceof SerialisedPCMapping) {
// Do nothing since no element table
} else if (elementMapping instanceof EmbeddedElementPCMapping) {
// Add any FKs for the fields of the (embedded) element
EmbeddedElementPCMapping embMapping = (EmbeddedElementPCMapping) elementMapping;
for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
// Field is a reference type, so add a FK to the table of the PC for each PC implementation
Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
foreignKeys.addAll(fks);
} else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfColumnMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
// Field is for a PC class with the FK at this side, so add a FK to the table of this PC
ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
if (fk != null) {
foreignKeys.add(fk);
}
}
}
} else if (elementMapping instanceof ReferenceMapping) {
JavaTypeMapping[] implJavaTypeMappings = ((ReferenceMapping) elementMapping).getJavaTypeMapping();
for (int i = 0; i < implJavaTypeMappings.length; i++) {
JavaTypeMapping implMapping = implJavaTypeMappings[i];
if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(implMapping.getType(), clr) != null && implMapping.getNumberOfColumnMappings() > 0) {
referencedTable = storeMgr.getDatastoreClass(implMapping.getType(), clr);
if (referencedTable != null) {
ForeignKey fk = getForeignKeyToElement(referencedTable, autoMode, implMapping);
if (fk != null) {
foreignKeys.add(fk);
}
}
}
}
} else {
referencedTable = storeMgr.getDatastoreClass(getElementType(), clr);
if (referencedTable != null) {
ForeignKey fk = getForeignKeyToElement(referencedTable, autoMode, elementMapping);
if (fk != null) {
foreignKeys.add(fk);
}
} else {
// Either no element table or multiple (where the user has element with "subclass-table" strategy, or using "complete-table")
// so do nothing since referential integrity will not allow multiple FKs.
}
}
} catch (NoTableManagedException e) {
// expected when no table exists
}
return foreignKeys;
}
use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.
the class AbstractCollectionStore method updateEmbeddedElement.
/**
* Method to update a field of an embedded element.
* @param op ObjectProvider of the owner
* @param element The element to update
* @param fieldNumber The number of the field to update
* @param value The value
* @return true if the datastore was updated
*/
public boolean updateEmbeddedElement(ObjectProvider op, E element, int fieldNumber, Object value) {
boolean modified = false;
if (elementMapping != null && elementMapping instanceof EmbeddedElementPCMapping) {
String fieldName = elementCmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
if (fieldName == null) {
// We have no mapping for this field so presumably is the owner field or a PK field
return false;
}
JavaTypeMapping fieldMapping = ((EmbeddedElementPCMapping) elementMapping).getJavaTypeMapping(fieldName);
if (fieldMapping == null) {
// We have no mapping for this field so presumably is the owner field or a PK field
return false;
}
modified = updateEmbeddedElement(op, element, fieldNumber, value, fieldMapping);
}
return modified;
}
Aggregations