Search in sources :

Example 1 with SingleFieldMapping

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

the class SmallIntRDBMSMapping method initialize.

/**
 * Initialise the mapping.
 */
private void initialize() {
    if (column != null) {
        column.checkPrimitive();
        // Valid Values
        JavaTypeMapping m = getJavaTypeMapping();
        if (m instanceof SingleFieldMapping) {
            Object[] validValues = ((SingleFieldMapping) m).getValidValues(0);
            if (validValues != null) {
                column.setCheckConstraints(storeMgr.getDatastoreAdapter().getCheckConstraintForValues(column.getIdentifier(), validValues, column.isNullable()));
            }
        }
        if (getJavaTypeMapping().getJavaType() == Boolean.class) {
            // With a Boolean we'll store it as 1, 0 (see setBoolean/getBoolean methods)
            StringBuilder constraints = new StringBuilder("CHECK (" + column.getIdentifier() + " IN (0,1)");
            if (column.isNullable()) {
                constraints.append(" OR " + column.getIdentifier() + " IS NULL");
            }
            constraints.append(')');
            column.setCheckConstraints(constraints.toString());
        }
    }
    initTypeInfo();
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SingleFieldMapping(org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping)

Example 2 with SingleFieldMapping

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

the class CharRDBMSMapping method initialize.

/**
 * Method to initialise the column mapping. Provides default length specifications for the CHAR column to
 * fit the data being stored.
 */
protected void initialize() {
    if (column != null) {
        // Default Length
        if (getJavaTypeMapping() instanceof SingleFieldMapping && column.getColumnMetaData().getLength() == null) {
            SingleFieldMapping m = (SingleFieldMapping) getJavaTypeMapping();
            if (m.getDefaultLength(0) > 0) {
                // No column length provided by user and the type has a default length so use it
                column.getColumnMetaData().setLength(m.getDefaultLength(0));
            }
        }
        column.getColumnMetaData().setJdbcType("CHAR");
        column.checkString();
        // Valid Values
        if (getJavaTypeMapping() instanceof SingleFieldMapping) {
            Object[] validValues = ((SingleFieldMapping) getJavaTypeMapping()).getValidValues(0);
            if (validValues != null) {
                column.setCheckConstraints(getDatastoreAdapter().getCheckConstraintForValues(column.getIdentifier(), validValues, column.isNullable()));
            }
        }
        if (getJavaTypeMapping().getJavaType() == Boolean.class) {
            // With a Boolean we'll store it as "Y", "N" (see setBoolean/getBoolean methods)
            column.getColumnMetaData().setLength(1);
            StringBuilder constraints = new StringBuilder("CHECK (" + column.getIdentifier() + " IN ('Y','N')");
            if (column.isNullable()) {
                constraints.append(" OR " + column.getIdentifier() + " IS NULL");
            }
            constraints.append(')');
            column.setCheckConstraints(constraints.toString());
        }
        // Check on max length of the type against the length we have set
        SQLTypeInfo typeInfo = getTypeInfo();
        int maxlength = typeInfo.getPrecision();
        if (column.getColumnMetaData().getLength().intValue() <= 0 || column.getColumnMetaData().getLength().intValue() > maxlength) {
            if (typeInfo.isAllowsPrecisionSpec()) {
                throw new NucleusUserException("String max length of " + column.getColumnMetaData().getLength() + " is outside the acceptable range [0, " + maxlength + "] for column \"" + column.getIdentifier() + "\"");
            }
        }
    }
    initTypeInfo();
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SingleFieldMapping(org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping) SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Aggregations

SingleFieldMapping (org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 SQLTypeInfo (org.datanucleus.store.rdbms.schema.SQLTypeInfo)1