use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method isRemovedFromPK.
/**
* Check whether current column was removed from PK attribute list or not.
*
* @param oldSchemaInfo
* @param newSchemaInfo
* @param oldSupers
* @param newSupers
* @param oldAttr
* @param newAttr
* @return
*/
private boolean isRemovedFromPK(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, DBAttribute oldAttr, DBAttribute newAttr) {
List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, newSchemaInfo);
allSupers.addAll(newSupers);
allSupers.addAll(oldSupers);
Constraint newPK = newSchemaInfo.getPK(allSupers);
Constraint oldPK = oldSchemaInfo.getPK(oldSupers);
if (oldPK == null) {
return false;
} else if (oldPK.getAttributes() == null) {
return false;
} else if (!oldPK.getAttributes().contains(oldAttr.getName())) {
return false;
} else if (newPK == null) {
return true;
} else if (newPK.getAttributes() == null) {
return true;
} else if (!newPK.getAttributes().contains(newAttr.getName())) {
return true;
}
return false;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getChangeColumnDDL.
/**
* Get the column change DDL
*
* @param tableName
* @param oldAttr
* @param newAttr
* @param oldSchemaInfo
* @param newSchemaInfo
* @param newSupers
* @return
*/
private String getChangeColumnDDL(String tableName, DBAttribute oldAttr, DBAttribute newAttr, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers) {
Constraint newPK = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = newPK == null ? new ArrayList<String>() : newPK.getAttributes();
StringBuffer sb = new StringBuffer();
sb.append("ALTER TABLE ");
sb.append(QuerySyntax.escapeKeyword(tableName));
sb.append(" CHANGE COLUMN ");
sb.append(QuerySyntax.escapeKeyword(oldAttr.getName())).append(" ");
sb.append(getInstanceAttributeDDL(newAttr, pkAttributes, newSchemaInfo, false));
String reorderString = getReorderString(newAttr, newSchemaInfo);
if (reorderString != null) {
sb.append(" " + reorderString);
}
sb.append(endLineChar);
sb.append(StringUtil.NEWLINE);
SerialInfo oldAutoIncrement = oldAttr.getAutoIncrement();
SerialInfo newAutoIncrement = newAttr.getAutoIncrement();
if (null != newAutoIncrement && !newAutoIncrement.equals(oldAutoIncrement)) {
String increment = getAlterAutoIncrementDDL(tableName, newAttr.getName());
sb.append(increment);
}
return sb.toString();
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getPKsDDL.
/**
* get the pk DDL
*
* @param schemaInfo SchemaInfo
* @return pk DDL
*/
public String getPKsDDL(SchemaInfo schemaInfo) {
String tableName = schemaInfo.getClassname();
StringBuffer ddlBuffer = new StringBuffer();
//Get the PK
List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
Constraint pk = schemaInfo.getPK(allSupers);
if (pk != null) {
List<String> pkAttributes = pk.getAttributes();
if (pkAttributes != null && pkAttributes.size() > 0) {
ddlBuffer.append(getAddPKDDL(tableName, pkAttributes, pk.getName()));
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
}
}
return ddlBuffer.toString();
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method appendChangedFkDDL.
/**
* Append the changed foreign key DDL
*
* @param oldSchemaInfo the old instance of SchemaInfo
* @param newSchemaInfo the new instance of SchemaInfo
* @param attrMap the map which includes all the attribute
* @param ddlBuffer the object of StringBuffer
* @param dropConstraintBF the object of StringBuffer which represent drop
* constraint
* @param tableName the table name
* @param log the object of SchemeChangeLog
*/
private void appendChangedFkDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, Map<String, String> attrMap, String tableName, SchemaChangeLog log, DDLGenerator generator) {
Constraint oldFK = null;
Constraint newFK = null;
if (log.getOldValue() != null) {
oldFK = oldSchemaInfo.getFKConstraint(log.getOldValue());
}
if (log.getNewValue() != null) {
newFK = newSchemaInfo.getFKConstraint(log.getNewValue());
}
if (log.getOldValue() == null && newFK != null) {
// add fk
String addFKDDL = getAddFKDDL(tableName, newFK) + endLineChar + StringUtil.NEWLINE;
generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_FK, newFK, addFKDDL);
} else if (log.getNewValue() == null && oldFK != null) {
// delete fk
String dropFKDDL = getDropFKDDL(tableName, oldFK.getName()) + endLineChar + StringUtil.NEWLINE;
generator.addPreDDLMode(DDLGenerator.TYPE_DROP_FK, oldFK, dropFKDDL);
} else if (oldFK != null && newFK != null) {
String oldDDL = getAddFKDDL(tableName, oldFK);
String newDDL = getAddFKDDL(tableName, newFK);
if (!oldDDL.equals(newDDL)) {
String dropFKDDL = getDropFKDDL(tableName, oldFK.getName()) + endLineChar + StringUtil.NEWLINE;
generator.addPreDDLMode(DDLGenerator.TYPE_DROP_FK, oldFK, dropFKDDL);
String addFKDDL = getAddFKDDL(tableName, newFK) + endLineChar + StringUtil.NEWLINE;
generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_FK, newFK, addFKDDL);
}
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method appendChangeAttributeDDL.
/**
* Append the change DDL for the attribute
*
* @param oldSchemaInfo
* @param newSchemaInfo
* @param oldSupers
* @param newSupers
* @param attrMap
* @param ddlBuffer
* @param tableName
* @param changeLog
* @param processedId
* @param isClassAttr
*/
private void appendChangeAttributeDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, Map<String, String> attrMap, String tableName, SchemaChangeLog changeLog, boolean isClassAttr, DDLGenerator generator) {
if (StringUtil.isEmpty(changeLog.getOldValue())) {
// add [class] column
DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
StringBuffer addDDL = new StringBuffer();
if (isClassAttr) {
addDDL.append(getAddClassColumnDDL(tableName, newAttr)).append(endLineChar).append(StringUtil.NEWLINE);
} else {
Constraint pk = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
addDDL.append(getAddColumnDDL(tableName, newAttr, pkAttributes, newSchemaInfo));
/*For bug TOOLS-1125.add the position DDL*/
StringBuffer sb = new StringBuffer();
String lastName = null;
for (DBAttribute attr : newSchemaInfo.getAttributes()) {
if (attr.getName().toLowerCase().equals(newAttr.getName().toLowerCase())) {
break;
} else {
lastName = attr.getName();
}
}
if (lastName == null) {
sb.append(" FIRST");
} else {
sb.append(" AFTER ").append(QuerySyntax.escapeKeyword(lastName));
}
addDDL.append(sb.toString());
addDDL.append(endLineChar);
addDDL.append(StringUtil.NEWLINE);
}
generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_ATTR, newAttr, addDDL.toString());
} else if (StringUtil.isEmpty(changeLog.getNewValue())) {
// drop [class] column
DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
String attrName = oldAttr.getName();
String dropDDL = null;
if (isClassAttr) {
dropDDL = getDropClassColumnDDL(tableName, attrName) + endLineChar + StringUtil.NEWLINE;
} else {
dropDDL = getDropColumnDDL(tableName, attrName) + endLineChar + StringUtil.NEWLINE;
}
generator.addSchemaDDLMode(DDLGenerator.TYPE_DROP_ATTR, oldAttr, dropDDL);
} else {
// edit column
DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
/*For bug TOOLS - 987*/
String editDDL = null;
/*For bug TOOLS-3046*/
boolean isNeedChangeDDLAfterChangePK = newAttr.isNotNull() && isRemovedFromPK(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, oldAttr, newAttr);
if (isNeedChangeDDL(oldAttr, newAttr, changeLog) || isNeedChangeDDLAfterChangePK) {
if (isClassAttr) {
editDDL = getChangeAttributeDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, newSupers);
} else {
editDDL = getChangeColumnDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers);
}
} else {
/*Just don't process SchemeInnerType.TYPE_POSITION log*/
if (oldAttr == null) {
return;
}
editDDL = getAlterAttrDDL(oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, isClassAttr, attrMap, tableName, generator);
}
generator.addSchemaDDLMode(DDLGenerator.TYPE_EDIT_ATTR, newAttr, editDDL);
}
}
Aggregations