Search in sources :

Example 6 with PersistableIdMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableIdMapping 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)

Aggregations

JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 PersistableIdMapping (org.datanucleus.store.rdbms.mapping.java.PersistableIdMapping)6 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)4 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)2 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 HashSet (java.util.HashSet)1