org.zodiac.fastorm.rdb.metadata.RDBIndexMetadata Maven / Gradle / Ivy
The newest version!
package org.zodiac.fastorm.rdb.metadata;
import org.zodiac.fastorm.core.meta.ObjectMetadata;
import org.zodiac.fastorm.core.meta.ObjectType;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
public class RDBIndexMetadata implements ObjectMetadata {
private String name;
private String tableName;
private String alias;
private String comment;
private List columns = new CopyOnWriteArrayList<>();
private boolean unique;
private boolean primaryKey;
public RDBIndexMetadata() {
super();
}
public RDBIndexMetadata(String name) {
this.name = name;
}
public String getName() {
return name;
}
public RDBIndexMetadata setName(String name) {
this.name = name;
return this;
}
public String getTableName() {
return tableName;
}
public RDBIndexMetadata setTableName(String tableName) {
this.tableName = tableName;
return this;
}
public String getAlias() {
return alias;
}
public RDBIndexMetadata setAlias(String alias) {
this.alias = alias;
return this;
}
public String getComment() {
return comment;
}
public RDBIndexMetadata setComment(String comment) {
this.comment = comment;
return this;
}
public List getColumns() {
return columns;
}
public RDBIndexMetadata setColumns(List columns) {
this.columns = columns;
return this;
}
public boolean isUnique() {
return unique;
}
public RDBIndexMetadata setUnique(boolean unique) {
this.unique = unique;
return this;
}
public boolean isPrimaryKey() {
return primaryKey;
}
public RDBIndexMetadata setPrimaryKey(boolean primaryKey) {
this.primaryKey = primaryKey;
return this;
}
public boolean contains(String column) {
return columns.stream().anyMatch(indexColumn -> indexColumn.getColumn().equals(column));
}
@Override
public ObjectType getObjectType() {
return RDBObjectType.index;
}
public boolean isChanged(RDBTableMetadata metadata, RDBIndexMetadata old) {
if (old.getColumns().size() != getColumns().size()) {
return true;
}
Map nameMapping = getColumns()
.stream()
.collect(Collectors.toMap(c -> metadata
.getColumn(c.column)
.map(RDBColumnMetadata::getName)
.orElseGet(RDBIndexMetadata.class::getName), Function.identity()));
for (IndexColumn oldColumn : old.getColumns()) {
String columnName = metadata
.getColumn(oldColumn.column)
.map(RDBColumnMetadata::getName)
.orElse(null);
if (columnName == null || !nameMapping.containsKey(columnName)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(alias, columns, comment, name, primaryKey, tableName, unique);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RDBIndexMetadata other = (RDBIndexMetadata)obj;
return Objects.equals(alias, other.alias) && Objects.equals(columns, other.columns)
&& Objects.equals(comment, other.comment) && Objects.equals(name, other.name)
&& primaryKey == other.primaryKey && Objects.equals(tableName, other.tableName) && unique == other.unique;
}
@Override
public RDBIndexMetadata clone() {
RDBIndexMetadata metadata = null;
try {
metadata = (RDBIndexMetadata) super.clone();
} catch (CloneNotSupportedException e) {
ExceptionUtil.chuck(e);
}
metadata.columns.clear();
columns.stream()
.map(IndexColumn::clone)
.forEach(metadata.columns::add);
return metadata;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(name)
.append(" ")
.append(unique ? "unique index" : "index")
.append(" on ")
.append(tableName);
builder.append("(");
int index = 0;
for (IndexColumn column : columns) {
if (index++ != 0) {
builder.append(",");
}
builder.append(column.getColumn())
.append(" ")
.append(column.getSort().name());
}
builder.append(")");
return builder.toString();
}
public enum IndexSort {
asc, desc
}
public static class IndexColumn implements Cloneable, Comparable {
private String column;
private IndexSort sort = IndexSort.asc;
private int sortIndex;
public IndexColumn() {
super();
}
public String getColumn() {
return column;
}
public IndexColumn setColumn(String column) {
this.column = column;
return this;
}
public IndexSort getSort() {
return sort;
}
public IndexColumn setSort(IndexSort sort) {
this.sort = sort;
return this;
}
public int getSortIndex() {
return sortIndex;
}
public IndexColumn setSortIndex(int sortIndex) {
this.sortIndex = sortIndex;
return this;
}
@Override
public IndexColumn clone() {
IndexColumn clone = null;
try {
clone = (IndexColumn) super.clone();
} catch (CloneNotSupportedException e) {
ExceptionUtil.chuck(e);
}
return clone;
}
@Override
public int compareTo(IndexColumn o) {
return Integer.compare(sortIndex, o.sortIndex);
}
@Override
public int hashCode() {
return Objects.hash(column, sort, sortIndex);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IndexColumn other = (IndexColumn)obj;
return Objects.equals(column, other.column) && sort == other.sort && sortIndex == other.sortIndex;
}
@Override
public String toString() {
return "[column=" + column + ", sort=" + sort + ", sortIndex=" + sortIndex + "]";
}
public static IndexColumn of(String column, IndexSort sort) {
IndexColumn indexColumn = new IndexColumn();
indexColumn.setColumn(column);
indexColumn.setSort(sort);
return indexColumn;
}
}
}