Package org.jpox.store.rdbms

Examples of org.jpox.store.rdbms.Column


            // FK specified via <column>
            for (int i=0;i<colmds.length;i++)
            {
                // Find the column and add to the source columns for the FK
                DatastoreIdentifier colId = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(colmds[i].getName());
                Column sourceCol = (Column)columnsByName.get(colId);
                if (sourceCol == null)
                {
                    JPOXLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058107",
                        acmd.getFullClassName(), fkmd.getTable(), colmds[i].getName(), toString()));
                    return null;
                }
                sourceCols.add(sourceCol);
            }
        }
        else if (fmds != null && fmds.length > 0)
        {
            // FK specified via <field>
            for (int i=0;i<fmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "foreign-key" field
                // and add all columns to the source columns for the FK
                AbstractMemberMetaData realFmd = getFieldMetaData(fmds[i].getName());
                JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(realFmd);
                int countDatastoreFields = fieldMapping.getNumberOfDatastoreFields();
                for (int j=0; j<countDatastoreFields; j++)
                {
                    // Add each column of this field to the FK definition
                    sourceCols.add(fieldMapping.getDataStoreMapping(i).getDatastoreField());
                }
            }
        }

        if (sourceCols.size() != targetCols.size())
        {
            // Different number of cols in this table and target table
            JPOXLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058108",
                acmd.getFullClassName(), fkmd.getTable(), "" + sourceCols.size(), "" + targetCols.size()));
        }

        // Add all column mappings to the ForeignKey
        if (sourceCols.size() > 0)
        {
            for (int i=0;i<sourceCols.size();i++)
            {
                Column source = (Column)sourceCols.get(i);
                String targetColName = colmds[i].getTarget();
                Column target = (Column)targetCols.get(i); // Default to matching via the natural order
                if (targetColName != null)
                {
                    // User has specified the target column for this col so try it in our target list
                    for (int j=0;j<targetCols.size();j++)
                    {
                        Column targetCol = (Column)targetCols.get(j);
                        if (targetCol.getIdentifier().getIdentifier().equalsIgnoreCase(targetColName))
                        {
                            // Found the required column
                            target = targetCol;
                            break;
                        }
View Full Code Here


        if (colmds != null && colmds.length > 0)
        {
            for (int i=0;i<colmds.length;i++)
            {
                DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(colmds[i].getName());
                Column col = (Column)columnsByName.get(colName);
                if (col == null)
                {
                    JPOXLogger.DATASTORE.warn(LOCALISER.msg("058202",
                        toString(), ck.getName(), colmds[i].getName()));
                    break;
View Full Code Here

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the key field
                                int countKeyFields = keyMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countKeyFields; i++)
                                {
                                    Column col = (Column) keyMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        JPOXLogger.DATASTORE.warn(LOCALISER.msg("057041",
                                            ownerfmd.getName()));
                                    }
                                }

                                if (candidateKeysByMapField.put(mfmd, ck) != null)
                                {
                                    // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                                    // cause problems. For example if they have the key field defined here for 2 different relations
                                    // so you may get keys/values appearing in the other relation that shouldn't be.
                                    // Logged as a WARNING for now.
                                    // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                                    JPOXLogger.DATASTORE.warn(LOCALISER.msg("057012",
                                        mfmd.getFullFieldName(), ownerfmd.getFullFieldName()));
                                }
                            }
                        }
                    }
                    else if (ownerfmd.getValueMetaData() != null && ownerfmd.getValueMetaData().getMappedBy() != null)
                    {
                        // Value field is stored in the key table
                        AbstractMemberMetaData vmd = null;
                        String value_field_name = ownerfmd.getValueMetaData().getMappedBy();
                        if (value_field_name != null)
                        {
                            vmd = cmd.getMetaDataForMember(value_field_name);
                        }
                        if (vmd == null)
                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057008", mfmd));
                        }

                        JavaTypeMapping ownerMapping = getFieldMapping(map_field_name);
                        JavaTypeMapping valueMapping = getFieldMapping(vmd.getName());

                        if (dba.supportsNullsInCandidateKeys() || (!ownerMapping.isNullable() && !valueMapping.isNullable()))
                        {
                            // If the owner and value fields are represented in this table then we can impose
                            // a unique constraint on them. If the value field is in a superclass then we
                            // cannot do this so just omit it.
                            if (valueMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the value field
                                int countValueFields = valueMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countValueFields; i++)
                                {
                                    Column col = (Column) valueMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
View Full Code Here

        if (unmappedColumns != null)
        {
            Iterator iter = unmappedColumns.iterator();
            while (iter.hasNext())
            {
                Column col = (Column)iter.next();
                consumer.consumeUnmappedDatastoreField(col);
            }
        }
    }
View Full Code Here

            if (fmd.hasExtension("allow-nulls") && fmd.getValueForExtension("allow-nulls").equalsIgnoreCase("true"))
            {
                // Make all element col(s) nullable so we can store null elements
                for (int i=0;i<elementMapping.getNumberOfDatastoreFields();i++)
                {
                    Column elementCol = (Column)elementMapping.getDataStoreMapping(i).getDatastoreField();
                    elementCol.setNullable();
                }
            }
            if (JPOXLogger.DATASTORE.isDebugEnabled())
            {
                debugMapping(elementMapping);
            }
        }

        // Add order mapping if required
        boolean orderRequired = false;
        if (fmd.getOrderMetaData() != null)
        {
            if (fmd.getOrderMetaData().isIndexedList())
            {
                // Indexed Collection with <order>, so add index mapping
                orderRequired = true;
            }
        }
        else if (List.class.isAssignableFrom(fmd.getType()))
        {
            // Indexed List with no <order>, so has index mapping
            orderRequired = true;
        }
        else if (requiresPrimaryKey() && !pkColsSpecified)
        {
            // PK is required so maybe need to add an index to form the PK
            if (isEmbeddedElementPC())
            {
                if (fmd.getCollection().getElementClassMetaData().getIdentityType() != IdentityType.APPLICATION)
                {
                    // Embedded PC with datastore id so we need an index to form the PK
                    orderRequired = true;
                }
            }
            else if (isSerialisedElement())
            {
                // Serialised element, so need an index to form the PK
                orderRequired = true;
            }
            else if (elementMapping instanceof ReferenceMapping)
            {
                // ReferenceMapping, so have order if more than 1 implementation
                ReferenceMapping refMapping = (ReferenceMapping)elementMapping;
                if (refMapping.getJavaTypeMapping().length > 1)
                {
                    orderRequired = true;
                }
            }
            else if (!(elementMapping instanceof PersistenceCapableMapping))
            {
                // Non-PC, so depends if the element column can be used as part of a PK
                // TODO This assumes the elementMapping has a single column but what if it is Color with 4 cols?
                Column elementCol = (Column)elementMapping.getDataStoreMapping(0).getDatastoreField();
                if (!((RDBMSAdapter)storeMgr.getDatastoreAdapter()).isValidPrimaryKeyType(elementCol.getJdbcType()))
                {
                    // Not possible to use this Non-PC type as part of the PK
                    orderRequired = true;
                }
            }
View Full Code Here

        Iterator i = storeMgr.getColumnInfoForTable(this, conn).iterator();
        while (i.hasNext())
        {
            ColumnInfo ci = (ColumnInfo)i.next();
            DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.COLUMN, ci.getColumnName());
            Column col = (Column)unvalidated.get(colName);
            if (col == null)
            {
                if (!hasColumnName(colName))
                {
                    throw new UnexpectedColumnException(this.toString(),colName.getIdentifier(), this.getSchemaName(), this.getCatalogName());
                }
                /*
                 * Otherwise it's a duplicate column name in the
                 * metadata and we ignore it.  Cloudscape is known to
                 * do this, although I think that's probably a bug.
                 */
            }
            else
            {
                if (validateColumnStructure)
                {
                    col.validate(ci);
                    unvalidated.remove(colName);
                }
                else
                {
                    unvalidated.remove(colName);
View Full Code Here

            }
            else if (!(keyMapping instanceof PersistenceCapableMapping))
            {
                // Non-PC, so depends if the key column can be used as part of a PK
                // TODO This assumes the keyMapping has a single column but what if it is Color with 4 cols?
                Column elementCol = (Column)keyMapping.getDataStoreMapping(0).getDatastoreField();
                if (!((RDBMSAdapter)storeMgr.getDatastoreAdapter()).isValidPrimaryKeyType(elementCol.getJdbcType()))
                {
                    // Not possible to use this Non-PC type as part of the PK
                    orderRequired = true;
                }
            }
View Full Code Here

        assertIsUninitialized();

        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        MappingManager mgr = getStoreManager().getMappingManager();
        classMapping = dba.getMapping(String.class, storeMgr);
        Column class_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("CLASS_NAME"), classMapping, null);
        mgr.createDatastoreMapping(classMapping, storeMgr, class_column, String.class.getName());
        class_column.getColumnMetaData().setLength(128);
        class_column.getColumnMetaData().setJdbcType("VARCHAR");
        class_column.setAsPrimaryKey();

        tableMapping = dba.getMapping(String.class, storeMgr);
        Column table_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("TABLE_NAME"), tableMapping, null);
        mgr.createDatastoreMapping(tableMapping, storeMgr, table_column, String.class.getName());
        table_column.getColumnMetaData().setLength(128);
        table_column.getColumnMetaData().setJdbcType("VARCHAR");

        typeMapping = dba.getMapping(String.class, storeMgr);
        Column type_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("TYPE"), typeMapping, null);
        mgr.createDatastoreMapping(typeMapping, storeMgr, type_column, String.class.getName());
        type_column.getColumnMetaData().setLength(4);
        type_column.getColumnMetaData().setJdbcType("VARCHAR");

        // TODO Change type to SMALLINT/BIT
        ownerMapping = dba.getMapping(String.class, storeMgr);
        Column owner_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("OWNER"), ownerMapping, null);
        mgr.createDatastoreMapping(ownerMapping, storeMgr, owner_column, String.class.getName());
        owner_column.getColumnMetaData().setLength(2);
        owner_column.getColumnMetaData().setJdbcType("VARCHAR");

        versionMapping = dba.getMapping(String.class, storeMgr);
        Column version_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("VERSION"), versionMapping, null);
        mgr.createDatastoreMapping(versionMapping, storeMgr, version_column, String.class.getName());
        version_column.getColumnMetaData().setLength(20);
        version_column.getColumnMetaData().setJdbcType("VARCHAR");

        interfaceNameMapping = dba.getMapping(String.class, storeMgr);
        Column interfaceName_column=(Column) addDatastoreField(String.class.getName(),
            idFactory.newDatastoreFieldIdentifier("INTERFACE_NAME"), interfaceNameMapping, null);
        mgr.createDatastoreMapping(interfaceNameMapping, storeMgr, interfaceName_column, String.class.getName());
        interfaceName_column.getColumnMetaData().setLength(255);
        interfaceName_column.getColumnMetaData().setJdbcType("VARCHAR");
        interfaceName_column.setNullable();
       
        // Set up JDBC statements for supported operations
        insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," +
            owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + ") VALUES (?,?,?,?,?,?)";
        deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + "=?";
        deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
        fetchAllStmt = "SELECT " + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," +
            owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + table_column.getIdentifier();
        fetchStmt = "SELECT 1 FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + " = ? ";

        state = TABLE_STATE_INITIALIZED;
    }
