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);
}
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));
}
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);
}
}
}
}
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;
}
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;
}
Aggregations