Package org.apache.openjpa.jdbc.schema

Examples of org.apache.openjpa.jdbc.schema.ForeignKey


            else
                tcols[i] = getEquivalentColumn(cols[i].getIdentifier(), target,
                    false);
        }

        ForeignKey newfk = table.addForeignKey();
        newfk.setJoins(cols, tcols);
        if (_fk != null) {
            cols = _fk.getConstantColumns();
            for (int i = 0; i < cols.length; i++)
                newfk.joinConstant(cols[i], _fk.getConstant(cols[i]));

            cols = _fk.getConstantPrimaryKeyColumns();
            for (int i = 0; i < cols.length; i++)
                newfk.joinConstant(_fk.getPrimaryKeyConstant(cols[i]),
                    getEquivalentColumn(cols[i].getIdentifier(), target, true));
        }
        return newfk;
    }
View Full Code Here


     * Select data for loading, starting in field table.
     */
    protected Joins selectAll(Select sel, ClassMapping elem,
        OpenJPAStateManager sm, JDBCStore store, JDBCFetchConfiguration fetch,
        int eagerMode) {
        ForeignKey fk = getJoinForeignKey(elem);
        Object oid = getObjectIdForJoin(fk, sm);
        sel.whereForeignKey(fk, oid, field.getDefiningMapping(), store);

        // order first, then select so that if the projection introduces
        // additional ordering, it will be after our required ordering
View Full Code Here

            // Don't create a join if the field's table is the same as the
            // class's table.
            table = null;
        }

        ForeignKey join = null;
        if (table != null)
            join = _info.getJoin(this, table, adapt);
        if (join == null && joinRequired)
            throw new MetaDataException(_loc.get("join-required", this));
View Full Code Here

    public boolean isBidirectionalJoinTableMappingOwner() {
      if (_bidirectionalJoinTableOwner != null)
        return _bidirectionalJoinTableOwner.booleanValue();
     
      _bidirectionalJoinTableOwner = false;
        ForeignKey fk = getForeignKey();
        if (fk != null)
          return false;
        ForeignKey jfk = getJoinForeignKey();
        if (jfk == null)
          return false;
        FieldMapping mappedBy = getValueMappedByMapping();
        if (mappedBy != null)
          return false;
        ValueMapping elem = getElementMapping();
        if (elem == null)
          return false;
        ClassMapping relType = elem.getDeclaredTypeMapping();
        if (relType == null)
          return false;
        FieldMapping[] relFmds = relType.getFieldMappings();
        for (int i=0; i<relFmds.length;i++) {
            FieldMapping rfm = relFmds[i];
            if (rfm.getDeclaredTypeMetaData() == getDeclaringMapping()) {
            ForeignKey rjfk = rfm.getJoinForeignKey();
            if (rjfk == null)
                continue;
                if (rjfk.getTable() == jfk.getTable() &&
                        jfk.getTable().getColumns().length ==
                        jfk.getColumns().length + rjfk.getColumns().length) {
              _bidirectionalJoinTableOwner = true;
              break;
            }
          }
        }
View Full Code Here

    public boolean isBidirectionalJoinTableMappingNonOwner() {
      if (_bidirectionalJoinTableNonOwner != null)
        return _bidirectionalJoinTableNonOwner.booleanValue();
     
      _bidirectionalJoinTableNonOwner = false;
        ForeignKey fk = getForeignKey();
        if (fk == null)
          return false;
        ForeignKey jfk = getJoinForeignKey();
        if (jfk == null)
          return false;
        FieldMapping mappedBy = getValueMappedByMapping();
        if (mappedBy != null)
          return false;
        ValueMapping elem = getElementMapping();
        if (elem == null)
          return false;
        ClassMapping relType = getDeclaredTypeMapping();
        if (relType == null)
          return false;
        FieldMapping[] relFmds = relType.getFieldMappings();
        for (int i=0; i<relFmds.length;i++) {
            FieldMapping rfm = relFmds[i];
            ValueMapping relem = rfm.getElementMapping();
            if (relem != null && relem.getDeclaredTypeMapping() ==
                    getDeclaringMapping()) {
            ForeignKey rjfk = rfm.getJoinForeignKey();
            if (rjfk == null)
                continue;
            if (rjfk.getTable() == jfk.getTable() &&
                    jfk.getTable().getColumns().length ==
                        jfk.getColumns().length + rjfk.getColumns().length) {
              _bidirectionalJoinTableNonOwner = true;
              break;
            }
          }
        }
View Full Code Here

            default:
                vm = src;
        }
        if (vm.getJoinDirection() != ValueMapping.JOIN_FORWARD)
            return false;
        ForeignKey fk = vm.getForeignKey();
        if (fk == null)
            return false;
       
        // foreign key must join to target columns
        Column[] rels = fk.getColumns();
        Column[] pks = target.getColumns();
        if (rels.length != pks.length)
            return false;
        for (int i = 0; i < rels.length; i++)
            if (fk.getPrimaryKeyColumn(rels[i]) != pks[i])
                return false;
        return true;
    }
