Search in sources :

Example 1 with LongMapping

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

the class ClassTable method addOrderColumn.

/**
 * Adds an ordering column to the element table (this) in FK list relationships.
 * Used to store the position of the element in the List.
 * If the <order> provides a mapped-by, this will return the existing column mapping.
 * @param mmd The MetaData of the field/property with the list for the column to map to
 * @return The Mapping for the order column
 */
private JavaTypeMapping addOrderColumn(AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
    Class indexType = Integer.class;
    JavaTypeMapping orderIndexMapping = new OrderIndexMapping();
    orderIndexMapping.initialize(storeMgr, indexType.getName());
    orderIndexMapping.setMemberMetaData(mmd);
    orderIndexMapping.setTable(this);
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier indexColumnName = null;
    ColumnMetaData colmd = null;
    // Allow for any user definition in OrderMetaData
    OrderMetaData omd = mmd.getOrderMetaData();
    if (omd != null) {
        colmd = (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 ? omd.getColumnMetaData()[0] : null);
        if (omd.getMappedBy() != null) {
            // User has defined ordering using the column(s) of an existing field.
            // Not adding anything so just set table back to "initialised"
            state = TABLE_STATE_INITIALIZED;
            JavaTypeMapping orderMapping = getMemberMapping(omd.getMappedBy());
            if (!(orderMapping instanceof IntegerMapping) && !(orderMapping instanceof LongMapping)) {
                throw new NucleusUserException(Localiser.msg("057022", mmd.getFullFieldName(), omd.getMappedBy()));
            }
            return orderMapping;
        }
        String colName = null;
        if (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 && omd.getColumnMetaData()[0].getName() != null) {
            // User-defined name so create an identifier using it
            colName = omd.getColumnMetaData()[0].getName();
            indexColumnName = idFactory.newColumnIdentifier(colName);
        }
    }
    if (indexColumnName == null) {
        // No name defined so generate one
        indexColumnName = idFactory.newForeignKeyFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(indexType), FieldRole.ROLE_INDEX);
    }
    Column column = addColumn(indexType.getName(), indexColumnName, orderIndexMapping, colmd);
    if (colmd == null || (colmd.getAllowsNull() == null) || (colmd.getAllowsNull() != null && colmd.isAllowsNull())) {
        // User either wants it nullable, or havent specified anything, so make it nullable
        column.setNullable(true);
    }
    storeMgr.getMappingManager().createDatastoreMapping(orderIndexMapping, column, indexType.getName());
    return orderIndexMapping;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) MacroString(org.datanucleus.util.MacroString) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) LongMapping(org.datanucleus.store.rdbms.mapping.java.LongMapping) OrderMetaData(org.datanucleus.metadata.OrderMetaData) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) OrderIndexMapping(org.datanucleus.store.rdbms.mapping.java.OrderIndexMapping) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 2 with LongMapping

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

the class OptionalOrElseMethod 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 (args == null || args.size() != 1) {
        throw new NucleusException("Optional.orElse should be passed 1 argument");
    }
    SQLExpression elseExpr = args.get(0);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    OptionalMapping opMapping = (OptionalMapping) ((OptionalExpression) expr).getJavaTypeMapping();
    JavaTypeMapping javaMapping = opMapping.getWrappedMapping();
    SQLExpression getExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping);
    SQLExpression isNotNullExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping).ne(new NullLiteral(stmt, javaMapping, null, null));
    if (javaMapping instanceof StringMapping) {
        return new CaseStringExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof IntegerMapping || javaMapping instanceof LongMapping || javaMapping instanceof ShortMapping || javaMapping instanceof FloatMapping || javaMapping instanceof DoubleMapping || javaMapping instanceof BigIntegerMapping || javaMapping instanceof BigDecimalMapping) // TODO Maybe use javaMapping.getJavaType compared to Number to avoid the check above
    {
        return new CaseNumericExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof BooleanMapping) {
        return new CaseBooleanExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    }
    return new CaseExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) BigDecimalMapping(org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) CaseNumericExpression(org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) BooleanMapping(org.datanucleus.store.rdbms.mapping.java.BooleanMapping) DoubleMapping(org.datanucleus.store.rdbms.mapping.java.DoubleMapping) OptionalMapping(org.datanucleus.store.rdbms.mapping.java.OptionalMapping) ShortMapping(org.datanucleus.store.rdbms.mapping.java.ShortMapping) CaseExpression(org.datanucleus.store.rdbms.sql.expression.CaseExpression) CaseStringExpression(org.datanucleus.store.rdbms.sql.expression.CaseStringExpression) LongMapping(org.datanucleus.store.rdbms.mapping.java.LongMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) FloatMapping(org.datanucleus.store.rdbms.mapping.java.FloatMapping) CaseBooleanExpression(org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression) NucleusException(org.datanucleus.exceptions.NucleusException) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Aggregations

IntegerMapping (org.datanucleus.store.rdbms.mapping.java.IntegerMapping)2 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)2 LongMapping (org.datanucleus.store.rdbms.mapping.java.LongMapping)2 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 OrderMetaData (org.datanucleus.metadata.OrderMetaData)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 BigDecimalMapping (org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping)1 BigIntegerMapping (org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping)1 BooleanMapping (org.datanucleus.store.rdbms.mapping.java.BooleanMapping)1 DoubleMapping (org.datanucleus.store.rdbms.mapping.java.DoubleMapping)1 FloatMapping (org.datanucleus.store.rdbms.mapping.java.FloatMapping)1 OptionalMapping (org.datanucleus.store.rdbms.mapping.java.OptionalMapping)1 OrderIndexMapping (org.datanucleus.store.rdbms.mapping.java.OrderIndexMapping)1 ShortMapping (org.datanucleus.store.rdbms.mapping.java.ShortMapping)1 StringMapping (org.datanucleus.store.rdbms.mapping.java.StringMapping)1 CaseBooleanExpression (org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression)1 CaseExpression (org.datanucleus.store.rdbms.sql.expression.CaseExpression)1