private void initPK(ConnectionProvider cp, String shortTableName) throws SQLException, DBException { ResultSet rs; IndexElement[] iearr = getIndexes(); if (iearr != null) { for (int i = 0; i < iearr.length; i++) if (iearr[i].isUnique()) { UniqueKeyElementImpl ukei = new UniqueKeyElementImpl( iearr[i].getName().getName(), false); // false = not primary key (primary flag is setted later) UniqueKeyElement uke = new UniqueKeyElement(ukei, (TableElement) element, iearr[i]); uke.setColumns(iearr[i].getColumns()); changeKeys(new UniqueKeyElement[] {uke}, DBElement.Impl.ADD); } UniqueKeyElement[] ukes = ((TableElement) element).getUniqueKeys(); rs = cp.getDatabaseMetaData() .getPrimaryKeys(cp.getConnection().getCatalog(), cp.getSchema(), shortTableName); TreeMap cols = new TreeMap(); Object keySeq; String colName; if (rs != null) { HashMap rset = new HashMap(); while (rs.next()) { keySeq = rs.getObject("KEY_SEQ"); // NOI18N colName = rs.getString("COLUMN_NAME").trim(); // NOI18N cols.put(keySeq, colName); // NOI18N } rs.close(); } boolean primary = false; if (cols != null && cols.size() > 0) primary = true; if (primary) { if (ukes == null || ukes.length == 0) { // issue 56492: no index defined for the primary key // generate a UniqueKeyElement and an IndexElement for it String indexName = "primary_key_index"; // NOI18N int i = 1; while (((TableElement) element).getIndex(DBIdentifier.create(indexName)) != null) { indexName = indexName + i; i++; } LinkedList idxs = new LinkedList(); for (Iterator it = cols.values().iterator(); it.hasNext(); ) { // non-unique = false, thus the index is unique -- see initIndexes() idxs.add(indexName + "." + it.next() + ".false"); // NOI18N } IndexElementImpl iei = new IndexElementImpl(this, indexName, true); IndexElement ie = new IndexElement(iei, (TableElement) element); iei.initColumns(idxs); changeIndexes(new IndexElement[] {ie}, DBElement.Impl.ADD); UniqueKeyElementImpl ukei = new UniqueKeyElementImpl(ie.getName().getName(), true); UniqueKeyElement uke = new UniqueKeyElement(ukei, (TableElement) element, ie); uke.setColumns(ie.getColumns()); changeKeys(new UniqueKeyElement[] {uke}, DBElement.Impl.ADD); } else if (ukes.length == 1) ukes[0].setPrimaryKey(primary); else { ColumnElement[] ces; Object[] o = cols.values().toArray(); boolean equals; for (int i = 0; i < ukes.length; i++) { ces = ukes[i].getColumns(); if (ces.length != o.length) continue; else { equals = true; for (int j = 0; j < ces.length; j++) if (!o[j].toString().equals(ces[j].getName().getName())) { equals = false; break; } if (equals) { ukes[i].setPrimaryKey(primary); break; } } } } } } }
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(); } }