View Full Code Here

    /**
     * Use the given join instance to create SQL joining its tables in
     * the traditional style.
     */
    public SQLBuffer toTraditionalJoin(Join join) {
        ForeignKey fk = join.getForeignKey();
        if (fk == null)
            return null;

        boolean inverse = join.isForeignKeyInversed();
        Column[] from = (inverse) ? fk.getPrimaryKeyColumns()
            : fk.getColumns();
        Column[] to = (inverse) ? fk.getColumns()
            : fk.getPrimaryKeyColumns();

        // do column joins
        SQLBuffer buf = new SQLBuffer(this);
        int count = 0;
        for (int i = 0; i < from.length; i++, count++) {
            if (count > 0)
                buf.append(" AND ");
            buf.append(join.getAlias1()).append(".").append(from[i]);
            buf.append(" = ");
            buf.append(join.getAlias2()).append(".").append(to[i]);
        }

        // do constant joins
        Column[] constCols = fk.getConstantColumns();
        for (int i = 0; i < constCols.length; i++, count++) {
            if (count > 0)
                buf.append(" AND ");
            if (inverse)
                buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
            else
                buf.append(join.getAlias1()).append(".").
                    append(constCols[i]);
            buf.append(" = ");

            if (inverse)
                buf.append(join.getAlias2()).append(".").
                    append(constCols[i]);
            else
                buf.appendValue(fk.getConstant(constCols[i]), constCols[i]);
        }

        Column[] constColsPK = fk.getConstantPrimaryKeyColumns();
        for (int i = 0; i < constColsPK.length; i++, count++) {
            if (count > 0)
                buf.append(" AND ");
            if (inverse)
                buf.append(join.getAlias1()).append(".").
                    append(constColsPK[i]);
            else
                buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
                    constColsPK[i]);
            buf.append(" = ");

            if (inverse)
                buf.appendValue(fk.getPrimaryKeyConstant(constColsPK[i]),
                    constColsPK[i]);
            else
                buf.append(join.getAlias2()).append(".").
                    append(constColsPK[i]);
        }
View Full Code Here

            List<ForeignKey> importedKeyList = new ArrayList<ForeignKey>();
            Map<FKMapKey, ForeignKey> fkMap = new HashMap<FKMapKey, ForeignKey>();

            while (keys != null && keys.next()) {
                ForeignKey nfk = newForeignKey(keys);
                if (!partialKeys) {
                    ForeignKey fk = combineForeignKey(fkMap, nfk);
                    // If the key returned != new key, fk col was combined
                    // with existing fk.
                    if (fk != nfk) {
                        continue;
                    }
View Full Code Here

     */
    protected ForeignKey combineForeignKey(Map<FKMapKey, ForeignKey> fkMap,
        ForeignKey fk) {
       
        FKMapKey fkmk = new FKMapKey(fk);
        ForeignKey baseKey = fkMap.get(fkmk);
        // Found the FK, add the additional column
        if (baseKey != null) {
            baseKey.addColumn(fk);
            return baseKey;
        }
        // fkey is new
        fkMap.put(fkmk, fk);
        return fk;
View Full Code Here

    /**
     * Create a new foreign key from the information in the schema metadata.
     */
    protected ForeignKey newForeignKey(ResultSet fkMeta)
        throws SQLException {
        ForeignKey fk = new ForeignKey();
        fk.setSchemaIdentifier(fromDBName(fkMeta.getString("FKTABLE_SCHEM"), DBIdentifierType.SCHEMA));
        fk.setTableIdentifier(fromDBName(fkMeta.getString("FKTABLE_NAME"), DBIdentifierType.TABLE));
        fk.setColumnIdentifier(fromDBName(fkMeta.getString("FKCOLUMN_NAME"), DBIdentifierType.COLUMN));
        fk.setIdentifier(fromDBName(fkMeta.getString("FK_NAME"), DBIdentifierType.FOREIGN_KEY));
        fk.setPrimaryKeySchemaIdentifier(fromDBName(fkMeta.getString("PKTABLE_SCHEM"), DBIdentifierType.SCHEMA));
        fk.setPrimaryKeyTableIdentifier(fromDBName(fkMeta.getString("PKTABLE_NAME"), DBIdentifierType.TABLE));
        fk.setPrimaryKeyColumnIdentifier(fromDBName(fkMeta.getString("PKCOLUMN_NAME"), DBIdentifierType.COLUMN));
        fk.setKeySequence(fkMeta.getShort("KEY_SEQ"));
        fk.setDeferred(fkMeta.getShort("DEFERRABILITY")
            == DatabaseMetaData.importedKeyInitiallyDeferred);

        int del = fkMeta.getShort("DELETE_RULE");
        switch (del) {
            case DatabaseMetaData.importedKeySetNull:
                fk.setDeleteAction(ForeignKey.ACTION_NULL);
                break;
            case DatabaseMetaData.importedKeySetDefault:
                fk.setDeleteAction(ForeignKey.ACTION_DEFAULT);
                break;
            case DatabaseMetaData.importedKeyCascade:
                fk.setDeleteAction(ForeignKey.ACTION_CASCADE);
                break;
            default:
                fk.setDeleteAction(ForeignKey.ACTION_RESTRICT);
                break;
        }
        return fk;
    }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.jdbc.schema.ForeignKey

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.