use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class TableEditorPart method makeChangeLogForIndex.
public boolean makeChangeLogForIndex(String attrName, DBAttribute editAttr, DBAttribute origAttr) {
List<Constraint> constrainList = newSchemaInfo.getConstraints();
if (constrainList.size() == 0) {
// FIXME move this logic to core module
return false;
}
List<Constraint> removedConstrainList = new ArrayList<Constraint>();
List<Constraint> addedConstrainList = new ArrayList<Constraint>();
for (Constraint cons : constrainList) {
if (cons == null) {
continue;
}
if (ConstraintType.INDEX.getText().equals(cons.getType()) || ConstraintType.REVERSEINDEX.getText().equals(cons.getType()) || ConstraintType.UNIQUE.getText().equals(cons.getType()) || ConstraintType.REVERSEUNIQUE.getText().equals(cons.getType())) {
List<String> attrs = cons.getAttributes();
if (!(attrs != null && attrs.contains(attrName))) {
continue;
}
removedConstrainList.add(cons);
Constraint newCons = new Constraint(true);
newCons.setType(cons.getType());
newCons.setName(cons.getName());
cons.replaceAttribute(attrName, editAttr.getName());
newCons.setAttributes(cons.getAttributes());
for (String origRule : cons.getRules()) {
if (cons.getRules().size() == 1) {
newCons.addRule(editAttr.getName() + origRule.substring(attrName.length()));
break;
}
int spaceIndex = origRule.indexOf(" ");
String attrNameFromRule = origRule.substring(0, spaceIndex);
if (attrName.equals(attrNameFromRule)) {
newCons.addRule(editAttr.getName() + origRule.substring(attrName.length()));
} else {
newCons.addRule(origRule);
}
}
addedConstrainList.add(newCons);
String key = cons.getDefaultName(newSchemaInfo.getClassname()) + "$" + cons.getName();
SchemaChangeLog changeLog = new SchemaChangeLog(key, key, SchemeInnerType.TYPE_INDEX);
schemaChangeMgr.addSchemeChangeLog(changeLog);
}
}
if (removedConstrainList.size() == 0) {
return false;
}
constrainList.removeAll(removedConstrainList);
constrainList.addAll(addedConstrainList);
indexTableView.setInput(newSchemaInfo);
return true;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class TableEditorPart method getNotNullChangedColumn.
/**
* Get all not null changed column
*
* @return
*/
private List<String[]> getNotNullChangedColumn() {
// FIXME move this logic to core module
List<String[]> notNullChangedColumn = new ArrayList<String[]>();
List<SchemaChangeLog> allAttrChanges = schemaChangeMgr.getAttrChangeLogs();
List<SchemaInfo> oldSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), oldSchemaInfo);
if (oldSupers == null) {
return notNullChangedColumn;
}
List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo);
if (newSupers == null) {
return notNullChangedColumn;
}
for (SchemaChangeLog changeLog : allAttrChanges) {
if (changeLog.getOldValue() == null || changeLog.getNewValue() == null) {
continue;
}
boolean isClassAttr = changeLog.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE;
DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
if (oldAttr == null || newAttr == null) {
continue;
}
boolean oldNotNull = oldAttr.isNotNull();
boolean newNotNull = newAttr.isNotNull();
Constraint newPK = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = newPK == null ? new ArrayList<String>() : newPK.getAttributes();
if (oldNotNull == newNotNull) {
continue;
}
boolean isChangedByPK = false;
if (newNotNull) {
// add a new PK
if (pkAttributes.contains(newAttr.getName())) {
isChangedByPK = true;
}
} else {
// drop an old PK
Constraint oldPK = oldSchemaInfo.getPK(oldSupers);
if (oldPK != null) {
List<String> oldPKAttrs = oldPK.getAttributes();
if (oldPKAttrs != null && oldPKAttrs.contains(newAttr.getName())) {
isChangedByPK = true;
}
}
}
if (!isChangedByPK) {
String[] newColumn = new String[] { newAttr.getName(), String.valueOf(newNotNull) };
notNullChangedColumn.add(newColumn);
}
}
return notNullChangedColumn;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ERTable method buildColumns.
/**
* Build the columns in the schemaInfo to {@code ERTable}, and add to the er
* table.
*
* @param schemaInfo
*/
public void buildColumns(SchemaInfo schemaInfo) {
List<DBAttribute> attributesList = schemaInfo.getAttributes();
Constraint pk = schemaInfo.getPK();
for (DBAttribute attribute : attributesList) {
if (attribute == null || attribute.getInherit() == null) {
continue;
}
boolean isPK = pk == null ? false : pk.contains(attribute.getName(), false);
ERTableColumn column = new ERTableColumn(this, attribute, isPK);
this.addColumn(column);
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ERTableColumn method setName.
public void setName(String newName) {
if (StringUtil.isEqual(name, newName)) {
return;
}
attr.setName(newName);
SchemaInfo schemaInfo = this.getTable().getSchemaInfo();
Constraint pk = schemaInfo.getPK();
if (pk != null && pk.contains(name, false)) {
pk.replaceAttribute(name, newName);
}
schemaInfo.updateAttrNameInIndex(name, newName);
name = newName;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class CubridTableParser method getReferredColumns.
/**
* Get the other table columns name which is referred on the foreign key
* constraint
*
* @param schemaInfo
* @param fkConstaint
* @return The results is all columns of pk in the primary table actually.
* @throws Exception
*/
private List<String> getReferredColumns(SchemaInfo schemaInfo, Constraint fkConstaint) throws Exception {
List<String> resultList = new ArrayList<String>();
String refTable = getReferencedTableName(fkConstaint);
SchemaInfo referedSchemaInfo = getReferencedTable(refTable);
if (referedSchemaInfo != null) {
Constraint pkConstaint = referedSchemaInfo.getPK();
if (pkConstaint == null) {
throw new ERException(Messages.bind(Messages.errFKColumnMatch, new String[] { schemaInfo.getClassname(), fkConstaint.getName() }));
}
List<String> pklist = pkConstaint.getAttributes();
for (int i = 0; i < pklist.size(); i++) {
String referedKey = pklist.get(i).replace(" ASC", "").replace(" DESC", "");
resultList.add(referedKey);
}
}
return resultList;
}
Aggregations