Package org.datanucleus.metadata

Examples of org.datanucleus.metadata.ColumnMetaData


        indexMapping.initialize(storeMgr, indexType.getName());
        indexMapping.setMemberMetaData(mmd);
        indexMapping.setDatastoreContainer(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.
                state = TABLE_STATE_INITIALIZED; // Not adding anything so just set table back to "initialised"
                JavaTypeMapping orderMapping = getMemberMapping(omd.getMappedBy());
                if (orderMapping == null)
                {
                    throw new NucleusUserException(LOCALISER.msg("057021",
                        mmd.getFullFieldName(), 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.newDatastoreFieldIdentifier(colName);
            }
        }
        if (indexColumnName == null)
        {
            // No name defined so generate one
            indexColumnName = idFactory.newForeignKeyFieldIdentifier(mmd, null, null,
                storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(indexType), FieldRole.ROLE_INDEX);
        }

        DatastoreField column = addDatastoreField(indexType.getName(), indexColumnName, indexMapping, colmd);
        if (colmd == null || (colmd != null && colmd.getAllowsNull() == null) ||
            (colmd != null && colmd.getAllowsNull() != null && colmd.isAllowsNull()))
        {
            // User either wants it nullable, or havent specified anything, so make it nullable
            column.setNullable();
        }
View Full Code Here


                debugMapping(elementMapping);
            }
        }

        // Add order mapping
        ColumnMetaData colmd = null;
        if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null &&
            mmd.getOrderMetaData().getColumnMetaData().length > 0)
        {
            // Specified "order" column info
            colmd = mmd.getOrderMetaData().getColumnMetaData()[0];
        }
        else
        {
            // No column name so use default
            DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
            colmd = new ColumnMetaData();
            colmd.setName(id.getIdentifierName());
        }
        orderMapping = storeMgr.getMappingManager().getMapping(int.class); // JDO2 spec [18.5] order column is assumed to be "int"
        ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, colmd, pkRequired && !pkColsSpecified);
        if (NucleusLogger.DATASTORE.isDebugEnabled())
        {
View Full Code Here

        setIdentifier(identifier);
        if (colmd == null)
        {
            // Create a default ColumnMetaData since none provided
            columnMetaData = new ColumnMetaData();
        }
        else
        {
            // TODO Consider making a copy here
            columnMetaData = colmd;
View Full Code Here

            }
        }
        if (orderRequired)
        {
            // Order/Adapter (index) column is required (integer based)
            ColumnMetaData orderColmd = null;
            if (mmd.getOrderMetaData() != null &&
                mmd.getOrderMetaData().getColumnMetaData() != null &&
                mmd.getOrderMetaData().getColumnMetaData().length > 0)
            {
                // Specified "order" column info
                orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
                if (orderColmd.getName() == null)
                {
                    orderColmd = new ColumnMetaData(orderColmd);
                    if (mmd.hasExtension("adapter-column-name"))
                    {
                        // Specified "extension" column name
                        // TODO Is this needed? The user can just specify <order column="...">
                        orderColmd.setName(mmd.getValueForExtension("adapter-column-name"));
                    }
                    else
                    {
                        // No column name so use default
                        DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                        orderColmd.setName(id.getIdentifierName());
                    }
                }
            }
            else
            {
                if (mmd.hasExtension("adapter-column-name"))
                {
                    // Specified "extension" column name
                    // TODO Is this needed? The user can just specify <order column="...">
                    orderColmd = new ColumnMetaData();
                    orderColmd.setName(mmd.getValueForExtension("adapter-column-name"));
                }
                else
                {
                    // No column name so use default
                    DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                    orderColmd = new ColumnMetaData();
                    orderColmd.setName(id.getIdentifierName());
                }
            }
            orderMapping = storeMgr.getMappingManager().getMapping(int.class); // JDO2 spec [18.5] order column is assumed to be "int"
            ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
            if (NucleusLogger.DATASTORE.isDebugEnabled())
View Full Code Here

            }
        }
        if (orderRequired)
        {
            // Order (index) column is required (integer based)
            ColumnMetaData orderColmd = null;
            if (mmd.getOrderMetaData() != null &&
                mmd.getOrderMetaData().getColumnMetaData() != null &&
                mmd.getOrderMetaData().getColumnMetaData().length > 0)
            {
                // Specified "order" column info
                orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
                if (orderColmd.getName() == null)
                {
                    orderColmd = new ColumnMetaData(orderColmd);
                    if (mmd.hasExtension("adapter-column-name"))
                    {
                        // Specified "extension" column name
                        // TODO Is this needed? The user can just specify <order column="...">
                        orderColmd.setName(mmd.getValueForExtension("adapter-column-name"));
                    }
                    else
                    {
                        // No column name so use default
                        DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                        orderColmd.setName(id.getIdentifierName());
                    }
                }
            }
            else
            {
                if (mmd.hasExtension("adapter-column-name"))
                {
                    // Specified "extension" column name
                    // TODO Is this needed? The user can just specify <order column="...">
                    orderColmd = new ColumnMetaData();
                    orderColmd.setName(mmd.getValueForExtension("adapter-column-name"));
                }
                else
                {
                    // No column name so use default
                    DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                    orderColmd = new ColumnMetaData();
                    orderColmd.setName(id.getIdentifierName());
                }
            }
            orderMapping = storeMgr.getMappingManager().getMapping(int.class); // JDO2 spec [18.5] order column is assumed to be "int"
            ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
            if (NucleusLogger.DATASTORE.isDebugEnabled())
View Full Code Here

        // Pass 1 : populate positions defined in metadata as vendor extension "index"
        Iterator<Column> iter = columns.iterator();
        while (iter.hasNext())
        {
            Column col = iter.next();
            ColumnMetaData colmd = col.getColumnMetaData();
            Integer colPos = (colmd != null ? colmd.getPosition() : null);
            if (colPos != null)
            {
                int index = colPos.intValue();
                if (index < columns.size() && index >= 0)
                {
                    if (cols == null)
                    {
                        cols = new Column[columns.size()];
                    }
                    if (cols[index] != null)
                    {
                        throw new NucleusUserException("Column index " + index +
                            " has been specified multiple times : " + cols[index] + " and " + col);
                    }
                    cols[index] = col;
                }
            }
        }

        // Pass 2 : fill in spaces for columns with undefined positions
        if (cols != null)
        {
            iter = columns.iterator();
            while (iter.hasNext())
            {
                Column col = iter.next();
                ColumnMetaData colmd = col.getColumnMetaData();
                Integer colPos = (colmd != null ? colmd.getPosition() : null);
                if (colPos == null)
                {
                    // No index set for this column, so assign to next free position
                    for (int i=0;i<cols.length;i++)
                    {
View Full Code Here

                {
                    m = storeMgr.getMappingManager().getMapping(clr.classForName(refColumn.getJavaTypeMapping().getType()));
                    ((PersistableMapping)masterMapping).addJavaTypeMapping(m);
                }

                ColumnMetaData userdefinedColumn = null;
                if (userdefinedCols != null)
                {
                    for (int k=0;k<userdefinedCols.length;k++)
                    {
                        if (refColumn.getIdentifier().toString().equals(userdefinedCols[k].getTarget()))
                        {
                            userdefinedColumn = userdefinedCols[k];
                            break;
                        }
                    }
                    if (userdefinedColumn == null && nextUserdefinedCol < userdefinedCols.length)
                    {
                        userdefinedColumn = userdefinedCols[nextUserdefinedCol++];
                    }
                }

                // Add this application identity column
                DatastoreField idColumn = null;
                if (userdefinedColumn != null)
                {
                    // User has provided a name for this column
                    // Currently we only use the column namings from the users definition but we could easily
                    // take more of their details.
                    idColumn = addDatastoreField(refColumn.getStoredJavaType(),
                        storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, userdefinedColumn.getName()),
                        m, refColumn.getColumnMetaData());
                }
                else
                {
                    // No name provided so take same as superclass
View Full Code Here

        datastoreIDMapping = new OIDMapping();
        datastoreIDMapping.setDatastoreContainer(this);
        datastoreIDMapping.initialize(storeMgr, cmd.getFullClassName());

        // Create a ColumnMetaData in the container if none is defined
        ColumnMetaData colmd = null;
        if (columnContainer == null)
        {
            colmd = new ColumnMetaData();
        }
        else if (columnContainer.getColumnMetaData().length < 1)
        {
            colmd = new ColumnMetaData();
        }
        else
        {
            colmd = columnContainer.getColumnMetaData()[0];
        }
        if (colmd.getName() == null)
        {
            // Provide default column naming if none is defined
            if (refTable != null)
            {
                colmd.setName(storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(refTable.getIdentifier().getIdentifierName(),
                    this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(OID.class), FieldRole.ROLE_OWNER).getIdentifierName());
            }
            else
            {
                colmd.setName(storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(identifier.getIdentifierName(),
                    this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(OID.class), FieldRole.ROLE_NONE).getIdentifierName());
            }
        }

        // Add the datastore identity column as the PK
        Column idColumn = (Column) addDatastoreField(OID.class.getName(),
            storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colmd.getName()), datastoreIDMapping, colmd);
        idColumn.setAsPrimaryKey();

        // Set the identity column type based on the IdentityStrategy
        String strategyName = cmd.getIdentityMetaData().getValueStrategy().toString();
        if (cmd.getIdentityMetaData().getValueStrategy().equals(IdentityStrategy.CUSTOM))
View Full Code Here

            if (colName == null)
            {
                // No column defined so use a fallback name
                colName = "RELATION_DISCRIM";
            }
            ColumnMetaData colmd = new ColumnMetaData();
            colmd.setName(colName);

            boolean relationDiscriminatorPk = false;
            if (mmd.hasExtension("relation-discriminator-pk") &&
                mmd.getValueForExtension("relation-discriminator-pk").equalsIgnoreCase("true"))
            {
                // Default this to not be part of the PK of the join table, but allow the user to override it
                relationDiscriminatorPk = true;
            }
            if (!relationDiscriminatorPk)
            {
                colmd.setAllowsNull(Boolean.TRUE); // Allow for elements not in any discriminated collection (when not PK)
            }

            // Create the mapping and its datastore column (only support String relation discriminators here)
            relationDiscriminatorMapping = storeMgr.getMappingManager().getMapping(String.class);
            ColumnCreator.createIndexColumn(relationDiscriminatorMapping, storeMgr, clr, this, colmd, relationDiscriminatorPk);
View Full Code Here

        AbstractMemberMetaData fmd = mapping.getMemberMetaData();
        int roleForField = mapping.getRoleForMember();
        DatastoreContainerObject datastoreContainer = mapping.getDatastoreContainer();

        // Take the column MetaData from the component that this mappings role relates to
        ColumnMetaData colmd = null;
        ColumnMetaDataContainer columnContainer = fmd;
        if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT ||
            roleForField == FieldRole.ROLE_ARRAY_ELEMENT)
        {
            columnContainer = fmd.getElementMetaData();
        }
        else if (roleForField == FieldRole.ROLE_MAP_KEY)
        {
            columnContainer = fmd.getKeyMetaData();
        }
        else if (roleForField == FieldRole.ROLE_MAP_VALUE)
        {
            columnContainer= fmd.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();
            colmd.setName(fmd.getColumn()); // TODO Avoid use of getColumn() - try getColumnMetaData() but test with spatial too
            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(fmd, null, null,
                    true, FieldRole.ROLE_COLLECTION_ELEMENT);
            }
            else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT)
            {
                // Join table array element
                identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
                    true, FieldRole.ROLE_ARRAY_ELEMENT);
            }
            else if (roleForField == FieldRole.ROLE_MAP_KEY)
            {
                // Join table map key
                identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
                    true, FieldRole.ROLE_MAP_KEY);
            }
            else if (roleForField == FieldRole.ROLE_MAP_VALUE)
            {
                // Join table map value
                identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
                    true, FieldRole.ROLE_MAP_VALUE);
            }
            else
            {
                identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName());
                int i=0;
                while (datastoreContainer.hasDatastoreField(identifier))
                {
                    identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName() + "_" + i);
                    i++;
                }
            }

            colmd.setName(identifier.getIdentifierName());
        }
        else
        {
            // User has specified a name, so try to keep this unmodified
            identifier = idFactory.newDatastoreFieldIdentifier(colmds[datastoreFieldIndex].getName(),
                storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(fmd.getType()),
                FieldRole.ROLE_CUSTOM);
        }

        // Create the column
        col = (Column) datastoreContainer.addDatastoreField(javaType, identifier, mapping, colmd);

        if (fmd.isPrimaryKey())
        {
            col.setAsPrimaryKey();
        }

        if (storeMgr.isStrategyDatastoreAttributed(fmd.getValueStrategy(), false))
        {
            if (datastoreContainer instanceof DatastoreClass)
            {
                if ((fmd.isPrimaryKey() && ((DatastoreClass)datastoreContainer).isBaseDatastoreClass()) ||
                    !fmd.isPrimaryKey())
                {
                    // Increment any PK field if we are in base class, and increment any other field
                    col.setIdentity(true);
                }
            }
        }

        if (fmd.getValueForExtension("select-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("select-function"),Column.WRAPPER_FUNCTION_SELECT);
        }
        if (fmd.getValueForExtension("insert-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("insert-function"),Column.WRAPPER_FUNCTION_INSERT);
        }
        if (fmd.getValueForExtension("update-function") != null)
        {
            col.setWrapperFunction(fmd.getValueForExtension("update-function"),Column.WRAPPER_FUNCTION_UPDATE);
        }

        setDatastoreFieldNullability(fmd, colmd, col);
        if (fmd.getNullValue() == NullValue.DEFAULT)
        {
            // Users default should be applied if a null is to be inserted
            col.setDefaultable();
            if (colmd.getDefaultValue() != null)
            {
                col.setDefaultValue(colmd.getDefaultValue());
            }
        }

        return col;
    }
View Full Code Here

TOP

Related Classes of org.datanucleus.metadata.ColumnMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.