use of org.datanucleus.metadata.VersionMetaData in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileUpdate.
/**
* Method to compile the result clause of the query into the SQLStatement.
* @param stmt UPDATE statement
*/
protected void compileUpdate(UpdateStatement stmt) {
if (compilation.getExprUpdate() != null) {
// Update statement, so generate update expression(s)
compileComponent = CompilationComponent.UPDATE;
Expression[] updateExprs = compilation.getExprUpdate();
SQLExpression[] updateSqlExprs = new SQLExpression[updateExprs.length];
// TODO If the field being set is in a different table omit it
boolean performingUpdate = false;
for (int i = 0; i < updateExprs.length; i++) {
// "field = value"
DyadicExpression updateExpr = (DyadicExpression) updateExprs[i];
// Left-side has to be PrimaryExpression
SQLExpression leftSqlExpr = null;
if (updateExpr.getLeft() instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) updateExpr.getLeft());
leftSqlExpr = stack.pop();
if (leftSqlExpr.getSQLTable() != stmt.getPrimaryTable()) {
// Set left to null to signify that it is not applicable to the table of this UPDATE statement
leftSqlExpr = null;
}
} else {
throw new NucleusException("Dont currently support update clause containing left expression of type " + updateExpr.getLeft());
}
if (leftSqlExpr != null) {
if (!stmt.getDatastoreAdapter().supportsOption(DatastoreAdapter.UPDATE_STATEMENT_ALLOW_TABLE_ALIAS_IN_SET_CLAUSE)) {
// This datastore doesn't allow table alias in UPDATE SET clause, so just use column name
for (int j = 0; j < leftSqlExpr.getNumberOfSubExpressions(); j++) {
ColumnExpression colExpr = leftSqlExpr.getSubExpression(j);
colExpr.setOmitTableFromString(true);
}
}
performingUpdate = true;
SQLExpression rightSqlExpr = null;
if (updateExpr.getRight() instanceof Literal) {
processLiteral((Literal) updateExpr.getRight());
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) updateExpr.getRight();
paramMappingForName.put(paramExpr.getId(), leftSqlExpr.getJavaTypeMapping());
processParameterExpression(paramExpr);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) updateExpr.getRight());
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof DyadicExpression) {
updateExpr.getRight().evaluate(this);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof CaseExpression) {
CaseExpression caseExpr = (CaseExpression) updateExpr.getRight();
processCaseExpression(caseExpr, leftSqlExpr);
rightSqlExpr = stack.pop();
} else if (updateExpr.getRight() instanceof VariableExpression) {
// Subquery?
processVariableExpression((VariableExpression) updateExpr.getRight());
rightSqlExpr = stack.pop();
if (rightSqlExpr instanceof UnboundExpression) {
// TODO Support whatever this is
throw new NucleusException("Found UnboundExpression in UPDATE clause!");
}
} else {
throw new NucleusException("Dont currently support update clause containing right expression of type " + updateExpr.getRight());
}
if (rightSqlExpr != null) {
updateSqlExprs[i] = leftSqlExpr.eq(rightSqlExpr);
}
}
}
if (candidateCmd.isVersioned() && options.contains(OPTION_BULK_UPDATE_VERSION)) {
SQLExpression updateSqlExpr = null;
ClassTable table = (ClassTable) stmt.getPrimaryTable().getTable();
JavaTypeMapping verMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
ClassTable verTable = table.getTableManagingMapping(verMapping);
if (verTable == stmt.getPrimaryTable().getTable()) {
VersionMetaData vermd = candidateCmd.getVersionMetaDataForClass();
if (vermd.getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
// Increment the version
SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
SQLExpression incrExpr = verExpr.add(new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(1), null));
updateSqlExpr = verExpr.eq(incrExpr);
SQLExpression[] oldArray = updateSqlExprs;
updateSqlExprs = new SQLExpression[oldArray.length + 1];
System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
updateSqlExprs[oldArray.length] = updateSqlExpr;
performingUpdate = true;
} else if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
// Set version to the time of update
SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
Object newVersion = ec.getLockManager().getNextVersion(vermd, null);
JavaTypeMapping valMapping = exprFactory.getMappingForType(newVersion.getClass(), false);
SQLExpression valExpr = new TemporalLiteral(stmt, valMapping, newVersion, null);
updateSqlExpr = verExpr.eq(valExpr);
SQLExpression[] oldArray = updateSqlExprs;
updateSqlExprs = new SQLExpression[oldArray.length + 1];
System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
updateSqlExprs[oldArray.length] = updateSqlExpr;
performingUpdate = true;
}
}
}
if (performingUpdate) {
// Only set the updates component of the SQLStatement if anything to update in this table
stmt.setUpdates(updateSqlExprs);
}
}
compileComponent = null;
}
use of org.datanucleus.metadata.VersionMetaData in project datanucleus-rdbms by datanucleus.
the class InsertRequest method execute.
/**
* Method performing the insertion of the record from the datastore.
* Takes the constructed insert query and populates with the specific record information.
* @param op The ObjectProvider for the record to be inserted
*/
public void execute(ObjectProvider op) {
ExecutionContext ec = op.getExecutionContext();
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
// Debug information about what we are inserting
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052207", op.getObjectAsPrintable(), table));
}
try {
VersionMetaData vermd = table.getVersionMetaData();
RDBMSStoreManager storeMgr = table.getStoreManager();
if (vermd != null && vermd.getFieldName() != null) {
// Version field - Update the version in the object
AbstractMemberMetaData verfmd = ((AbstractClassMetaData) vermd.getParent()).getMetaDataForMember(vermd.getFieldName());
Object currentVersion = op.getVersion();
if (currentVersion instanceof Number) {
// Cater for Integer based versions
currentVersion = Long.valueOf(((Number) currentVersion).longValue());
}
Object nextOptimisticVersion = ec.getLockManager().getNextVersion(vermd, currentVersion);
if (verfmd.getType() == Integer.class || verfmd.getType() == int.class) {
// Cater for Integer based versions
nextOptimisticVersion = Integer.valueOf(((Number) nextOptimisticVersion).intValue());
}
op.replaceField(verfmd.getAbsoluteFieldNumber(), nextOptimisticVersion);
}
// Set the state to "inserting" (may already be at this state if multiple inheritance level INSERT)
op.changeActivityState(ActivityState.INSERTING);
SQLController sqlControl = storeMgr.getSQLController();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, insertStmt, batch, hasIdentityColumn && storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.GET_GENERATED_KEYS_STATEMENT));
try {
StatementClassMapping mappingDefinition = new StatementClassMapping();
StatementMappingIndex[] idxs = stmtMappings;
for (int i = 0; i < idxs.length; i++) {
if (idxs[i] != null) {
mappingDefinition.addMappingForMember(i, idxs[i]);
}
}
// Provide the primary key field(s)
if (table.getIdentityType() == IdentityType.DATASTORE) {
if (!table.isObjectIdDatastoreAttributed() || !table.isBaseDatastoreClass()) {
int[] paramNumber = { IDPARAMNUMBER };
table.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false).setObject(ec, ps, paramNumber, op.getInternalObjectId());
}
} else if (table.getIdentityType() == IdentityType.APPLICATION) {
op.provideFields(pkFieldNumbers, new ParameterSetter(op, ps, mappingDefinition));
}
// This provides "persistence-by-reachability" for these fields
if (insertFieldNumbers.length > 0) {
// TODO Support surrogate current-user, create-timestamp
int numberOfFieldsToProvide = 0;
for (int i = 0; i < insertFieldNumbers.length; i++) {
if (insertFieldNumbers[i] < op.getClassMetaData().getMemberCount()) {
AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(insertFieldNumbers[i]);
if (mmd.isCreateTimestamp()) {
// Set create timestamp to time for the start of this transaction
op.replaceField(insertFieldNumbers[i], new Timestamp(ec.getTransaction().getIsActive() ? ec.getTransaction().getBeginTime() : System.currentTimeMillis()));
} else if (mmd.isCreateUser()) {
// Set create user to current user
op.replaceField(insertFieldNumbers[i], ec.getNucleusContext().getCurrentUser(ec));
}
numberOfFieldsToProvide++;
}
}
int j = 0;
int[] fieldNums = new int[numberOfFieldsToProvide];
for (int i = 0; i < insertFieldNumbers.length; i++) {
if (insertFieldNumbers[i] < op.getClassMetaData().getMemberCount()) {
fieldNums[j++] = insertFieldNumbers[i];
}
}
op.provideFields(fieldNums, new ParameterSetter(op, ps, mappingDefinition));
}
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (versionMapping != null) {
// Surrogate version - set the new version for the object
Object currentVersion = op.getVersion();
Object nextOptimisticVersion = ec.getLockManager().getNextVersion(vermd, currentVersion);
for (int k = 0; k < versionStmtMapping.getNumberOfParameterOccurrences(); k++) {
versionMapping.setObject(ec, ps, versionStmtMapping.getParameterPositionsForOccurrence(k), nextOptimisticVersion);
}
op.setTransactionalVersion(nextOptimisticVersion);
} else if (vermd != null && vermd.getFieldName() != null) {
// Version field - set the new version for the object
Object currentVersion = op.getVersion();
Object nextOptimisticVersion = ec.getLockManager().getNextVersion(vermd, currentVersion);
op.setTransactionalVersion(nextOptimisticVersion);
}
if (multitenancyStmtMapping != null) {
// Multitenancy mapping
table.getSurrogateMapping(SurrogateColumnType.MULTITENANCY, false).setObject(ec, ps, multitenancyStmtMapping.getParameterPositionsForOccurrence(0), ec.getNucleusContext().getMultiTenancyId(ec, op.getClassMetaData()));
}
if (softDeleteStmtMapping != null) {
// Soft-Delete mapping
table.getSurrogateMapping(SurrogateColumnType.SOFTDELETE, false).setObject(ec, ps, softDeleteStmtMapping.getParameterPositionsForOccurrence(0), Boolean.FALSE);
}
JavaTypeMapping discrimMapping = table.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, false);
if (discrimMapping != null) {
// Discriminator mapping
Object discVal = op.getClassMetaData().getDiscriminatorValue();
for (int k = 0; k < discriminatorStmtMapping.getNumberOfParameterOccurrences(); k++) {
discrimMapping.setObject(ec, ps, discriminatorStmtMapping.getParameterPositionsForOccurrence(k), discVal);
}
}
// External FK columns (optional)
if (externalFKStmtMappings != null) {
for (int i = 0; i < externalFKStmtMappings.length; i++) {
Object fkValue = op.getAssociatedValue(externalFKStmtMappings[i].getMapping());
if (fkValue != null) {
// Need to provide the owner field number so PCMapping can work out if it is inserted yet
AbstractMemberMetaData ownerFmd = table.getMetaDataForExternalMapping(externalFKStmtMappings[i].getMapping(), MappingType.EXTERNAL_FK);
for (int k = 0; k < externalFKStmtMappings[i].getNumberOfParameterOccurrences(); k++) {
externalFKStmtMappings[i].getMapping().setObject(ec, ps, externalFKStmtMappings[i].getParameterPositionsForOccurrence(k), fkValue, null, ownerFmd.getAbsoluteFieldNumber());
}
} else {
// We're inserting a null so don't need the owner field
for (int k = 0; k < externalFKStmtMappings[i].getNumberOfParameterOccurrences(); k++) {
externalFKStmtMappings[i].getMapping().setObject(ec, ps, externalFKStmtMappings[i].getParameterPositionsForOccurrence(k), null);
}
}
}
}
// External FK discriminator columns (optional)
if (externalFKDiscrimStmtMappings != null) {
for (int i = 0; i < externalFKDiscrimStmtMappings.length; i++) {
Object discrimValue = op.getAssociatedValue(externalFKDiscrimStmtMappings[i].getMapping());
for (int k = 0; k < externalFKDiscrimStmtMappings[i].getNumberOfParameterOccurrences(); k++) {
externalFKDiscrimStmtMappings[i].getMapping().setObject(ec, ps, externalFKDiscrimStmtMappings[i].getParameterPositionsForOccurrence(k), discrimValue);
}
}
}
// External order columns (optional)
if (externalOrderStmtMappings != null) {
for (int i = 0; i < externalOrderStmtMappings.length; i++) {
Object orderValue = op.getAssociatedValue(externalOrderStmtMappings[i].getMapping());
if (orderValue == null) {
// No order value so use -1
orderValue = Integer.valueOf(-1);
}
for (int k = 0; k < externalOrderStmtMappings[i].getNumberOfParameterOccurrences(); k++) {
externalOrderStmtMappings[i].getMapping().setObject(ec, ps, externalOrderStmtMappings[i].getParameterPositionsForOccurrence(k), orderValue);
}
}
}
sqlControl.executeStatementUpdate(ec, mconn, insertStmt, ps, !batch);
if (hasIdentityColumn) {
// Identity was set in the datastore using auto-increment/identity/serial etc
Object newId = getInsertedDatastoreIdentity(ec, sqlControl, op, mconn, ps);
if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled()) {
NucleusLogger.DATASTORE_PERSIST.debug(Localiser.msg("052206", op.getObjectAsPrintable(), newId));
}
op.setPostStoreNewObjectId(newId);
}
// Execute any mapping actions on the insert of the fields (e.g Oracle CLOBs/BLOBs)
for (int i = 0; i < callbacks.length; ++i) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052222", op.getObjectAsPrintable(), ((JavaTypeMapping) callbacks[i]).getMemberMetaData().getFullFieldName()));
}
callbacks[i].insertPostProcessing(op);
}
// Update the insert status for this table via the StoreManager
storeMgr.setObjectIsInsertedToLevel(op, table);
// (if we did it the other way around we would get a NotYetFlushedException thrown above).
for (int i = 0; i < relationFieldNumbers.length; i++) {
Object value = op.provideField(relationFieldNumbers[i]);
if (value != null && ec.getApiAdapter().isDetached(value)) {
Object valueAttached = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
op.replaceField(relationFieldNumbers[i], valueAttached);
}
}
// Perform reachability on all fields that have no datastore column (1-1 bi non-owner, N-1 bi join)
if (reachableFieldNumbers.length > 0) {
int numberOfReachableFields = 0;
for (int i = 0; i < reachableFieldNumbers.length; i++) {
if (reachableFieldNumbers[i] < op.getClassMetaData().getMemberCount()) {
numberOfReachableFields++;
}
}
int[] fieldNums = new int[numberOfReachableFields];
int j = 0;
for (int i = 0; i < reachableFieldNumbers.length; i++) {
if (reachableFieldNumbers[i] < op.getClassMetaData().getMemberCount()) {
fieldNums[j++] = reachableFieldNumbers[i];
}
}
mappingDefinition = new StatementClassMapping();
idxs = retrievedStmtMappings;
for (int i = 0; i < idxs.length; i++) {
if (idxs[i] != null) {
mappingDefinition.addMappingForMember(i, idxs[i]);
}
}
NucleusLogger.PERSISTENCE.debug("Performing reachability on fields " + StringUtils.intArrayToString(fieldNums));
op.provideFields(fieldNums, new ParameterSetter(op, ps, mappingDefinition));
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
String msg = Localiser.msg("052208", op.getObjectAsPrintable(), insertStmt, e.getMessage());
NucleusLogger.DATASTORE_PERSIST.warn(msg);
List exceptions = new ArrayList();
exceptions.add(e);
while ((e = e.getNextException()) != null) {
exceptions.add(e);
}
throw new NucleusDataStoreException(msg, (Throwable[]) exceptions.toArray(new Throwable[exceptions.size()]));
}
// (things like inserting any association parent-child).
for (int i = 0; i < callbacks.length; ++i) {
try {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052209", op.getObjectAsPrintable(), ((JavaTypeMapping) callbacks[i]).getMemberMetaData().getFullFieldName()));
}
callbacks[i].postInsert(op);
} catch (NotYetFlushedException e) {
op.updateFieldAfterInsert(e.getPersistable(), ((JavaTypeMapping) callbacks[i]).getMemberMetaData().getAbsoluteFieldNumber());
}
}
}
use of org.datanucleus.metadata.VersionMetaData in project datanucleus-rdbms by datanucleus.
the class LocateBulkRequest method processResults.
private ObjectProvider[] processResults(ResultSet rs, ObjectProvider[] ops) throws SQLException {
List<ObjectProvider> missingOps = new ArrayList<>();
for (int i = 0; i < ops.length; i++) {
missingOps.add(ops[i]);
}
ExecutionContext ec = ops[0].getExecutionContext();
while (rs.next()) {
FieldManager resultFM = new ResultSetGetter(ec, rs, resultMapping, cmd);
Object id = null;
Object key = null;
if (cmd.getIdentityType() == IdentityType.DATASTORE) {
StatementMappingIndex idx = resultMapping.getMappingForMemberPosition(SurrogateColumnType.DATASTORE_ID.getFieldNumber());
JavaTypeMapping idMapping = idx.getMapping();
key = idMapping.getObject(ec, rs, idx.getColumnPositions());
if (IdentityUtils.isDatastoreIdentity(key)) {
// If mapping is OIDMapping then returns an OID rather than the column value
key = IdentityUtils.getTargetKeyForDatastoreIdentity(key);
}
} else if (cmd.getIdentityType() == IdentityType.APPLICATION) {
if (cmd.usesSingleFieldIdentityClass()) {
int[] pkFieldNums = cmd.getPKMemberPositions();
AbstractMemberMetaData pkMmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[0]);
if (pkMmd.getType() == int.class) {
key = resultFM.fetchIntField(pkFieldNums[0]);
} else if (pkMmd.getType() == short.class) {
key = resultFM.fetchShortField(pkFieldNums[0]);
} else if (pkMmd.getType() == long.class) {
key = resultFM.fetchLongField(pkFieldNums[0]);
} else if (pkMmd.getType() == char.class) {
key = resultFM.fetchCharField(pkFieldNums[0]);
} else if (pkMmd.getType() == boolean.class) {
key = resultFM.fetchBooleanField(pkFieldNums[0]);
} else if (pkMmd.getType() == byte.class) {
key = resultFM.fetchByteField(pkFieldNums[0]);
} else if (pkMmd.getType() == double.class) {
key = resultFM.fetchDoubleField(pkFieldNums[0]);
} else if (pkMmd.getType() == float.class) {
key = resultFM.fetchFloatField(pkFieldNums[0]);
} else if (pkMmd.getType() == String.class) {
key = resultFM.fetchStringField(pkFieldNums[0]);
} else {
key = resultFM.fetchObjectField(pkFieldNums[0]);
}
} else {
id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, cmd, null, true, resultFM);
}
}
// Find which ObjectProvider this row is for
ObjectProvider op = null;
for (ObjectProvider missingOp : missingOps) {
Object opId = missingOp.getInternalObjectId();
if (cmd.getIdentityType() == IdentityType.DATASTORE) {
Object opKey = IdentityUtils.getTargetKeyForDatastoreIdentity(opId);
if (key != null && opKey.getClass() != key.getClass()) {
opKey = TypeConversionHelper.convertTo(opKey, key.getClass());
}
if (opKey.equals(key)) {
op = missingOp;
break;
}
} else if (cmd.getIdentityType() == IdentityType.APPLICATION) {
if (cmd.usesSingleFieldIdentityClass()) {
Object opKey = IdentityUtils.getTargetKeyForSingleFieldIdentity(opId);
if (opKey.equals(key)) {
op = missingOp;
break;
}
} else {
if (opId.equals(id)) {
op = missingOp;
break;
}
}
}
}
if (op != null) {
// Mark ObjectProvider as processed
missingOps.remove(op);
// Load up any unloaded fields that we have selected
int[] selectedMemberNums = resultMapping.getMemberNumbers();
int[] unloadedMemberNums = ClassUtils.getFlagsSetTo(op.getLoadedFields(), selectedMemberNums, false);
if (unloadedMemberNums != null && unloadedMemberNums.length > 0) {
op.replaceFields(unloadedMemberNums, resultFM);
}
// Load version if present and not yet set
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (op.getTransactionalVersion() == null && versionMapping != null) {
VersionMetaData currentVermd = table.getVersionMetaData();
Object datastoreVersion = null;
if (currentVermd != null) {
if (currentVermd.getFieldName() == null) {
// Surrogate version
// Why use true now?
versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
StatementMappingIndex verIdx = resultMapping.getMappingForMemberPosition(SurrogateColumnType.VERSION.getFieldNumber());
datastoreVersion = versionMapping.getObject(ec, rs, verIdx.getColumnPositions());
} else {
datastoreVersion = op.provideField(cmd.getAbsolutePositionOfMember(currentVermd.getFieldName()));
}
op.setVersion(datastoreVersion);
}
}
}
}
if (!missingOps.isEmpty()) {
return missingOps.toArray(new ObjectProvider[missingOps.size()]);
}
return null;
}
use of org.datanucleus.metadata.VersionMetaData in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method getObject.
/**
* Returns an instance of a persistable class.
* Processes the FK field and generates the id of the object from the result values, and hence the object itself.
* TODO Pass in the discriminator/version columns also where available
* @param ec execution context
* @param rs The ResultSet
* @param resultIndexes indexes in the ResultSet to retrieve
* @return The persistable object
*/
public Object getObject(ExecutionContext ec, final ResultSet rs, int[] resultIndexes) {
// Check for null FK
if (storeMgr.getResultValueAtPosition(rs, this, resultIndexes[0]) == null) {
// Assumption : if the first param is null, then the field is null
return null;
}
if (cmd == null) {
cmd = ec.getMetaDataManager().getMetaDataForClass(getType(), ec.getClassLoaderResolver());
}
// Return the object represented by this mapping
Object pc = null;
if (cmd.getIdentityType() == IdentityType.DATASTORE) {
pc = MappingHelper.getObjectForDatastoreIdentity(ec, this, rs, resultIndexes, cmd);
} else if (cmd.getIdentityType() == IdentityType.APPLICATION) {
pc = MappingHelper.getObjectForApplicationIdentity(ec, this, rs, resultIndexes, cmd);
} else {
return null;
}
// Sanity check that we have loaded the version also
ObjectProvider pcOP = ec.findObjectProvider(pc);
if (pcOP != null) {
VersionMetaData vermd = cmd.getVersionMetaDataForTable();
if (vermd != null && vermd.getVersionStrategy() != VersionStrategy.NONE && ec.getTransaction().getOptimistic() && !pcOP.isVersionLoaded()) {
// For some reason the version was not loaded on this object, and wanting to delete it, so load the version (+DFG) now.
// This can happen when we have 1-1 between A and B and we loaded the B field of A via FetchRequest but didn't pull in the version since the inheritance wasn't knowable
pcOP.loadUnloadedFieldsInFetchPlan();
}
}
return pc;
}
use of org.datanucleus.metadata.VersionMetaData in project datanucleus-api-jdo by datanucleus.
the class JDOAnnotationReader method processClassAnnotations.
/**
* Method to process the "class" level annotations and create the outline ClassMetaData object.
* Supports classes annotated with @PersistenceCapable, classes annotated with @PersistenceAware, and classes which have neither of those but have @Queries or @Query.
* @param pmd Parent PackageMetaData
* @param cls The class
* @param annotations Annotations for this class
* @param clr ClassLoader resolver
* @return The ClassMetaData/InterfaceMetaData (or null if no annotations)
*/
protected AbstractClassMetaData processClassAnnotations(PackageMetaData pmd, Class cls, AnnotationObject[] annotations, ClassLoaderResolver clr) {
if (annotations == null || annotations.length == 0) {
return null;
}
AbstractClassMetaData cmd = null;
AnnotationObject pcAnnotation = isClassPersistable(annotations);
if (pcAnnotation != null) {
// PersistenceCapable class
cmd = (cls.isInterface()) ? pmd.newInterfaceMetadata(ClassUtils.getClassNameForClass(cls)) : pmd.newClassMetadata(ClassUtils.getClassNameForClass(cls));
cmd.setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_CAPABLE);
// Process all attributes here in case needed for other annotations
processPersistenceCapableAnnotation(cls, cmd, pcAnnotation.getNameValueMap());
} else if (isClassPersistenceAware(annotations)) {
// PersistenceAware class
cmd = pmd.newClassMetadata(ClassUtils.getClassNameForClass(cls));
cmd.setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_AWARE);
} else if (doesClassHaveNamedQueries(annotations)) {
// Class with named query specified
cmd = pmd.newClassMetadata(ClassUtils.getClassNameForClass(cls));
cmd.setPersistenceModifier(ClassPersistenceModifier.NON_PERSISTENT);
} else {
// Not involved in the persistence process
return null;
}
// Cater for named queries being specified on a persistence aware, or other class
processNamedQueries(cmd, cls, annotations);
if (cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_CAPABLE) {
// Not persistable, so no further information needed
return cmd;
}
// Class is persistable so process annotations
for (AnnotationObject annotation : annotations) {
String annName = annotation.getName();
if (annName.equals(JDOAnnotationUtils.PERSISTENCE_CAPABLE)) {
// @PersistenceCapable is merged and processed above
continue;
}
Map<String, Object> annotationValues = annotation.getNameValueMap();
if (annName.equals(JDOAnnotationUtils.EMBEDDED_ONLY)) {
cmd.setEmbeddedOnly(true);
} else if (annName.equals(JDOAnnotationUtils.VERSION)) {
VersionStrategy versionStrategy = (VersionStrategy) annotationValues.get("strategy");
String strategy = JDOAnnotationUtils.getVersionStrategyString(versionStrategy);
String indexed = (String) annotationValues.get("indexed");
String column = (String) annotationValues.get("column");
Column[] columns = (Column[]) annotationValues.get("columns");
VersionMetaData vermd = new VersionMetaData();
vermd.setStrategy(strategy);
vermd.setColumnName(column);
vermd.setIndexed(IndexedValue.getIndexedValue(indexed));
if (columns != null && columns.length > 0) {
// Only use the first column
ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(columns[0]);
vermd.setColumnMetaData(colmd);
}
JDOAnnotationUtils.addExtensionsToMetaData(vermd, (Extension[]) annotationValues.get("extensions"));
vermd.setParent(cmd);
cmd.setVersionMetaData(vermd);
} else if (annName.equals(JDOAnnotationUtils.DATASTORE_IDENTITY)) {
String strategy = JDOAnnotationUtils.getValueGenerationStrategyString((IdGeneratorStrategy) annotationValues.get("strategy"));
String customStrategy = (String) annotationValues.get("customStrategy");
if (!StringUtils.isWhitespace(customStrategy)) {
// User has provided an extension strategy
strategy = customStrategy;
}
String sequence = (String) annotationValues.get("sequence");
String column = (String) annotationValues.get("column");
Column[] columns = (Column[]) annotationValues.get("columns");
IdentityMetaData idmd = new IdentityMetaData();
idmd.setColumnName(column);
idmd.setValueStrategy(ValueGenerationStrategy.getIdentityStrategy(strategy));
idmd.setSequence(sequence);
if (columns != null && columns.length > 0) {
// Only use the first column
ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(columns[0]);
idmd.setColumnMetaData(colmd);
}
JDOAnnotationUtils.addExtensionsToMetaData(idmd, (Extension[]) annotationValues.get("extensions"));
idmd.setParent(cmd);
cmd.setIdentityMetaData(idmd);
} else if (annName.equals(JDOAnnotationUtils.PRIMARY_KEY)) {
String pkName = (String) annotationValues.get("name");
String pkColumn = (String) annotationValues.get("column");
Column[] columns = (Column[]) annotationValues.get("columns");
PrimaryKeyMetaData pkmd = new PrimaryKeyMetaData();
pkmd.setName(pkName);
pkmd.setColumnName(pkColumn);
if (columns != null && columns.length > 0) {
for (Column column : columns) {
pkmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(column));
}
}
JDOAnnotationUtils.addExtensionsToMetaData(pkmd, (Extension[]) annotationValues.get("extensions"));
pkmd.setParent(cmd);
cmd.setPrimaryKeyMetaData(pkmd);
} else if (annName.equals(JDOAnnotationUtils.JOINS)) {
Join[] js = (Join[]) annotationValues.get("value");
if (js != null && js.length > 0) {
for (Join join : js) {
JoinMetaData joinmd = cmd.newJoinMetaData();
joinmd.setTable(join.table());
joinmd.setColumnName(join.column());
joinmd.setIndexed(IndexedValue.getIndexedValue(join.indexed()));
joinmd.setOuter(MetaDataUtils.getBooleanForString(join.outer(), false));
joinmd.setUnique(join.unique());
joinmd.setDeleteAction(JDOAnnotationUtils.getForeignKeyActionString(join.deleteAction()));
JDOAnnotationUtils.addExtensionsToMetaData(joinmd, join.extensions());
}
}
} else if (annName.equals(JDOAnnotationUtils.JOIN)) {
JoinMetaData joinmd = cmd.newJoinMetaData();
joinmd.setTable((String) annotationValues.get("table"));
joinmd.setColumnName((String) annotationValues.get("column"));
joinmd.setIndexed(IndexedValue.getIndexedValue((String) annotationValues.get("indexed")));
joinmd.setOuter(MetaDataUtils.getBooleanForString((String) annotationValues.get("outer"), false));
joinmd.setUnique((String) annotationValues.get("unique"));
joinmd.setDeleteAction(((ForeignKeyAction) annotationValues.get("deleteAction")).toString());
JDOAnnotationUtils.addExtensionsToMetaData(joinmd, (Extension[]) annotationValues.get("extensions"));
} else if (annName.equals(JDOAnnotationUtils.INHERITANCE)) {
String strategy = JDOAnnotationUtils.getInheritanceStrategyString((InheritanceStrategy) annotationValues.get("strategy"));
String customStrategy = (String) annotationValues.get("customStrategy");
if (!StringUtils.isWhitespace(customStrategy)) {
// User has provided an extension strategy
strategy = customStrategy;
}
InheritanceMetaData inhmd = cmd.getInheritanceMetaData();
if (inhmd == null) {
inhmd = cmd.newInheritanceMetadata();
}
inhmd.setStrategy(strategy);
} else if (annName.equals(JDOAnnotationUtils.DISCRIMINATOR)) {
DiscriminatorStrategy discriminatorStrategy = (DiscriminatorStrategy) annotationValues.get("strategy");
String strategy = JDOAnnotationUtils.getDiscriminatorStrategyString(discriminatorStrategy);
String column = (String) annotationValues.get("column");
String indexed = (String) annotationValues.get("indexed");
String value = (String) annotationValues.get("value");
Column[] columns = (Column[]) annotationValues.get("columns");
InheritanceMetaData inhmd = cmd.getInheritanceMetaData();
if (inhmd == null) {
inhmd = cmd.newInheritanceMetadata();
}
DiscriminatorMetaData dismd = inhmd.newDiscriminatorMetadata();
dismd.setColumnName(column);
dismd.setValue(value);
dismd.setStrategy(strategy);
dismd.setIndexed(indexed);
if (columns != null && columns.length > 0) {
// Only use the first column
ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(columns[0]);
dismd.setColumnMetaData(colmd);
}
} else if (annName.equals(JDOAnnotationUtils.FETCHPLANS)) {
FileMetaData filemd = (FileMetaData) pmd.getParent();
FetchPlan[] plans = (FetchPlan[]) annotationValues.get("value");
for (FetchPlan plan : plans) {
FetchPlanMetaData fpmd = filemd.newFetchPlanMetadata(plan.name());
fpmd.setFetchSize(plan.fetchSize());
fpmd.setMaxFetchDepth(plan.maxFetchDepth());
int numGroups = plan.fetchGroups().length;
for (int k = 0; k < numGroups; k++) {
fpmd.addFetchGroup(new FetchGroupMetaData(plan.fetchGroups()[k]));
}
}
} else if (annName.equals(JDOAnnotationUtils.FETCHPLAN)) {
FileMetaData filemd = (FileMetaData) pmd.getParent();
FetchPlanMetaData fpmd = filemd.newFetchPlanMetadata((String) annotationValues.get("name"));
fpmd.setFetchSize(((Integer) annotationValues.get("fetchSize")).intValue());
fpmd.setMaxFetchDepth(((Integer) annotationValues.get("maxFetchDepth")).intValue());
String[] fpFetchGroups = (String[]) annotationValues.get("fetchGroups");
for (String fpFetchGroup : fpFetchGroups) {
fpmd.addFetchGroup(new FetchGroupMetaData(fpFetchGroup));
}
} else if (annName.equals(JDOAnnotationUtils.FETCHGROUPS)) {
FetchGroup[] groups = (FetchGroup[]) annotationValues.get("value");
for (FetchGroup group : groups) {
FetchGroupMetaData fgmd = cmd.newFetchGroupMetaData(group.name());
if (!StringUtils.isWhitespace(group.postLoad())) {
fgmd.setPostLoad(Boolean.valueOf(group.postLoad()));
}
int numFields = group.members().length;
for (int k = 0; k < numFields; k++) {
FetchGroupMemberMetaData fgmmd = new FetchGroupMemberMetaData(fgmd, group.members()[k].name());
fgmmd.setRecursionDepth(group.members()[k].recursionDepth());
fgmd.addMember(fgmmd);
}
int numGroups = group.fetchGroups().length;
for (int k = 0; k < numGroups; k++) {
fgmd.addFetchGroup(new FetchGroupMetaData(group.fetchGroups()[k]));
}
}
} else if (annName.equals(JDOAnnotationUtils.FETCHGROUP)) {
FetchGroupMetaData fgmd = cmd.newFetchGroupMetaData((String) annotationValues.get("name"));
String postLoadStr = (String) annotationValues.get("postLoad");
if (!StringUtils.isWhitespace(postLoadStr)) {
fgmd.setPostLoad(Boolean.valueOf(postLoadStr));
}
Persistent[] fields = (Persistent[]) annotationValues.get("members");
if (fields != null) {
for (Persistent field : fields) {
FetchGroupMemberMetaData fgmmd = new FetchGroupMemberMetaData(fgmd, field.name());
fgmmd.setRecursionDepth(field.recursionDepth());
fgmd.addMember(fgmmd);
}
}
} else if (annName.equals(JDOAnnotationUtils.SEQUENCE)) {
String seqName = (String) annotationValues.get("name");
String seqStrategy = JDOAnnotationUtils.getSequenceStrategyString((SequenceStrategy) annotationValues.get("strategy"));
String seqSeq = (String) annotationValues.get("datastoreSequence");
Class seqFactory = (Class) annotationValues.get("factoryClass");
String seqFactoryClassName = null;
if (seqFactory != null && seqFactory != void.class) {
seqFactoryClassName = seqFactory.getName();
}
Integer seqSize = (Integer) annotationValues.get("allocationSize");
Integer seqStart = (Integer) annotationValues.get("initialValue");
if (StringUtils.isWhitespace(seqName)) {
throw new InvalidClassMetaDataException("044155", cmd.getFullClassName());
}
SequenceMetaData seqmd = new SequenceMetaData(seqName, seqStrategy);
seqmd.setFactoryClass(seqFactoryClassName);
seqmd.setDatastoreSequence(seqSeq);
if (seqSize != null) {
seqmd.setAllocationSize(seqSize);
}
if (seqStart != null) {
seqmd.setInitialValue(seqStart);
}
JDOAnnotationUtils.addExtensionsToMetaData(seqmd, (Extension[]) annotationValues.get("extensions"));
// Sequence - currently only allowing 1 per class (should really be on the package)
cmd.getPackageMetaData().addSequence(seqmd);
} else if (annName.equals(JDOAnnotationUtils.INDICES)) {
// Multiple Indices for the class
Index[] values = (Index[]) annotationValues.get("value");
if (values != null && values.length > 0) {
for (Index idx : values) {
IndexMetaData idxmd = JDOAnnotationUtils.getIndexMetaData(idx.name(), idx.table(), "" + idx.unique(), idx.members(), idx.columns());
if (idxmd.getNumberOfColumns() == 0 && idxmd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044204", cls.getName()));
} else {
cmd.addIndex(idxmd);
idxmd.setParent(cmd);
}
}
}
} else if (annName.equals(JDOAnnotationUtils.INDEX)) {
// Single Index for the class
String name = (String) annotationValues.get("name");
String table = (String) annotationValues.get("table");
String unique = (String) annotationValues.get("unique");
String[] members = (String[]) annotationValues.get("members");
Column[] columns = (Column[]) annotationValues.get("columns");
IndexMetaData idxmd = JDOAnnotationUtils.getIndexMetaData(name, table, unique, members, columns);
JDOAnnotationUtils.addExtensionsToMetaData(idxmd, (Extension[]) annotationValues.get("extensions"));
if (idxmd.getNumberOfColumns() == 0 && idxmd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044204", cls.getName()));
} else {
cmd.addIndex(idxmd);
idxmd.setParent(cmd);
}
} else if (annName.equals(JDOAnnotationUtils.UNIQUES)) {
// Multiple Unique Constraints for the class
Unique[] values = (Unique[]) annotationValues.get("value");
if (values != null && values.length > 0) {
for (Unique uni : values) {
UniqueMetaData unimd = JDOAnnotationUtils.getUniqueMetaData(uni.name(), uni.table(), "" + uni.deferred(), uni.members(), uni.columns());
if (unimd.getNumberOfColumns() == 0 && unimd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044205", cls.getName()));
} else {
cmd.addUniqueConstraint(unimd);
unimd.setParent(cmd);
}
}
}
} else if (annName.equals(JDOAnnotationUtils.UNIQUE)) {
// Single Unique constraint for the class
String name = (String) annotationValues.get("name");
String table = (String) annotationValues.get("table");
String deferred = (String) annotationValues.get("deferred");
String[] members = (String[]) annotationValues.get("members");
Column[] columns = (Column[]) annotationValues.get("columns");
UniqueMetaData unimd = JDOAnnotationUtils.getUniqueMetaData(name, table, deferred, members, columns);
JDOAnnotationUtils.addExtensionsToMetaData(unimd, (Extension[]) annotationValues.get("extensions"));
if (unimd.getNumberOfColumns() == 0 && unimd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044205", cls.getName()));
} else {
cmd.addUniqueConstraint(unimd);
unimd.setParent(cmd);
}
} else if (annName.equals(JDOAnnotationUtils.FOREIGNKEYS)) {
// Multiple FKs for the class
ForeignKey[] values = (ForeignKey[]) annotationValues.get("value");
if (values != null && values.length > 0) {
for (ForeignKey fk : values) {
String deleteAction = JDOAnnotationUtils.getForeignKeyActionString(fk.deleteAction());
String updateAction = JDOAnnotationUtils.getForeignKeyActionString(fk.updateAction());
ForeignKeyMetaData fkmd = JDOAnnotationUtils.getFKMetaData(fk.name(), fk.table(), fk.unique(), "" + fk.deferred(), deleteAction, updateAction, fk.members(), fk.columns());
if (fkmd.getNumberOfColumns() == 0 && fkmd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044206", cls.getName()));
} else {
cmd.addForeignKey(fkmd);
fkmd.setParent(cmd);
}
}
}
} else if (annName.equals(JDOAnnotationUtils.FOREIGNKEY)) {
// Single FK constraint for the class
String name = (String) annotationValues.get("name");
String table = (String) annotationValues.get("table");
String unique = (String) annotationValues.get("unique");
String deferred = (String) annotationValues.get("deferred");
String deleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
String updateAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("updateAction"));
String[] members = (String[]) annotationValues.get("members");
Column[] columns = (Column[]) annotationValues.get("columns");
ForeignKeyMetaData fkmd = JDOAnnotationUtils.getFKMetaData(name, table, unique, deferred, deleteAction, updateAction, members, columns);
JDOAnnotationUtils.addExtensionsToMetaData(fkmd, (Extension[]) annotationValues.get("extensions"));
if (fkmd.getNumberOfColumns() == 0 && fkmd.getNumberOfMembers() == 0) {
NucleusLogger.METADATA.warn(Localiser.msg("044206", cls.getName()));
} else {
cmd.addForeignKey(fkmd);
fkmd.setParent(cmd);
}
} else if (annName.equals(JDOAnnotationUtils.COLUMNS)) {
// Unmapped column specification
Column[] cols = (Column[]) annotationValues.get("value");
if (cols != null && cols.length > 0) {
for (Column col : cols) {
ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(col);
JDOAnnotationUtils.addExtensionsToMetaData(colmd, col.extensions());
colmd.setParent(cmd);
cmd.addUnmappedColumn(colmd);
}
}
} else if (annName.equals(JDOAnnotationUtils.CACHEABLE)) {
String cache = (String) annotationValues.get("value");
if (cache != null && cache.equalsIgnoreCase("false")) {
cmd.setCacheable(false);
}
} else if (annName.equals(JDOAnnotationUtils.EXTENSIONS)) {
Extension[] values = (Extension[]) annotationValues.get("value");
if (values != null && values.length > 0) {
for (Extension ext : values) {
String vendorName = ext.vendorName();
if (StringUtils.isWhitespace(vendorName)) {
throw new InvalidMetaDataException("044160", vendorName, ext.key().toString(), ext.value().toString());
} else if (vendorName.equalsIgnoreCase(MetaData.VENDOR_NAME)) {
cmd.addExtension(ext.key().toString(), ext.value().toString());
}
}
}
} else if (annName.equals(JDOAnnotationUtils.EXTENSION)) {
String vendorName = (String) annotationValues.get("vendorName");
if (StringUtils.isWhitespace(vendorName)) {
throw new InvalidMetaDataException("044160", vendorName, annotationValues.get("key"), annotationValues.get("value"));
} else if (vendorName.equalsIgnoreCase(MetaData.VENDOR_NAME)) {
cmd.addExtension((String) annotationValues.get("key"), (String) annotationValues.get("value"));
}
} else {
if (!annName.equals(JDOAnnotationUtils.PERSISTENCE_AWARE) && !annName.equals(JDOAnnotationUtils.QUERIES) && !annName.equals(JDOAnnotationUtils.QUERY)) {
NucleusLogger.METADATA.debug(Localiser.msg("044203", cls.getName(), annotation.getName()));
}
}
}
NucleusLogger.METADATA.debug(Localiser.msg("044200", cls.getName(), "JDO"));
return cmd;
}
Aggregations