Search in sources :

Example 96 with JavaTypeMapping

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

the class TemporalDayMethod4 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 expr, List<SQLExpression> args) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "DAY");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping, "%d"));
    funcArgs.add(invokedExpr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 97 with JavaTypeMapping

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

the class SumFunction 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 expr, List<SQLExpression> args) {
    if (expr == null) {
        if (args == null || args.size() != 1) {
            throw new NucleusException(getFunctionName() + " is only supported with a single argument");
        }
        // Use same java type as the argument
        SQLExpression argExpr = args.get(0);
        JavaTypeMapping m = null;
        Class cls = argExpr.getJavaTypeMapping().getJavaType();
        if (cls == Integer.class || cls == Short.class || cls == Long.class) {
            m = stmt.getSQLExpressionFactory().getMappingForType(Long.class, true);
        } else if (Number.class.isAssignableFrom(cls)) {
            m = stmt.getSQLExpressionFactory().getMappingForType(argExpr.getJavaTypeMapping().getJavaType(), true);
        } else {
            throw new NucleusUserException("Cannot perform static SUM with arg of type " + cls.getName());
        }
        return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
    }
    throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AggregateNumericExpression(org.datanucleus.store.rdbms.sql.expression.AggregateNumericExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 98 with JavaTypeMapping

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

the class ClassTable method provideExternalMappings.

/**
 * Provide the mappings to the consumer for all external fields mapped to this table
 * of the specified type
 * @param consumer Consumer for the mappings
 * @param mappingType Type of external mapping
 */
public final void provideExternalMappings(MappingConsumer consumer, MappingType mappingType) {
    if (mappingType == MappingType.EXTERNAL_FK && externalFkMappings != null) {
        consumer.preConsumeMapping(highestMemberNumber + 1);
        Iterator<AbstractMemberMetaData> iter = externalFkMappings.keySet().iterator();
        while (iter.hasNext()) {
            AbstractMemberMetaData fmd = iter.next();
            JavaTypeMapping fieldMapping = externalFkMappings.get(fmd);
            if (fieldMapping != null) {
                consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_FK);
            }
        }
    } else if (mappingType == MappingType.EXTERNAL_FK_DISCRIMINATOR && externalFkDiscriminatorMappings != null) {
        consumer.preConsumeMapping(highestMemberNumber + 1);
        Iterator<AbstractMemberMetaData> iter = externalFkDiscriminatorMappings.keySet().iterator();
        while (iter.hasNext()) {
            AbstractMemberMetaData fmd = iter.next();
            JavaTypeMapping fieldMapping = externalFkDiscriminatorMappings.get(fmd);
            if (fieldMapping != null) {
                consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_FK_DISCRIMINATOR);
            }
        }
    } else if (mappingType == MappingType.EXTERNAL_INDEX && externalOrderMappings != null) {
        consumer.preConsumeMapping(highestMemberNumber + 1);
        Iterator<AbstractMemberMetaData> iter = externalOrderMappings.keySet().iterator();
        while (iter.hasNext()) {
            AbstractMemberMetaData fmd = iter.next();
            JavaTypeMapping fieldMapping = externalOrderMappings.get(fmd);
            if (fieldMapping != null) {
                consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_INDEX);
            }
        }
    }
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Iterator(java.util.Iterator) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 99 with JavaTypeMapping

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

the class ClassTable method getMemberMapping.

/**
 * Accessor for the field mapping for the named field.
 * The field may exist in a parent table or a secondary table.
 * Throws a NoSuchPersistentFieldException if the field name is not found.
 * TODO Use of this is discouraged since the fieldName is not fully qualified
 * and if a superclass-table inheritance is used we could have 2 fields of that name here.
 * @param memberName Name of field/property
 * @return The mapping.
 * @throws NoSuchPersistentFieldException Thrown when the field/property is not found
 */
public JavaTypeMapping getMemberMapping(String memberName) {
    assertIsInitialized();
    AbstractMemberMetaData mmd = getMetaDataForMember(memberName);
    JavaTypeMapping m = getMemberMapping(mmd);
    if (m == null) {
        throw new NoSuchPersistentFieldException(cmd.getFullClassName(), memberName);
    }
    return m;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NoSuchPersistentFieldException(org.datanucleus.store.rdbms.exceptions.NoSuchPersistentFieldException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 100 with JavaTypeMapping

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

the class ClassTable method getExpectedCandidateKeys.

/**
 * Accessor for the expected candidate keys for this table.
 * @return The expected candidate keys.
 */
protected List<CandidateKey> getExpectedCandidateKeys() {
    assertIsInitialized();
    // The candidate keys required by the basic table
    List<CandidateKey> candidateKeys = super.getExpectedCandidateKeys();
    // Add any constraints required for a FK map
    Iterator<CandidateKey> cks = candidateKeysByMapField.values().iterator();
    while (cks.hasNext()) {
        CandidateKey ck = cks.next();
        candidateKeys.add(ck);
    }
    // Add on any user-required candidate keys for the fields
    Set fieldNumbersSet = memberMappingsMap.keySet();
    Iterator iter = fieldNumbersSet.iterator();
    while (iter.hasNext()) {
        AbstractMemberMetaData fmd = (AbstractMemberMetaData) iter.next();
        JavaTypeMapping fieldMapping = memberMappingsMap.get(fmd);
        if (fieldMapping instanceof EmbeddedPCMapping) {
            // Add indexes for fields of this embedded PC object
            EmbeddedPCMapping embMapping = (EmbeddedPCMapping) fieldMapping;
            for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
                if (umd != null) {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null) {
                        candidateKeys.add(ck);
                    }
                }
            }
        } else {
            // Add any required candidate key for this field
            UniqueMetaData umd = fmd.getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, fieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    }
    // Add on any user-required candidate keys for the class(es) as a whole (composite keys)
    Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
    while (cmdIter.hasNext()) {
        AbstractClassMetaData thisCmd = cmdIter.next();
        List<UniqueMetaData> classCKs = thisCmd.getUniqueMetaData();
        if (classCKs != null) {
            for (UniqueMetaData unimd : classCKs) {
                CandidateKey ck = getCandidateKeyForUniqueMetaData(unimd);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    }
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        // Make sure there is no reuse of PK fields that cause a duplicate index for the PK. Remove it if required
        PrimaryKey pk = getPrimaryKey();
        Iterator<CandidateKey> candidatesIter = candidateKeys.iterator();
        while (candidatesIter.hasNext()) {
            CandidateKey key = candidatesIter.next();
            if (key.getColumnList().equals(pk.getColumnList())) {
                NucleusLogger.DATASTORE_SCHEMA.debug("Candidate key " + key + " is for the same columns as the PrimaryKey so being removed from expected set of candidates. PK is always unique");
                candidatesIter.remove();
            }
        }
    }
    return candidateKeys;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) Iterator(java.util.Iterator) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)230 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)96 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)82 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)81 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)76 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)69 ArrayList (java.util.ArrayList)66 NucleusException (org.datanucleus.exceptions.NucleusException)57 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)50 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)47 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)43 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)37 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)30 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)30 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)27 List (java.util.List)25 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)22 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)21 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)21