/** * Returns a clone of the given index column. * * @param source The source index column * @param targetTable The table containing the column to be used by the clone * @param caseSensitive Whether comparison is case sensitive (for finding the columns * in the target table) * @return The clone */ public IndexColumn clone(IndexColumn source, Table targetTable, boolean caseSensitive) { IndexColumn result = new IndexColumn(); result.setColumn(targetTable.findColumn(source.getName(), caseSensitive)); result.setOrdinalPosition(source.getOrdinalPosition()); result.setSize(source.getSize()); return result; }
IndexColumn indexColumn = new IndexColumn(); indexColumn.setName((String)values.get("COLUMN_NAME")); if (values.containsKey("ORDINAL_POSITION")) indexColumn.setOrdinalPosition(((Short)values.get("ORDINAL_POSITION")).intValue());
/** * {@inheritDoc} */ public boolean hasColumn(Column column) { for (int idx = 0; idx < _columns.size(); idx++) { IndexColumn curColumn = getColumn(idx); if (column.equals(curColumn.getColumn())) { return true; } } return false; }
/** * Creates a new change object. * * @param tableName The name of the changed table * @param index The index; note that this change object will not maintain a reference * to the index object */ public IndexChangeImplBase(String tableName, Index index) { super(tableName); for (int colIdx = 0; colIdx < index.getColumnCount(); colIdx++) { _columnNames.add(index.getColumn(colIdx).getName()); } }
/** * Reads an index column element from the XML stream reader. * * @param xmlReader The reader * @return The index column object */ private IndexColumn readIndexColumnElement(XMLStreamReader xmlReader) throws XMLStreamException, IOException { IndexColumn indexColumn = new IndexColumn(); for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) { QName attrQName = xmlReader.getAttributeName(idx); if (isSameAs(attrQName, QNAME_ATTRIBUTE_NAME)) { indexColumn.setName(xmlReader.getAttributeValue(idx)); } else if (isSameAs(attrQName, QNAME_ATTRIBUTE_SIZE)) { indexColumn.setSize(getAttributeValueBeingNullAware(xmlReader, idx)); } } consumeRestOfElement(xmlReader); return indexColumn; }
/** * {@inheritDoc} */ public void apply(Database model, boolean caseSensitive) { Table table = findChangedTable(model, caseSensitive); table.addIndex(_newIndex); for (int idx = 0; idx < _newIndex.getColumnCount(); idx++) { IndexColumn idxColumn = _newIndex.getColumn(idx); Column tmpColumn = idxColumn.getColumn(); idxColumn.setColumn(table.findColumn(tmpColumn.getName(), caseSensitive)); } } }
/** * {@inheritDoc} */ public boolean equalsIgnoreCase(Index other) { if (other instanceof UniqueIndex) { UniqueIndex otherIndex = (UniqueIndex)other; boolean checkName = (_name != null) && (_name.length() > 0) && (otherIndex._name != null) && (otherIndex._name.length() > 0); if ((!checkName || _name.equalsIgnoreCase(otherIndex._name)) && (getColumnCount() == otherIndex.getColumnCount())) { for (int idx = 0; idx < getColumnCount(); idx++) { if (!getColumn(idx).equalsIgnoreCase(otherIndex.getColumn(idx))) { return false; } } return true; } } return false; }
/** * {@inheritDoc} */ public void addColumn(IndexColumn column) { if (column != null) { for (int idx = 0; idx < _columns.size(); idx++) { IndexColumn curColumn = getColumn(idx); if (curColumn.getOrdinalPosition() > column.getOrdinalPosition()) { _columns.add(idx, column); return; } } _columns.add(column); } }
/** * {@inheritDoc} */ public boolean hasColumn(String columnName, boolean caseSensitive) { for (int idx = 0; idx < _columns.size(); idx++) { IndexColumn curColumn = getColumn(idx); if (StringUtilsExt.equals(columnName, curColumn.getName(), caseSensitive)) { return true; } } return false; }
/** * Writes the index column object to the given XML writer. * * @param indexColumn The index column object * @param isUnique Whether the index that the index column belongs to, is unique * @param xmlWriter The XML writer */ private void writeIndexColumnElement(IndexColumn indexColumn, boolean isUnique, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException { xmlWriter.indentIfPrettyPrinting(3); writeElementStart(xmlWriter, isUnique ? QNAME_ELEMENT_UNIQUE_COLUMN : QNAME_ELEMENT_INDEX_COLUMN); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, indexColumn.getName()); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_SIZE, indexColumn.getSize()); writeElementEnd(xmlWriter); }
/** * {@inheritDoc} */ public boolean equalsIgnoreCase(Index other) { if (other instanceof NonUniqueIndex) { NonUniqueIndex otherIndex = (NonUniqueIndex)other; boolean checkName = (_name != null) && (_name.length() > 0) && (otherIndex._name != null) && (otherIndex._name.length() > 0); if ((!checkName || _name.equalsIgnoreCase(otherIndex._name)) && (getColumnCount() == otherIndex.getColumnCount())) { for (int idx = 0; idx < getColumnCount(); idx++) { if (!getColumn(idx).equalsIgnoreCase(otherIndex.getColumn(idx))) { return false; } } return true; } } return false; }
private static void generateOneIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> values, List<Index> indexes) throws SQLException { if (StringUtils.isBlank((String) values.get("INDEX_NAME"))) { //sqlserver数据库取到的元数据中有INDEX_NAME为空的情况,此处做一下过滤 return; } Optional<Index> optional = indexes.stream().filter(i -> i.getName().equals((String) values.get("INDEX_NAME"))).findFirst(); IndexColumn column = new IndexColumn(); column.setName((String) values.get("COLUMN_NAME")); column.setOrdinalPosition(Integer.valueOf((String) values.get("ORDINAL_POSITION"))); if (optional.isPresent()) { Index index = optional.get(); index.addColumn(column); } else { String nonUnique = (String) values.get("NON_UNIQUE"); if ("false".equals(nonUnique)) { UniqueIndex uniqueIndex = new UniqueIndex(); uniqueIndex.setName((String) values.get("INDEX_NAME")); uniqueIndex.addColumn(column); indexes.add(uniqueIndex); } else { NonUniqueIndex nonUniqueIndex = new NonUniqueIndex(); nonUniqueIndex.setName((String) values.get("INDEX_NAME")); nonUniqueIndex.addColumn(column); indexes.add(nonUniqueIndex); } } }
/** * Checks whether the given index matches the column list. * * @param index The index * @param columnsToSearchFor The names of the columns that the index should be for * @return <code>true</code> if the index matches the columns */ protected boolean matches(Index index, List columnsToSearchFor) { if (index.getColumnCount() != columnsToSearchFor.size()) { return false; } for (int columnIdx = 0; columnIdx < index.getColumnCount(); columnIdx++) { if (!columnsToSearchFor.get(columnIdx).equals(index.getColumn(columnIdx).getName())) { return false; } } return true; }
/** * {@inheritDoc} */ protected List checkForRemovedIndexes(Database sourceModel, Table sourceTable, Database intermediateModel, Table intermediateTable, Database targetModel, Table targetTable) { // Handling for http://bugs.mysql.com/bug.php?id=21395: we need to drop and then recreate FKs that reference columns // included in indexes that will be dropped List changes = super.checkForRemovedIndexes(sourceModel, sourceTable, intermediateModel, intermediateTable, targetModel, targetTable); Set columnNames = new HashSet(); for (Iterator it = changes.iterator(); it.hasNext();) { RemoveIndexChange change = (RemoveIndexChange)it.next(); Index index = change.findChangedIndex(sourceModel, isCaseSensitive()); for (int colIdx = 0; colIdx < index.getColumnCount(); colIdx++) { columnNames.add(index.getColumn(colIdx).getName()); } } if (!columnNames.isEmpty()) { // this is only relevant if the columns are referenced by foreign keys in the same table changes.addAll(getForeignKeyRecreationChanges(intermediateTable, targetTable, columnNames)); } return changes; }
Optional<MediaColumnMappingInfo> optional = mappingList .stream() .filter(m -> m.getSourceColumn().equalsIgnoreCase(column.getName())) .findFirst(); throw new ValidationException( String.format("Column [%s] is part of the unique index [%s] in table [%s],can not be ignored in the data sync.", column.getName(), uniqueIndex.getName(), table.getName())
String curColName = curIndex.getColumn(colIdx).getName(); String expectedColName = (String)_columnNames.get(colIdx);