View Full Code Here

            JavaTypeMapping idMapping = table.getIDMapping();
            if (idMapping != null)
            {
                for (int i=0;i<idMapping.getNumberOfDatastoreFields();i++)
                {
                    Column col = (Column)idMapping.getDataStoreMapping(i).getDatastoreField();
                    if (col.isAutoIncrement())
                    {
                        columnName = col.getIdentifier().toString();
                        break;
                    }
                }
            }
            String autoIncStmt =
View Full Code Here

                    // create the expressions index (columns index)
                    int parametersIndex[] = new int[m.getNumberOfDatastoreFields()];
                    for (int j = 0; j < parametersIndex.length; j++)
                    {
                        // check if the column was not already assigned
                        Column c = (Column)m.getDataStoreMapping(j).getDatastoreField();
                        DatastoreIdentifier columnId = c.getIdentifier();
                        if (!assignedColumns.containsKey(columnId.toString()))
                        {
                            // Either we are a field in a secondary table.
                            // Or we are a subclass table.
                            // Or we are not datastore attributed.
                            if (table instanceof SecondaryTable ||
                                (!table.getStoreManager().isStrategyDatastoreAttributed(fmd.getValueStrategy(), false)
                                    && !c.isAutoIncrement()) ||
                                !table.isBaseDatastoreClass())
                            {
                                if (columnNames.length() > 0)
                                {
                                    columnNames.append(',');
                                    columnValues.append(',');
                                }
                                columnNames.append(columnId);
                                columnValues.append(((RDBMSMapping)m.getDataStoreMapping(j)).getInsertionInputParameter());

                                if (((RDBMSMapping)m.getDataStoreMapping(j)).insertValuesOnInsert())
                                {
                                    // only add fields to be replaced by the real values only if the param value has ?
                                    Integer abs_field_num = new Integer(fmd.getAbsoluteFieldNumber());
                                    if (fmd.isPrimaryKey())
                                    {
                                        if (!pkFields.contains(abs_field_num))
                                        {
                                            pkFields.add(abs_field_num);
                                        }
                                    }
                                    else
                                    {
                                        if (!insertFields.contains(abs_field_num))
                                        {
                                            insertFields.add(abs_field_num);
                                        }
                                    }
                                    parametersIndex[j] = paramIndex++;
                                }
                                assignedColumns.put(c.getIdentifier().toString(),new Integer(fmd.getAbsoluteFieldNumber()));
                            }
                            else
                            {
                                hasAutoIncrementColumn = true;
                            }
                        }
                        else
                        {
                            parametersIndex[j] = ((Integer)assignedColumns.get(c.getIdentifier().toString())).intValue();
                        }
                        statementExpressionIndex[fmd.getAbsoluteFieldNumber()].setParameterIndex(parametersIndex);
                    }
                }
            }
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.