Search in sources :

Example 16 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class DB2Adapter method initialiseTypes.

/**
 * Initialise the types for this datastore.
 * @param handler SchemaHandler that we initialise the types for
 * @param mconn Managed connection to use
 */
public void initialiseTypes(StoreSchemaHandler handler, ManagedConnection mconn) {
    super.initialiseTypes(handler, mconn);
    // Add on any missing JDBC types
    SQLTypeInfo sqlType = new org.datanucleus.store.rdbms.adapter.DB2TypeInfo("FLOAT", (short) Types.FLOAT, 53, null, null, null, 1, false, (short) 2, false, false, false, null, (short) 0, (short) 0, 0);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.FLOAT, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.DB2TypeInfo("NUMERIC", (short) Types.NUMERIC, 31, null, null, "PRECISION,SCALE", 1, false, (short) 2, false, false, false, null, (short) 0, (short) 31, 0);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.NUMERIC, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.DB2TypeInfo("BIGINT", (short) Types.BIGINT, 20, null, null, null, 1, false, (short) 2, false, true, false, null, (short) 0, (short) 0, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.BIGINT, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.DB2TypeInfo("XML", (short) Types.SQLXML, 2147483647, null, null, null, 1, false, (short) 2, false, false, false, null, (short) 0, (short) 0, 0);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.SQLXML, sqlType, true);
    // DB2 doesn't have "BIT" JDBC type mapped, so map as SMALLINT
    sqlType = new org.datanucleus.store.rdbms.adapter.DB2TypeInfo("SMALLINT", (short) Types.SMALLINT, 5, null, null, null, 1, false, (short) 2, false, true, false, null, (short) 0, (short) 0, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.BIT, sqlType, true);
}
Also used : SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Example 17 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class CharColumnMapping 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)

Example 18 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class ClassTable method manageUnmappedColumns.

/**
 * Adds on management of the columns in the defined MetaData that are "unmapped" (have no field associated).
 * @param theCmd ClassMetaData for the class to be managed
 * @param clr The ClassLoaderResolver
 */
private void manageUnmappedColumns(AbstractClassMetaData theCmd, ClassLoaderResolver clr) {
    List cols = theCmd.getUnmappedColumns();
    if (cols != null && cols.size() > 0) {
        Iterator colsIter = cols.iterator();
        while (colsIter.hasNext()) {
            ColumnMetaData colmd = (ColumnMetaData) colsIter.next();
            // Create a column with the specified name and jdbc-type
            if (colmd.getJdbcType() == JdbcType.VARCHAR && colmd.getLength() == null) {
                colmd.setLength(storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_DEFAULT_LENGTH));
            }
            IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
            DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, colmd.getName());
            Column col = addColumn(null, colIdentifier, null, colmd);
            SQLTypeInfo sqlTypeInfo = storeMgr.getSQLTypeInfoForJDBCType(dba.getJDBCTypeForName(colmd.getJdbcTypeName()));
            col.setTypeInfo(sqlTypeInfo);
            if (unmappedColumns == null) {
                unmappedColumns = new HashSet();
            }
            if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
                NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057011", col.toString(), colmd.getJdbcType()));
            }
            unmappedColumns.add(col);
        }
    }
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo) HashSet(java.util.HashSet)

Aggregations

SQLTypeInfo (org.datanucleus.store.rdbms.schema.SQLTypeInfo)18 JDBCTypeInfo (org.datanucleus.store.rdbms.schema.JDBCTypeInfo)3 RDBMSTypesInfo (org.datanucleus.store.rdbms.schema.RDBMSTypesInfo)3 Iterator (java.util.Iterator)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 SingleFieldMapping (org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping)2 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)1 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)1 UnsupportedDataTypeException (org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)1 RDBMSSchemaInfo (org.datanucleus.store.rdbms.schema.RDBMSSchemaInfo)1 RDBMSTableInfo (org.datanucleus.store.rdbms.schema.RDBMSTableInfo)1