public UniqueIndex getIndex(UniqueKey uniqueKey) { for (UniqueIndex idx : map.values()) { if (idx.table.equalsIgnoreCase(uniqueKey.getTable().getName())) { List<String> cols = uniqueKey.getColumns().stream().map(x -> x.getName().toUpperCase()) .collect(Collectors.toList()); if (idx.columns.containsAll(cols) && cols.containsAll(idx.columns)) { return idx; } } } return null; }
@Override public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) { // First check that this unique key actually is present, as this is a // lot faster than trying to drop it and then fail. initIndices(); UniqueIndex idx = indices.getIndex(uniqueKey); if (idx == null) { return null; } // Remove from cache indices.removeIndex(uniqueKey); final StringBuilder buf = new StringBuilder("DROP INDEX "); buf.append(dialect.quote(uniqueKey.getName())); return buf.toString(); }
@SuppressWarnings("unchecked") private boolean hasUniqueIndex(Index index, Table table) { HashSet<Column> indexCols = new HashSet<Column>(); Iterator<Column> icolIter = index.getColumnIterator(); while( icolIter.hasNext() ) { Column col = icolIter.next(); indexCols.add(col); if( index.getColumnSpan() == 1 && table.getColumn(col).isUnique() ) { return true; } } Iterator<UniqueKey> iter = table.getUniqueKeyIterator(); while( iter.hasNext() ) { UniqueKey uk = iter.next(); if( uk.getColumnSpan() == indexCols.size() && indexCols.containsAll(uk.getColumns()) ) { return true; } } return false; }
@Override public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) { ConfigurationService config = metadata.getDatabase().getBuildingOptions().getServiceRegistry() .getService(ConfigurationService.class); if (config != null) { String value = config.getSetting("hibernate.hbm2ddl.auto", StandardConverters.STRING); if (!value.equalsIgnoreCase("update")) { // We should only check whether it is already present in an // update scenario, in all other scenarios, just return the // actual create statement. return org.hibernate.mapping.Index.buildSqlCreateIndexString(dialect, uniqueKey.getName(), uniqueKey.getTable(), uniqueKey.columnIterator(), uniqueKey.getColumnOrderMap(), true, metadata); } } // First check that this unique key is not already present, as this is a // lot faster than trying to create it and then fail. initIndices(); UniqueIndex idx = indices.getIndex(uniqueKey); if (idx != null) { return null; } return org.hibernate.mapping.Index.buildSqlCreateIndexString(dialect, uniqueKey.getName(), uniqueKey.getTable(), uniqueKey.columnIterator(), uniqueKey.getColumnOrderMap(), true, metadata); }
@SuppressWarnings("rawtypes") private void normalizeUniqueKeys(Table table, String entityName) { Iterator iterator = table.getUniqueKeyIterator(); while (iterator.hasNext()) { UniqueKey uk = (UniqueKey) iterator.next(); String name = strategy.uniqueKeyName(entityName, table.getName()); uk.setName(name); } }
@SuppressWarnings("rawtypes") private void validateUniqueKeys(Table table) { Iterator iterator = table.getUniqueKeyIterator(); while (iterator.hasNext()) { UniqueKey uk = (UniqueKey) iterator.next(); validateMaxLength(uk.getName()); } }
public void removeIndex(UniqueKey uniqueKey) { String key = uniqueKey.getTable().getName() + "." + uniqueKey.getName(); map.remove(key); }
@SuppressWarnings({"unchecked"}) public List<String> getAddIndexesAndConstraintsForColumns(String tableName, boolean includeForeignKeyConstraints, String... columnNames) { Set<Column> colSet = new HashSet<Column>(); for( String columnName : columnNames ) { colSet.add(new Column(columnName)); } List<String> sqlStrings = new ArrayList<String>(); Table table = findTable(tableName); if( !extDialect.supportsModifyWithConstraints() ) { for( Column col : colSet ) { Column realCol = table.getColumn(col); if( realCol.isUnique() ) { table.createUniqueKey(Collections.singletonList(realCol)); } } } Iterator<UniqueKey> keyIter = table.getUniqueKeyIterator(); if( dialect.supportsUniqueConstraintInCreateAlterTable() ) { while( keyIter.hasNext() ) { UniqueKey uk = keyIter.next(); if( !Collections.disjoint(uk.getColumns(), colSet) ) { StringBuilder buf = new StringBuilder("alter table "); buf.append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)); buf.append(" add constraint "); buf.append(extDialect.getRandomIdentifier()); buf.append(' '); String constraint = uk.sqlConstraintString(dialect); if( constraint != null ) { buf.append(constraint); sqlStrings.add(buf.toString()); } } } } else { while( keyIter.hasNext() ) { UniqueKey ukey = keyIter.next(); if( !Collections.disjoint(ukey.getColumns(), colSet) ) { sqlStrings.add(ukey.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } } } addIndexSQL(sqlStrings, table, colSet); // Caller may opt to skip foreign key constraints if( includeForeignKeyConstraints ) { Iterator<ForeignKey> fkeyIter = table.getForeignKeyIterator(); while( fkeyIter.hasNext() ) { ForeignKey fkey = fkeyIter.next(); if( !Collections.disjoint(fkey.getColumns(), colSet) ) { sqlStrings.add(fkey.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } } } return sqlStrings; }
@Override public boolean includeUniqueKey(Table table, UniqueKey uk) { return includeTable(table); }
@Override public boolean includeUniqueKey(Table table, UniqueKey uk) { return true; }
boolean includeUniqueKey(Table table, UniqueKey uk);