コード例 #1
0
  public ColumnPairElement getColumnPair(DBIdentifier name) {
    ColumnPairElement cpe = (ColumnPairElement) columnPairs.find(name);
    if (cpe == null)
      try {
        String fullName = name.getFullName();
        if (fullName == null) {
          return null;
        }

        int pos = fullName.indexOf(";");
        String firstHalf = fullName.substring(0, pos);
        String secondHalf = fullName.substring(pos + 1);

        ColumnElement lce = getColumn(DBIdentifier.create(firstHalf));

        pos = secondHalf.lastIndexOf(".");
        TableElement te =
            ((TableElement) element)
                .getDeclaringSchema()
                .getTable(DBIdentifier.create(secondHalf.substring(0, pos)));
        if (te == null) return null;

        ColumnElement fce = te.getColumn(DBIdentifier.create(secondHalf));

        if (lce == null || fce == null) return null;

        ColumnPairElementImpl cpei =
            new ColumnPairElementImpl(
                lce.getName().getFullName() + ";" + fce.getName().getFullName()); // NOI18N
        cpe = new ColumnPairElement(cpei, lce, fce, (TableElement) element);
        changeColumnPairs(new ColumnPairElement[] {cpe}, DBElement.Impl.ADD);
      } catch (DBException exc) {
        exc.printStackTrace();
        return null;
      }

    return cpe;
  }
コード例 #2
0
  /**
   * @param expectRelatedTables specifies whether all related tables are expected to be provided.
   */
  private void initFKs(ConnectionProvider cp, String shortTableName, boolean expectRelatedTables)
      throws SQLException, DBException {
    ResultSet rs;

    rs =
        cp.getDatabaseMetaData()
            .getImportedKeys(cp.getConnection().getCatalog(), cp.getSchema(), shortTableName);

    String name, fkColName, pkTableName, pkColName, c1, c2, s1, s2;
    if (rs != null) {
      HashMap rset = new HashMap();
      while (rs.next()) {
        // test references between two schemas
        c1 = rs.getString("PKTABLE_CAT"); // NOI18N
        s1 = rs.getString("PKTABLE_SCHEM"); // NOI18N
        c2 = rs.getString("FKTABLE_CAT"); // NOI18N
        s2 = rs.getString("FKTABLE_SCHEM"); // NOI18N

        name = rs.getString("FK_NAME"); // NOI18N
        fkColName = rs.getString("FKCOLUMN_NAME").trim(); // NOI18N
        pkTableName = rs.getString("PKTABLE_NAME").trim(); // NOI18N
        pkColName = rs.getString("PKCOLUMN_NAME").trim(); // NOI18N

        if (comp(c1, c2)) {
          if (!comp(s1, s2)) continue;
        } else continue;

        ColumnPairElement cpe;

        if (name == null || name.trim().equals("")) name = "GENERATED_FK_" + pkTableName;
        else name = name.trim();

        ColumnElement lce = getColumn(DBIdentifier.create(fkColName)); // NOI18N
        if (lce == null) // should be null only in same cases when FK is computed for view
        continue;

        SchemaElement se = ((TableElement) element).getDeclaringSchema();
        TableElement fte = se.getTable(DBIdentifier.create(pkTableName));
        // table could not be found since all related tables were not necessarily provided
        if (fte == null && !expectRelatedTables) {
          continue;
        }
        ColumnElement fce = fte.getColumn(DBIdentifier.create(pkColName));
        ColumnPairElementImpl cpei =
            new ColumnPairElementImpl(
                lce.getName().getFullName() + ";" + fce.getName().getFullName()); // NOI18N
        cpe = new ColumnPairElement(cpei, lce, fce, (TableElement) element);
        changeColumnPairs(new ColumnPairElement[] {cpe}, DBElement.Impl.ADD);

        ForeignKeyElement fk = (ForeignKeyElement) keys.find(DBIdentifier.create(name));
        if (fk != null) fk.addColumnPair(cpe); // add pair
        else {
          ForeignKeyElementImpl fkei = new ForeignKeyElementImpl(this, name);
          ForeignKeyElement fke = new ForeignKeyElement(fkei, (TableElement) element);
          fke.addColumnPair(cpe);
          changeKeys(new ForeignKeyElement[] {fke}, DBElement.Impl.ADD);
        }
      }
      rs.close();
    }
  }