Search in sources :

Example 31 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class JDOHelperGetObjectIdMethod method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression ignore, List<SQLExpression> args) {
    if (args == null || args.size() == 0) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getObjectId without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    }
    if (expr instanceof SQLLiteral) {
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        ApiAdapter api = storeMgr.getApiAdapter();
        Object id = api.getIdForObject(((SQLLiteral) expr).getValue());
        if (id == null) {
            return new NullLiteral(stmt, null, null, null);
        }
        JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(id.getClass(), true);
        return new ObjectLiteral(stmt, m, id, null);
    } else if (ObjectExpression.class.isAssignableFrom(expr.getClass())) {
        // When the expression represents a PC object need to extract out as the identity
        if (expr.getJavaTypeMapping() instanceof PersistableMapping) {
            JavaTypeMapping mapping = new PersistableIdMapping((PersistableMapping) expr.getJavaTypeMapping());
            return new ObjectExpression(stmt, expr.getSQLTable(), mapping);
        } else if (expr.getJavaTypeMapping() instanceof ReferenceMapping) {
            JavaTypeMapping mapping = new ReferenceIdMapping((ReferenceMapping) expr.getJavaTypeMapping());
            return new ObjectExpression(stmt, expr.getSQLTable(), mapping);
        }
        return expr;
    }
    throw new IllegalExpressionOperationException("JDOHelper.getObjectId", expr);
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) ObjectLiteral(org.datanucleus.store.rdbms.sql.expression.ObjectLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ReferenceIdMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceIdMapping) PersistableIdMapping(org.datanucleus.store.rdbms.mapping.java.PersistableIdMapping) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Example 32 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class JDOHelperGetVersionMethod method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression ignore, List<SQLExpression> args) {
    if (args == null || args.size() == 0) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getVersion without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getVersion on null expression");
    }
    if (expr instanceof SQLLiteral) {
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        ApiAdapter api = storeMgr.getApiAdapter();
        Object obj = ((SQLLiteral) expr).getValue();
        if (obj == null || !api.isPersistable(obj)) {
            return new NullLiteral(stmt, null, null, null);
        }
        Object ver = stmt.getRDBMSManager().getApiAdapter().getVersionForObject(obj);
        JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(ver.getClass(), true);
        return new ObjectLiteral(stmt, m, ver, null);
    } else if (ObjectExpression.class.isAssignableFrom(expr.getClass())) {
        if (((ObjectExpression) expr).getJavaTypeMapping() instanceof PersistableMapping) {
            JavaTypeMapping mapping = ((ObjectExpression) expr).getJavaTypeMapping();
            DatastoreClass table = (DatastoreClass) expr.getSQLTable().getTable();
            if (// Version of candidate
            table.getIdMapping() == mapping) {
                mapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
                if (mapping == null) {
                    throw new NucleusUserException("Cannot use JDOHelper.getVersion on object that has no version information");
                }
                if (table.getVersionMetaData().getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
                    return new NumericExpression(stmt, expr.getSQLTable(), mapping);
                }
                return new TemporalExpression(stmt, expr.getSQLTable(), mapping);
            }
            throw new NucleusUserException("Dont currently support JDOHelper.getVersion(ObjectExpression) for expr=" + expr + " on table=" + expr.getSQLTable());
        // TODO Implement this
        }
        return expr;
    }
    throw new IllegalExpressionOperationException("JDOHelper.getVersion", expr);
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ApiAdapter(org.datanucleus.api.ApiAdapter) ObjectLiteral(org.datanucleus.store.rdbms.sql.expression.ObjectLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Example 33 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class ExpressionUtils method populatePrimaryKeyMappingsValuesForPCMapping.

/**
 * Convenience method to populate PK mappings/values allowing for recursion where a PK field is itself
 * a PCMapping, that itself has PK mappings, which in turn may include PCMappings.
 * The pkMappings/pkFieldValues arrays are already created and we populate from "position".
 * @param pkMappings The array of pk mappings to be populated
 * @param pkFieldValues The array of pk field values to be populated
 * @param position The current position needing populating
 * @param pcMapping The PC mapping we are processing
 * @param cmd ClassMetaData for the owning class with this PCMapping field
 * @param mmd Field metadata for the field that this PCMapping represents
 * @param fieldValue The value for the PCMapping field in the owning object
 * @param storeMgr Store Manager
 * @param clr ClassLoader resolver
 * @return The current position (after our processing)
 */
public static int populatePrimaryKeyMappingsValuesForPCMapping(JavaTypeMapping[] pkMappings, Object[] pkFieldValues, int position, PersistableMapping pcMapping, AbstractClassMetaData cmd, AbstractMemberMetaData mmd, Object fieldValue, RDBMSStoreManager storeMgr, ClassLoaderResolver clr) {
    ExecutionContext ec = storeMgr.getApiAdapter().getExecutionContext(fieldValue);
    JavaTypeMapping[] subMappings = pcMapping.getJavaTypeMapping();
    if (subMappings.length == 0) {
        // Embedded PC has no PK so must be embedded-only so use mapping from owner table
        DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
        JavaTypeMapping ownerMapping = table.getMemberMapping(mmd);
        EmbeddedMapping embMapping = (EmbeddedMapping) ownerMapping;
        for (int k = 0; k < embMapping.getNumberOfJavaTypeMappings(); k++) {
            JavaTypeMapping subMapping = embMapping.getJavaTypeMapping(k);
            pkMappings[position] = subMapping;
            pkFieldValues[position] = getValueForMemberOfObject(ec, subMapping.getMemberMetaData(), fieldValue);
            position++;
        }
    } else {
        AbstractClassMetaData pcCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(pcMapping.getType(), clr);
        int[] pcPkPositions = pcCmd.getPKMemberPositions();
        for (int k = 0; k < subMappings.length; k++) {
            AbstractMemberMetaData pcMmd = pcCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcPkPositions[k]);
            if (subMappings[k] instanceof PersistableMapping) {
                Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
                position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappings, pkFieldValues, position, (PersistableMapping) subMappings[k], pcCmd, pcMmd, val, storeMgr, clr);
            } else {
                Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
                pkMappings[position] = subMappings[k];
                pkFieldValues[position] = val;
                position++;
            }
        }
    }
    return position;
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ExecutionContext(org.datanucleus.ExecutionContext) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Aggregations

PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)33 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)27 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)17 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)17 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)16 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)15 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)13 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)10 Collection (java.util.Collection)7 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 Map (java.util.Map)5 NucleusException (org.datanucleus.exceptions.NucleusException)5 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)5 FetchPlanForClass (org.datanucleus.FetchPlanForClass)4