/** Creates new TableElementImpl */
  public TableElementImpl(String table) {
    super(table);
    columns = new DBElementsCollection(this, new ColumnElement[0]);
    // workaround for bug #4396371
    // http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
    Object hc = String.valueOf(columns.hashCode());
    while (DBElementsCollection.instances.contains(hc)) {
      columns = new DBElementsCollection(this, new ColumnElement[0]);
      hc = String.valueOf(columns.hashCode());
    }
    DBElementsCollection.instances.add(hc);

    indexes = new DBElementsCollection(this, new IndexElement[0]);
    // workaround for bug #4396371
    // http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
    hc = String.valueOf(indexes.hashCode());
    while (DBElementsCollection.instances.contains(hc)) {
      indexes = new DBElementsCollection(this, new IndexElement[0]);
      hc = String.valueOf(indexes.hashCode());
    }
    DBElementsCollection.instances.add(hc);

    keys = new DBElementsCollection(this, new KeyElement[0]);
    // workaround for bug #4396371
    // http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
    hc = String.valueOf(keys.hashCode());
    while (DBElementsCollection.instances.contains(hc)) {
      keys = new DBElementsCollection(this, new KeyElement[0]);
      hc = String.valueOf(keys.hashCode());
    }
    DBElementsCollection.instances.add(hc);

    columnPairs = new DBElementsCollection(this, new ColumnPairElement[0]);
    // workaround for bug #4396371
    // http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
    hc = String.valueOf(columnPairs.hashCode());
    while (DBElementsCollection.instances.contains(hc)) {
      columnPairs = new DBElementsCollection(this, new ColumnPairElement[0]);
      hc = String.valueOf(columnPairs.hashCode());
    }
    DBElementsCollection.instances.add(hc);

    this.table = table;
  }
  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;
  }
 public void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException {
   columnPairs.changeElements(pairs, action);
 }
  /**
   * @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();
    }
  }
 public ColumnPairElement[] getColumnPairs() {
   DBElement[] dbe = columnPairs.getElements();
   return (ColumnPairElement[]) Arrays.asList(dbe).toArray(new ColumnPairElement[dbe.length]);
 }
 /**
  * Get all keys.
  *
  * @return the keys
  */
 public KeyElement[] getKeys() {
   DBElement[] dbe = keys.getElements();
   return (KeyElement[]) Arrays.asList(dbe).toArray(new KeyElement[dbe.length]);
 }
 /**
  * Find a key by name.
  *
  * @param name the name for which to look
  * @return the key, or <code>null</code> if it does not exist
  */
 public KeyElement getKey(DBIdentifier name) {
   return (KeyElement) keys.find(name);
 }
  protected void initIndexes(ConnectionProvider cp, String tbl) {
    if (cp != null)
      try {
        boolean unique;
        DatabaseMetaData dmd = cp.getDatabaseMetaData();
        String shortTableName;
        if (tbl == null) shortTableName = getName().getName();
        else shortTableName = tbl;

        ResultSet rs;
        //                    rs = dmd.getIndexInfo(cp.getConnection().getCatalog(),
        // dmd.getUserName().trim(), shortTableName, false, true);
        rs =
            dmd.getIndexInfo(
                cp.getConnection().getCatalog(), cp.getSchema(), shortTableName, false, true);

        String name, columnName;
        boolean unq;
        LinkedList idxs = new LinkedList();
        if (rs != null) {
          HashMap rset = new HashMap();
          String uniqueStr;
          while (rs.next()) {
            name = rs.getString("INDEX_NAME"); // NOI18N
            columnName = rs.getString("COLUMN_NAME"); // NOI18N
            if (columnName != null) columnName = columnName.trim();
            unq = rs.getBoolean("NON_UNIQUE"); // NOI18N
            // hack for PostgreSQL bug 3480: the driver returns quotes around quoted column names
            if (columnName != null
                && columnName.length() >= 2
                && columnName.startsWith("\"")
                && columnName.endsWith("\"")) { // NOI18N
              columnName = columnName.substring(1, columnName.length() - 1);
            }

            if (name == null) continue;
            else name = name.trim();

            if (unq) idxs.add(name + "." + columnName + ".false"); // NOI18N
            else idxs.add(name + "." + columnName + ".true"); // NOI18N
          }
          rs.close();
        }

        String info;
        int start, end;
        for (int i = 0; i < idxs.size(); i++) {
          info = idxs.get(i).toString();
          start = info.indexOf('.'); // NOI18N
          end = info.lastIndexOf('.'); // NOI18N
          name = info.substring(0, start);
          if ((info.substring(end + 1)).equals("true")) // NOI18N
          unique = true;
          else unique = false;

          if (indexes.find(DBIdentifier.create(name)) != null) continue;

          IndexElementImpl iei = new IndexElementImpl(this, name, unique);
          IndexElement[] ie = {new IndexElement(iei, (TableElement) element)};
          iei.initColumns(idxs);
          changeIndexes(ie, DBElement.Impl.ADD);
        }
      } catch (Exception exc) {
        if (Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
        exc.printStackTrace();
      }
  }
 /**
  * Change the set of keys.
  *
  * @param elems the keys to change
  * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
  * @exception DBException if the action cannot be handled
  */
 public void changeKeys(KeyElement[] elems, int action) throws DBException {
   keys.changeElements(elems, action);
 }
 /**
  * Find an index by name.
  *
  * @param name the name for which to look
  * @return the index, or <code>null</code> if it does not exist
  */
 public IndexElement getIndex(DBIdentifier name) {
   return (IndexElement) indexes.find(name);
 }
 /**
  * Get all indexes.
  *
  * @return the indexes
  */
 public IndexElement[] getIndexes() {
   DBElement[] dbe = indexes.getElements();
   return (IndexElement[]) Arrays.asList(dbe).toArray(new IndexElement[dbe.length]);
 }
 /**
  * Change the set of indexes.
  *
  * @param elems the indexes to change
  * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
  * @exception DBException if the action cannot be handled
  */
 public void changeIndexes(IndexElement[] elems, int action) throws DBException {
   indexes.changeElements(elems, action);
 }
 /**
  * Find a column by name.
  *
  * @param name the name for which to look
  * @return the column, or <code>null</code> if it does not exist
  */
 public ColumnElement getColumn(DBIdentifier name) {
   return (ColumnElement) columns.find(name);
 }
 /**
  * Change the set of columns.
  *
  * @param elems the columns to change
  * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
  * @exception DBException if the action cannot be handled
  */
 public void changeColumns(ColumnElement[] elems, int action) throws DBException {
   columns.changeElements(elems, action);
 }