Package org.jpox.store.rdbms

Examples of org.jpox.store.rdbms.Column


    {
        StringBuffer s = new StringBuffer("(");
        Iterator i = cols.iterator();
        while (i.hasNext())
        {
            Column col = (Column)i.next();

            if (col == null)
            {
                s.append('?');
            }
            else
            {
                s.append(col.getIdentifier());
            }

            if (i.hasNext())
            {
                s.append(',');
View Full Code Here


     * @return The datastore mapping
     */
    public DatastoreMapping createDatastoreMapping(JavaTypeMapping mapping, MappedStoreManager storeMgr, DatastoreField column, String javaType)
    {
        ClassLoaderResolver clr = storeMgr.getOMFContext().getClassLoaderResolver(null);
        Column col = (Column)column;
        String jdbcType = null;
        String sqlType = null;
        if (col != null && col.getColumnMetaData() != null)
        {
            // Utilise the jdbc and sql types if specified
            jdbcType = col.getColumnMetaData().getJdbcType();
            sqlType = col.getColumnMetaData().getSqlType();
        }
        Class datastoreMappingClass = getDatastoreMappingClass(null, javaType, jdbcType, sqlType, clr);

        DatastoreMapping datastoreMapping = DatastoreMappingFactory.createMapping(datastoreMappingClass, mapping, storeMgr, column);
        if (column != null)
View Full Code Here

        else if (roleForField == JavaTypeMapping.MAPPING_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(fmd, fmd.getColumn());
            if (columnContainer != null)
            {
                columnContainer.addColumn(colmd);
                colmds = columnContainer.getColumnMetaData();
            }
            else
            {
                colmds = new ColumnMetaData[1];
                colmds[0] = colmd;
            }
        }

        // Generate the column identifier
        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier identifier = null;
        if (colmd.getName() == null)
        {
            // No name specified, so generate the identifier from the field name
            if (roleForField == JavaTypeMapping.MAPPING_FIELD)
            {
                identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName());
                int i=0;
                while (datastoreContainer.hasDatastoreField(identifier))
                {
                    identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName() + "_" + i);
                    i++;
                }
            }
            else if (roleForField == JavaTypeMapping.MAPPING_COLLECTION_ELEMENT)
            {
                // Join table collection element
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_ARRAY_ELEMENT)
            {
                // Join table array element
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_MAP_KEY)
            {
                // Join table map key
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_MAP_KEY);
            }
            else if (roleForField == JavaTypeMapping.MAPPING_MAP_VALUE)
            {
                // Join table map value
                identifier = ((RDBMSIdentifierFactory)idFactory).newJoinTableFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
            }

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

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

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

        if (datastoreContainer.getStoreManager().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.setAutoIncrement(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

    public DatastoreField createDatastoreField(JavaTypeMapping mapping, String javaType, ColumnMetaData colmd)
    {
        AbstractMemberMetaData fmd = mapping.getFieldMetaData();
        DatastoreContainerObject datastoreContainer = mapping.getDatastoreContainer();

        Column col;
        if (colmd == null)
        {
            // If column specified add one (use any column name specified on field element)
            colmd = new ColumnMetaData(fmd, fmd.getColumn());
            fmd.addColumn(colmd);
        }

        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        if (colmd.getName() == null)
        {
            // No name specified, so generate the identifier from the field name
            DatastoreIdentifier identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName());
            int i=0;
            while (datastoreContainer.hasDatastoreField(identifier))
            {
                identifier = idFactory.newIdentifier(IdentifierFactory.COLUMN, fmd.getName() + "_" + i);
                i++;
            }

            colmd.setName(identifier.getIdentifier());
            col = (Column) datastoreContainer.addDatastoreField(javaType, identifier, mapping, colmd);
        }
        else
        {
            // User has specified a name, so try to keep this unmodified
            col = (Column) datastoreContainer.addDatastoreField(javaType,
                idFactory.newDatastoreFieldIdentifier(colmd.getName(),
                    datastoreContainer.getStoreManager().getOMFContext().getTypeManager().isDefaultEmbeddedType(fmd.getType()),
                    FieldRole.ROLE_CUSTOM),
                mapping, colmd);
        }

        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

        else
        {
            // User has specified a name, so try to keep this unmodified
            identifier = idFactory.newDatastoreFieldIdentifier(colmd.getName(), false, FieldRole.ROLE_CUSTOM);
        }
        Column col = (Column)datastoreContainer.addDatastoreField(fmd.getType().getName(), identifier, mapping, colmd);

        // Copy the characteristics of the reference column to this one
        reference.copyConfigurationTo(col);

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

        if (datastoreContainer.getStoreManager().isStrategyDatastoreAttributed(fmd.getValueStrategy(), false))
        {
            if ((fmd.isPrimaryKey() && ((DatastoreClass)datastoreContainer).isBaseDatastoreClass()) || !fmd.isPrimaryKey())
            {
                // Increment any PK field if we are in base class, and increment any other field
                col.setAutoIncrement(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

                    this.storeMgr.getOMFContext().getTypeManager().isDefaultEmbeddedType(OID.class), FieldRole.ROLE_NONE).getIdentifier());
            }
        }

        // Add the datastore identity column as the PK
        Column idColumn = (Column) addDatastoreField(OID.class.getName(),
            storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.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))
        {
            strategyName = cmd.getIdentityMetaData().getValueStrategy().getCustomName();
        }

        // Check the POID type being stored
        Class poidClass = Long.class;
        ConfigurationElement elem =
            storeMgr.getOMFContext().getPluginManager().getConfigurationElementForExtension(
                "org.jpox.store_valuegenerator",
                new String[]{"name", "unique"}, new String[] {strategyName, "true"});
        if (elem == null)
        {
            // Not datastore-independent, so try for this datastore
            elem = storeMgr.getOMFContext().getPluginManager().getConfigurationElementForExtension(
                    "org.jpox.store_valuegenerator",
                    new String[]{"name", "datastore"}, new String[] {strategyName, storeMgr.getStoreManagerKey()});
        }
        if (elem != null)
        {
            // Set the generator name (for use by the PoidManager)
            String generatorClassName = elem.getAttribute("class-name");
            Class generatorClass =
                getStoreManager().getOMFContext().getClassLoaderResolver(null).classForName(generatorClassName);
            try
            {
                Method storageClassMethod = generatorClass.getMethod("getStorageClass", null);
                poidClass = (Class) storageClassMethod.invoke(null, null);
            }
            catch (Exception e)
            {
                // Unable to get the storage class from the PoidGenerator class
                JPOXLogger.POID.warn("Error retrieving storage class for POID generator " + generatorClassName +
                    " " + e.getMessage());
            }
        }

        dba.getMappingManager().createDatastoreMapping(datastoreIDMapping, storeMgr, idColumn,
            poidClass.getName());

        // Handle any auto-increment requirement
        if (isObjectIDDatastoreAttributed())
        {
            if (this instanceof DatastoreClass && ((DatastoreClass)this).isBaseDatastoreClass())
            {
                // Only the base class can be autoincremented
                idColumn.setAutoIncrement(true);
            }
        }

        // Check if auto-increment and that it is supported by this RDBMS
        if (idColumn.isAutoIncrement() && !dba.supportsIdentityFields())
        {
            throw new JPOXException(LOCALISER.msg("057020",
                cmd.getFullClassName(), "datastore-identity")).setFatal();
        }
    }
View Full Code Here

        {
            duplicateName = true;
        }

        // Create the column
        Column col = new Column(this, storedJavaType, name, (ColumnMetaData) colmd);

        if (duplicateName && colmd != null)
        {
            // Verify if a duplicate column is valid. A duplicate column name is (currently) valid when :-
            // 1. subclasses defining the duplicated column are using "super class table" strategy
            //
            // Find the MetaData for the existing column
            Column existingCol = (Column) columnsByName.get(name);
            MetaData md = existingCol.getColumnMetaData().getParent();
            while (!(md instanceof AbstractClassMetaData))
            {
                if (md == null)
                {
                    // ColumnMetaData for existing column has no parent class somehow!
                    throw new JPOXUserException(LOCALISER.msg("057043",
                        name.getIdentifier(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
                }
                md = md.getParent();
            }

            // Find the MetaData for the column to be added
            MetaData dupMd = colmd.getParent();
            while (!(dupMd instanceof AbstractClassMetaData))
            {
                dupMd = dupMd.getParent();
                if (dupMd == null)
                {
                    // ColumnMetaData for required column has no parent class somehow!
                    throw new JPOXUserException(LOCALISER.msg("057044",
                        name.getIdentifier(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
                }
            }
            if (((AbstractClassMetaData)md).getFullClassName().equals(((AbstractClassMetaData)dupMd).getFullClassName()))
            {
                // compare the current column defining class and the duplicated column defining class. if the same class,
                // we raise an exception when within one class it is defined a column twice
                // in some cases it could still be possible to have these duplicated columns, but does not make too
                // much sense in most of the cases. (this whole block of duplicated column check, could be optional, like a pmf property)
                throw new DuplicateColumnNameException(this.toString(), existingCol, col);
            }

            // Make sure the field JavaTypeMappings are compatible
            if (mapping != null &&
                !mapping.getClass().isAssignableFrom(existingCol.getMapping().getClass()) &&
                !existingCol.getMapping().getClass().isAssignableFrom(mapping.getClass()))
            {
                // the mapping class must be the same (not really required, but to avoid user mistakes)
                throw new DuplicateColumnNameException(this.toString(), existingCol, col);
            }
View Full Code Here

        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();

        // "SEQUENCE_NAME" column
        sequenceNameMapping = dba.getMapping(String.class, storeMgr);
        Column colSequenceName=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier(sequenceNameColumnName), sequenceNameMapping, null);
        colSequenceName.setAsPrimaryKey();
        colSequenceName.getColumnMetaData().setLength(Integer.valueOf("255"));
        colSequenceName.getColumnMetaData().setJdbcType("VARCHAR");
    getStoreManager().getMappingManager().createDatastoreMapping(sequenceNameMapping, storeMgr, colSequenceName,
            String.class.getName());

        // "NEXT_VAL" column
        nextValMapping = dba.getMapping(Long.class, storeMgr);
        DatastoreField colNextVal=addDatastoreField(Long.class.getName(),
            idFactory.newDatastoreFieldIdentifier(nextValColumnName), nextValMapping, null);
    getStoreManager().getMappingManager().createDatastoreMapping(nextValMapping, storeMgr, colNextVal, Long.class.getName());

        // Set up JDBC statements for supported operations
        insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + colSequenceName.getIdentifier() + "," + colNextVal.getIdentifier() +
            ") VALUES (?,?)";
        incrementByStmt = "UPDATE " + identifier.getFullyQualifiedName(false) + " SET " + colNextVal.getIdentifier() + "=(" +
            colNextVal.getIdentifier() + "+?) WHERE " + colSequenceName.getIdentifier() + "=?";
        deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + colSequenceName.getIdentifier() + "=?";
        deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
        fetchStmt = "SELECT " + colNextVal.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " WHERE " +
            colSequenceName.getIdentifier() + "=?";
        if (dba.supportsLockWithSelectForUpdate())
        {
            fetchStmt += " FOR UPDATE";
        }
        fetchAllStmt = "SELECT " + colNextVal.getIdentifier() + "," + colSequenceName.getIdentifier() +
            " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + colSequenceName.getIdentifier();

        state = TABLE_STATE_INITIALIZED;
    }
View Full Code Here

    {
        PrimaryKey pk = new PrimaryKey(this);
        Iterator i = columns.iterator();
        while (i.hasNext())
        {
            Column col = (Column) i.next();
            if (col.isPrimaryKey())
            {
                pk.addDatastoreField(col);
            }
        }
View Full Code Here

            ColumnInfo ci = (ColumnInfo) i.next();

            // Create an identifier to use for the real column - use "CUSTOM" because we dont want truncation
            DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(ci.getColumnName(),
                this.storeMgr.getOMFContext().getTypeManager().isDefaultEmbeddedType(String.class), FieldRole.ROLE_CUSTOM);
            Column col = (Column) unvalidated.get(colName);
            if (col != null)
            {
                if (validateColumnStructure)
                {
                    col.initializeColumnInfoFromDatastore(ci);
                    col.validate(ci);
                    unvalidated.remove(colName);
                }
                else
                {
                    unvalidated.remove(colName);
                }
            }
        }

        if (unvalidated.size() > 0)
        {
            if (autoCreate)
            {
                // Add all missing columns
                List stmts = new ArrayList();
                Set columnKeys = unvalidated.keySet();
                Iterator columnKeysIter = columnKeys.iterator();
                while (columnKeysIter.hasNext())
                {
                    DatastoreIdentifier colName = (DatastoreIdentifier)columnKeysIter.next();

                    Column col = (Column)(unvalidated.get(colName));
                    String addColStmt = dba.getAddColumnStatement(this, col);
                    stmts.add(addColStmt);
                    JPOXLogger.DATASTORE_SCHEMA.info(LOCALISER.msg("057031", col.getIdentifier(), this.toString()));
                }

                try
                {
                    executeDdlStatementList(stmts, conn);
View Full Code Here

TOP

Related Classes of org.jpox.store.rdbms.Column

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.