Search in sources :

Example 21 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createColumn.

/**
 * Method to create a datastore field for a persistable mapping.
 * @param mmd MetaData for the field whose mapping it is
 * @param table Datastore class where we create the datastore field
 * @param mapping The Java type for this field
 * @param colmd The columnMetaData for this datastore field
 * @param reference The datastore field we are referencing
 * @param clr ClassLoader resolver
 * @return The column
 */
public Column createColumn(AbstractMemberMetaData mmd, Table table, JavaTypeMapping mapping, ColumnMetaData colmd, Column reference, ClassLoaderResolver clr) {
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier identifier = null;
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
        identifier = idFactory.newForeignKeyFieldIdentifier(relatedMmds != null ? relatedMmds[0] : null, mmd, reference.getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), FieldRole.ROLE_OWNER);
        colmd.setName(identifier.getName());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmd.getName(), false, null, true);
    }
    Column col = table.addColumn(mmd.getType().getName(), identifier, mapping, colmd);
    // Copy the characteristics of the reference column to this one
    reference.copyConfigurationTo(col);
    if (mmd.isPrimaryKey()) {
        col.setPrimaryKey();
    }
    if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
    // Embedded so can't be datastore-attributed
    } else {
        if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber())) {
            if ((mmd.isPrimaryKey() && ((DatastoreClass) table).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
                // Increment any PK field if we are in base class, and increment any other field
                col.setIdentity(true);
            }
        }
    }
    if (mmd.getValueForExtension("select-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension("insert-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension("update-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
    }
    setColumnNullability(mmd, colmd, col);
    if (mmd.getNullValue() == NullValue.DEFAULT) {
        // Users default should be applied if a null is to be inserted
        col.setDefaultable(colmd.getDefaultValue());
    }
    return col;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 22 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createColumn.

/**
 * Method to create a column for a Java type mapping.
 * This is NOT used for persistable mappings - see method below.
 * @param mapping Java type mapping for the field
 * @param javaType The type of field being stored in this column
 * @param datastoreFieldIndex Index of the datastore field to use
 * @return The datastore field
 */
public Column createColumn(JavaTypeMapping mapping, String javaType, int datastoreFieldIndex) {
    AbstractMemberMetaData mmd = mapping.getMemberMetaData();
    FieldRole roleForField = mapping.getRoleForMember();
    Table tbl = mapping.getTable();
    // Take the column MetaData from the component that this mappings role relates to
    ColumnMetaData colmd = null;
    ColumnMetaDataContainer columnContainer = mmd;
    if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT || roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
        columnContainer = mmd.getElementMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
        columnContainer = mmd.getKeyMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
        columnContainer = mmd.getValueMetaData();
    }
    Column col;
    ColumnMetaData[] colmds;
    if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex) {
        colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
        colmds = columnContainer.getColumnMetaData();
    } else {
        // If column specified add one (use any column name specified on field element)
        colmd = new ColumnMetaData();
        if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > datastoreFieldIndex) {
            colmd.setName(mmd.getColumnMetaData()[datastoreFieldIndex].getName());
        }
        if (columnContainer != null) {
            columnContainer.addColumn(colmd);
            colmds = columnContainer.getColumnMetaData();
        } else {
            colmds = new ColumnMetaData[1];
            colmds[0] = colmd;
        }
    }
    // Generate the column identifier
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier identifier = null;
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT) {
            // Join table collection element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
            // Join table array element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
            // Join table map key
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_KEY);
        } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
            // Join table map value
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
        } else {
            identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
            int i = 0;
            while (tbl.hasColumn(identifier)) {
                identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
                i++;
            }
        }
        colmd.setName(identifier.getName());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmds[datastoreFieldIndex].getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
    }
    // Create the column
    col = tbl.addColumn(javaType, identifier, mapping, colmd);
    if (mmd.isPrimaryKey()) {
        col.setPrimaryKey();
    }
    if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
    // Embedded so can't be datastore-attributed
    } else {
        /*if (!mmd.getClassName(true).equals(mmd.getAbstractClassMetaData().getFullClassName()))
            {
                if (storeMgr.isStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass)
                {
                    if ((mmd.isPrimaryKey() && ((DatastoreClass)tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey())
                    {
                        NucleusLogger.GENERAL.info(">> Column addition " + mmd.getFullFieldName() + " IGNORING use of IDENTITY since override of base metadata! See RDBMSMappingManager");
                    }
                }
                // Overriding member, so ignore TODO This can be incorrect in many cases
            }
            else
            {*/
        if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass) {
            if ((mmd.isPrimaryKey() && ((DatastoreClass) tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
                // Increment any PK field if we are in base class, and increment any other field
                col.setIdentity(true);
            }
        }
    /*}*/
    }
    if (mmd.getValueForExtension("select-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension("insert-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension("update-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
    }
    setColumnNullability(mmd, colmd, col);
    if (mmd.getNullValue() == NullValue.DEFAULT) {
        // Users default should be applied if a null is to be inserted
        col.setDefaultable(colmd.getDefaultValue());
    }
    return col;
}
Also used : Table(org.datanucleus.store.rdbms.table.Table) ColumnMetaDataContainer(org.datanucleus.metadata.ColumnMetaDataContainer) Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) FieldRole(org.datanucleus.metadata.FieldRole) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 23 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class DatastoreMappingFactory method createMapping.

/**
 * Get a new instance of the DatastoreMapping using the mapping, StoreManager and column.
 * @param mappingClass the Mapping class to be created
 * @param mapping The java mapping type
 * @param storeMgr The Store Manager
 * @param column The column to map
 * @return The DatastoreMapping
 */
public static DatastoreMapping createMapping(Class mappingClass, JavaTypeMapping mapping, RDBMSStoreManager storeMgr, Column column) {
    Object obj = null;
    try {
        Object[] args = new Object[] { mapping, storeMgr, column };
        Constructor ctr = (Constructor) DATASTORE_MAPPING_CONSTRUCTOR_BY_CLASS.get(mappingClass);
        if (ctr == null) {
            ctr = mappingClass.getConstructor(DATASTORE_MAPPING_CTR_ARG_CLASSES);
            DATASTORE_MAPPING_CONSTRUCTOR_BY_CLASS.put(mappingClass, ctr);
        }
        try {
            obj = ctr.newInstance(args);
        } catch (InvocationTargetException e) {
            throw new NucleusException(Localiser.msg("041009", mappingClass.getName(), e.getTargetException()), e.getTargetException()).setFatal();
        } catch (Exception e) {
            throw new NucleusException(Localiser.msg("041009", mappingClass.getName(), e), e).setFatal();
        }
    } catch (NoSuchMethodException nsme) {
        throw new NucleusException(Localiser.msg("041007", JavaTypeMapping.class, RDBMSStoreManager.class, Column.class, mappingClass.getName())).setFatal();
    }
    return (DatastoreMapping) obj;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Column(org.datanucleus.store.rdbms.table.Column) Constructor(java.lang.reflect.Constructor) NucleusException(org.datanucleus.exceptions.NucleusException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NucleusException(org.datanucleus.exceptions.NucleusException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 24 with Column

use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.

the class Key method getColumnList.

/**
 * Method to return the list of columns which the key applies to.
 * @param cols The columns.
 * @return The column list.
 */
public static String getColumnList(List<Column> cols) {
    StringBuilder s = new StringBuilder("(");
    Iterator<Column> i = cols.iterator();
    while (i.hasNext()) {
        Column col = i.next();
        if (col == null) {
            s.append('?');
        } else {
            s.append(col.getIdentifier());
        }
        if (i.hasNext()) {
            s.append(',');
        }
    }
    s.append(')');
    return s.toString();
}
Also used : Column(org.datanucleus.store.rdbms.table.Column)

Aggregations

Column (org.datanucleus.store.rdbms.table.Column)24 MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)5 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)3 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)3 DatastoreMapping (org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ForeignKey (org.datanucleus.store.rdbms.key.ForeignKey)2 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)2 Table (org.datanucleus.store.rdbms.table.Table)2 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1