Search in sources :

Example 1 with DatastoreIdMapping

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

the class AbstractClassTable method addDatastoreId.

/**
 * Utility to create the datastore identity column and mapping.
 * This is used in 2 modes. The first is where we have a (primary) class table
 * and we aren't creating the OID mapping as a FK to another class. The second
 * is where we have a (secondary) class table and we are creating the OID mapping
 * as a FK to the primary class. In the second case the refTable will be specified.
 * @param columnMetaData The column MetaData for the datastore id
 * @param refTable Table used as a reference (if any)
 * @param cmd The MetaData for the class
 */
void addDatastoreId(ColumnMetaData columnMetaData, DatastoreClass refTable, AbstractClassMetaData cmd) {
    // Create the mapping, setting its table
    datastoreIdMapping = new DatastoreIdMapping();
    datastoreIdMapping.setTable(this);
    datastoreIdMapping.initialize(storeMgr, cmd.getFullClassName());
    // Create a ColumnMetaData in the container if none is defined
    ColumnMetaData colmd = null;
    if (columnMetaData == null) {
        colmd = new ColumnMetaData();
    } else {
        colmd = columnMetaData;
    }
    if (colmd.getName() == null) {
        // Provide default column naming if none is defined
        if (refTable != null) {
            colmd.setName(storeMgr.getIdentifierFactory().newColumnIdentifier(refTable.getIdentifier().getName(), this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(DatastoreId.class), FieldRole.ROLE_OWNER, false).getName());
        } else {
            colmd.setName(storeMgr.getIdentifierFactory().newColumnIdentifier(identifier.getName(), this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(DatastoreId.class), FieldRole.ROLE_NONE, false).getName());
        }
    }
    // Add the datastore identity column as the PK
    Column idColumn = addColumn(DatastoreId.class.getName(), storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colmd.getName()), datastoreIdMapping, colmd);
    idColumn.setPrimaryKey();
    // Set the identity column type based on the IdentityStrategy
    String strategyName = cmd.getIdentityMetaData().getValueStrategy().toString();
    if (cmd.getIdentityMetaData().getValueStrategy().equals(ValueGenerationStrategy.CUSTOM)) {
        strategyName = cmd.getIdentityMetaData().getValueStrategy().getCustomName();
    }
    if (strategyName != null && ValueGenerationStrategy.NATIVE.toString().equals(strategyName)) {
        strategyName = storeMgr.getValueGenerationStrategyForNative(cmd, -1);
    }
    // Check the value generator type being stored
    Class valueGeneratedType = Long.class;
    if (strategyName != null && ValueGenerationStrategy.IDENTITY.toString().equals(strategyName)) {
        valueGeneratedType = dba.getAutoIncrementJavaTypeForType(valueGeneratedType);
        if (valueGeneratedType != Long.class) {
            NucleusLogger.DATASTORE_SCHEMA.debug("Class " + cmd.getFullClassName() + " uses IDENTITY strategy and rather than using BIGINT " + " for the column type, using " + valueGeneratedType.getName() + " since the datastore requires that");
        }
    } else {
        // Get the type that will be generated by the chosen ValueGeneration strategy
        valueGeneratedType = storeMgr.getValueGenerationManager().getTypeForValueGeneratorForMember(strategyName, storeMgr.getValueGenerationManager().getMemberKey(cmd, -1));
    }
    storeMgr.getMappingManager().createDatastoreMapping(datastoreIdMapping, idColumn, valueGeneratedType.getName());
    logMapping("DATASTORE_ID", datastoreIdMapping);
    // Handle any auto-increment requirement
    if (isObjectIdDatastoreAttributed()) {
        if (this instanceof DatastoreClass && ((DatastoreClass) this).isBaseDatastoreClass()) {
            // Only the base class can be autoincremented
            idColumn.setIdentity(true);
        }
    }
    // Check if auto-increment and that it is supported by this RDBMS
    if (idColumn.isIdentity() && !dba.supportsOption(DatastoreAdapter.IDENTITY_COLUMNS)) {
        throw new NucleusException(Localiser.msg("057020", cmd.getFullClassName(), "datastore-identity")).setFatal();
    }
}
Also used : DatastoreIdMapping(org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping) DatastoreId(org.datanucleus.identity.DatastoreId) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 2 with DatastoreIdMapping

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

the class QueryToSQLMapper method processCreatorExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processCreatorExpression(org.datanucleus.query.expression.CreatorExpression)
     */
@Override
protected Object processCreatorExpression(CreatorExpression expr) {
    String className = expr.getId();
    Class cls = null;
    try {
        cls = clr.classForName(className);
    } catch (ClassNotResolvedException cnre) {
        if (importsDefinition != null) {
            cls = importsDefinition.resolveClassDeclaration(className, clr, null);
        }
    }
    List<SQLExpression> ctrArgExprs = null;
    List args = expr.getArguments();
    if (args != null) {
        Class[] ctrArgTypes = new Class[args.size()];
        boolean[] ctrArgTypeCheck = new boolean[args.size()];
        ctrArgExprs = new ArrayList<>(args.size());
        Iterator iter = args.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Expression argExpr = (Expression) iter.next();
            SQLExpression sqlExpr = (SQLExpression) evaluate(argExpr);
            // TODO Cater for "SQL_function" mapping on to ANY type of constructor argument at that position since we don't know the precise type
            if (argExpr instanceof InvokeExpression && ((InvokeExpression) argExpr).getOperation().equalsIgnoreCase("SQL_function")) {
                ctrArgTypeCheck[i] = false;
            } else {
                ctrArgTypeCheck[i] = true;
            }
            ctrArgExprs.add(sqlExpr);
            if (sqlExpr instanceof NewObjectExpression) {
                ctrArgTypes[i] = ((NewObjectExpression) sqlExpr).getNewClass();
            } else if (sqlExpr.getJavaTypeMapping() instanceof DatastoreIdMapping || sqlExpr.getJavaTypeMapping() instanceof PersistableMapping) {
                ctrArgTypes[i] = clr.classForName(sqlExpr.getJavaTypeMapping().getType());
            } else {
                ctrArgTypes[i] = sqlExpr.getJavaTypeMapping().getJavaType();
            }
            i++;
        }
        // Check that this class has the required constructor
        Constructor ctr = ClassUtils.getConstructorWithArguments(cls, ctrArgTypes, ctrArgTypeCheck);
        if (ctr == null) {
            throw new NucleusUserException(Localiser.msg("021033", className, StringUtils.objectArrayToString(ctrArgTypes)));
        }
    }
    // TODO Retain the selected constructor (above)
    NewObjectExpression newExpr = new NewObjectExpression(stmt, cls, ctrArgExprs);
    stack.push(newExpr);
    return newExpr;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) Constructor(java.lang.reflect.Constructor) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) DatastoreIdMapping(org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping) CaseExpression(org.datanucleus.query.expression.CaseExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Expression(org.datanucleus.query.expression.Expression) TypeExpression(org.datanucleus.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FetchPlanForClass(org.datanucleus.FetchPlanForClass) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DatastoreIdMapping (org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping)2 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 FetchPlanForClass (org.datanucleus.FetchPlanForClass)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 DatastoreId (org.datanucleus.identity.DatastoreId)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)1 CaseExpression (org.datanucleus.query.expression.CaseExpression)1 ClassExpression (org.datanucleus.query.expression.ClassExpression)1 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)1 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)1 Expression (org.datanucleus.query.expression.Expression)1 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)1 JoinExpression (org.datanucleus.query.expression.JoinExpression)1