org.zodiac.fastorm.rdb.metadata.RDBTableMetadata Maven / Gradle / Ivy
The newest version!
package org.zodiac.fastorm.rdb.metadata;
import org.zodiac.fastorm.core.meta.ObjectType;
import org.zodiac.fastorm.rdb.metadata.key.ForeignKeyMetadata;
import org.zodiac.fastorm.rdb.operator.builder.fragments.delete.DefaultDeleteSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.insert.BatchInsertSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.update.DefaultUpdateSqlBuilder;
import org.zodiac.fastorm.rdb.operator.dml.upsert.DefaultSaveOrUpdateOperator;
import org.zodiac.sdk.toolkit.util.ClassUtil;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* Database table structure metadata definition information, used to define table fields, indexes, and other information
*
* @see AbstractTableOrViewMetadata
* @see RDBIndexMetadata
* @see ConstraintMetadata
*/
public class RDBTableMetadata extends AbstractTableOrViewMetadata implements Cloneable {
/**
* Table comment.
*/
private String comment;
/**
* Indexes.
*/
private List indexes = new ArrayList<>();
/**
* Foreign keys.
*/
private List constraints = new ArrayList<>();
public RDBTableMetadata(String name) {
this();
setName(name);
}
public RDBTableMetadata() {
super();
/*Default SQL support for adding, deleting, modifying, and querying.*/
addFeature(BatchInsertSqlBuilder.of(this));
addFeature(DefaultUpdateSqlBuilder.of(this));
addFeature(DefaultDeleteSqlBuilder.of(this));
addFeature(DefaultSaveOrUpdateOperator.of(this));
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public List getIndexes() {
return indexes;
}
public void setIndexes(List indexes) {
this.indexes = indexes;
}
public List getConstraints() {
return constraints;
}
public void setConstraints(List constraints) {
this.constraints = constraints;
}
/**
* Obtain foreign key constraint.
*
* @param name Constraint name.
* @return The {@link ConstraintMetadata} .
*/
public Optional getConstraint(String name) {
return constraints
.stream()
.filter(metadata -> metadata.getName().equalsIgnoreCase(name))
.findFirst();
}
/**
* Obtain index.
*
* @param indexName Index name.
* @return The {@link RDBIndexMetadata} .
*/
public Optional getIndex(String indexName) {
return indexes
.stream()
.filter(index -> index.getName().equalsIgnoreCase(indexName))
.findFirst();
}
/**
*Add constraint.
*
* @param metadata The {@link ConstraintMetadata} .
* @see ConstraintMetadata
*/
public void addConstraint(ConstraintMetadata metadata) {
Objects.requireNonNull(metadata.getName(), "Constraint name can not be null");
metadata.setTableName(this.getName());
constraints.add(metadata);
}
/**
* Add index.
*
* @param index The {@link RDBIndexMetadata} .
* @see RDBIndexMetadata
*/
public void addIndex(RDBIndexMetadata index) {
Objects.requireNonNull(index.getName(), "index name can not be null");
index.setTableName(getName());
indexes.add(index);
for (RDBIndexMetadata.IndexColumn column : index.getColumns()) {
getColumn(column.getColumn())
.ifPresent(columnMeta -> {
/*Retrieve the column and set it as the primary key.*/
if (index.isPrimaryKey()) {
columnMeta.setPrimaryKey(true);
}
});
}
}
@Override
public ObjectType getObjectType() {
return RDBObjectType.table;
}
@Override
public RDBTableMetadata clone() {
RDBTableMetadata clone = null;
try {
clone = (RDBTableMetadata) super.clone();
clone.setAllColumns(new ConcurrentHashMap<>());
this.getColumns()
.stream()
.map(RDBColumnMetadata::clone)
.forEach(clone::addColumn);
clone.setFeatures(new ConcurrentHashMap<>(getFeatures()));
clone.setIndexes(getIndexes()
.stream()
.map(RDBIndexMetadata::clone)
.collect(Collectors.toList()));
this.getForeignKey()
.stream()
.map(ForeignKeyMetadata::clone)
.map(ClassUtil::cast)
.forEach(clone::addForeignKey);
} catch (Exception e) {
ExceptionUtil.chuck(e);
}
return clone;
}
@Override
public void merge(TableOrViewMetadata metadata) {
super.merge(metadata);
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(comment, constraints, indexes);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
RDBTableMetadata other = (RDBTableMetadata)obj;
return Objects.equals(comment, other.comment) && Objects.equals(constraints, other.constraints)
&& Objects.equals(indexes, other.indexes);
}
@Override
public String toString() {
return "[comment=" + comment + ", indexes=" + indexes + ", constraints=" + constraints
+ ", allColumns=" + allColumns + ", columnView=" + columnView + ", foreignKey=" + foreignKey + ", features="
+ features + "]";
}
}