use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class SchemaAlterDDLTest method indexTest1.
//add indexes, then drop them
private void indexTest1() {
changeList = new SchemaChangeManager(databaseInfo, false);
ddl = new SchemaDDL(changeList, databaseInfo);
SchemaInfo newSchema = sup3.clone();
Constraint constraint = null;
String indexType = null;
String indexName = "";
String tableName = newSchema.getClassname();
//add unique
constraint = new Constraint(false);
indexType = "UNIQUE";
constraint.setType(indexType);
constraint.addAttribute("integer");
constraint.addRule("integer DESC");
constraint.addAttribute("bigint");
constraint.addRule("bigint ASC");
if (indexName.equals("")) {
//$NON-NLS-1$
indexName = constraint.getDefaultName(tableName);
}
constraint.setName(indexName);
newSchema.addConstraint(constraint);
changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
constraint.getName(), SchemeInnerType.TYPE_INDEX));
//add index
constraint = new Constraint(false);
indexType = "INDEX";
constraint.setType(indexType);
constraint.addAttribute("numeric1");
constraint.addRule("numeric1 ASC");
constraint.addAttribute("numeric2");
constraint.addRule("numeric2 DESC");
constraint.addAttribute("float");
constraint.addRule("float ASC");
indexName = constraint.getDefaultName(tableName);
constraint.setName(indexName);
newSchema.addConstraint(constraint);
changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
constraint.getName(), SchemeInnerType.TYPE_INDEX));
//add reverse unique
constraint = new Constraint(false);
indexType = "REVERSE UNIQUE";
constraint.setType(indexType);
constraint.addAttribute("numeric2");
constraint.addRule("numeric2 DESC");
constraint.addAttribute("float");
constraint.addRule("float DESC");
constraint.addAttribute("setint");
constraint.addRule("setint DESC");
indexName = constraint.getDefaultName(tableName);
constraint.setName(indexName);
newSchema.addConstraint(constraint);
changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
constraint.getName(), SchemeInnerType.TYPE_INDEX));
//add reverse index
constraint = new Constraint(false);
indexType = "REVERSE INDEX";
constraint.setType(indexType);
constraint.addAttribute("integer");
constraint.addRule("integer DESC");
constraint.addAttribute("bigint");
constraint.addRule("bigint DESC");
indexName = constraint.getDefaultName(tableName);
constraint.setName(indexName);
newSchema.addConstraint(constraint);
changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
constraint.getName(), SchemeInnerType.TYPE_INDEX));
String expected = "CREATE UNIQUE INDEX u_sup3_integer_d_bigint ON sup3([integer] DESC,[bigint]);" + StringUtil.NEWLINE;
expected += "CREATE INDEX i_sup3_numeric1_numeric2_d_float ON sup3(numeric1,numeric2 DESC,[float]);" + StringUtil.NEWLINE;
expected += "CREATE REVERSE UNIQUE INDEX ru_sup3_numeric2_float_setint ON sup3(numeric2 DESC,[float] DESC,setint DESC);" + StringUtil.NEWLINE;
expected += "CREATE REVERSE INDEX ri_sup3_integer_bigint ON sup3([integer] DESC,[bigint] DESC);";
String alterDDL = ddl.getAlterDDL(sup3, newSchema);
assertEquals(expected, alterDDL.trim());
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class SchemaAlterDDLTest method editColumn.
/**
* edit a column
*
* @param newSchema
* @param superList
* @param editAttribute
* @param oldAttribute
* @param isEditAll
*/
private void editColumn(SchemaInfo newSchema, DBAttribute editAttribute, DBAttribute oldAttribute, boolean isEditAll) {
if (editAttribute != null) {
editAttribute.setInherit(newSchema.getClassname());
} else {
return;
}
String newAttrName = editAttribute.getName();
boolean isNewAttrClass = editAttribute.isClassAttribute();
boolean isOldAttrClass = oldAttribute.isClassAttribute();
String attrName = oldAttribute.getName();
String tableName = newSchema.getClassname();
if (isEditAll) {
if (isOldAttrClass != isNewAttrClass) {
// attribute
// type
// changed
newSchema.removeDBAttributeByName(attrName, isOldAttrClass);
addDropAttrLog(attrName, isOldAttrClass);
newSchema.addDBAttribute(editAttribute, isNewAttrClass);
addNewAttrLog(newAttrName, isNewAttrClass);
} else {
newSchema.replaceDBAttributeByName(oldAttribute, editAttribute, isNewAttrClass, superList);
addEditAttrLog(attrName, newAttrName, isNewAttrClass);
}
if (!oldAttribute.isUnique() && editAttribute.isUnique()) {
Constraint unique = new Constraint(false);
unique.setType(Constraint.ConstraintType.UNIQUE.getText());
unique.addAttribute(newAttrName);
unique.addRule(newAttrName + " ASC");
unique.setName(ConstraintNamingUtil.getUniqueName(tableName, unique.getRules()));
newSchema.addConstraint(unique);
} else if (oldAttribute.isUnique() && !editAttribute.isUnique()) {
newSchema.removeUniqueByAttrName(attrName);
}
} else {
newSchema.replaceDBAttributeByName(oldAttribute, editAttribute, isNewAttrClass, superList);
addEditAttrLog(attrName, newAttrName, isNewAttrClass);
}
SuperClassUtil.fireSuperClassChanged(databaseInfo, testedSchemaInfo, newSchema, newSchema.getSuperClasses());
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getAddReorderColumnDDL.
/**
*
* Return the new added and reorder attribute DDL
*
* @param oldSchemaInfo SchemaInfo
* @param newSchemaInfo SchemaInfo
* @param newSupers List<SchemaInfo>
* @param tableName String
* @return String
*/
private String getAddReorderColumnDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> newSupers, String tableName) {
StringBuffer ddlBuffer = new StringBuffer();
List<DBAttribute> dbAttributeList = newSchemaInfo.getAttributes();
List<SchemaChangeLog> attrChanges = changeLogMgr.getAttrChangeLogs();
DBAttribute lastAttr = null;
for (DBAttribute dbAttribute : dbAttributeList) {
boolean isInheritAttr = dbAttribute.getInherit() != null && !dbAttribute.getInherit().trim().equalsIgnoreCase(tableName.trim());
if (isInheritAttr) {
continue;
}
for (SchemaChangeLog changeLog : attrChanges) {
String newAttrName = changeLog.getNewValue();
if (newAttrName != null && newAttrName.equals(dbAttribute.getName()) && changeLog.getOldValue() == null) {
// add [class] column
Constraint pk = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
ddlBuffer.append("ALTER TABLE ").append(QuerySyntax.escapeKeyword(tableName)).append(" ADD COLUMN ");
ddlBuffer.append(getInstanceAttributeDDL(dbAttribute, pkAttributes, newSchemaInfo, false));
if (lastAttr == null) {
ddlBuffer.append(" FIRST");
} else {
ddlBuffer.append(" AFTER ").append(QuerySyntax.escapeKeyword(lastAttr.getName()));
}
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
break;
}
}
lastAttr = dbAttribute;
}
return ddlBuffer.toString();
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method appendChanedIndexDDL.
/**
* Append changed index DDL
*
* @param oldSchemaInfo the old instance of SchemaInfo
* @param newSchemaInfo the new instance of SchemaInfo
* @param ddlBuffer the instance 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 appendChanedIndexDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, String tableName, SchemaChangeLog log, DDLGenerator generator) {
Constraint oldConstraint = null;
Constraint newConstraint = null;
if (log.getOldValue() != null) {
oldConstraint = oldSchemaInfo.getConstraintByName(getConstraintName(log.getOldValue()));
}
if (log.getNewValue() != null) {
newConstraint = newSchemaInfo.getConstraintByName(getConstraintName(log.getNewValue()));
}
if (oldConstraint == null && newConstraint != null) {
// add index
String addIndexDDL = getCreateIndexDDL(tableName, newConstraint);
if (!"".equals(addIndexDDL)) {
addIndexDDL = addIndexDDL + endLineChar + StringUtil.NEWLINE;
generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_INDEX, newConstraint, addIndexDDL);
}
} else if (log.getNewValue() == null && oldConstraint != null) {
// delete index
String dropIndexDDL = getDropIndexDDL(tableName, oldConstraint) + endLineChar + StringUtil.NEWLINE;
generator.addPreDDLMode(DDLGenerator.TYPE_DROP_INDEX, oldConstraint, dropIndexDDL);
} else if (newConstraint != null && oldConstraint != null) {
// modify index
String oldDDL = getCreateIndexDDL(tableName, oldConstraint);
String newDDL = getCreateIndexDDL(tableName, newConstraint);
if (!oldDDL.equals(newDDL)) {
String dropIndexDDL = getDropIndexDDL(tableName, oldConstraint) + endLineChar + StringUtil.NEWLINE;
generator.addPreDDLMode(DDLGenerator.TYPE_DROP_INDEX, oldConstraint, dropIndexDDL);
String addIndexDDL = newDDL + endLineChar + endLineChar;
generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_INDEX, newConstraint, addIndexDDL);
}
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getFKDDL.
/**
* Return the DDL of FK in creating and altering a schema
*
* @param tableName String the given table name
* @param fkConstaint Constraint the given reference of a Constraint
* object,which includes the info of foreign key
* @return String a string indicates the info of foreign key DDL
*/
private String getFKDDL(String tableName, Constraint fkConstaint) {
StringBuffer bf = new StringBuffer();
String defaultName = fkConstaint.getDefaultName(tableName);
if (!defaultName.equals(fkConstaint.getName())) {
bf.append(" CONSTRAINT ");
bf.append(" ").append(QuerySyntax.escapeKeyword(fkConstaint.getName()));
}
bf.append(" FOREIGN KEY");
List<String> list = fkConstaint.getAttributes();
bf.append(" (");
for (int i = 0; i < list.size(); i++) {
if (i != 0) {
bf.append(",");
}
if (list.get(i).indexOf(" DESC") != -1 || list.get(i).indexOf(" ASC") != -1) {
bf.append(list.get(i));
} else {
bf.append(QuerySyntax.escapeKeyword(list.get(i)));
}
}
bf.append(")");
List<String> rlist = fkConstaint.getRules();
String refTable = rlist != null && rlist.size() > 0 ? rlist.get(0).replace("REFERENCES ", "") : "";
bf.append(" REFERENCES ").append(QuerySyntax.escapeKeyword(refTable));
bf.append("(");
SchemaInfo schemaInfo = databaseInfo.getSchemaInfo(refTable);
if (schemaInfo == null) {
bf.append(" {ERROR : Cannot retrieve foreign key metadata on old version of cubrid} )");
} else {
List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
Constraint pkConstaint = schemaInfo.getPK(newSupers);
List<String> pklist = pkConstaint.getAttributes();
for (int i = 0; i < pklist.size(); i++) {
if (i != 0) {
bf.append(",");
}
bf.append(QuerySyntax.escapeKeyword(pklist.get(i).replace(" ASC", "").replace(" DESC", "")));
}
bf.append(")");
for (int i = 1; i < rlist.size(); i++) {
String rule = rlist.get(i);
String tmp = rule.trim().toUpperCase();
if (tmp.startsWith("ON CACHE OBJECT")) {
tmp = tmp.replace("ON CACHE OBJECT", "").trim().toLowerCase();
bf.append(" ON CACHE OBJECT ").append(QuerySyntax.escapeKeyword(tmp));
} else {
bf.append(" ").append(rule);
}
}
}
return bf.toString();
}
Aggregations