com.agimatec.sql.meta.TableDescription Maven / Gradle / Ivy
package com.agimatec.sql.meta;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Description:
* Copyright: Copyright (c) 2007
* Company: Agimatec GmbH
*
* @author Roman Stumm
*/
@XStreamAlias("table")
public class TableDescription implements Serializable, Cloneable {
private String comment;
private String tableName;
private List columns =
new ArrayList(); // list of ColumnDescription
private IndexDescription primaryKey;
private List indices = new ArrayList(); // list of IndexDescription
private List constraints =
new ArrayList(); // list of IndexDescription
private List foreignKeys =
new ArrayList(); // list of ForeignKeyDescription
public TableDescription() {
}
/**
* deep copy
*
* @return
*/
public TableDescription deepCopy() {
try {
TableDescription clone = (TableDescription) clone();
clone.columns = new ArrayList(columns.size());
for (ColumnDescription each : columns) {
clone.columns.add(each.deepCopy());
}
clone.indices = new ArrayList(indices.size());
for (IndexDescription each : indices) {
clone.indices.add(each.deepCopy());
}
clone.constraints = new ArrayList(constraints.size());
for (IndexDescription each : constraints) {
clone.constraints.add(each.deepCopy());
}
clone.foreignKeys = new ArrayList(foreignKeys.size());
for (ForeignKeyDescription each : foreignKeys) {
clone.foreignKeys.add(each.deepCopy());
}
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
}
public String getTableName() {
return tableName;
}
public void setTableName(String aTableName) {
tableName = aTableName;
}
public String getComment() {
return comment;
}
public void setComment(String aComment) {
comment = aComment;
}
public boolean isForeignKeyColumn(String columnName) {
for (ForeignKeyDescription fk : getForeignKeys()) {
if (fk.containsColumn(columnName)) return true;
}
return false;
}
public boolean isPrimaryKeyColumn(String columnName) {
if (getPrimaryKey() == null) return false; // no primary key
for (String pkCol : getPrimaryKey().getColumns()) {
if (pkCol.equalsIgnoreCase(columnName)) return true;
}
return false;
}
public boolean isUnique(List columns) {
List indices = findIndicesForColumns(columns);
for(IndexDescription index : indices) {
if(index.isUnique()) return true;
}
return false;
}
public boolean isUnique(String column) {
List columns = new ArrayList(1);
columns.add(column);
return isUnique(columns);
}
public IndexDescription getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(IndexDescription aPrimaryKey) {
primaryKey = aPrimaryKey;
}
public IndexDescription getIndex(String indexName) {
for (IndexDescription indice : indices) {
if (indice.getIndexName().equalsIgnoreCase(indexName)) return indice;
}
return null;
}
public void removeIndex(String indexName) {
for (Iterator iter = indices.iterator(); iter.hasNext();) {
IndexDescription theindexDescription = (IndexDescription) iter.next();
if (theindexDescription.getIndexName().equalsIgnoreCase(indexName)) {
iter.remove();
return;
}
}
}
public IndexDescription getConstraint(String indexName) {
for (IndexDescription indice : constraints) {
if (indice.getIndexName().equalsIgnoreCase(indexName)) return indice;
}
return null;
}
public void removeConstraint(String indexName) {
for (Iterator iter = constraints.iterator(); iter.hasNext();) {
IndexDescription theindexDescription = (IndexDescription) iter.next();
if (theindexDescription.getIndexName().equalsIgnoreCase(indexName)) {
iter.remove();
return;
}
}
}
public List getForeignKeys() {
return foreignKeys;
}
public List getConstraints() {
return constraints;
}
public List getIndices() {
return indices;
}
public IndexDescription getIndex(int i) {
return indices.get(i);
}
public int getIndexSize() {
return indices.size();
}
public int getConstraintSize() {
return constraints.size();
}
public void addIndex(IndexDescription aIndex) {
indices.add(aIndex);
}
public void addConstraint(IndexDescription aIndex) {
constraints.add(aIndex);
}
public IndexDescription getConstraint(int i) {
return constraints.get(i);
}
public ForeignKeyDescription getForeignKey(String constraintName) {
for (ForeignKeyDescription foreignKey : foreignKeys) {
if (foreignKey.getConstraintName()
.equalsIgnoreCase(constraintName)) return foreignKey;
}
return null;
}
public ForeignKeyDescription getForeignKey(int i) {
return foreignKeys.get(i);
}
public int getForeignKeySize() {
return foreignKeys.size();
}
public void addForeignKey(ForeignKeyDescription aFK) {
foreignKeys.add(aFK);
}
/**
* column names of this table in alphabetic order
*/
public String[] getColumnNames() {
String[] columnNames = new String[columns.size()];
Iterator iter = columns.iterator();
int i = 0;
while (iter.hasNext()) {
ColumnDescription theColumnDescription = (ColumnDescription) iter.next();
columnNames[i++] = theColumnDescription.getColumnName();
}
Arrays.sort(columnNames);
return columnNames;
}
/**
* list of ColumnDescription
*
* @return
*/
public List getColumns() {
return columns;
}
public ColumnDescription getColumn(int i) {
return columns.get(i);
}
/**
* @param columnName - name to search for (ignore case)
* @return null or the columndescription
*/
public ColumnDescription getColumn(String columnName) {
for (ColumnDescription column : columns) {
if (column.getColumnName().equalsIgnoreCase(columnName)) return column;
}
return null;
}
public void removeColumn(String columnName) {
ColumnDescription colDesc = getColumn(columnName);
if(colDesc != null) {
columns.remove(colDesc);
}
}
public int getColumnSize() {
return columns.size();
}
public void addColumn(ColumnDescription aColumn) {
columns.add(aColumn);
}
public String toString() {
return getTableName();
}
public IndexDescription findIndexForColumns(List columns) {
for (IndexDescription index : getIndices()) {
if (index.isSameColumns(columns)) return index;
}
return null;
}
public IndexDescription findConstraintForColumns(List columns) {
if (primaryKey != null && primaryKey.isSameColumns(columns)) return primaryKey;
for (IndexDescription index : getConstraints()) {
if (index.isSameColumns(columns)) return index;
}
return null;
}
public List findIndicesForColumns(List columns) {
List indices = new ArrayList();
if (getPrimaryKey() != null) {
if (getPrimaryKey().isSameColumns(columns)) {
indices.add(getPrimaryKey());
}
}
for (IndexDescription index : getConstraints()) {
if (index.isSameColumns(columns) && index.isUnique()) {
indices.add(index);
}
}
for (IndexDescription index : getIndices()) {
if (index.isSameColumns(columns) && index.isUnique()) {
indices.add(index);
}
}
return indices;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy